diff --git a/lib/linguist/blob_helper.rb b/lib/linguist/blob_helper.rb index 8800afa3..b6ae5409 100644 --- a/lib/linguist/blob_helper.rb +++ b/lib/linguist/blob_helper.rb @@ -137,5 +137,10 @@ module Linguist return if !text? || large? lexer.colorize(data) end + + def colorize_without_wrapper + return if !text? || large? + lexer.colorize_without_wrapper(data) + end end end diff --git a/lib/linguist/language.rb b/lib/linguist/language.rb index 230dbbd3..17ba0878 100644 --- a/lib/linguist/language.rb +++ b/lib/linguist/language.rb @@ -81,6 +81,10 @@ module Linguist lexer.colorize(text) end + def colorize_without_wrapper(text) + lexer.colorize_without_wrapper(text) + end + def ==(other) eql?(other) end diff --git a/lib/linguist/lexer.rb b/lib/linguist/lexer.rb index fc9707d4..a41e4d2c 100644 --- a/lib/linguist/lexer.rb +++ b/lib/linguist/lexer.rb @@ -23,7 +23,15 @@ module Linguist end def colorize(text) - Albino.colorize(text, to_s) + Albino.colorize(text, self) + end + + def colorize_without_wrapper(text) + if text = colorize(text) + text[%r{
(.*?)
\s*
}m, 1] + else + '' + end end def ==(other) diff --git a/test/test_blob.rb b/test/test_blob.rb index 1acd1457..ef93d9d5 100644 --- a/test/test_blob.rb +++ b/test/test_blob.rb @@ -179,4 +179,11 @@ class TestBlob < Test::Unit::TestCase HTML end + + def test_colorize_without_wrapper + assert_equal <<-HTML, blob("foo.rb").colorize_without_wrapper +module Foo +end + HTML + end end diff --git a/test/test_language.rb b/test/test_language.rb index 4a8934e6..b21ee59d 100644 --- a/test/test_language.rb +++ b/test/test_language.rb @@ -111,4 +111,16 @@ class TestLanguage < Test::Unit::TestCase HTML end + + def test_colorize_without_wrapper + assert_equal <<-HTML, Language['Text'].colorize_without_wrapper("Hello") +Hello + HTML + + assert_equal <<-HTML, Language['Ruby'].colorize_without_wrapper("def foo\n 'foo'\nend\n") +def foo + 'foo' +end + HTML + end end diff --git a/test/test_lexer.rb b/test/test_lexer.rb index f7cf0a9a..df9caa4f 100644 --- a/test/test_lexer.rb +++ b/test/test_lexer.rb @@ -50,4 +50,16 @@ class TestLexer < Test::Unit::TestCase HTML end + + def test_colorize_without_wrapper + assert_equal <<-HTML, Lexer['Text only'].colorize_without_wrapper("Hello") +Hello + HTML + + assert_equal <<-HTML, Lexer['Ruby'].colorize_without_wrapper("def foo\n 'foo'\nend\n") +def foo + 'foo' +end + HTML + end end