Skip to content

Commit

Permalink
Replace Lexer with Pygments::Lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Aug 23, 2011
1 parent bb11317 commit 0864b98
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 1,995 deletions.
2 changes: 2 additions & 0 deletions Gemfile
@@ -1,2 +1,4 @@
source :rubygems
gemspec

gem 'pygments.rb', :git => 'git://github.com/tmm1/pygments.rb.git'
14 changes: 0 additions & 14 deletions Rakefile
@@ -1,21 +1,7 @@
require 'rake/clean'
require 'rake/testtask'

task :default => :test

Rake::TestTask.new do |t|
t.warning = true
end

CLOBBER.include 'lib/linguist/lexers.yml'

file 'lib/linguist/lexers.yml' do |f|
# GitHub vendored pygments path
# vendor/python/pygments
path = File.expand_path('../../../python/pygments', __FILE__)
ENV['PYTHONPATH'] = path if File.directory?(path)

sh "python ./bin/pygments-lexers > #{f.name}"
end

task :lexers => 'lib/linguist/lexers.yml'
27 changes: 0 additions & 27 deletions bin/pygments-lexers

This file was deleted.

19 changes: 14 additions & 5 deletions lib/linguist/blob_helper.rb
Expand Up @@ -3,6 +3,7 @@
require 'linguist/pathname'

require 'escape_utils'
require 'pygments'
require 'yaml'

module Linguist
Expand Down Expand Up @@ -343,7 +344,7 @@ def guess_language
#
# Returns a Lexer.
def lexer
language ? language.lexer : Lexer['Text only']
language ? language.lexer : Pygments::Lexer.find_by_name('Text only')
end

# Internal: Disambiguates between multiple language extensions.
Expand Down Expand Up @@ -512,19 +513,27 @@ def shebang_language

# Public: Highlight syntax of blob
#
# options - A Hash of options (defaults to {})
#
# Returns html String
def colorize
def colorize(options = {})
return if !text? || large?
lexer.colorize(data)
lexer.highlight(data, options)
end

# Public: Highlight syntax of blob without the outer highlight div
# wrapper.
#
# options - A Hash of options (defaults to {})
#
# Returns html String
def colorize_without_wrapper
def colorize_without_wrapper(options = {})
return if !text? || large?
lexer.colorize_without_wrapper(data)
if text = lexer.highlight(data, options)
text[%r{<div class="highlight"><pre>(.*?)</pre>\s*</div>}m, 1]
else
''
end
end

Language.overridden_extensions.each do |extension|
Expand Down
22 changes: 6 additions & 16 deletions lib/linguist/language.rb
@@ -1,6 +1,5 @@
require 'linguist/lexer'

require 'yaml'
require 'pygments'

module Linguist
# Language names that are recognizable by GitHub. Defined languages
Expand Down Expand Up @@ -211,7 +210,7 @@ def initialize(attributes = {})
@aliases = [default_alias_name] + (attributes[:aliases] || [])

# Lookup Lexer object
@lexer = Lexer.find_by_name(attributes[:lexer] || name) ||
@lexer = Pygments::Lexer.find_by_name(attributes[:lexer] || name) ||
raise(ArgumentError, "#{@name} is missing lexer")

# Set legacy search term
Expand Down Expand Up @@ -370,21 +369,12 @@ def searchable?

# Public: Highlight syntax of text
#
# text - String of code to be highlighted
#
# Returns html String
def colorize(text)
lexer.colorize(text)
end

# Public: Highlight syntax of text without the outer highlight div
# wrapper.
#
# text - String of code to be highlighted
# text - String of code to be highlighted
# options - A Hash of options (defaults to {})
#
# Returns html String
def colorize_without_wrapper(text)
lexer.colorize_without_wrapper(text)
def colorize(text, options = {})
lexer.highlight(text, options = {})
end

# Public: Return name as String representation
Expand Down
167 changes: 0 additions & 167 deletions lib/linguist/lexer.rb

This file was deleted.

0 comments on commit 0864b98

Please sign in to comment.