mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	* sample/ripper/colorize.rb: removed (replaced by ruby2html.rb).
* sample/ripper/ruby2html.rb: added. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9285 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
		
							parent
							
								
									1d62cc0ecc
								
							
						
					
					
						commit
						4a092d08f4
					
				
					 3 changed files with 118 additions and 30 deletions
				
			
		| 
						 | 
				
			
			@ -1,3 +1,9 @@
 | 
			
		|||
Sat Sep 24 07:59:01 2005  Minero Aoki  <aamine@loveruby.net>
 | 
			
		||||
 | 
			
		||||
	* sample/ripper/colorize.rb: removed (replaced by ruby2html.rb).
 | 
			
		||||
 | 
			
		||||
	* sample/ripper/ruby2html.rb: added.
 | 
			
		||||
 | 
			
		||||
Sat Sep 24 06:35:15 2005  Minero Aoki  <aamine@loveruby.net>
 | 
			
		||||
 | 
			
		||||
	* ext/ripper: no longer generates .rb files.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,30 +0,0 @@
 | 
			
		|||
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 = {
 | 
			
		||||
    '&' => '&',
 | 
			
		||||
    '<' => '<',
 | 
			
		||||
    '>' => '>'
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  def escape(str)
 | 
			
		||||
    tbl = ESC
 | 
			
		||||
    str.gsub(/[&<>]/) {|ch| tbl[ch] }
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
if $0 == __FILE__
 | 
			
		||||
  ColorizeFilter.new(ARGF).parse($stdout)
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										112
									
								
								sample/ripper/ruby2html.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								sample/ripper/ruby2html.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,112 @@
 | 
			
		|||
#!/usr/bin/env ruby
 | 
			
		||||
# $originalId: ruby2html.rb,v 1.2 2005/09/23 22:53:47 aamine Exp $
 | 
			
		||||
 | 
			
		||||
TEMPLATE_LINE = __LINE__ + 2
 | 
			
		||||
TEMPLATE = <<-EndTemplate
 | 
			
		||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 | 
			
		||||
<html>
 | 
			
		||||
<head>
 | 
			
		||||
  <meta http-equiv="Content-Type" content="text/html; charset=<%= encoding %>">
 | 
			
		||||
<% if  css %>
 | 
			
		||||
  <link rel="stylesheet" type="text/css" href="<%= css %>">
 | 
			
		||||
<% end  %>
 | 
			
		||||
  <title><%= File.basename(f.path) %></title>
 | 
			
		||||
</head>
 | 
			
		||||
<body>
 | 
			
		||||
<pre>
 | 
			
		||||
<%
 | 
			
		||||
    if print_line_number
 | 
			
		||||
      Ruby2HTML.compile(f).each_with_index do |line, idx|
 | 
			
		||||
%><%= sprintf('%4d  %s', idx+1, line) %><%
 | 
			
		||||
      end
 | 
			
		||||
    else
 | 
			
		||||
%><%= Ruby2HTML.compile(f) %><%
 | 
			
		||||
    end
 | 
			
		||||
%>
 | 
			
		||||
</pre>
 | 
			
		||||
</body>
 | 
			
		||||
</html>
 | 
			
		||||
EndTemplate
 | 
			
		||||
 | 
			
		||||
require 'ripper'
 | 
			
		||||
require 'stringio'
 | 
			
		||||
require 'cgi'
 | 
			
		||||
require 'erb'
 | 
			
		||||
require 'optparse'
 | 
			
		||||
 | 
			
		||||
def main
 | 
			
		||||
  encoding = 'us-ascii'
 | 
			
		||||
  css = nil
 | 
			
		||||
  print_line_number = false
 | 
			
		||||
  parser = OptionParser.new
 | 
			
		||||
  parser.banner = "Usage: #{File.basename($0)} [-l] [<file>...]"
 | 
			
		||||
  parser.on('--encoding=NAME', 'Character encoding [us-ascii].') {|name|
 | 
			
		||||
    encoding = name
 | 
			
		||||
  }
 | 
			
		||||
  parser.on('--css=URL', 'Set a link to CSS.') {|url|
 | 
			
		||||
    css = url
 | 
			
		||||
  }
 | 
			
		||||
  parser.on('-l', '--line-number', 'Show line number.') {
 | 
			
		||||
    print_line_number = true
 | 
			
		||||
  }
 | 
			
		||||
  parser.on('--help', 'Prints this message and quit.') {
 | 
			
		||||
    puts parser.help
 | 
			
		||||
    exit 0
 | 
			
		||||
  }
 | 
			
		||||
  begin
 | 
			
		||||
    parser.parse!
 | 
			
		||||
  rescue OptionParser::ParseError => err
 | 
			
		||||
    $stderr.puts err
 | 
			
		||||
    $stderr.puts parser.help
 | 
			
		||||
    exit 1
 | 
			
		||||
  end
 | 
			
		||||
  puts ruby2html(ARGF, encoding, css, print_line_number)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
class ERB
 | 
			
		||||
  attr_accessor :lineno
 | 
			
		||||
 | 
			
		||||
  remove_method :result
 | 
			
		||||
  def result(b)
 | 
			
		||||
    eval(@src, b, (@filename || '(erb)'), (@lineno || 1))
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
def ruby2html(f, encoding, css, print_line_number)
 | 
			
		||||
  erb = ERB.new(TEMPLATE, nil, '>')
 | 
			
		||||
  erb.filename = __FILE__
 | 
			
		||||
  erb.lineno = TEMPLATE_LINE
 | 
			
		||||
  erb.result(binding())
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
class Ruby2HTML < Ripper::Filter
 | 
			
		||||
  def Ruby2HTML.compile(f)
 | 
			
		||||
    buf = StringIO.new
 | 
			
		||||
    Ruby2HTML.new(f).parse(buf)
 | 
			
		||||
    buf.string
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def on_default(event, tok, f)
 | 
			
		||||
    f << CGI.escapeHTML(tok)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def on_kw(tok, f)
 | 
			
		||||
    f << %Q[<span class="resword">#{CGI.escapeHTML(tok)}</span>]
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def on_comment(tok, f)
 | 
			
		||||
    f << %Q[<span class="comment">#{CGI.escapeHTML(tok.rstrip)}</span>\n]
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def on_tstring_beg(tok, f)
 | 
			
		||||
    f << %Q[<span class="string">#{CGI.escapeHTML(tok)}]
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def on_tstring_end(tok, f)
 | 
			
		||||
    f << %Q[#{CGI.escapeHTML(tok)}</span>]
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
if $0 == __FILE__
 | 
			
		||||
  main
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue