Class: RDoc::Markup::ToRdoc
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
| Subclasses: | |
| Super Chains via Extension / Inclusion / Inheritance | |
| Class Chain: 
          self,
           Formatter | |
| Instance Chain: 
          self,
           Formatter | |
| Inherits: | RDoc::Markup::Formatter 
 | 
| Defined in: | lib/rdoc/markup/to_rdoc.rb | 
Overview
Outputs RDoc markup as ::RDoc::RDoc markup! (mostly)
Constant Summary
- 
    DEFAULT_HEADINGS =
    
 # File 'lib/rdoc/markup/to_rdoc.rb', line 6{ 1 => ['= ', ''], 2 => ['== ', ''], 3 => ['=== ', ''], 4 => ['==== ', ''], 5 => ['===== ', ''], 6 => ['====== ', ''] }
Class Method Summary
- 
    
      .new(markup = nil)  ⇒ ToRdoc 
    
    constructor
    Creates a new formatter that will output (mostly) RDoc markup. 
Formatter - Inherited
| .gen_relative_url | Converts a target url to one that is relative to a given path. | 
| .new | Creates a new  | 
Instance Attribute Summary
- 
    
      #indent  
    
    rw
    Current indent amount for output in characters. 
- 
    
      #list_index  
    
    readonly
    Stack of current list indexes for alphabetic and numeric lists. 
- 
    
      #list_type  
    
    readonly
    Stack of list types. 
- 
    
      #list_width  
    
    readonly
    Stack of list widths for indentation. 
- 
    
      #prefix  
    
    readonly
    Prefix for the next list item. 
- 
    
      #res  
    
    readonly
    Output accumulator. 
- 
    
      #width  
    
    rw
    Output width in characters. 
Formatter - Inherited
| #in_tt? | Are we currently inside tt tags? | 
Instance Method Summary
- 
    
      #accept_blank_line(blank_line)  
    
    Adds blank_lineto the output.
- 
    
      #accept_block_quote(block_quote)  
    
    Adds paragraphto the output.
- 
    
      #accept_heading(heading)  
    
    Adds headingto the output.
- 
    
      #accept_indented_paragraph(paragraph)  
    
    Adds paragraphto the output.
- 
    
      #accept_list_end(list)  
    
    Finishes consumption of list
- 
    
      #accept_list_item_end(list_item)  
    
    Finishes consumption of list_item
- 
    
      #accept_list_item_start(list_item)  
    
    Prepares the visitor for consuming list_item
- 
    
      #accept_list_start(list)  
    
    Prepares the visitor for consuming list
- 
    
      #accept_paragraph(paragraph)  
    
    Adds paragraphto the output.
- 
    
      #accept_raw(raw)  
    
    Adds rawto the output.
- 
    
      #accept_rule(rule)  
    
    Adds ruleto the output.
- 
    
      #accept_table(header, body, aligns)  
    
    Adds tableto the output.
- 
    
      #accept_verbatim(verbatim)  
    
    Outputs verbatimindented 2 columns.
- 
    
      #attributes(text)  
    
    Applies attribute-specific markup to textusingRDoc::AttributeManager
- 
    
      #end_accepting  
    
    Returns the generated output. 
- 
    
      #handle_regexp_HARD_BREAK(target)  
    
    Adds a newline to the output. 
- 
    
      #handle_regexp_SUPPRESSED_CROSSREF(target)  
    
    Removes preceding \ from the suppressed crossref target
- 
    
      #init_tags  
    
    Maps attributes to HTML sequences. 
- 
    
      #start_accepting  
    
    Prepares the visitor for text generation. 
- 
    
      #use_prefix  
    
    Adds the stored #prefix to the output and clears it. 
- 
    
      #wrap(text)  
    
    Wraps textto #width
Formatter - Inherited
| #accept_document | Adds  | 
| #add_regexp_handling_RDOCLINK | Adds a regexp handling for links of the form rdoc-…: | 
| #add_regexp_handling_TIDYLINK | Adds a regexp handling for links of the form  | 
| #add_tag | Add a new set of tags for an attribute. | 
| #annotate | Allows  | 
| #convert | Marks up  | 
| #convert_flow | Converts flow items  | 
| #convert_regexp_handling | Converts added regexp handlings. | 
| #convert_string | Converts a string to be fancier if desired. | 
| #each_attr_tag, | |
| #ignore | Use ignore in your subclass to ignore the content of a node. | 
| #off_tags | Turns off tags for  | 
| #on_tags | Turns on tags for  | 
| #parse_url | Extracts and a scheme, url and an anchor id from  | 
| #tt? | Is  | 
| #tt_tag? | |
Constructor Details
    .new(markup = nil)  ⇒ ToRdoc 
  
Creates a new formatter that will output (mostly) RDoc markup
# File 'lib/rdoc/markup/to_rdoc.rb', line 55
def initialize(markup = nil) super nil, markup @markup.add_regexp_handling(/\\\S/, :SUPPRESSED_CROSSREF) @width = 78 @headings = DEFAULT_HEADINGS.dup @hard_break = "\n" end
Instance Attribute Details
#indent (rw)
Current indent amount for output in characters
# File 'lib/rdoc/markup/to_rdoc.rb', line 20
attr_accessor :indent
#list_index (readonly)
Stack of current list indexes for alphabetic and numeric lists
# File 'lib/rdoc/markup/to_rdoc.rb', line 30
attr_reader :list_index
#list_type (readonly)
Stack of list types
# File 'lib/rdoc/markup/to_rdoc.rb', line 35
attr_reader :list_type
#list_width (readonly)
Stack of list widths for indentation
# File 'lib/rdoc/markup/to_rdoc.rb', line 40
attr_reader :list_width
#prefix (readonly)
Prefix for the next list item. See #use_prefix
# File 'lib/rdoc/markup/to_rdoc.rb', line 45
attr_reader :prefix
#res (readonly)
Output accumulator
# File 'lib/rdoc/markup/to_rdoc.rb', line 50
attr_reader :res
#width (rw)
Output width in characters
# File 'lib/rdoc/markup/to_rdoc.rb', line 25
attr_accessor :width
Instance Method Details
#accept_blank_line(blank_line)
Adds blank_line to the output
# File 'lib/rdoc/markup/to_rdoc.rb', line 78
def accept_blank_line(blank_line) @res << "\n" end
#accept_block_quote(block_quote)
Adds paragraph to the output
# File 'lib/rdoc/markup/to_rdoc.rb', line 85
def accept_block_quote(block_quote) @indent += 2 block_quote.parts.each do |part| @prefix = '> ' part.accept self end @indent -= 2 end
#accept_heading(heading)
Adds heading to the output
# File 'lib/rdoc/markup/to_rdoc.rb', line 100
def accept_heading(heading) use_prefix or @res << ' ' * @indent @res << @headings[heading.level][0] @res << attributes(heading.text) @res << @headings[heading.level][1] @res << "\n" end
#accept_indented_paragraph(paragraph)
Adds paragraph to the output
# File 'lib/rdoc/markup/to_rdoc.rb', line 212
def accept_indented_paragraph(paragraph) @indent += paragraph.indent text = paragraph.text @hard_break wrap attributes text @indent -= paragraph.indent end
#accept_list_end(list)
Finishes consumption of list
# File 'lib/rdoc/markup/to_rdoc.rb', line 111
def accept_list_end(list) @list_index.pop @list_type.pop @list_width.pop end
#accept_list_item_end(list_item)
Finishes consumption of list_item
# File 'lib/rdoc/markup/to_rdoc.rb', line 120
def accept_list_item_end(list_item) width = case @list_type.last when :BULLET then 2 when :NOTE, :LABEL then if @prefix then @res << @prefix.strip @prefix = nil end @res << "\n" 2 else bullet = @list_index.last.to_s @list_index[-1] = @list_index.last.succ bullet.length + 2 end @indent -= width end
#accept_list_item_start(list_item)
Prepares the visitor for consuming list_item
# File 'lib/rdoc/markup/to_rdoc.rb', line 144
def accept_list_item_start(list_item) type = @list_type.last case type when :NOTE, :LABEL then stripped_labels = Array(list_item.label).map do |label| attributes(label).strip end bullets = case type when :NOTE stripped_labels.map { |b| "#{b}::" } when :LABEL stripped_labels.map { |b| "[#{b}]" } end bullets = bullets.join("\n") bullets << "\n" unless stripped_labels.empty? @prefix = ' ' * @indent @indent += 2 @prefix << bullets + (' ' * @indent) else bullet = type == :BULLET ? '*' : @list_index.last.to_s + '.' @prefix = (' ' * @indent) + bullet.ljust(bullet.length + 1) width = bullet.length + 1 @indent += width end end
#accept_list_start(list)
Prepares the visitor for consuming list
# File 'lib/rdoc/markup/to_rdoc.rb', line 177
def accept_list_start(list) case list.type when :BULLET then @list_index << nil @list_width << 1 when :LABEL, :NOTE then @list_index << nil @list_width << 2 when :LALPHA then @list_index << 'a' @list_width << list.items.length.to_s.length when :NUMBER then @list_index << 1 @list_width << list.items.length.to_s.length when :UALPHA then @list_index << 'A' @list_width << list.items.length.to_s.length else raise RDoc::Error, "invalid list type #{list.type}" end @list_type << list.type end
#accept_paragraph(paragraph)
Adds paragraph to the output
# File 'lib/rdoc/markup/to_rdoc.rb', line 204
def accept_paragraph(paragraph) text = paragraph.text @hard_break wrap attributes text end
#accept_raw(raw)
Adds raw to the output
# File 'lib/rdoc/markup/to_rdoc.rb', line 222
def accept_raw(raw) @res << raw.parts.join("\n") end
#accept_rule(rule)
Adds rule to the output
# File 'lib/rdoc/markup/to_rdoc.rb', line 229
def accept_rule(rule) use_prefix or @res << ' ' * @indent @res << '-' * (@width - @indent) @res << "\n" end
#accept_table(header, body, aligns)
Adds table to the output
# File 'lib/rdoc/markup/to_rdoc.rb', line 252
def accept_table(header, body, aligns) widths = header.zip(*body).map do |cols| cols.map(&:size).max end aligns = aligns.map do |a| case a when nil, :center :center when :left :ljust when :right :rjust end end @res << header.zip(widths, aligns).map do |h, w, a| h.__send__(a, w) end.join("|").rstrip << "\n" @res << widths.map {|w| "-" * w }.join("|") << "\n" body.each do |row| @res << row.zip(widths, aligns).map do |t, w, a| t.__send__(a, w) end.join("|").rstrip << "\n" end end
#accept_verbatim(verbatim)
Outputs verbatim indented 2 columns
#attributes(text)
Applies attribute-specific markup to text using RDoc::AttributeManager
# File 'lib/rdoc/markup/to_rdoc.rb', line 280
def attributes(text) flow = @am.flow text.dup convert_flow flow end
#end_accepting
Returns the generated output
# File 'lib/rdoc/markup/to_rdoc.rb', line 288
def end_accepting @res.join end
#handle_regexp_HARD_BREAK(target)
Adds a newline to the output
# File 'lib/rdoc/markup/to_rdoc.rb', line 304
def handle_regexp_HARD_BREAK(target) "\n" end
#handle_regexp_SUPPRESSED_CROSSREF(target)
Removes preceding \ from the suppressed crossref target
# File 'lib/rdoc/markup/to_rdoc.rb', line 295
def handle_regexp_SUPPRESSED_CROSSREF(target) text = target.text text = text.sub('\\', '') unless in_tt? text end
#init_tags
Maps attributes to HTML sequences
# File 'lib/rdoc/markup/to_rdoc.rb', line 69
def add_tag :BOLD, "<b>", "</b>" add_tag :TT, "<tt>", "</tt>" add_tag :EM, "<em>", "</em>" end
#start_accepting
Prepares the visitor for text generation
# File 'lib/rdoc/markup/to_rdoc.rb', line 311
def start_accepting @res = [""] @indent = 0 @prefix = nil @list_index = [] @list_type = [] @list_width = [] end
#use_prefix
Adds the stored #prefix to the output and clears it. Lists generate a prefix for later consumption.
#wrap(text)
Wraps text to #width
# File 'lib/rdoc/markup/to_rdoc.rb', line 335
def wrap(text) return unless text && !text.empty? text_len = @width - @indent text_len = 20 if text_len < 20 next_prefix = ' ' * @indent prefix = @prefix || next_prefix @prefix = nil text.scan(/\G(?:([^ \n]{#{text_len}})(?=[^ \n])|(.{1,#{text_len}})(?:[ \n]|\z))/) do @res << prefix << ($1 || $2) << "\n" prefix = next_prefix end end