1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/sample/ripper/colorize.rb
aamine 2c17921671 * parse.y [ripper]: on__scan event removed.
* parse.y [ripper]: event name is changed: on__XXX -> on_XXX.
* ext/ripper/eventids2.c: ditto.
* ext/ripper/ripper.rb.in: ditto.
* ext/ripper/lib/ripper.rb: sync with ripper.rb.in.
* ext/ripper/lib/ripper/tokenizer: ditto.
* ext/ripper/lib/ripper/filter: new file.
* sample/ripper/colorize.rb: new file.
* sample/ripper/strip-comment.rb: new file.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6952 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-09-22 05:22:50 +00:00

30 lines
525 B
Ruby

require 'ripper/filter'
class ColorizeFilter < Ripper::Filter
def on_default(event, tok, f)
f << escape(tok)
end
def on_comment(tok, f)
f << %Q[<span class="comment">#{escape(tok)}</span>]
end
def on_tstring_content(tok, f)
f << %Q[<span class="string">#{escape(tok)}</span>]
end
ESC = {
'&' => '&amp;',
'<' => '&lt;',
'>' => '&gt;'
}
def escape(str)
tbl = ESC
str.gsub(/[&<>]/) {|ch| tbl[ch] }
end
end
if $0 == __FILE__
ColorizeFilter.new(ARGF).parse($stdout)
end