2008-01-08 05:18:41 -05:00
|
|
|
require 'rdoc/ri'
|
2008-01-18 19:06:19 -05:00
|
|
|
require 'rdoc/markup'
|
2008-01-08 05:18:41 -05:00
|
|
|
|
|
|
|
class RDoc::RI::Formatter
|
|
|
|
|
2008-04-26 12:14:19 -04:00
|
|
|
attr_writer :indent
|
2008-01-31 01:48:35 -05:00
|
|
|
attr_accessor :output
|
2008-01-08 05:18:41 -05:00
|
|
|
|
2008-01-18 19:06:19 -05:00
|
|
|
FORMATTERS = { }
|
|
|
|
|
|
|
|
def self.for(name)
|
|
|
|
FORMATTERS[name.downcase]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.list
|
|
|
|
FORMATTERS.keys.sort.join ", "
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(output, width, indent)
|
|
|
|
@output = output
|
|
|
|
@width = width
|
|
|
|
@indent = indent
|
2008-04-26 12:14:19 -04:00
|
|
|
@original_indent = indent.dup
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def draw_line(label=nil)
|
|
|
|
len = @width
|
2008-01-18 19:06:19 -05:00
|
|
|
len -= (label.size + 1) if label
|
|
|
|
|
|
|
|
if len > 0 then
|
|
|
|
@output.print '-' * len
|
|
|
|
if label
|
|
|
|
@output.print ' '
|
|
|
|
bold_print label
|
|
|
|
end
|
|
|
|
|
|
|
|
@output.puts
|
|
|
|
else
|
|
|
|
@output.print '-' * @width
|
|
|
|
@output.puts
|
|
|
|
|
|
|
|
@output.puts label
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-04-26 12:14:19 -04:00
|
|
|
def indent
|
|
|
|
return @indent unless block_given?
|
|
|
|
|
|
|
|
begin
|
|
|
|
indent = @indent.dup
|
|
|
|
@indent += @original_indent
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
@indent = indent
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-18 19:06:19 -05:00
|
|
|
def wrap(txt, prefix=@indent, linelen=@width)
|
2008-01-08 05:18:41 -05:00
|
|
|
return unless txt && !txt.empty?
|
2008-01-18 19:06:19 -05:00
|
|
|
|
2008-01-08 05:18:41 -05:00
|
|
|
work = conv_markup(txt)
|
|
|
|
textLen = linelen - prefix.length
|
|
|
|
patt = Regexp.new("^(.{0,#{textLen}})[ \n]")
|
|
|
|
next_prefix = prefix.tr("^ ", " ")
|
|
|
|
|
|
|
|
res = []
|
|
|
|
|
|
|
|
while work.length > textLen
|
|
|
|
if work =~ patt
|
|
|
|
res << $1
|
|
|
|
work.slice!(0, $&.length)
|
|
|
|
else
|
|
|
|
res << work.slice!(0, textLen)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
res << work if work.length.nonzero?
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.puts(prefix + res.join("\n" + next_prefix))
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def blankline
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.puts
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
2008-01-15 20:36:19 -05:00
|
|
|
##
|
|
|
|
# Called when we want to ensure a new 'wrap' starts on a newline. Only
|
|
|
|
# needed for HtmlFormatter, because the rest do their own line breaking.
|
2008-01-08 05:18:41 -05:00
|
|
|
|
|
|
|
def break_to_newline
|
|
|
|
end
|
|
|
|
|
|
|
|
def bold_print(txt)
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print txt
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def raw_print_line(txt)
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.puts txt
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
2008-01-15 20:36:19 -05:00
|
|
|
##
|
|
|
|
# Convert HTML entities back to ASCII
|
2008-01-08 05:18:41 -05:00
|
|
|
|
|
|
|
def conv_html(txt)
|
* ext/json/lib/json/pure/generator.rb,
ext/json/lib/json/pure/parser.rb, ext/openssl/lib/openssl/x509.rb,
ext/win32ole/sample/olegen.rb, lib/date/format.rb, lib/irb/context.rb,
lib/irb/workspace.rb, lib/net/http.rb, lib/net/imap.rb,
lib/rdoc/generator.rb, lib/rdoc/markup/to_html.rb,
lib/rdoc/markup/to_latex.rb, lib/rdoc/parsers/parse_c.rb,
lib/rdoc/ri/formatter.rb, lib/rexml/parsers/baseparser.rb,
lib/rexml/quickpath.rb, lib/rexml/text.rb, lib/rss/parser.rb,
lib/uri/common.rb, lib/uri/generic.rb, lib/webrick/httpresponse.rb,
lib/webrick/httpservlet/filehandler.rb, lib/yaml/baseemitter.rb,
lib/yaml/encoding.rb: performance tuning arround String#gsub.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15442 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-02-12 01:18:06 -05:00
|
|
|
txt = txt.gsub(/>/, '>')
|
|
|
|
txt.gsub!(/</, '<')
|
|
|
|
txt.gsub!(/"/, '"')
|
|
|
|
txt.gsub!(/&/, '&')
|
|
|
|
txt
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
2008-01-15 20:36:19 -05:00
|
|
|
##
|
|
|
|
# Convert markup into display form
|
|
|
|
|
2008-01-08 05:18:41 -05:00
|
|
|
def conv_markup(txt)
|
* ext/json/lib/json/pure/generator.rb,
ext/json/lib/json/pure/parser.rb, ext/openssl/lib/openssl/x509.rb,
ext/win32ole/sample/olegen.rb, lib/date/format.rb, lib/irb/context.rb,
lib/irb/workspace.rb, lib/net/http.rb, lib/net/imap.rb,
lib/rdoc/generator.rb, lib/rdoc/markup/to_html.rb,
lib/rdoc/markup/to_latex.rb, lib/rdoc/parsers/parse_c.rb,
lib/rdoc/ri/formatter.rb, lib/rexml/parsers/baseparser.rb,
lib/rexml/quickpath.rb, lib/rexml/text.rb, lib/rss/parser.rb,
lib/uri/common.rb, lib/uri/generic.rb, lib/webrick/httpresponse.rb,
lib/webrick/httpservlet/filehandler.rb, lib/yaml/baseemitter.rb,
lib/yaml/encoding.rb: performance tuning arround String#gsub.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15442 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-02-12 01:18:06 -05:00
|
|
|
txt = txt.gsub(%r{<tt>(.*?)</tt>}, '+\1+')
|
|
|
|
txt.gsub!(%r{<code>(.*?)</code>}, '+\1+')
|
|
|
|
txt.gsub!(%r{<b>(.*?)</b>}, '*\1*')
|
|
|
|
txt.gsub!(%r{<em>(.*?)</em>}, '_\1_')
|
|
|
|
txt
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def display_list(list)
|
|
|
|
case list.type
|
2008-01-18 19:06:19 -05:00
|
|
|
when :BULLET
|
2008-01-08 05:18:41 -05:00
|
|
|
prefixer = proc { |ignored| @indent + "* " }
|
|
|
|
|
2008-01-18 19:06:19 -05:00
|
|
|
when :NUMBER, :UPPERALPHA, :LOWERALPHA then
|
2008-01-08 05:18:41 -05:00
|
|
|
start = case list.type
|
2008-01-18 19:06:19 -05:00
|
|
|
when :NUMBER then 1
|
|
|
|
when :UPPERALPHA then 'A'
|
|
|
|
when :LOWERALPHA then 'a'
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
2008-01-21 19:01:59 -05:00
|
|
|
|
2008-01-08 05:18:41 -05:00
|
|
|
prefixer = proc do |ignored|
|
|
|
|
res = @indent + "#{start}.".ljust(4)
|
|
|
|
start = start.succ
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2008-01-21 19:01:59 -05:00
|
|
|
when :LABELED, :NOTE then
|
2008-01-08 05:18:41 -05:00
|
|
|
longest = 0
|
2008-01-21 19:01:59 -05:00
|
|
|
|
2008-01-08 05:18:41 -05:00
|
|
|
list.contents.each do |item|
|
2008-01-21 19:01:59 -05:00
|
|
|
if RDoc::Markup::Flow::LI === item and item.label.length > longest then
|
2008-01-08 05:18:41 -05:00
|
|
|
longest = item.label.length
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-21 19:01:59 -05:00
|
|
|
longest += 1
|
|
|
|
|
|
|
|
prefixer = proc { |li| @indent + li.label.ljust(longest) }
|
2008-01-08 05:18:41 -05:00
|
|
|
|
|
|
|
else
|
2008-01-18 19:06:19 -05:00
|
|
|
raise ArgumentError, "unknown list type #{list.type}"
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
list.contents.each do |item|
|
2008-01-21 19:01:59 -05:00
|
|
|
if RDoc::Markup::Flow::LI === item then
|
|
|
|
prefix = prefixer.call item
|
|
|
|
display_flow_item item, prefix
|
2008-01-08 05:18:41 -05:00
|
|
|
else
|
2008-01-21 19:01:59 -05:00
|
|
|
display_flow_item item
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-21 19:01:59 -05:00
|
|
|
def display_flow_item(item, prefix = @indent)
|
2008-01-08 05:18:41 -05:00
|
|
|
case item
|
2008-01-13 22:34:05 -05:00
|
|
|
when RDoc::Markup::Flow::P, RDoc::Markup::Flow::LI
|
2008-01-08 05:18:41 -05:00
|
|
|
wrap(conv_html(item.body), prefix)
|
|
|
|
blankline
|
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
when RDoc::Markup::Flow::LIST
|
2008-01-08 05:18:41 -05:00
|
|
|
display_list(item)
|
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
when RDoc::Markup::Flow::VERB
|
2008-01-08 05:18:41 -05:00
|
|
|
display_verbatim_flow_item(item, @indent)
|
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
when RDoc::Markup::Flow::H
|
2008-01-08 05:18:41 -05:00
|
|
|
display_heading(conv_html(item.text), item.level, @indent)
|
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
when RDoc::Markup::Flow::RULE
|
2008-01-08 05:18:41 -05:00
|
|
|
draw_line
|
|
|
|
|
|
|
|
else
|
2008-01-21 19:01:59 -05:00
|
|
|
raise RDoc::Error, "Unknown flow element: #{item.class}"
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def display_verbatim_flow_item(item, prefix=@indent)
|
|
|
|
item.body.split(/\n/).each do |line|
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print @indent, conv_html(line), "\n"
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
blankline
|
|
|
|
end
|
|
|
|
|
|
|
|
def display_heading(text, level, indent)
|
2008-01-21 19:01:59 -05:00
|
|
|
text = strip_attributes text
|
|
|
|
|
2008-01-08 05:18:41 -05:00
|
|
|
case level
|
2008-01-21 19:01:59 -05:00
|
|
|
when 1 then
|
2008-01-08 05:18:41 -05:00
|
|
|
ul = "=" * text.length
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.puts
|
|
|
|
@output.puts text.upcase
|
|
|
|
@output.puts ul
|
2008-01-08 05:18:41 -05:00
|
|
|
|
2008-01-21 19:01:59 -05:00
|
|
|
when 2 then
|
2008-01-08 05:18:41 -05:00
|
|
|
ul = "-" * text.length
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.puts
|
|
|
|
@output.puts text
|
|
|
|
@output.puts ul
|
2008-01-08 05:18:41 -05:00
|
|
|
else
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print indent, text, "\n"
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
2008-01-21 19:01:59 -05:00
|
|
|
|
|
|
|
@output.puts
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def display_flow(flow)
|
|
|
|
flow.each do |f|
|
|
|
|
display_flow_item(f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-21 19:01:59 -05:00
|
|
|
def strip_attributes(text)
|
|
|
|
text.gsub(/(<\/?(?:b|code|em|i|tt)>)/, '')
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Handle text with attributes. We're a base class: there are different
|
|
|
|
# presentation classes (one, for example, uses overstrikes to handle bold and
|
|
|
|
# underlining, while another using ANSI escape sequences.
|
|
|
|
|
|
|
|
class RDoc::RI::AttributeFormatter < RDoc::RI::Formatter
|
|
|
|
|
|
|
|
BOLD = 1
|
|
|
|
ITALIC = 2
|
|
|
|
CODE = 4
|
|
|
|
|
|
|
|
ATTR_MAP = {
|
|
|
|
"b" => BOLD,
|
|
|
|
"code" => CODE,
|
|
|
|
"em" => ITALIC,
|
|
|
|
"i" => ITALIC,
|
|
|
|
"tt" => CODE
|
|
|
|
}
|
|
|
|
|
2008-01-21 19:01:59 -05:00
|
|
|
AttrChar = Struct.new :char, :attr
|
2008-01-08 05:18:41 -05:00
|
|
|
|
|
|
|
class AttributeString
|
|
|
|
attr_reader :txt
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@txt = []
|
|
|
|
@optr = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def <<(char)
|
|
|
|
@txt << char
|
|
|
|
end
|
|
|
|
|
|
|
|
def empty?
|
|
|
|
@optr >= @txt.length
|
|
|
|
end
|
|
|
|
|
|
|
|
# accept non space, then all following spaces
|
|
|
|
def next_word
|
|
|
|
start = @optr
|
|
|
|
len = @txt.length
|
|
|
|
|
|
|
|
while @optr < len && @txt[@optr].char != " "
|
|
|
|
@optr += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
while @optr < len && @txt[@optr].char == " "
|
|
|
|
@optr += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
@txt[start...@optr]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2008-01-18 19:06:19 -05:00
|
|
|
# Overrides base class. Looks for <tt>...</tt> etc sequences and generates
|
|
|
|
# an array of AttrChars. This array is then used as the basis for the
|
|
|
|
# split.
|
2008-01-08 05:18:41 -05:00
|
|
|
|
2008-01-18 19:06:19 -05:00
|
|
|
def wrap(txt, prefix=@indent, linelen=@width)
|
2008-01-08 05:18:41 -05:00
|
|
|
return unless txt && !txt.empty?
|
|
|
|
|
|
|
|
txt = add_attributes_to(txt)
|
|
|
|
next_prefix = prefix.tr("^ ", " ")
|
|
|
|
linelen -= prefix.size
|
|
|
|
|
|
|
|
line = []
|
|
|
|
|
|
|
|
until txt.empty?
|
|
|
|
word = txt.next_word
|
|
|
|
if word.size + line.size > linelen
|
|
|
|
write_attribute_text(prefix, line)
|
|
|
|
prefix = next_prefix
|
|
|
|
line = []
|
|
|
|
end
|
|
|
|
line.concat(word)
|
|
|
|
end
|
|
|
|
|
|
|
|
write_attribute_text(prefix, line) if line.length > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def write_attribute_text(prefix, line)
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print prefix
|
2008-01-08 05:18:41 -05:00
|
|
|
line.each do |achar|
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print achar.char
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.puts
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def bold_print(txt)
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print txt
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def add_attributes_to(txt)
|
|
|
|
tokens = txt.split(%r{(</?(?:b|code|em|i|tt)>)})
|
|
|
|
text = AttributeString.new
|
|
|
|
attributes = 0
|
|
|
|
tokens.each do |tok|
|
|
|
|
case tok
|
|
|
|
when %r{^</(\w+)>$} then attributes &= ~(ATTR_MAP[$1]||0)
|
|
|
|
when %r{^<(\w+)>$} then attributes |= (ATTR_MAP[$1]||0)
|
|
|
|
else
|
|
|
|
tok.split(//).each {|ch| text << AttrChar.new(ch, attributes)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
text
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# This formatter generates overstrike-style formatting, which works with
|
|
|
|
# pagers such as man and less.
|
|
|
|
|
|
|
|
class RDoc::RI::OverstrikeFormatter < RDoc::RI::AttributeFormatter
|
|
|
|
|
|
|
|
BS = "\C-h"
|
|
|
|
|
|
|
|
def write_attribute_text(prefix, line)
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print prefix
|
2008-01-21 19:01:59 -05:00
|
|
|
|
2008-01-08 05:18:41 -05:00
|
|
|
line.each do |achar|
|
|
|
|
attr = achar.attr
|
2008-01-21 19:01:59 -05:00
|
|
|
@output.print "_", BS if (attr & (ITALIC + CODE)) != 0
|
|
|
|
@output.print achar.char, BS if (attr & BOLD) != 0
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print achar.char
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
2008-01-21 19:01:59 -05:00
|
|
|
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.puts
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2008-01-15 20:36:19 -05:00
|
|
|
# Draw a string in bold
|
2008-01-08 05:18:41 -05:00
|
|
|
|
|
|
|
def bold_print(text)
|
|
|
|
text.split(//).each do |ch|
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print ch, BS, ch
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# This formatter uses ANSI escape sequences to colorize stuff works with
|
|
|
|
# pagers such as man and less.
|
|
|
|
|
|
|
|
class RDoc::RI::AnsiFormatter < RDoc::RI::AttributeFormatter
|
|
|
|
|
|
|
|
def initialize(*args)
|
|
|
|
super
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print "\033[0m"
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def write_attribute_text(prefix, line)
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print prefix
|
2008-01-08 05:18:41 -05:00
|
|
|
curr_attr = 0
|
|
|
|
line.each do |achar|
|
|
|
|
attr = achar.attr
|
|
|
|
if achar.attr != curr_attr
|
|
|
|
update_attributes(achar.attr)
|
|
|
|
curr_attr = achar.attr
|
|
|
|
end
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print achar.char
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
update_attributes(0) unless curr_attr.zero?
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.puts
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def bold_print(txt)
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print "\033[1m#{txt}\033[m"
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
HEADINGS = {
|
2008-01-21 19:01:59 -05:00
|
|
|
1 => ["\033[1;32m", "\033[m"],
|
|
|
|
2 => ["\033[4;32m", "\033[m"],
|
|
|
|
3 => ["\033[32m", "\033[m"],
|
2008-01-08 05:18:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
def display_heading(text, level, indent)
|
|
|
|
level = 3 if level > 3
|
|
|
|
heading = HEADINGS[level]
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print indent
|
|
|
|
@output.print heading[0]
|
|
|
|
@output.print strip_attributes(text)
|
|
|
|
@output.puts heading[1]
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
ATTR_MAP = {
|
|
|
|
BOLD => "1",
|
|
|
|
ITALIC => "33",
|
|
|
|
CODE => "36"
|
|
|
|
}
|
|
|
|
|
|
|
|
def update_attributes(attr)
|
|
|
|
str = "\033["
|
|
|
|
for quality in [ BOLD, ITALIC, CODE]
|
|
|
|
unless (attr & quality).zero?
|
|
|
|
str << ATTR_MAP[quality]
|
|
|
|
end
|
|
|
|
end
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print str, "m"
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# This formatter uses HTML.
|
|
|
|
|
|
|
|
class RDoc::RI::HtmlFormatter < RDoc::RI::AttributeFormatter
|
|
|
|
|
|
|
|
def write_attribute_text(prefix, line)
|
|
|
|
curr_attr = 0
|
|
|
|
line.each do |achar|
|
|
|
|
attr = achar.attr
|
|
|
|
if achar.attr != curr_attr
|
|
|
|
update_attributes(curr_attr, achar.attr)
|
|
|
|
curr_attr = achar.attr
|
|
|
|
end
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print(escape(achar.char))
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
update_attributes(curr_attr, 0) unless curr_attr.zero?
|
|
|
|
end
|
|
|
|
|
|
|
|
def draw_line(label=nil)
|
|
|
|
if label != nil
|
|
|
|
bold_print(label)
|
|
|
|
end
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.puts("<hr>")
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def bold_print(txt)
|
|
|
|
tag("b") { txt }
|
|
|
|
end
|
|
|
|
|
|
|
|
def blankline()
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.puts("<p>")
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def break_to_newline
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.puts("<br>")
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def display_heading(text, level, indent)
|
|
|
|
level = 4 if level > 4
|
|
|
|
tag("h#{level}") { text }
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.puts
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def display_list(list)
|
|
|
|
case list.type
|
2008-01-18 19:06:19 -05:00
|
|
|
when :BULLET then
|
2008-01-08 05:18:41 -05:00
|
|
|
list_type = "ul"
|
|
|
|
prefixer = proc { |ignored| "<li>" }
|
|
|
|
|
2008-01-18 19:06:19 -05:00
|
|
|
when :NUMBER, :UPPERALPHA, :LOWERALPHA then
|
2008-01-08 05:18:41 -05:00
|
|
|
list_type = "ol"
|
|
|
|
prefixer = proc { |ignored| "<li>" }
|
|
|
|
|
2008-01-18 19:06:19 -05:00
|
|
|
when :LABELED then
|
2008-01-08 05:18:41 -05:00
|
|
|
list_type = "dl"
|
|
|
|
prefixer = proc do |li|
|
2008-04-26 12:14:19 -04:00
|
|
|
"<dt><b>" + escape(li.label) + "</b><dd>"
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
2008-01-18 19:06:19 -05:00
|
|
|
when :NOTE then
|
2008-01-08 05:18:41 -05:00
|
|
|
list_type = "table"
|
|
|
|
prefixer = proc do |li|
|
2008-04-26 12:14:19 -04:00
|
|
|
%{<tr valign="top"><td>#{li.label.gsub(/ /, ' ')}</td><td>}
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
else
|
|
|
|
fail "unknown list type"
|
|
|
|
end
|
|
|
|
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print "<#{list_type}>"
|
2008-01-08 05:18:41 -05:00
|
|
|
list.contents.each do |item|
|
2008-01-13 22:34:05 -05:00
|
|
|
if item.kind_of? RDoc::Markup::Flow::LI
|
2008-01-08 05:18:41 -05:00
|
|
|
prefix = prefixer.call(item)
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print prefix
|
2008-01-08 05:18:41 -05:00
|
|
|
display_flow_item(item, prefix)
|
|
|
|
else
|
|
|
|
display_flow_item(item)
|
|
|
|
end
|
|
|
|
end
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print "</#{list_type}>"
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def display_verbatim_flow_item(item, prefix=@indent)
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print("<pre>")
|
2008-01-08 05:18:41 -05:00
|
|
|
item.body.split(/\n/).each do |line|
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.puts conv_html(line)
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.puts("</pre>")
|
2008-01-08 05:18:41 -05:00
|
|
|
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
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print str
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def tag(code)
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print("<#{code}>")
|
|
|
|
@output.print(yield)
|
|
|
|
@output.print("</#{code}>")
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def escape(str)
|
* ext/json/lib/json/pure/generator.rb,
ext/json/lib/json/pure/parser.rb, ext/openssl/lib/openssl/x509.rb,
ext/win32ole/sample/olegen.rb, lib/date/format.rb, lib/irb/context.rb,
lib/irb/workspace.rb, lib/net/http.rb, lib/net/imap.rb,
lib/rdoc/generator.rb, lib/rdoc/markup/to_html.rb,
lib/rdoc/markup/to_latex.rb, lib/rdoc/parsers/parse_c.rb,
lib/rdoc/ri/formatter.rb, lib/rexml/parsers/baseparser.rb,
lib/rexml/quickpath.rb, lib/rexml/text.rb, lib/rss/parser.rb,
lib/uri/common.rb, lib/uri/generic.rb, lib/webrick/httpresponse.rb,
lib/webrick/httpservlet/filehandler.rb, lib/yaml/baseemitter.rb,
lib/yaml/encoding.rb: performance tuning arround String#gsub.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15442 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-02-12 01:18:06 -05:00
|
|
|
str = str.gsub(/&/n, '&')
|
|
|
|
str.gsub!(/\"/n, '"')
|
|
|
|
str.gsub!(/>/n, '>')
|
|
|
|
str.gsub!(/</n, '<')
|
|
|
|
str
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# This formatter reduces extra lines for a simpler output. It improves way
|
|
|
|
# output looks for tools like IRC bots.
|
|
|
|
|
|
|
|
class RDoc::RI::SimpleFormatter < RDoc::RI::Formatter
|
|
|
|
|
|
|
|
##
|
|
|
|
# No extra blank lines
|
|
|
|
|
|
|
|
def blankline
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Display labels only, no lines
|
|
|
|
|
|
|
|
def draw_line(label=nil)
|
|
|
|
unless label.nil? then
|
|
|
|
bold_print(label)
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.puts
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Place heading level indicators inline with heading.
|
|
|
|
|
|
|
|
def display_heading(text, level, indent)
|
|
|
|
text = strip_attributes(text)
|
|
|
|
case level
|
|
|
|
when 1
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.puts "= " + text.upcase
|
2008-01-08 05:18:41 -05:00
|
|
|
when 2
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.puts "-- " + text
|
2008-01-08 05:18:41 -05:00
|
|
|
else
|
2008-01-18 19:06:19 -05:00
|
|
|
@output.print indent, text, "\n"
|
2008-01-08 05:18:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2008-01-18 19:06:19 -05:00
|
|
|
RDoc::RI::Formatter::FORMATTERS['plain'] = RDoc::RI::Formatter
|
|
|
|
RDoc::RI::Formatter::FORMATTERS['simple'] = RDoc::RI::SimpleFormatter
|
|
|
|
RDoc::RI::Formatter::FORMATTERS['bs'] = RDoc::RI::OverstrikeFormatter
|
|
|
|
RDoc::RI::Formatter::FORMATTERS['ansi'] = RDoc::RI::AnsiFormatter
|
|
|
|
RDoc::RI::Formatter::FORMATTERS['html'] = RDoc::RI::HtmlFormatter
|