diff options
author | murphy <murphy@rubychan.de> | 2008-12-25 01:22:30 +0000 |
---|---|---|
committer | murphy <murphy@rubychan.de> | 2008-12-25 01:22:30 +0000 |
commit | 79a313948f2277cf0f2e238d5765a71fee03e089 (patch) | |
tree | 7f6c2de879231f1b7a30d9ba481984e79fede9c1 /lib/coderay | |
parent | bca06f1ec361537c4c7f1368ca8838be2940dafe (diff) | |
download | coderay-79a313948f2277cf0f2e238d5765a71fee03e089.tar.gz |
Fixes for Ruby 1.9.
Diffstat (limited to 'lib/coderay')
-rw-r--r-- | lib/coderay/encoder.rb | 13 | ||||
-rw-r--r-- | lib/coderay/encoders/html.rb | 91 | ||||
-rw-r--r-- | lib/coderay/scanners/json.rb | 10 |
3 files changed, 60 insertions, 54 deletions
diff --git a/lib/coderay/encoder.rb b/lib/coderay/encoder.rb index 28b387b..07733a1 100644 --- a/lib/coderay/encoder.rb +++ b/lib/coderay/encoder.rb @@ -133,7 +133,7 @@ module CodeRay # whether +text+ is a String. def token text, kind out = - if text.is_a? ::String # Ruby 1.9: watch out, :open.is_a? String is true + if text.is_a? ::String text_token text, kind elsif text.is_a? ::Symbol block_token text, kind @@ -171,9 +171,14 @@ module CodeRay # # The already created +tokens+ object must be used; it can be a # TokenStream or a Tokens object. - def compile tokens, options - tokens.each { |text, kind| token text, kind } # FIXME for Ruby 1.9? - #tokens.each(&self) + if RUBY_VERSION >= '1.9' + def compile tokens, options + tokens.each { |text, kind| token text, kind } # FIXME for Ruby 1.9? + end + else + def compile tokens, options + tokens.each(&self) + end end end diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb index 8d13cf5..035ee65 100644 --- a/lib/coderay/encoders/html.rb +++ b/lib/coderay/encoders/html.rb @@ -220,8 +220,13 @@ module Encoders super end - def token text, type - if text.is_a? ::String + def token text, type = :plain + case text + + when nil + # raise 'Token with nil as text was given: %p' % [[text, type]] + + when String if text =~ /#{HTML_ESCAPE_PATTERN}/o text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] } end @@ -231,53 +236,49 @@ module Encoders else @out << text end - else - - case text - - # token groups, eg. strings - when :open - @opened[0] = type - @out << (@css_style[@opened] || '<span>') - @opened << type - when :close - 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 token (%p) \ - that is not open. Open are: %p.' % [type, @opened[1..-1]] - end - @out << '</span>' - @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('<span', '<div') - else - @out << '<div>' - 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 << '</div>' - @opened.pop + + # token groups, eg. strings + when :open + @opened[0] = type + @out << (@css_style[@opened] || '<span>') + @opened << type + when :close + 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 token (%p) \ + that is not open. Open are: %p.' % [type, @opened[1..-1]] end - - when nil - raise 'Token with nil as text was given: %p' % [[text, type]] + @out << '</span>' + @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('<span', '<div') else - raise 'unknown token kind: %p' % text + @out << '<div>' 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 << '</div>' + @opened.pop + end + + else + raise 'unknown token kind: %p' % [text] + end end diff --git a/lib/coderay/scanners/json.rb b/lib/coderay/scanners/json.rb index ae941a0..eb67347 100644 --- a/lib/coderay/scanners/json.rb +++ b/lib/coderay/scanners/json.rb @@ -34,11 +34,11 @@ module Scanners elsif match = scan(/ [:,\[{\]}] /x) kind = :operator case match - when '{': stack << :object; key_expected = true - when '[': stack << :array - when ':': key_expected = false - when ',': key_expected = true if stack.last == :object - when '}', ']': stack.pop # no error recovery, but works for valid JSON + when '{' then stack << :object; key_expected = true + when '[' then stack << :array + when ':' then key_expected = false + when ',' then key_expected = true if stack.last == :object + when '}', ']' then stack.pop # no error recovery, but works for valid JSON end elsif match = scan(/ true | false | null /x) kind = IDENT_KIND[match] |