From bebae09fded5cfa9d1c01474fafb586655637fb6 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Fri, 20 May 2011 14:01:12 -0500 Subject: [PATCH] Add colorize --- lib/linguist/blob_helper.rb | 5 +++++ lib/linguist/language.rb | 4 ++++ lib/linguist/lexer.rb | 5 +++++ test/test_blob.rb | 9 +++++++++ test/test_language.rb | 16 ++++++++++++++++ test/test_lexer.rb | 16 ++++++++++++++++ 6 files changed, 55 insertions(+) diff --git a/lib/linguist/blob_helper.rb b/lib/linguist/blob_helper.rb index ebd95594..8800afa3 100644 --- a/lib/linguist/blob_helper.rb +++ b/lib/linguist/blob_helper.rb @@ -132,5 +132,10 @@ module Linguist end end end + + def colorize + return if !text? || large? + lexer.colorize(data) + end end end diff --git a/lib/linguist/language.rb b/lib/linguist/language.rb index 1ad021ce..230dbbd3 100644 --- a/lib/linguist/language.rb +++ b/lib/linguist/language.rb @@ -77,6 +77,10 @@ module Linguist !popular? end + def colorize(text) + lexer.colorize(text) + end + def ==(other) eql?(other) end diff --git a/lib/linguist/lexer.rb b/lib/linguist/lexer.rb index ab03a815..fc9707d4 100644 --- a/lib/linguist/lexer.rb +++ b/lib/linguist/lexer.rb @@ -1,3 +1,4 @@ +require 'albino' require 'yaml' module Linguist @@ -21,6 +22,10 @@ module Linguist aliases.first end + def colorize(text) + Albino.colorize(text, to_s) + end + def ==(other) eql?(other) end diff --git a/test/test_blob.rb b/test/test_blob.rb index 7453e68c..1acd1457 100644 --- a/test/test_blob.rb +++ b/test/test_blob.rb @@ -170,4 +170,13 @@ class TestBlob < Test::Unit::TestCase assert_equal nil, blob("script.foo").shebang_language assert_equal nil, blob("foo.rb").shebang_language end + + def test_colorize + assert_equal <<-HTML, blob("foo.rb").colorize +
module Foo
+end
+
+
+ HTML + end end diff --git a/test/test_language.rb b/test/test_language.rb index dbb2fda2..4a8934e6 100644 --- a/test/test_language.rb +++ b/test/test_language.rb @@ -95,4 +95,20 @@ class TestLanguage < Test::Unit::TestCase assert Language['Assembly'].unpopular? assert Language['Brainfuck'].unpopular? end + + def test_colorize + assert_equal <<-HTML, Language['Text'].colorize("Hello") +
Hello
+
+
+ HTML + + assert_equal <<-HTML, Language['Ruby'].colorize("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 b81fd5a6..f7cf0a9a 100644 --- a/test/test_lexer.rb +++ b/test/test_lexer.rb @@ -34,4 +34,20 @@ class TestLexer < Test::Unit::TestCase assert !Lexer['Ruby'].eql?(Lexer['Python']) assert !Lexer['Ruby'].eql?(Lexer.new('Ruby')) end + + def test_colorize + assert_equal <<-HTML, Lexer['Text only'].colorize("Hello") +
Hello
+
+
+ HTML + + assert_equal <<-HTML, Lexer['Ruby'].colorize("def foo\n 'foo'\nend\n") +
def foo
+  'foo'
+end
+
+
+ HTML + end end