mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Add HTML formatter to ri
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5440 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
db4ec3c56a
commit
b4d94277bb
3 changed files with 157 additions and 29 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Mon Jan 12 02:24:07 2004 Dave Thomas <dave@pragprog.com>
|
||||||
|
|
||||||
|
* lib/rdoc/ri/ri_formatter.rb (RI::HtmlFormatter): Add HTML
|
||||||
|
generation support to ri (Elliot Hughes)
|
||||||
|
|
||||||
Sun Jan 11 23:54:41 2004 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Sun Jan 11 23:54:41 2004 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* env.h (ruby_frame, ruby_scope, ruby_in_eval, ruby_class,
|
* env.h (ruby_frame, ruby_scope, ruby_in_eval, ruby_class,
|
||||||
|
|
|
@ -2,12 +2,13 @@ module RI
|
||||||
class TextFormatter
|
class TextFormatter
|
||||||
|
|
||||||
def TextFormatter.list
|
def TextFormatter.list
|
||||||
"plain, bs, ansi"
|
"plain, html, bs, ansi"
|
||||||
end
|
end
|
||||||
|
|
||||||
def TextFormatter.for(name)
|
def TextFormatter.for(name)
|
||||||
case name
|
case name
|
||||||
when /plain/i then TextFormatter
|
when /plain/i then TextFormatter
|
||||||
|
when /html/i then HtmlFormatter
|
||||||
when /bs/i then OverstrikeFormatter
|
when /bs/i then OverstrikeFormatter
|
||||||
when /ansi/i then AnsiFormatter
|
when /ansi/i then AnsiFormatter
|
||||||
else nil
|
else nil
|
||||||
|
@ -159,10 +160,7 @@ module RI
|
||||||
display_list(item)
|
display_list(item)
|
||||||
|
|
||||||
when SM::Flow::VERB
|
when SM::Flow::VERB
|
||||||
item.body.split(/\n/).each do |line|
|
display_verbatim_flow_item(item, @indent)
|
||||||
print @indent, conv_html(line), "\n"
|
|
||||||
end
|
|
||||||
blankline
|
|
||||||
|
|
||||||
when SM::Flow::H
|
when SM::Flow::H
|
||||||
display_heading(conv_html(item.text.join), item.level, @indent)
|
display_heading(conv_html(item.text.join), item.level, @indent)
|
||||||
|
@ -177,6 +175,15 @@ module RI
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
|
def display_verbatim_flow_item(item, prefix=@indent)
|
||||||
|
item.body.split(/\n/).each do |line|
|
||||||
|
print @indent, conv_html(line), "\n"
|
||||||
|
end
|
||||||
|
blankline
|
||||||
|
end
|
||||||
|
|
||||||
|
######################################################################
|
||||||
|
|
||||||
def display_heading(text, level, indent)
|
def display_heading(text, level, indent)
|
||||||
case level
|
case level
|
||||||
when 1
|
when 1
|
||||||
|
@ -431,24 +438,144 @@ module RI
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# options = "options"
|
##################################################
|
||||||
# def options.width
|
|
||||||
# 70
|
# This formatter uses HTML.
|
||||||
# end
|
|
||||||
# a = OverstrikeFormatter.new(options, " ")
|
class HtmlFormatter < AttributeFormatter
|
||||||
# a.wrap(
|
|
||||||
# "The quick <b>brown</b> and <i>italic</i> dog " +
|
def initialize(*args)
|
||||||
# "The quick <b>brown and <i>italic</i></b> dog " +
|
super
|
||||||
# "The quick <b>brown and <i>italic</i></b> dog " +
|
end
|
||||||
# "The quick <b>brown and <i>italic</i></b> dog " +
|
|
||||||
# "The quick <b>brown and <i>italic</i></b> dog " +
|
def write_attribute_text(prefix, line)
|
||||||
# "The quick <b>brown and <i>italic</i></b> dog " +
|
curr_attr = 0
|
||||||
# "The quick <b>brown and <i>italic</i></b> dog " +
|
line.each do |achar|
|
||||||
# "The quick <b>brown and <i>italic</i></b> dog " +
|
attr = achar.attr
|
||||||
# "The quick <b>brown and <i>italic</i></b> dog " +
|
if achar.attr != curr_attr
|
||||||
# "The quick <b>brown and <i>italic</i></b> dog " +
|
update_attributes(curr_attr, achar.attr)
|
||||||
# "The quick <b>brown and <i>italic</i></b> dog "
|
curr_attr = achar.attr
|
||||||
# )
|
end
|
||||||
|
print(escape(achar.char))
|
||||||
|
end
|
||||||
|
update_attributes(curr_attr, 0) unless curr_attr.zero?
|
||||||
|
puts
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw_line(label=nil)
|
||||||
|
if label != nil
|
||||||
|
bold_print(label)
|
||||||
|
end
|
||||||
|
puts("<hr /><p />")
|
||||||
|
end
|
||||||
|
|
||||||
|
def bold_print(txt)
|
||||||
|
tag("b") { txt }
|
||||||
|
end
|
||||||
|
|
||||||
|
def blankline()
|
||||||
|
puts("<p>")
|
||||||
|
end
|
||||||
|
|
||||||
|
def display_heading(text, level, indent)
|
||||||
|
level = 4 if level > 4
|
||||||
|
tag("h#{level}") { text }
|
||||||
|
puts
|
||||||
|
end
|
||||||
|
|
||||||
|
######################################################################
|
||||||
|
|
||||||
|
def display_list(list)
|
||||||
|
|
||||||
|
case list.type
|
||||||
|
when SM::ListBase::BULLET
|
||||||
|
list_type = "ul"
|
||||||
|
prefixer = proc { |ignored| "<li>" }
|
||||||
|
|
||||||
|
when SM::ListBase::NUMBER,
|
||||||
|
SM::ListBase::UPPERALPHA,
|
||||||
|
SM::ListBase::LOWERALPHA
|
||||||
|
list_type = "ol"
|
||||||
|
prefixer = proc { |ignored| "<li>" }
|
||||||
|
|
||||||
|
when SM::ListBase::LABELED
|
||||||
|
list_type = "dl"
|
||||||
|
prefixer = proc do |li|
|
||||||
|
"<dt><b>" + escape(li.label) + "</b><dd>"
|
||||||
|
end
|
||||||
|
|
||||||
|
when SM::ListBase::NOTE
|
||||||
|
list_type = "table"
|
||||||
|
prefixer = proc do |li|
|
||||||
|
%{<tr valign="top"><td>#{li.label.gsub(/ /, ' ')}</td><td>}
|
||||||
|
end
|
||||||
|
else
|
||||||
|
fail "unknown list type"
|
||||||
|
end
|
||||||
|
|
||||||
|
print "<#{list_type}>"
|
||||||
|
list.contents.each do |item|
|
||||||
|
if item.kind_of? SM::Flow::LI
|
||||||
|
prefix = prefixer.call(item)
|
||||||
|
print prefix
|
||||||
|
display_flow_item(item, prefix)
|
||||||
|
else
|
||||||
|
display_flow_item(item)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
print "</#{list_type}>"
|
||||||
|
end
|
||||||
|
|
||||||
|
def display_verbatim_flow_item(item, prefix=@indent)
|
||||||
|
print("<pre>")
|
||||||
|
item.body.split(/\n/).each do |line|
|
||||||
|
puts conv_html(line)
|
||||||
|
end
|
||||||
|
puts("</pre>")
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
ATTR_MAP = {
|
||||||
|
BOLD => "b>",
|
||||||
|
ITALIC => "i>",
|
||||||
|
CODE => "tt>"
|
||||||
|
}
|
||||||
|
|
||||||
|
def update_attributes(current, wanted)
|
||||||
|
str = ""
|
||||||
|
# first turn off unwanted ones
|
||||||
|
off = current & ~wanted
|
||||||
|
for quality in [ BOLD, ITALIC, CODE]
|
||||||
|
if (off & quality) > 0
|
||||||
|
str << "</" + ATTR_MAP[quality]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# now turn on wanted
|
||||||
|
for quality in [ BOLD, ITALIC, CODE]
|
||||||
|
unless (wanted & quality).zero?
|
||||||
|
str << "<" << ATTR_MAP[quality]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
print str
|
||||||
|
end
|
||||||
|
|
||||||
|
def tag(code)
|
||||||
|
print("<#{code}>")
|
||||||
|
print(yield)
|
||||||
|
print("</#{code}>")
|
||||||
|
end
|
||||||
|
|
||||||
|
def escape(str)
|
||||||
|
str.
|
||||||
|
gsub(/&/n, '&').
|
||||||
|
gsub(/\"/n, '"').
|
||||||
|
gsub(/>/n, '>').
|
||||||
|
gsub(/</n, '<')
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -662,10 +662,6 @@ waitall_each(pid, status, ary)
|
||||||
*
|
*
|
||||||
* 0:: Waits for any child whose process group ID equals that of the
|
* 0:: Waits for any child whose process group ID equals that of the
|
||||||
* calling process.
|
* calling process.
|
||||||
* adsasdasd sads adada dsa a sad ad asd sad sa dsa dasdsad asd asd
|
|
||||||
* adsasdasd sads adada dsa a sad ad asd sad sa dsa dasdsad asd asd
|
|
||||||
* adsasdasd sads adada dsa a sad ad asd sad sa dsa dasdsad asd asd
|
|
||||||
* adsasdasd sads adada dsa a sad ad asd sad sa dsa dasdsad asd asd
|
|
||||||
*
|
*
|
||||||
* -1:: Waits for any child process (the default if no _pid_ is
|
* -1:: Waits for any child process (the default if no _pid_ is
|
||||||
* given).
|
* given).
|
||||||
|
|
Loading…
Reference in a new issue