Skip to content

Commit

Permalink
Fixes for Ruby 1.9.
Browse files Browse the repository at this point in the history
  • Loading branch information
korny committed Dec 25, 2008
1 parent bca06f1 commit 79a3139
Show file tree
Hide file tree
Showing 28 changed files with 2,228 additions and 83 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ task '19' do
end

task '18' do
RUBY.replace 'ruby'
RUBY.replace 'ruby18'
end

task '187' do
Expand Down
2 changes: 1 addition & 1 deletion bench/bench.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require 'profile' if ARGV.include? '-p'

MYDIR = File.dirname(__FILE__)
LIBDIR = Pathname.new(MYDIR).join('..', 'lib').cleanpath
LIBDIR = Pathname.new(MYDIR).join('..', 'lib').cleanpath.to_s
$LOAD_PATH.unshift MYDIR, LIBDIR
require 'coderay'

Expand Down
10 changes: 6 additions & 4 deletions etc/coderay-lib.tmproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<key>documents</key>
<array>
<dict>
<key>expanded</key>
<true/>
<key>name</key>
<string>lib</string>
<key>regexFolderFilter</key>
Expand Down Expand Up @@ -83,14 +85,12 @@
<string>../diff</string>
<key>lastUsed</key>
<date>2008-09-30T16:57:58Z</date>
<key>selected</key>
<true/>
</dict>
<dict>
<key>filename</key>
<string>../TODO</string>
<key>lastUsed</key>
<date>2008-01-21T03:03:08Z</date>
<date>2008-11-06T18:26:56Z</date>
</dict>
<dict>
<key>name</key>
Expand All @@ -104,7 +104,9 @@
<key>filename</key>
<string>../test/scanners/coderay_suite.rb</string>
<key>lastUsed</key>
<date>2008-09-30T15:54:46Z</date>
<date>2008-12-25T01:19:30Z</date>
<key>selected</key>
<true/>
</dict>
<dict>
<key>filename</key>
Expand Down
2 changes: 0 additions & 2 deletions etc/coderay.tmproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
<string>coderay</string>
<key>regexFolderFilter</key>
<string>!.*/(\.[^/]*|CVS|_darcs|\{arch\}|blib|.*~\.nib|.*\.(framework|app|pbproj|pbxproj|xcode(proj)?|bundle))$</string>
<key>selected</key>
<true/>
<key>sourceDirectory</key>
<string>..</string>
</dict>
Expand Down
13 changes: 9 additions & 4 deletions lib/coderay/encoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def setup options
# 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
Expand Down Expand Up @@ -171,9 +171,14 @@ def finish options
#
# 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
Expand Down
91 changes: 46 additions & 45 deletions lib/coderay/encoders/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,13 @@ def finish options
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
Expand All @@ -231,53 +236,49 @@ def token text, type
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

Expand Down
10 changes: 5 additions & 5 deletions lib/coderay/scanners/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ def scan_tokens tokens, options
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]
Expand Down
40 changes: 24 additions & 16 deletions test/functional/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,40 @@ def test_duo_stream
CodeRay::Duo[:plain, :plain].highlight(RUBY_TEST_CODE, :stream => true))
end

def test_for_redcloth
begin
require 'rubygems'
require 'coderay/for_redcloth'
assert_equal "<p><span lang=\"ruby\" class=\"CodeRay\">puts <span style=\"background-color:#fff0f0;color:#D20\"><span style=\"color:#710\">\"</span><span style=\"\">Hello, World!</span><span style=\"color:#710\">\"</span></span></span></p>",
RedCloth.new('@[ruby]puts "Hello, World!"@').to_html
assert_equal <<-BLOCKCODE.chomp,
gem 'RedCloth', '>= 4.0.3' rescue nil
require 'redcloth'

def test_for_redcloth
require 'rubygems'
require 'coderay/for_redcloth'
assert_equal "<p><span lang=\"ruby\" class=\"CodeRay\">puts <span style=\"background-color:#fff0f0;color:#D20\"><span style=\"color:#710\">\"</span><span style=\"\">Hello, World!</span><span style=\"color:#710\">\"</span></span></span></p>",
RedCloth.new('@[ruby]puts "Hello, World!"@').to_html
assert_equal <<-BLOCKCODE.chomp,
<div lang="ruby" class="CodeRay">
<div class="code"><pre>puts <span style="background-color:#fff0f0;color:#D20"><span style="color:#710">&quot;</span><span style="">Hello, World!</span><span style="color:#710">&quot;</span></span></pre></div>
</div>
</pre>
BLOCKCODE
RedCloth.new('bc[ruby]. puts "Hello, World!"').to_html
end
BLOCKCODE
RedCloth.new('bc[ruby]. puts "Hello, World!"').to_html
end

def test_for_redcloth_escapes
require 'rubygems'
require 'coderay/for_redcloth'
assert_equal '<p><span lang="ruby" class="CodeRay">&gt;</span></p>',
RedCloth.new('@[ruby]>@').to_html
assert_equal <<-BLOCKCODE.chomp,
def test_for_redcloth_escapes
require 'rubygems'
require 'coderay/for_redcloth'
assert_equal '<p><span lang="ruby" class="CodeRay">&gt;</span></p>',
RedCloth.new('@[ruby]>@').to_html
assert_equal <<-BLOCKCODE.chomp,
<div lang="ruby" class="CodeRay">
<div class="code"><pre>&amp;</pre></div>
</div>
</pre>
BLOCKCODE
RedCloth.new('bc[ruby]. &').to_html
BLOCKCODE
RedCloth.new('bc[ruby]. &').to_html
end
rescue LoadError
$stderr.puts 'RedCloth not found.'
end

ENCODERS_LIST = %w(
Expand Down
2 changes: 1 addition & 1 deletion test/functional/suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'pathname'

MYDIR = File.dirname(__FILE__)
LIBDIR = Pathname.new(MYDIR).join('..', '..', 'lib').cleanpath
LIBDIR = Pathname.new(MYDIR).join('..', '..', 'lib').cleanpath.to_s
$LOAD_PATH.unshift MYDIR, LIBDIR

require 'basic'
Expand Down
2 changes: 2 additions & 0 deletions test/lib/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Contents:
- test/unit: We need the old Test::Unit for the scanner test suite to work with Ruby 1.9.
Loading

0 comments on commit 79a3139

Please sign in to comment.