diff options
-rw-r--r-- | etc/coderay-lib.tmproj | 6 | ||||
-rw-r--r-- | lib/coderay/encoders/html.rb | 11 | ||||
-rw-r--r-- | lib/coderay/encoders/html/numerization.rb | 12 | ||||
-rw-r--r-- | lib/coderay/styles/cycnus.rb | 1 | ||||
-rw-r--r-- | test/scanners/coderay_suite.rb | 16 |
5 files changed, 37 insertions, 9 deletions
diff --git a/etc/coderay-lib.tmproj b/etc/coderay-lib.tmproj index b7c9f24..fc48f5c 100644 --- a/etc/coderay-lib.tmproj +++ b/etc/coderay-lib.tmproj @@ -90,13 +90,13 @@ <key>filename</key> <string>../Rakefile</string> <key>lastUsed</key> - <date>2009-01-08T15:37:13Z</date> + <date>2009-01-13T15:38:32Z</date> </dict> <dict> <key>filename</key> <string>../diff</string> <key>lastUsed</key> - <date>2009-01-13T06:56:37Z</date> + <date>2009-01-13T16:07:48Z</date> </dict> <dict> <key>filename</key> @@ -116,7 +116,7 @@ <key>filename</key> <string>../test/scanners/coderay_suite.rb</string> <key>lastUsed</key> - <date>2009-01-13T06:27:23Z</date> + <date>2009-01-13T15:34:57Z</date> </dict> <dict> <key>filename</key> diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index 035ee65..f7f99f7 100644 --- a/lib/coderay/encoders/html.rb +++ b/lib/coderay/encoders/html.rb @@ -56,6 +56,16 @@ module Encoders # # Default: 10 # + # === :highlight_lines + # + # Highlights certain line numbers now by using the :highlight_lines option. + # Can be any Enumerable, typically just an Array or Range, of numbers. + # + # Bolding is deactivated when :highlight_lines is set. It only makes sense + # in combination with :line_numbers. + # + # Default: nil + # # === :hint # Include some information into the output using the title attribute. # Can be :info (show token type on mouse-over), :info_long (with full path) @@ -82,6 +92,7 @@ module Encoders :line_numbers => nil, :line_number_start => 1, :bold_every => 10, + :highlight_lines => nil, :hint => false, } diff --git a/lib/coderay/encoders/html/numerization.rb b/lib/coderay/encoders/html/numerization.rb index 9f1667f..0b6cf7a 100644 --- a/lib/coderay/encoders/html/numerization.rb +++ b/lib/coderay/encoders/html/numerization.rb @@ -32,9 +32,19 @@ module Encoders #end bold_every = options[:bold_every] + highlight_lines = options[:highlight_lines] bolding = - if bold_every == false + if bold_every == false && highlight_lines == nil proc { |line| line.to_s } + elsif highlight_lines.is_a? Enumerable + highlight_lines = highlight_lines.to_set + proc do |line| + if highlight_lines.include? line + "<strong class=\"highlighted\">#{line}</strong>" # highlighted line numbers in bold + else + line.to_s + end + end elsif bold_every.is_a? Integer raise ArgumentError, ":bolding can't be 0." if bold_every == 0 proc do |line| diff --git a/lib/coderay/styles/cycnus.rb b/lib/coderay/styles/cycnus.rb index fd44385..d274545 100644 --- a/lib/coderay/styles/cycnus.rb +++ b/lib/coderay/styles/cycnus.rb @@ -32,6 +32,7 @@ table.CodeRay td { padding: 2px 4px; vertical-align: top } text-align: right; } .CodeRay .line_numbers tt { font-weight: bold } +.CodeRay .line_numbers .highlighted { color: red } .CodeRay .no { padding: 0px 4px } .CodeRay .code { width: 100% } diff --git a/test/scanners/coderay_suite.rb b/test/scanners/coderay_suite.rb index bdcbdce..9971889 100644 --- a/test/scanners/coderay_suite.rb +++ b/test/scanners/coderay_suite.rb @@ -209,12 +209,12 @@ module CodeRay print '-skipped- '.concealed end - tokens, ok = complete_test scanner, code, name + tokens, ok, changed_lines = complete_test scanner, code, name identity_test scanner, tokens unless ENV['nohighlighting'] or (code.size > MAX_CODE_SIZE_TO_HIGHLIGHT and not ENV['only']) - highlight_test tokens, name, ok + highlight_test tokens, name, ok, changed_lines else print '-- skipped -- '.concealed end @@ -303,6 +303,12 @@ module CodeRay File.open(actual_filename, 'wb') { |f| f.write result } diff = expected_filename.sub(/\.expected\..*/, '.debug.diff') system "diff --unified=0 --text #{expected_filename} #{actual_filename} > #{diff}" + changed_lines = [] + File.read(diff).scan(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/) do |offset, size| + offset = offset.to_i + size = (size || 1).to_i + changed_lines.concat Array(offset...offset + size) + end end assert(ok, "Scan error: unexpected output".red) if ENV['assert'] @@ -315,7 +321,7 @@ module CodeRay ok = true end - return tokens, ok + return tokens, ok, changed_lines end def identity_test scanner, tokens @@ -340,10 +346,10 @@ module CodeRay :css => :class ) - def highlight_test tokens, name, okay + def highlight_test tokens, name, okay, changed_lines report 'highlighting' do begin - highlighted = Highlighter.encode_tokens tokens + highlighted = Highlighter.encode_tokens tokens, { :highlight_lines => changed_lines } rescue flunk 'highlighting test failed!' if ENV['assert'] return false |