Switch to Pygments.rb

This commit is contained in:
Joshua Peek
2011-08-22 10:55:39 -05:00
parent e70ffb93ba
commit bb11317546
5 changed files with 36 additions and 52 deletions

View File

@@ -1,4 +1,4 @@
require 'albino' require 'pygments'
require 'yaml' require 'yaml'
module Linguist module Linguist
@@ -55,16 +55,6 @@ module Linguist
end end
end end
# Internal: Test if system has Pygments
#
# Only used in tests to disable tests that require Pygments.
#
# Returns true if `pygmentize` in is PATH otherwise false.
def self.has_pygments?
`which #{Albino.bin}`
$?.success?
end
# Public: Get all Lexers # Public: Get all Lexers
# #
# Returns an Array of Lexers # Returns an Array of Lexers
@@ -141,7 +131,7 @@ module Linguist
# #
# Returns html String # Returns html String
def colorize(text) def colorize(text)
Albino.new(text, self).colorize(:O => 'stripnl=false') Pygments.highlight(text, :lexer => aliases.first, :options => {:stripnl => false})
end end
# Public: Highlight syntax of text without the outer highlight div # Public: Highlight syntax of text without the outer highlight div

View File

@@ -6,8 +6,8 @@ Gem::Specification.new do |s|
s.files = Dir['lib/**/*'] s.files = Dir['lib/**/*']
s.executables << 'linguist' s.executables << 'linguist'
s.add_dependency 'albino', '1.3.2'
s.add_dependency 'escape_utils', '0.2.3' s.add_dependency 'escape_utils', '0.2.3'
s.add_dependency 'mime-types', '1.16' s.add_dependency 'mime-types', '1.16'
s.add_dependency 'pygments.rb'
s.add_development_dependency 'rake' s.add_development_dependency 'rake'
end end

View File

@@ -380,21 +380,19 @@ class TestBlob < Test::Unit::TestCase
assert_equal nil, blob("foo.rb").shebang_language assert_equal nil, blob("foo.rb").shebang_language
end end
if Lexer.has_pygments? def test_colorize
def test_colorize assert_equal <<-HTML, blob("foo.rb").colorize
assert_equal <<-HTML, blob("foo.rb").colorize
<div class="highlight"><pre><span class="k">module</span> <span class="nn">Foo</span> <div class="highlight"><pre><span class="k">module</span> <span class="nn">Foo</span>
<span class="k">end</span> <span class="k">end</span>
</pre> </pre>
</div> </div>
HTML HTML
end end
def test_colorize_without_wrapper def test_colorize_without_wrapper
assert_equal <<-HTML, blob("foo.rb").colorize_without_wrapper assert_equal <<-HTML, blob("foo.rb").colorize_without_wrapper
<span class="k">module</span> <span class="nn">Foo</span> <span class="k">module</span> <span class="nn">Foo</span>
<span class="k">end</span> <span class="k">end</span>
HTML HTML
end
end end
end end

View File

@@ -296,43 +296,41 @@ class TestLanguage < Test::Unit::TestCase
end end
if Lexer.has_pygments? def test_colorize
def test_colorize assert_equal <<-HTML, Language['Text'].colorize("Hello")
assert_equal <<-HTML, Language['Text'].colorize("Hello")
<div class="highlight"><pre>Hello <div class="highlight"><pre>Hello
</pre> </pre>
</div> </div>
HTML HTML
assert_equal <<-HTML, Language['Ruby'].colorize("def foo\n 'foo'\nend\n") assert_equal <<-HTML, Language['Ruby'].colorize("def foo\n 'foo'\nend\n")
<div class="highlight"><pre><span class="k">def</span> <span class="nf">foo</span> <div class="highlight"><pre><span class="k">def</span> <span class="nf">foo</span>
<span class="s1">&#39;foo&#39;</span> <span class="s1">&#39;foo&#39;</span>
<span class="k">end</span> <span class="k">end</span>
</pre> </pre>
</div> </div>
HTML HTML
end end
def test_colorize_without_wrapper def test_colorize_without_wrapper
assert_equal <<-HTML, Language['Text'].colorize_without_wrapper("Hello") assert_equal <<-HTML, Language['Text'].colorize_without_wrapper("Hello")
Hello Hello
HTML HTML
assert_equal <<-HTML, Language['Ruby'].colorize_without_wrapper("def foo\n 'foo'\nend\n") assert_equal <<-HTML, Language['Ruby'].colorize_without_wrapper("def foo\n 'foo'\nend\n")
<span class="k">def</span> <span class="nf">foo</span> <span class="k">def</span> <span class="nf">foo</span>
<span class="s1">&#39;foo&#39;</span> <span class="s1">&#39;foo&#39;</span>
<span class="k">end</span> <span class="k">end</span>
HTML HTML
end end
def test_colorize_doesnt_strip_newlines def test_colorize_doesnt_strip_newlines
assert_equal <<-HTML, Language['Ruby'].colorize_without_wrapper("\n\n# Foo\ndef 'foo'\nend\n") assert_equal <<-HTML, Language['Ruby'].colorize_without_wrapper("\n\n# Foo\ndef 'foo'\nend\n")
<span class="c1"># Foo</span> <span class="c1"># Foo</span>
<span class="k">def</span> <span class="s1">&#39;foo&#39;</span> <span class="k">def</span> <span class="s1">&#39;foo&#39;</span>
<span class="k">end</span> <span class="k">end</span>
HTML HTML
end
end end
end end

View File

@@ -61,33 +61,31 @@ class TestLexer < Test::Unit::TestCase
assert !Lexer['Ruby'].eql?(Lexer.new('Ruby')) assert !Lexer['Ruby'].eql?(Lexer.new('Ruby'))
end end
if Lexer.has_pygments? def test_colorize
def test_colorize assert_equal <<-HTML, Lexer['Text only'].colorize("Hello")
assert_equal <<-HTML, Lexer['Text only'].colorize("Hello")
<div class="highlight"><pre>Hello <div class="highlight"><pre>Hello
</pre> </pre>
</div> </div>
HTML HTML
assert_equal <<-HTML, Lexer['Ruby'].colorize("def foo\n 'foo'\nend\n") assert_equal <<-HTML, Lexer['Ruby'].colorize("def foo\n 'foo'\nend\n")
<div class="highlight"><pre><span class="k">def</span> <span class="nf">foo</span> <div class="highlight"><pre><span class="k">def</span> <span class="nf">foo</span>
<span class="s1">&#39;foo&#39;</span> <span class="s1">&#39;foo&#39;</span>
<span class="k">end</span> <span class="k">end</span>
</pre> </pre>
</div> </div>
HTML HTML
end end
def test_colorize_without_wrapper def test_colorize_without_wrapper
assert_equal <<-HTML, Lexer['Text only'].colorize_without_wrapper("Hello") assert_equal <<-HTML, Lexer['Text only'].colorize_without_wrapper("Hello")
Hello Hello
HTML HTML
assert_equal <<-HTML, Lexer['Ruby'].colorize_without_wrapper("def foo\n 'foo'\nend\n") assert_equal <<-HTML, Lexer['Ruby'].colorize_without_wrapper("def foo\n 'foo'\nend\n")
<span class="k">def</span> <span class="nf">foo</span> <span class="k">def</span> <span class="nf">foo</span>
<span class="s1">&#39;foo&#39;</span> <span class="s1">&#39;foo&#39;</span>
<span class="k">end</span> <span class="k">end</span>
HTML HTML
end
end end
end end