summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/coderay/encoders/lines_of_code.rb19
-rwxr-xr-xtest/functional/basic.rb30
2 files changed, 49 insertions, 0 deletions
diff --git a/lib/coderay/encoders/lines_of_code.rb b/lib/coderay/encoders/lines_of_code.rb
new file mode 100644
index 0000000..8ffe9dc
--- /dev/null
+++ b/lib/coderay/encoders/lines_of_code.rb
@@ -0,0 +1,19 @@
+module CodeRay
+module Encoders
+
+ class LinesOfCode < Encoder
+
+ register_for :lines_of_code
+
+ def compile tokens, options
+ @loc = tokens.token_class_filter(:exclude => [:comment, :doctype]).text.scan(/^\s*\S.*$/).size
+ end
+
+ def finish options
+ @loc
+ end
+
+ end
+
+end
+end
diff --git a/test/functional/basic.rb b/test/functional/basic.rb
index ed4b3cc..bdb8608 100755
--- a/test/functional/basic.rb
+++ b/test/functional/basic.rb
@@ -35,6 +35,36 @@ class BasicTest < Test::Unit::TestCase
CodeRay::Duo[:plain, :plain].highlight(RUBY_TEST_CODE, :stream => true))
end
+ def test_comment_filter
+ assert_equal <<-EXPECTED, CodeRay.scan(<<-INPUT, :ruby).comment_filter.text
+#!/usr/bin/env ruby
+
+code
+
+more code
+ EXPECTED
+#!/usr/bin/env ruby
+=begin
+A multi-line comment.
+=end
+code
+# A single-line comment.
+more code # and another comment, in-line.
+ INPUT
+ end
+
+ def test_lines_of_code
+ assert_equal 2, CodeRay.scan(<<-INPUT, :ruby).lines_of_code
+#!/usr/bin/env ruby
+=begin
+A multi-line comment.
+=end
+code
+# A single-line comment.
+more code # and another comment, in-line.
+ INPUT
+ end
+
begin
require 'rubygems'
gem 'RedCloth', '>= 4.0.3' rescue nil