require 'irb/ruby-lex'
require 'cgi'
module RubyToken
class << Token
def to_html(text)
"<span class='#{html_class}'>#{CGI.escapeHTML(text)}</span>"
end
def html_class
"any"
end
end
class << TkSPACE
def to_html(text)
text
end
end
class << TkNL
def to_html(text)
text
end
end
@HtmlClasses = {
'Keyword' => [:TkId],
'Symbol' => [:TkOp, :TkASSIGN, :TkDOT, :TkLPAREN, :TkLBRACK, :TkLBRACE, :TkRPAREN, :TkRBRACK, :TkRBRACE,
:TkCOMMA, :TkSEMICOLON, :TkfLPAREN, :TkfLBRACK, :TkfLBRACE, :TkSTAR, :TkAMPER, :TkSYMBEG,
:TkBACKSLASH, :TkAT, :TkDOLLAR, :TkUnknownChar],
'Comment' => [:TkCOMMENT],
'RDocComment' => [:TkRD_COMMENT],
'String' => [:TkSTRING, :TkXSTRING, :TkDSTRING, :TkDXSTRING],
'RegExp' => [:TkREGEXP, :TkDREGEXP ],
'RegExpRef' => [:TkNTH_REF, :TkBACK_REF],
'Name' => [:TkIDENTIFIER, :TkFID, :TkGVAR, :TkCVAR, :TkIVAR],
'Constant' => [:TkCONSTANT],
'Value' => [:TkVal],
'Error' => [:TkError]
}
@HtmlClasses.each_pair do |html, tokens|
tokens.each do |token|
begin
eval("class << #{token}; def html_class() '#{html}'; end; end;")
rescue NameError
end
end
end
end
class RubyToHtml
@RCS_ID='-$Id: ruby_html.rb,v 1.2 2004/01/26 22:01:49 gra Exp $-'
Template = <<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
<title>%s</title>
<link rel="stylesheet" type="text/css" href="/ruby.css">
</head>
<body>
%s
</body>
</html>
EOF
class << self
def ruby_html(input)
result = "<pre class='ruby'>\n"
lex = RubyLex.new
lex.set_input(input)
while tk = lex.token
text = lex.get_readed
result << tk.class.to_html(text)
end
result << "</pre>\n"
end
def process
if ENV['QUERY_STRING']
fileName = ENV['QUERY_STRING']
filePath = ENV['DOCUMENT_ROOT'] + '/' + fileName
print "Content-Type: text/html;charset=US-ASCII\n\n"
elsif ENV['PATH_TRANSLATED']
filePath = ENV['PATH_TRANSLATED']
print "Content-Type: text/html;charset=US-ASCII\n\n"
elsif ARGV[0]
filePath = ARGV[0]
else
filePath = $0
end
fileName = File.split(filePath)[-1]
File.open(filePath) do |input|
printf(Template, fileName, ruby_html(input))
end
end
end
end
RubyToHtml::process()