1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/rdoc/markup: Remove ListBase and Line constants.

* lib/rdoc/ri: Allow output IO to be specified.

* test/rdoc/parser/test_parse_c.rb: Move up one level, fixed.

* test/rdoc/parser/test_rdoc_markup_attribute_manager.rb: Renamed to
  match new class name, updated to match new classes.

* test/rdoc/test_rdoc_ri_formatter.rb: Start of RI formatting tests.

* test/rdoc/test_rdoc_ri_attribute_manager.rb: Start of
  RDoc::RI::AttributeManager tests.

* test/rdoc/test_simple_markup.rb: Moved to match new class name.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15120 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
drbrain 2008-01-19 00:06:19 +00:00
parent 9872d4d600
commit 8d37cefaf8
15 changed files with 443 additions and 261 deletions

View file

@ -1,3 +1,21 @@
Sat Jan 19 08:58:47 2008 Eric Hodel <drbrain@segment7.net>
* lib/rdoc/markup: Remove ListBase and Line constants.
* lib/rdoc/ri: Allow output IO to be specified.
* test/rdoc/parser/test_parse_c.rb: Move up one level, fixed.
* test/rdoc/parser/test_rdoc_markup_attribute_manager.rb: Renamed to
match new class name, updated to match new classes.
* test/rdoc/test_rdoc_ri_formatter.rb: Start of RI formatting tests.
* test/rdoc/test_rdoc_ri_attribute_manager.rb: Start of
RDoc::RI::AttributeManager tests.
* test/rdoc/test_simple_markup.rb: Moved to match new class name.
Sat Jan 19 08:35:36 2008 Nobuyoshi Nakada <nobu@ruby-lang.org> Sat Jan 19 08:35:36 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* parse.y (parser_prepare): get encoding from the first line. * parse.y (parser_prepare): get encoding from the first line.

View file

@ -159,7 +159,6 @@ require 'rdoc'
# #
#-- #--
# Author:: Dave Thomas, dave@pragmaticprogrammer.com # Author:: Dave Thomas, dave@pragmaticprogrammer.com
# Version:: 0.0
# License:: Ruby license # License:: Ruby license
class RDoc::Markup class RDoc::Markup
@ -256,7 +255,7 @@ class RDoc::Markup
while line = @lines.next while line = @lines.next
if line.isBlank? if line.isBlank?
line.stamp(Line::BLANK, level) line.stamp :BLANK, level
next next
end end
@ -282,7 +281,7 @@ class RDoc::Markup
# #
if /^(---+)\s*$/ =~ active_line if /^(---+)\s*$/ =~ active_line
line.stamp(Line::RULE, level, $1.length-2) line.stamp :RULE, level, $1.length-2
next next
end end
@ -296,14 +295,14 @@ class RDoc::Markup
prefix_length = prefix.length prefix_length = prefix.length
flag = case prefix flag = case prefix
when "*","-" then ListBase::BULLET when "*","-" then :BULLET
when /^\d/ then ListBase::NUMBER when /^\d/ then :NUMBER
when /^[A-Z]/ then ListBase::UPPERALPHA when /^[A-Z]/ then :UPPERALPHA
when /^[a-z]/ then ListBase::LOWERALPHA when /^[a-z]/ then :LOWERALPHA
else raise "Invalid List Type: #{self.inspect}" else raise "Invalid List Type: #{self.inspect}"
end end
line.stamp(Line::LIST, level+1, prefix, flag) line.stamp :LIST, level+1, prefix, flag
text[margin, prefix_length] = " " * prefix_length text[margin, prefix_length] = " " * prefix_length
assign_types_to_lines(offset, level + 1) assign_types_to_lines(offset, level + 1)
next next
@ -328,7 +327,7 @@ class RDoc::Markup
if active_line[0] == ?= and active_line =~ /^(=+)\s*(.*)/ if active_line[0] == ?= and active_line =~ /^(=+)\s*(.*)/
prefix_length = $1.length prefix_length = $1.length
prefix_length = 6 if prefix_length > 6 prefix_length = 6 if prefix_length > 6
line.stamp(Line::HEADING, 0, prefix_length) line.stamp :HEADING, 0, prefix_length
line.strip_leading(margin + prefix_length) line.strip_leading(margin + prefix_length)
next next
end end
@ -338,9 +337,9 @@ class RDoc::Markup
if active_line[0] == SPACE if active_line[0] == SPACE
line.strip_leading(margin) if margin > 0 line.strip_leading(margin) if margin > 0
line.stamp(Line::VERBATIM, level) line.stamp :VERBATIM, level
else else
line.stamp(Line::PARAGRAPH, level) line.stamp :PARAGRAPH, level
end end
end end
end end
@ -369,10 +368,10 @@ class RDoc::Markup
flag = nil flag = nil
case prefix case prefix
when /^\[/ when /^\[/
flag = ListBase::LABELED flag = :LABELED
prefix = prefix[1, prefix.length-2] prefix = prefix[1, prefix.length-2]
when /:$/ when /:$/
flag = ListBase::NOTE flag = :NOTE
prefix.chop! prefix.chop!
else raise "Invalid List Type: #{self.inspect}" else raise "Invalid List Type: #{self.inspect}"
end end
@ -403,7 +402,7 @@ class RDoc::Markup
end end
end end
line.stamp(Line::LIST, level+1, prefix, flag) line.stamp :LIST, level+1, prefix, flag
text[margin, prefix_length] = " " * prefix_length text[margin, prefix_length] = " " * prefix_length
assign_types_to_lines(offset, level + 1) assign_types_to_lines(offset, level + 1)
return true return true
@ -431,12 +430,12 @@ class RDoc::Markup
else else
group = block.fragment_for(line) group = block.fragment_for(line)
block.add(group) block.add(group)
if line.type == Line::LIST if line.type == :LIST
wantedType = Line::PARAGRAPH wantedType = :PARAGRAPH
else else
wantedType = line.type wantedType = line.type
end end
wantedLevel = line.type == Line::HEADING ? line.param : line.level wantedLevel = line.type == :HEADING ? line.param : line.level
end end
end end

View file

@ -11,6 +11,22 @@ class RDoc::Markup
attr_reader :level, :param, :txt attr_reader :level, :param, :txt
attr_accessor :type attr_accessor :type
######
# This is a simple factory system that lets us associate fragement
# types (a string) with a subclass of fragment
TYPE_MAP = {}
def self.type_name(name)
TYPE_MAP[name] = self
end
def self.for(line)
klass = TYPE_MAP[line.type] ||
raise("Unknown line type: '#{line.type.inspect}:' '#{line.text}'")
return klass.new(line.level, line.param, line.flag, line.text)
end
def initialize(level, param, type, txt) def initialize(level, param, type, txt)
@level = level @level = level
@param = param @param = param
@ -28,21 +44,6 @@ class RDoc::Markup
"L#@level: #{self.class.name.split('::')[-1]}\n#@txt" "L#@level: #{self.class.name.split('::')[-1]}\n#@txt"
end end
######
# This is a simple factory system that lets us associate fragement
# types (a string) with a subclass of fragment
TYPE_MAP = {}
def Fragment.type_name(name)
TYPE_MAP[name] = self
end
def Fragment.for(line)
klass = TYPE_MAP[line.type] ||
raise("Unknown line type: '#{line.type.inspect}:' '#{line.text}'")
return klass.new(line.level, line.param, line.flag, line.text)
end
end end
## ##
@ -50,15 +51,15 @@ class RDoc::Markup
# newlines when we're created, and have them put back on output. # newlines when we're created, and have them put back on output.
class Paragraph < Fragment class Paragraph < Fragment
type_name Line::PARAGRAPH type_name :PARAGRAPH
end end
class BlankLine < Paragraph class BlankLine < Paragraph
type_name Line::BLANK type_name :BLANK
end end
class Heading < Paragraph class Heading < Paragraph
type_name Line::HEADING type_name :HEADING
def head_level def head_level
@param.to_i @param.to_i
@ -69,17 +70,18 @@ class RDoc::Markup
# A List is a fragment with some kind of label # A List is a fragment with some kind of label
class ListBase < Paragraph class ListBase < Paragraph
# List types LIST_TYPES = [
BULLET = :BULLET :BULLET,
NUMBER = :NUMBER :NUMBER,
UPPERALPHA = :UPPERALPHA :UPPERALPHA,
LOWERALPHA = :LOWERALPHA :LOWERALPHA,
LABELED = :LABELED :LABELED,
NOTE = :NOTE :NOTE,
]
end end
class ListItem < ListBase class ListItem < ListBase
type_name Line::LIST type_name :LIST
# def label # def label
# am = AttributeManager.new(@param) # am = AttributeManager.new(@param)
@ -103,7 +105,7 @@ class RDoc::Markup
# Verbatim code contains lines that don't get wrapped. # Verbatim code contains lines that don't get wrapped.
class Verbatim < Fragment class Verbatim < Fragment
type_name Line::VERBATIM type_name :VERBATIM
def add_text(txt) def add_text(txt)
@txt << txt.chomp << "\n" @txt << txt.chomp << "\n"
@ -115,7 +117,7 @@ class RDoc::Markup
# A horizontal rule # A horizontal rule
class Rule < Fragment class Rule < Fragment
type_name Line::RULE type_name :RULE
end end
## ##

View file

@ -8,12 +8,14 @@ class RDoc::Markup
class Line class Line
INFINITY = 9999 INFINITY = 9999
BLANK = :BLANK LINE_TYPES = [
HEADING = :HEADING :BLANK,
LIST = :LIST :HEADING,
RULE = :RULE :LIST,
PARAGRAPH = :PARAGRAPH :PARAGRAPH,
VERBATIM = :VERBATIM :RULE,
:VERBATIM,
]
# line type # line type
attr_accessor :type attr_accessor :type
@ -132,7 +134,7 @@ class RDoc::Markup
def normalize def normalize
margin = @lines.collect{|l| l.leading_spaces}.min margin = @lines.collect{|l| l.leading_spaces}.min
margin = 0 if margin == Line::INFINITY margin = 0 if margin == :INFINITY
@lines.each {|line| line.strip_leading(margin) } if margin > 0 @lines.each {|line| line.strip_leading(margin) } if margin > 0
end end

View file

@ -24,12 +24,12 @@ class RDoc::Markup
class ToFlow class ToFlow
LIST_TYPE_TO_HTML = { LIST_TYPE_TO_HTML = {
RDoc::Markup::ListBase::BULLET => [ "<ul>", "</ul>" ], :BULLET => [ "<ul>", "</ul>" ],
RDoc::Markup::ListBase::NUMBER => [ "<ol>", "</ol>" ], :NUMBER => [ "<ol>", "</ol>" ],
RDoc::Markup::ListBase::UPPERALPHA => [ "<ol>", "</ol>" ], :UPPERALPHA => [ "<ol>", "</ol>" ],
RDoc::Markup::ListBase::LOWERALPHA => [ "<ol>", "</ol>" ], :LOWERALPHA => [ "<ol>", "</ol>" ],
RDoc::Markup::ListBase::LABELED => [ "<dl>", "</dl>" ], :LABELED => [ "<dl>", "</dl>" ],
RDoc::Markup::ListBase::NOTE => [ "<table>", "</table>" ], :NOTE => [ "<table>", "</table>" ],
} }
InlineTag = Struct.new(:bit, :on, :off) InlineTag = Struct.new(:bit, :on, :off)

View file

@ -6,12 +6,12 @@ require 'cgi'
class RDoc::Markup::ToHtml class RDoc::Markup::ToHtml
LIST_TYPE_TO_HTML = { LIST_TYPE_TO_HTML = {
RDoc::Markup::ListBase::BULLET => [ "<ul>", "</ul>" ], :BULLET => [ "<ul>", "</ul>" ],
RDoc::Markup::ListBase::NUMBER => [ "<ol>", "</ol>" ], :NUMBER => [ "<ol>", "</ol>" ],
RDoc::Markup::ListBase::UPPERALPHA => [ "<ol>", "</ol>" ], :UPPERALPHA => [ "<ol>", "</ol>" ],
RDoc::Markup::ListBase::LOWERALPHA => [ "<ol>", "</ol>" ], :LOWERALPHA => [ "<ol>", "</ol>" ],
RDoc::Markup::ListBase::LABELED => [ "<dl>", "</dl>" ], :LABELED => [ "<dl>", "</dl>" ],
RDoc::Markup::ListBase::NOTE => [ "<table>", "</table>" ], :NOTE => [ "<table>", "</table>" ],
} }
InlineTag = Struct.new(:bit, :on, :off) InlineTag = Struct.new(:bit, :on, :off)
@ -241,22 +241,22 @@ class RDoc::Markup::ToHtml
def list_item_start(am, fragment) def list_item_start(am, fragment)
case fragment.type case fragment.type
when RDoc::Markup::ListBase::BULLET, RDoc::Markup::ListBase::NUMBER then when :BULLET, :NUMBER then
annotate("<li>") annotate("<li>")
when RDoc::Markup::ListBase::UPPERALPHA then when :UPPERALPHA then
annotate("<li type=\"A\">") annotate("<li type=\"A\">")
when RDoc::Markup::ListBase::LOWERALPHA then when :LOWERALPHA then
annotate("<li type=\"a\">") annotate("<li type=\"a\">")
when RDoc::Markup::ListBase::LABELED then when :LABELED then
annotate("<dt>") + annotate("<dt>") +
convert_flow(am.flow(fragment.param)) + convert_flow(am.flow(fragment.param)) +
annotate("</dt>") + annotate("</dt>") +
annotate("<dd>") annotate("<dd>")
when RDoc::Markup::ListBase::NOTE then when :NOTE then
annotate("<tr>") + annotate("<tr>") +
annotate("<td valign=\"top\">") + annotate("<td valign=\"top\">") +
convert_flow(am.flow(fragment.param)) + convert_flow(am.flow(fragment.param)) +
@ -269,13 +269,11 @@ class RDoc::Markup::ToHtml
def list_end_for(fragment_type) def list_end_for(fragment_type)
case fragment_type case fragment_type
when RDoc::Markup::ListBase::BULLET, RDoc::Markup::ListBase::NUMBER, when :BULLET, :NUMBER, :UPPERALPHA, :LOWERALPHA then
RDoc::Markup::ListBase::UPPERALPHA,
RDoc::Markup::ListBase::LOWERALPHA then
"</li>" "</li>"
when RDoc::Markup::ListBase::LABELED then when :LABELED then
"</dd>" "</dd>"
when RDoc::Markup::ListBase::NOTE then when :NOTE then
"</td></tr>" "</td></tr>"
else else
raise "Invalid list type" raise "Invalid list type"

View file

@ -29,12 +29,12 @@ class RDoc::Markup::ToLaTeX
end end
LIST_TYPE_TO_LATEX = { LIST_TYPE_TO_LATEX = {
RDoc::Markup::ListBase::BULLET => [ l("\\begin{itemize}"), l("\\end{itemize}") ], :BULLET => [ l("\\begin{itemize}"), l("\\end{itemize}") ],
RDoc::Markup::ListBase::NUMBER => [ l("\\begin{enumerate}"), l("\\end{enumerate}"), "\\arabic" ], :NUMBER => [ l("\\begin{enumerate}"), l("\\end{enumerate}"), "\\arabic" ],
RDoc::Markup::ListBase::UPPERALPHA => [ l("\\begin{enumerate}"), l("\\end{enumerate}"), "\\Alph" ], :UPPERALPHA => [ l("\\begin{enumerate}"), l("\\end{enumerate}"), "\\Alph" ],
RDoc::Markup::ListBase::LOWERALPHA => [ l("\\begin{enumerate}"), l("\\end{enumerate}"), "\\alph" ], :LOWERALPHA => [ l("\\begin{enumerate}"), l("\\end{enumerate}"), "\\alph" ],
RDoc::Markup::ListBase::LABELED => [ l("\\begin{description}"), l("\\end{description}") ], :LABELED => [ l("\\begin{description}"), l("\\end{description}") ],
RDoc::Markup::ListBase::NOTE => [ :NOTE => [
l("\\begin{tabularx}{\\linewidth}{@{} l X @{}}"), l("\\begin{tabularx}{\\linewidth}{@{} l X @{}}"),
l("\\end{tabularx}") ], l("\\end{tabularx}") ],
} }
@ -299,15 +299,13 @@ class RDoc::Markup::ToLaTeX
def list_item_start(am, fragment) def list_item_start(am, fragment)
case fragment.type case fragment.type
when RDoc::Markup::ListBase::BULLET, RDoc::Markup::ListBase::NUMBER, when :BULLET, :NUMBER, :UPPERALPHA, :LOWERALPHA then
RDoc::Markup::ListBase::UPPERALPHA,
RDoc::Markup::ListBase::LOWERALPHA then
"\\item " "\\item "
when RDoc::Markup::ListBase::LABELED then when :LABELED then
"\\item[" + convert_flow(am.flow(fragment.param)) + "] " "\\item[" + convert_flow(am.flow(fragment.param)) + "] "
when RDoc::Markup::ListBase::NOTE then when :NOTE then
convert_flow(am.flow(fragment.param)) + " & " convert_flow(am.flow(fragment.param)) + " & "
else else
raise "Invalid list type" raise "Invalid list type"
@ -316,13 +314,9 @@ class RDoc::Markup::ToLaTeX
def list_end_for(fragment_type) def list_end_for(fragment_type)
case fragment_type case fragment_type
when RDoc::Markup::ListBase::BULLET, when :BULLET, :NUMBER, :UPPERALPHA, :LOWERALPHA, :LABELED then
RDoc::Markup::ListBase::NUMBER,
RDoc::Markup::ListBase::UPPERALPHA,
RDoc::Markup::ListBase::LOWERALPHA,
RDoc::Markup::ListBase::LABELED then
"" ""
when RDoc::Markup::ListBase::NOTE when :NOTE
"\\\\\n" "\\\\\n"
else else
raise "Invalid list type" raise "Invalid list type"

View file

@ -0,0 +1,49 @@
require 'rdoc/markup'
##
# This Markup outputter is used for testing purposes.
class RDoc::Markup::ToTest
def start_accepting
@res = []
end
def end_accepting
@res
end
def accept_paragraph(am, fragment)
@res << fragment.to_s
end
def accept_verbatim(am, fragment)
@res << fragment.to_s
end
def accept_list_start(am, fragment)
@res << fragment.to_s
end
def accept_list_end(am, fragment)
@res << fragment.to_s
end
def accept_list_item(am, fragment)
@res << fragment.to_s
end
def accept_blank_line(am, fragment)
@res << fragment.to_s
end
def accept_heading(am, fragment)
@res << fragment.to_s
end
def accept_rule(am, fragment)
@res << fragment.to_s
end
end

View file

@ -34,7 +34,7 @@ class RDoc::RI::DefaultDisplay
def initialize(formatter, width, use_stdout) def initialize(formatter, width, use_stdout)
@use_stdout = use_stdout @use_stdout = use_stdout
@formatter = formatter.new width, " " @formatter = formatter.new $stdout, width, " "
end end
def display_method_info(method) def display_method_info(method)

View file

@ -1,27 +1,49 @@
require 'rdoc/ri' require 'rdoc/ri'
require 'rdoc/markup'
class RDoc::RI::Formatter class RDoc::RI::Formatter
attr_reader :indent attr_reader :indent
def initialize(width, indent) FORMATTERS = { }
@width = width
@indent = indent 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
end end
def draw_line(label=nil) def draw_line(label=nil)
len = @width len = @width
len -= (label.size+1) if label len -= (label.size + 1) if label
print "-"*len
if label if len > 0 then
print(" ") @output.print '-' * len
bold_print(label) if label
@output.print ' '
bold_print label
end
@output.puts
else
@output.print '-' * @width
@output.puts
@output.puts label
end end
puts
end end
def wrap(txt, prefix=@indent, linelen=@width) def wrap(txt, prefix=@indent, linelen=@width)
return unless txt && !txt.empty? return unless txt && !txt.empty?
work = conv_markup(txt) work = conv_markup(txt)
textLen = linelen - prefix.length textLen = linelen - prefix.length
patt = Regexp.new("^(.{0,#{textLen}})[ \n]") patt = Regexp.new("^(.{0,#{textLen}})[ \n]")
@ -38,11 +60,11 @@ class RDoc::RI::Formatter
end end
end end
res << work if work.length.nonzero? res << work if work.length.nonzero?
puts(prefix + res.join("\n" + next_prefix)) @output.puts(prefix + res.join("\n" + next_prefix))
end end
def blankline def blankline
puts @output.puts
end end
## ##
@ -53,11 +75,11 @@ class RDoc::RI::Formatter
end end
def bold_print(txt) def bold_print(txt)
print txt @output.print txt
end end
def raw_print_line(txt) def raw_print_line(txt)
puts txt @output.puts txt
end end
## ##
@ -69,7 +91,6 @@ class RDoc::RI::Formatter
gsub(/&lt;/, '<'). gsub(/&lt;/, '<').
gsub(/&quot;/, '"'). gsub(/&quot;/, '"').
gsub(/&amp;/, '&') gsub(/&amp;/, '&')
end end
## ##
@ -78,25 +99,22 @@ class RDoc::RI::Formatter
def conv_markup(txt) def conv_markup(txt)
txt. txt.
gsub(%r{<tt>(.*?)</tt>}) { "+#$1+" } . gsub(%r{<tt>(.*?)</tt>}) { "+#$1+" } .
gsub(%r{<code>(.*?)</code>}) { "+#$1+" } . gsub(%r{<code>(.*?)</code>}) { "+#$1+" } .
gsub(%r{<b>(.*?)</b>}) { "*#$1*" } . gsub(%r{<b>(.*?)</b>}) { "*#$1*" } .
gsub(%r{<em>(.*?)</em>}) { "_#$1_" } gsub(%r{<em>(.*?)</em>}) { "_#$1_" }
end end
def display_list(list) def display_list(list)
case list.type case list.type
when :BULLET
when RDoc::Markup::ListBase::BULLET
prefixer = proc { |ignored| @indent + "* " } prefixer = proc { |ignored| @indent + "* " }
when RDoc::Markup::ListBase::NUMBER, when :NUMBER, :UPPERALPHA, :LOWERALPHA then
RDoc::Markup::ListBase::UPPERALPHA,
RDoc::Markup::ListBase::LOWERALPHA
start = case list.type start = case list.type
when RDoc::Markup::ListBase::NUMBER then 1 when :NUMBER then 1
when RDoc::Markup::ListBase::UPPERALPHA then 'A' when :UPPERALPHA then 'A'
when RDoc::Markup::ListBase::LOWERALPHA then 'a' when :LOWERALPHA then 'a'
end end
prefixer = proc do |ignored| prefixer = proc do |ignored|
res = @indent + "#{start}.".ljust(4) res = @indent + "#{start}.".ljust(4)
@ -104,12 +122,12 @@ class RDoc::RI::Formatter
res res
end end
when RDoc::Markup::ListBase::LABELED when :LABELED then
prefixer = proc do |li| prefixer = proc do |li|
li.label li.label
end end
when RDoc::Markup::ListBase::NOTE when :NOTE then
longest = 0 longest = 0
list.contents.each do |item| list.contents.each do |item|
if item.kind_of?(RDoc::Markup::Flow::LI) && item.label.length > longest if item.kind_of?(RDoc::Markup::Flow::LI) && item.label.length > longest
@ -122,8 +140,7 @@ class RDoc::RI::Formatter
end end
else else
fail "unknown list type" raise ArgumentError, "unknown list type #{list.type}"
end end
list.contents.each do |item| list.contents.each do |item|
@ -161,7 +178,7 @@ class RDoc::RI::Formatter
def display_verbatim_flow_item(item, prefix=@indent) def display_verbatim_flow_item(item, prefix=@indent)
item.body.split(/\n/).each do |line| item.body.split(/\n/).each do |line|
print @indent, conv_html(line), "\n" @output.print @indent, conv_html(line), "\n"
end end
blankline blankline
end end
@ -171,19 +188,19 @@ class RDoc::RI::Formatter
case level case level
when 1 when 1
ul = "=" * text.length ul = "=" * text.length
puts @output.puts
puts text.upcase @output.puts text.upcase
puts ul @output.puts ul
# puts # puts
when 2 when 2
ul = "-" * text.length ul = "-" * text.length
puts @output.puts
puts text @output.puts text
puts ul @output.puts ul
# puts # puts
else else
print indent, text, "\n" @output.print indent, text, "\n"
end end
end end
@ -274,11 +291,11 @@ class RDoc::RI::AttributeFormatter < RDoc::RI::Formatter
end end
## ##
# Overrides base class. Looks for <tt>...</tt> etc sequences # Overrides base class. Looks for <tt>...</tt> etc sequences and generates
# and generates an array of AttrChars. This array is then used # an array of AttrChars. This array is then used as the basis for the
# as the basis for the split # split.
def wrap(txt, prefix=@indent, linelen=@width) def wrap(txt, prefix=@indent, linelen=@width)
return unless txt && !txt.empty? return unless txt && !txt.empty?
txt = add_attributes_to(txt) txt = add_attributes_to(txt)
@ -303,15 +320,15 @@ class RDoc::RI::AttributeFormatter < RDoc::RI::Formatter
protected protected
def write_attribute_text(prefix, line) def write_attribute_text(prefix, line)
print prefix @output.print prefix
line.each do |achar| line.each do |achar|
print achar.char @output.print achar.char
end end
puts @output.puts
end end
def bold_print(txt) def bold_print(txt)
print txt @output.print txt
end end
private private
@ -342,18 +359,18 @@ class RDoc::RI::OverstrikeFormatter < RDoc::RI::AttributeFormatter
BS = "\C-h" BS = "\C-h"
def write_attribute_text(prefix, line) def write_attribute_text(prefix, line)
print prefix @output.print prefix
line.each do |achar| line.each do |achar|
attr = achar.attr attr = achar.attr
if (attr & (ITALIC+CODE)) != 0 if (attr & (ITALIC+CODE)) != 0
print "_", BS @output.print "_", BS
end end
if (attr & BOLD) != 0 if (attr & BOLD) != 0
print achar.char, BS @output.print achar.char, BS
end end
print achar.char @output.print achar.char
end end
puts @output.puts
end end
## ##
@ -361,7 +378,7 @@ class RDoc::RI::OverstrikeFormatter < RDoc::RI::AttributeFormatter
def bold_print(text) def bold_print(text)
text.split(//).each do |ch| text.split(//).each do |ch|
print ch, BS, ch @output.print ch, BS, ch
end end
end end
@ -374,12 +391,12 @@ end
class RDoc::RI::AnsiFormatter < RDoc::RI::AttributeFormatter class RDoc::RI::AnsiFormatter < RDoc::RI::AttributeFormatter
def initialize(*args) def initialize(*args)
print "\033[0m"
super super
@output.print "\033[0m"
end end
def write_attribute_text(prefix, line) def write_attribute_text(prefix, line)
print prefix @output.print prefix
curr_attr = 0 curr_attr = 0
line.each do |achar| line.each do |achar|
attr = achar.attr attr = achar.attr
@ -387,14 +404,14 @@ class RDoc::RI::AnsiFormatter < RDoc::RI::AttributeFormatter
update_attributes(achar.attr) update_attributes(achar.attr)
curr_attr = achar.attr curr_attr = achar.attr
end end
print achar.char @output.print achar.char
end end
update_attributes(0) unless curr_attr.zero? update_attributes(0) unless curr_attr.zero?
puts @output.puts
end end
def bold_print(txt) def bold_print(txt)
print "\033[1m#{txt}\033[m" @output.print "\033[1m#{txt}\033[m"
end end
HEADINGS = { HEADINGS = {
@ -406,10 +423,10 @@ class RDoc::RI::AnsiFormatter < RDoc::RI::AttributeFormatter
def display_heading(text, level, indent) def display_heading(text, level, indent)
level = 3 if level > 3 level = 3 if level > 3
heading = HEADINGS[level] heading = HEADINGS[level]
print indent @output.print indent
print heading[0] @output.print heading[0]
print strip_attributes(text) @output.print strip_attributes(text)
puts heading[1] @output.puts heading[1]
end end
private private
@ -427,7 +444,7 @@ class RDoc::RI::AnsiFormatter < RDoc::RI::AttributeFormatter
str << ATTR_MAP[quality] str << ATTR_MAP[quality]
end end
end end
print str, "m" @output.print str, "m"
end end
end end
@ -445,7 +462,7 @@ class RDoc::RI::HtmlFormatter < RDoc::RI::AttributeFormatter
update_attributes(curr_attr, achar.attr) update_attributes(curr_attr, achar.attr)
curr_attr = achar.attr curr_attr = achar.attr
end end
print(escape(achar.char)) @output.print(escape(achar.char))
end end
update_attributes(curr_attr, 0) unless curr_attr.zero? update_attributes(curr_attr, 0) unless curr_attr.zero?
end end
@ -454,7 +471,7 @@ class RDoc::RI::HtmlFormatter < RDoc::RI::AttributeFormatter
if label != nil if label != nil
bold_print(label) bold_print(label)
end end
puts("<hr>") @output.puts("<hr>")
end end
def bold_print(txt) def bold_print(txt)
@ -462,38 +479,36 @@ class RDoc::RI::HtmlFormatter < RDoc::RI::AttributeFormatter
end end
def blankline() def blankline()
puts("<p>") @output.puts("<p>")
end end
def break_to_newline def break_to_newline
puts("<br>") @output.puts("<br>")
end end
def display_heading(text, level, indent) def display_heading(text, level, indent)
level = 4 if level > 4 level = 4 if level > 4
tag("h#{level}") { text } tag("h#{level}") { text }
puts @output.puts
end end
def display_list(list) def display_list(list)
case list.type case list.type
when RDoc::Markup::ListBase::BULLET when :BULLET then
list_type = "ul" list_type = "ul"
prefixer = proc { |ignored| "<li>" } prefixer = proc { |ignored| "<li>" }
when RDoc::Markup::ListBase::NUMBER, when :NUMBER, :UPPERALPHA, :LOWERALPHA then
RDoc::Markup::ListBase::UPPERALPHA,
RDoc::Markup::ListBase::LOWERALPHA
list_type = "ol" list_type = "ol"
prefixer = proc { |ignored| "<li>" } prefixer = proc { |ignored| "<li>" }
when RDoc::Markup::ListBase::LABELED when :LABELED then
list_type = "dl" list_type = "dl"
prefixer = proc do |li| prefixer = proc do |li|
"<dt><b>" + escape(li.label) + "</b><dd>" "<dt><b>" + escape(li.label) + "</b><dd>"
end end
when RDoc::Markup::ListBase::NOTE when :NOTE then
list_type = "table" list_type = "table"
prefixer = proc do |li| prefixer = proc do |li|
%{<tr valign="top"><td>#{li.label.gsub(/ /, '&nbsp;')}</td><td>} %{<tr valign="top"><td>#{li.label.gsub(/ /, '&nbsp;')}</td><td>}
@ -502,25 +517,25 @@ class RDoc::RI::HtmlFormatter < RDoc::RI::AttributeFormatter
fail "unknown list type" fail "unknown list type"
end end
print "<#{list_type}>" @output.print "<#{list_type}>"
list.contents.each do |item| list.contents.each do |item|
if item.kind_of? RDoc::Markup::Flow::LI if item.kind_of? RDoc::Markup::Flow::LI
prefix = prefixer.call(item) prefix = prefixer.call(item)
print prefix @output.print prefix
display_flow_item(item, prefix) display_flow_item(item, prefix)
else else
display_flow_item(item) display_flow_item(item)
end end
end end
print "</#{list_type}>" @output.print "</#{list_type}>"
end end
def display_verbatim_flow_item(item, prefix=@indent) def display_verbatim_flow_item(item, prefix=@indent)
print("<pre>") @output.print("<pre>")
item.body.split(/\n/).each do |line| item.body.split(/\n/).each do |line|
puts conv_html(line) @output.puts conv_html(line)
end end
puts("</pre>") @output.puts("</pre>")
end end
private private
@ -547,13 +562,13 @@ class RDoc::RI::HtmlFormatter < RDoc::RI::AttributeFormatter
str << "<" << ATTR_MAP[quality] str << "<" << ATTR_MAP[quality]
end end
end end
print str @output.print str
end end
def tag(code) def tag(code)
print("<#{code}>") @output.print("<#{code}>")
print(yield) @output.print(yield)
print("</#{code}>") @output.print("</#{code}>")
end end
def escape(str) def escape(str)
@ -584,7 +599,7 @@ class RDoc::RI::SimpleFormatter < RDoc::RI::Formatter
def draw_line(label=nil) def draw_line(label=nil)
unless label.nil? then unless label.nil? then
bold_print(label) bold_print(label)
puts @output.puts
end end
end end
@ -595,36 +610,18 @@ class RDoc::RI::SimpleFormatter < RDoc::RI::Formatter
text = strip_attributes(text) text = strip_attributes(text)
case level case level
when 1 when 1
puts "= " + text.upcase @output.puts "= " + text.upcase
when 2 when 2
puts "-- " + text @output.puts "-- " + text
else else
print indent, text, "\n" @output.print indent, text, "\n"
end end
end end
end end
## RDoc::RI::Formatter::FORMATTERS['plain'] = RDoc::RI::Formatter
# Finally, fill in the list of known formatters RDoc::RI::Formatter::FORMATTERS['simple'] = RDoc::RI::SimpleFormatter
RDoc::RI::Formatter::FORMATTERS['bs'] = RDoc::RI::OverstrikeFormatter
class RDoc::RI::Formatter RDoc::RI::Formatter::FORMATTERS['ansi'] = RDoc::RI::AnsiFormatter
RDoc::RI::Formatter::FORMATTERS['html'] = RDoc::RI::HtmlFormatter
FORMATTERS = {
"plain" => RDoc::RI::Formatter,
"simple" => RDoc::RI::SimpleFormatter,
"bs" => RDoc::RI::OverstrikeFormatter,
"ansi" => RDoc::RI::AnsiFormatter,
"html" => RDoc::RI::HtmlFormatter,
}
def self.list
FORMATTERS.keys.sort.join(", ")
end
def self.for(name)
FORMATTERS[name.downcase]
end
end

View file

@ -17,7 +17,7 @@ class TestRdocC_Parser < Test::Unit::TestCase
@top_level = RDoc::TopLevel.new filename @top_level = RDoc::TopLevel.new filename
@fn = filename @fn = filename
@options = Options.instance @options = RDoc::Options.new Hash.new
@stats = RDoc::Stats.new @stats = RDoc::Stats.new
@progress = StringIO.new @progress = StringIO.new

View file

@ -1,62 +1,19 @@
require 'test/unit' require 'test/unit'
require 'rdoc/markup/simple_markup' require 'rdoc/markup'
require 'rdoc/markup/to_test'
class TestSimpleMarkup < Test::Unit::TestCase class TestRDocMarkup < Test::Unit::TestCase
class MockOutput
def start_accepting
@res = []
end
def end_accepting
@res
end
def accept_paragraph(am, fragment)
@res << fragment.to_s
end
def accept_verbatim(am, fragment)
@res << fragment.to_s
end
def accept_list_start(am, fragment)
@res << fragment.to_s
end
def accept_list_end(am, fragment)
@res << fragment.to_s
end
def accept_list_item(am, fragment)
@res << fragment.to_s
end
def accept_blank_line(am, fragment)
@res << fragment.to_s
end
def accept_heading(am, fragment)
@res << fragment.to_s
end
def accept_rule(am, fragment)
@res << fragment.to_s
end
end
def basic_conv(str) def basic_conv(str)
sm = SM::SimpleMarkup.new sm = RDoc::Markup.new
mock = MockOutput.new mock = RDoc::Markup::ToTest.new
sm.convert(str, mock) sm.convert(str, mock)
sm.content sm.content
end end
def line_groups(str, expected) def line_groups(str, expected)
p = SM::SimpleMarkup.new p = RDoc::Markup.new
mock = MockOutput.new mock = RDoc::Markup::ToTest.new
block = p.convert(str, mock) block = p.convert(str, mock)
@ -72,8 +29,8 @@ class TestSimpleMarkup < Test::Unit::TestCase
end end
def line_types(str, expected) def line_types(str, expected)
p = SM::SimpleMarkup.new p = RDoc::Markup.new
mock = MockOutput.new mock = RDoc::Markup::ToTest.new
p.convert(str, mock) p.convert(str, mock)
assert_equal(expected, p.get_line_types.map{|type| type.to_s[0,1]}.join('')) assert_equal(expected, p.get_line_types.map{|type| type.to_s[0,1]}.join(''))
end end

View file

@ -1,10 +1,10 @@
require "test/unit" require "test/unit"
require "rdoc/markup/simple_markup/inline" require "rdoc/markup/inline"
class TestSimpleMarkupAttributeManager < Test::Unit::TestCase class TestRDocMarkupAttributeManager < Test::Unit::TestCase
def setup def setup
@am = SM::AttributeManager.new @am = RDoc::Markup::AttributeManager.new
@bold_on = @am.changed_attribute_by_name([], [:BOLD]) @bold_on = @am.changed_attribute_by_name([], [:BOLD])
@bold_off = @am.changed_attribute_by_name([:BOLD], []) @bold_off = @am.changed_attribute_by_name([:BOLD], [])
@ -28,11 +28,11 @@ class TestSimpleMarkupAttributeManager < Test::Unit::TestCase
end end
def crossref(text) def crossref(text)
crossref_bitmap = SM::Attribute.bitmap_for(:_SPECIAL_) | crossref_bitmap = RDoc::Markup::Attribute.bitmap_for(:_SPECIAL_) |
SM::Attribute.bitmap_for(:CROSSREF) RDoc::Markup::Attribute.bitmap_for(:CROSSREF)
[ @am.changed_attribute_by_name([], [:CROSSREF] | [:_SPECIAL_]), [ @am.changed_attribute_by_name([], [:CROSSREF] | [:_SPECIAL_]),
SM::Special.new(crossref_bitmap, text), RDoc::Markup::Special.new(crossref_bitmap, text),
@am.changed_attribute_by_name([:CROSSREF] | [:_SPECIAL_], []) @am.changed_attribute_by_name([:CROSSREF] | [:_SPECIAL_], [])
] ]
end end

View file

@ -0,0 +1,42 @@
require 'stringio'
require 'test/unit'
require 'rdoc/ri/formatter'
class TestRDocRIAttributeFormatter < Test::Unit::TestCase
def setup
@output = StringIO.new
@width = 78
@indent = ' '
@f = RDoc::RI::AttributeFormatter.new @output, @width, @indent
end
def test_wrap_empty
@f.wrap ''
assert_equal '', @output.string
end
def test_wrap_long
@f.wrap 'a ' * (@width / 2)
assert_equal " a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a \n a \n",
@output.string
end
def test_wrap_markup
@f.wrap 'a <tt>b</tt> c'
assert_equal " a b c\n", @output.string
end
def test_wrap_nil
@f.wrap nil
assert_equal '', @output.string
end
def test_wrap_short
@f.wrap 'a b c'
assert_equal " a b c\n", @output.string
end
end

View file

@ -0,0 +1,124 @@
require 'stringio'
require 'test/unit'
require 'rdoc/ri/formatter'
require 'rdoc/markup/to_flow'
class TestRDocRIFormatter < Test::Unit::TestCase
def setup
@output = StringIO.new
@width = 78
@indent = ' '
@f = RDoc::RI::Formatter.new @output, @width, @indent
@markup = RDoc::Markup.new
@flow = RDoc::Markup::ToFlow.new
end
def test_blankline
@f.blankline
assert_equal "\n", @output.string
end
def test_bold_print
@f.bold_print 'a b c'
assert_equal 'a b c', @output.string
end
def test_break_to_newline
@f.break_to_newline
assert_equal '', @output.string
end
def test_conv_html
assert_equal '> < " &', @f.conv_html('&gt; &lt; &quot; &amp;')
end
def test_conv_markup
text = '<tt>a</tt> <code>b</code> <b>c</b> <em>d</em>'
expected = '+a+ +b+ *c* _d_'
assert_equal expected, @f.conv_markup(text)
end
def test_display_list_bullet
list = util_convert('* a b c').first
@f.display_list list
assert_equal " * a b c\n\n", @output.string
end
def test_display_list_unknown
list = util_convert('* a b c').first
list.instance_variable_set :@type, :UNKNOWN
e = assert_raise ArgumentError do
@f.display_list list
end
assert_equal 'unknown list type UNKNOWN', e.message
end
def test_draw_line
@f.draw_line
expected = '-' * @width + "\n"
assert_equal expected, @output.string
end
def test_draw_line_label
@f.draw_line 'label'
expected = '-' * (@width - 6) + " label\n"
assert_equal expected, @output.string
end
def test_draw_line_label_long
@f.draw_line 'a' * @width
expected = '-' * @width + "\n" + ('a' * @width) + "\n"
assert_equal expected, @output.string
end
def test_raw_print_line
@f.raw_print_line 'a b c'
assert_equal "a b c\n", @output.string
end
def test_wrap_empty
@f.wrap ''
assert_equal '', @output.string
end
def test_wrap_long
@f.wrap 'a ' * (@width / 2)
assert_equal " a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a\n a \n",
@output.string
end
def test_wrap_markup
@f.wrap 'a <tt>b</tt> c'
assert_equal " a +b+ c\n", @output.string
end
def test_wrap_nil
@f.wrap nil
assert_equal '', @output.string
end
def test_wrap_short
@f.wrap 'a b c'
assert_equal " a b c\n", @output.string
end
def util_convert(text)
@markup.convert text, @flow
end
end