Created
July 6, 2011 03:40
-
-
Save aisuii/1066509 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ripper' | |
require 'cgi' | |
require 'digest/sha1' | |
class Ruby2HTML < Ripper::Filter | |
def on_default(event, tok, f) | |
f << CGI.escapeHTML(tok) | |
end | |
private | |
def respond_to?(name, priv = false) | |
!!event_pattern_matching(name) or super | |
end | |
def method_missing(name, *args) | |
if event = event_pattern_matching(name) | |
tok, f = args | |
f << %Q[<span class="#{event}">#{CGI.escapeHTML(tok)}</span>] | |
else | |
super | |
end | |
end | |
def event_pattern_matching(on_name) | |
on_name.to_s =~ /^on_(.+)$/ and Ripper::SCANNER_EVENTS.include? $1.to_sym and $1 | |
end | |
end | |
if $0 == __FILE__ | |
def css | |
"".tap do |css| | |
Ripper::SCANNER_EVENTS.each do |event| | |
color = Digest::SHA1.hexdigest(event.to_s)[0, 3] | |
css << ".#{event} { color: ##{color} }\n" | |
end | |
end | |
end | |
def html(out = $stdout, &block) | |
out.write(<<-EOL) | |
<html> | |
<head> | |
<style type="text/css"> | |
* { font-family: monospace } | |
pre { padding: 10px; background: #333; } | |
#{css} | |
</style> | |
</head> | |
<body> | |
<pre> | |
#{yield} | |
</pre> | |
</body> | |
</html> | |
EOL | |
end | |
html do | |
Ruby2HTML.new(ARGF).parse('') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ruby iro.rb NANIKA_RUBY_SCRIPT.rb > hoge.html