From 8d37cefaf8795fe80d457f101fb9678fc7f6adf6 Mon Sep 17 00:00:00 2001 From: drbrain Date: Sat, 19 Jan 2008 00:06:19 +0000 Subject: * 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 --- lib/rdoc/markup/fragments.rb | 58 +++++++++++++++++++++++--------------------- lib/rdoc/markup/lines.rb | 16 ++++++------ lib/rdoc/markup/to_flow.rb | 12 ++++----- lib/rdoc/markup/to_html.rb | 30 +++++++++++------------ lib/rdoc/markup/to_latex.rb | 28 +++++++++------------ lib/rdoc/markup/to_test.rb | 49 +++++++++++++++++++++++++++++++++++++ 6 files changed, 119 insertions(+), 74 deletions(-) create mode 100644 lib/rdoc/markup/to_test.rb (limited to 'lib/rdoc/markup') diff --git a/lib/rdoc/markup/fragments.rb b/lib/rdoc/markup/fragments.rb index 2dd3614fc1..39b63cae22 100644 --- a/lib/rdoc/markup/fragments.rb +++ b/lib/rdoc/markup/fragments.rb @@ -11,6 +11,22 @@ class RDoc::Markup attr_reader :level, :param, :txt 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) @level = level @param = param @@ -28,21 +44,6 @@ class RDoc::Markup "L#@level: #{self.class.name.split('::')[-1]}\n#@txt" 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 ## @@ -50,15 +51,15 @@ class RDoc::Markup # newlines when we're created, and have them put back on output. class Paragraph < Fragment - type_name Line::PARAGRAPH + type_name :PARAGRAPH end class BlankLine < Paragraph - type_name Line::BLANK + type_name :BLANK end class Heading < Paragraph - type_name Line::HEADING + type_name :HEADING def head_level @param.to_i @@ -69,17 +70,18 @@ class RDoc::Markup # A List is a fragment with some kind of label class ListBase < Paragraph - # List types - BULLET = :BULLET - NUMBER = :NUMBER - UPPERALPHA = :UPPERALPHA - LOWERALPHA = :LOWERALPHA - LABELED = :LABELED - NOTE = :NOTE + LIST_TYPES = [ + :BULLET, + :NUMBER, + :UPPERALPHA, + :LOWERALPHA, + :LABELED, + :NOTE, + ] end class ListItem < ListBase - type_name Line::LIST + type_name :LIST # def label # am = AttributeManager.new(@param) @@ -103,7 +105,7 @@ class RDoc::Markup # Verbatim code contains lines that don't get wrapped. class Verbatim < Fragment - type_name Line::VERBATIM + type_name :VERBATIM def add_text(txt) @txt << txt.chomp << "\n" @@ -115,7 +117,7 @@ class RDoc::Markup # A horizontal rule class Rule < Fragment - type_name Line::RULE + type_name :RULE end ## diff --git a/lib/rdoc/markup/lines.rb b/lib/rdoc/markup/lines.rb index 5fb5bde993..985304c225 100644 --- a/lib/rdoc/markup/lines.rb +++ b/lib/rdoc/markup/lines.rb @@ -8,12 +8,14 @@ class RDoc::Markup class Line INFINITY = 9999 - BLANK = :BLANK - HEADING = :HEADING - LIST = :LIST - RULE = :RULE - PARAGRAPH = :PARAGRAPH - VERBATIM = :VERBATIM + LINE_TYPES = [ + :BLANK, + :HEADING, + :LIST, + :PARAGRAPH, + :RULE, + :VERBATIM, + ] # line type attr_accessor :type @@ -132,7 +134,7 @@ class RDoc::Markup def normalize 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 end diff --git a/lib/rdoc/markup/to_flow.rb b/lib/rdoc/markup/to_flow.rb index 58ae52be24..cb7da5fa88 100644 --- a/lib/rdoc/markup/to_flow.rb +++ b/lib/rdoc/markup/to_flow.rb @@ -24,12 +24,12 @@ class RDoc::Markup class ToFlow LIST_TYPE_TO_HTML = { - RDoc::Markup::ListBase::BULLET => [ "" ], - RDoc::Markup::ListBase::NUMBER => [ "
    ", "
" ], - RDoc::Markup::ListBase::UPPERALPHA => [ "
    ", "
" ], - RDoc::Markup::ListBase::LOWERALPHA => [ "
    ", "
" ], - RDoc::Markup::ListBase::LABELED => [ "
", "
" ], - RDoc::Markup::ListBase::NOTE => [ "", "
" ], + :BULLET => [ "" ], + :NUMBER => [ "
    ", "
" ], + :UPPERALPHA => [ "
    ", "
" ], + :LOWERALPHA => [ "
    ", "
" ], + :LABELED => [ "
", "
" ], + :NOTE => [ "", "
" ], } InlineTag = Struct.new(:bit, :on, :off) diff --git a/lib/rdoc/markup/to_html.rb b/lib/rdoc/markup/to_html.rb index 98259cfd16..0238d5ae67 100644 --- a/lib/rdoc/markup/to_html.rb +++ b/lib/rdoc/markup/to_html.rb @@ -6,12 +6,12 @@ require 'cgi' class RDoc::Markup::ToHtml LIST_TYPE_TO_HTML = { - RDoc::Markup::ListBase::BULLET => [ "" ], - RDoc::Markup::ListBase::NUMBER => [ "
    ", "
" ], - RDoc::Markup::ListBase::UPPERALPHA => [ "
    ", "
" ], - RDoc::Markup::ListBase::LOWERALPHA => [ "
    ", "
" ], - RDoc::Markup::ListBase::LABELED => [ "
", "
" ], - RDoc::Markup::ListBase::NOTE => [ "", "
" ], + :BULLET => [ "" ], + :NUMBER => [ "
    ", "
" ], + :UPPERALPHA => [ "
    ", "
" ], + :LOWERALPHA => [ "
    ", "
" ], + :LABELED => [ "
", "
" ], + :NOTE => [ "", "
" ], } InlineTag = Struct.new(:bit, :on, :off) @@ -241,22 +241,22 @@ class RDoc::Markup::ToHtml def list_item_start(am, fragment) case fragment.type - when RDoc::Markup::ListBase::BULLET, RDoc::Markup::ListBase::NUMBER then + when :BULLET, :NUMBER then annotate("
  • ") - when RDoc::Markup::ListBase::UPPERALPHA then + when :UPPERALPHA then annotate("
  • ") - when RDoc::Markup::ListBase::LOWERALPHA then + when :LOWERALPHA then annotate("
  • ") - when RDoc::Markup::ListBase::LABELED then + when :LABELED then annotate("
    ") + convert_flow(am.flow(fragment.param)) + annotate("
    ") + annotate("
    ") - when RDoc::Markup::ListBase::NOTE then + when :NOTE then annotate("") + annotate("") + convert_flow(am.flow(fragment.param)) + @@ -269,13 +269,11 @@ class RDoc::Markup::ToHtml def list_end_for(fragment_type) case fragment_type - when RDoc::Markup::ListBase::BULLET, RDoc::Markup::ListBase::NUMBER, - RDoc::Markup::ListBase::UPPERALPHA, - RDoc::Markup::ListBase::LOWERALPHA then + when :BULLET, :NUMBER, :UPPERALPHA, :LOWERALPHA then "
  • " - when RDoc::Markup::ListBase::LABELED then + when :LABELED then "" - when RDoc::Markup::ListBase::NOTE then + when :NOTE then "" else raise "Invalid list type" diff --git a/lib/rdoc/markup/to_latex.rb b/lib/rdoc/markup/to_latex.rb index 7389ba112f..8b7e33719b 100644 --- a/lib/rdoc/markup/to_latex.rb +++ b/lib/rdoc/markup/to_latex.rb @@ -29,12 +29,12 @@ class RDoc::Markup::ToLaTeX end LIST_TYPE_TO_LATEX = { - RDoc::Markup::ListBase::BULLET => [ l("\\begin{itemize}"), l("\\end{itemize}") ], - RDoc::Markup::ListBase::NUMBER => [ l("\\begin{enumerate}"), l("\\end{enumerate}"), "\\arabic" ], - RDoc::Markup::ListBase::UPPERALPHA => [ l("\\begin{enumerate}"), l("\\end{enumerate}"), "\\Alph" ], - RDoc::Markup::ListBase::LOWERALPHA => [ l("\\begin{enumerate}"), l("\\end{enumerate}"), "\\alph" ], - RDoc::Markup::ListBase::LABELED => [ l("\\begin{description}"), l("\\end{description}") ], - RDoc::Markup::ListBase::NOTE => [ + :BULLET => [ l("\\begin{itemize}"), l("\\end{itemize}") ], + :NUMBER => [ l("\\begin{enumerate}"), l("\\end{enumerate}"), "\\arabic" ], + :UPPERALPHA => [ l("\\begin{enumerate}"), l("\\end{enumerate}"), "\\Alph" ], + :LOWERALPHA => [ l("\\begin{enumerate}"), l("\\end{enumerate}"), "\\alph" ], + :LABELED => [ l("\\begin{description}"), l("\\end{description}") ], + :NOTE => [ l("\\begin{tabularx}{\\linewidth}{@{} l X @{}}"), l("\\end{tabularx}") ], } @@ -299,15 +299,13 @@ class RDoc::Markup::ToLaTeX def list_item_start(am, fragment) case fragment.type - when RDoc::Markup::ListBase::BULLET, RDoc::Markup::ListBase::NUMBER, - RDoc::Markup::ListBase::UPPERALPHA, - RDoc::Markup::ListBase::LOWERALPHA then + when :BULLET, :NUMBER, :UPPERALPHA, :LOWERALPHA then "\\item " - when RDoc::Markup::ListBase::LABELED then + when :LABELED then "\\item[" + convert_flow(am.flow(fragment.param)) + "] " - when RDoc::Markup::ListBase::NOTE then + when :NOTE then convert_flow(am.flow(fragment.param)) + " & " else raise "Invalid list type" @@ -316,13 +314,9 @@ class RDoc::Markup::ToLaTeX def list_end_for(fragment_type) case fragment_type - when RDoc::Markup::ListBase::BULLET, - RDoc::Markup::ListBase::NUMBER, - RDoc::Markup::ListBase::UPPERALPHA, - RDoc::Markup::ListBase::LOWERALPHA, - RDoc::Markup::ListBase::LABELED then + when :BULLET, :NUMBER, :UPPERALPHA, :LOWERALPHA, :LABELED then "" - when RDoc::Markup::ListBase::NOTE + when :NOTE "\\\\\n" else raise "Invalid list type" diff --git a/lib/rdoc/markup/to_test.rb b/lib/rdoc/markup/to_test.rb new file mode 100644 index 0000000000..eb183e5ba4 --- /dev/null +++ b/lib/rdoc/markup/to_test.rb @@ -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 + -- cgit v1.2.3