From a4bd413ca4e835fd3d1fdc24eebce67cd54231ca Mon Sep 17 00:00:00 2001 From: murphy Date: Tue, 30 Sep 2008 16:42:53 +0000 Subject: New: *Simple Diff Scanner* (closes #22). * Highlights unified diffs, especially like the ones svn diff outputs. * Changes to make highlighting of whole lines were necessary. * I added two example files. More changes: * Added token classes :head, :delete, :insert, and :change along with styles. * Added two new special token types: :begin_line and :end_line. They mark token groups that explicitly span whole lines and should be highlighted as such. * The HTML encoder converts these new tokens to DIVs. May need more work. * The Debug Encoder uses square brackets for line tokens. * Some cleanups. --- lib/coderay/encoders/html.rb | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'lib/coderay/encoders/html.rb') diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index 2edcf1d..8d13cf5 100644 --- a/lib/coderay/encoders/html.rb +++ b/lib/coderay/encoders/html.rb @@ -226,13 +226,16 @@ module Encoders text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] } end @opened[0] = type - if style = @css_style[@opened] + if text != "\n" && style = @css_style[@opened] @out << style << text << '' else @out << text end else + case text + + # token groups, eg. strings when :open @opened[0] = type @out << (@css_style[@opened] || '') @@ -248,6 +251,28 @@ module Encoders @out << '' @opened.pop end + + # whole lines to be highlighted, eg. a deleted line in a diff + when :begin_line + @opened[0] = type + if style = @css_style[@opened] + @out << style.sub('' + end + @opened << type + when :end_line + if @opened.empty? + # nothing to close + else + if $DEBUG and (@opened.size == 1 or @opened.last != type) + raise 'Malformed token stream: Trying to close a line (%p) \ + that is not open. Open are: %p.' % [type, @opened[1..-1]] + end + @out << '' + @opened.pop + end + when nil raise 'Token with nil as text was given: %p' % [[text, type]] else -- cgit v1.2.1