class RDoc::Generator::Mdoc::Formatter

Format an RDoc::Document into mdoc.

Attributes

list_types[RW]
parts[RW]

Public Class Methods

new(options=nil, markup = nil) click to toggle source

Instantiate a mdoc formatter that escapes special mdoc characters.

Calls superclass method
# File lib/rdoc/generator/mdoc/formatter.rb, line 12
def initialize(options=nil, markup = nil)
  super
  init_attribute_manager_tags
end

Public Instance Methods

accept_blank_line(blank_line) click to toggle source

Output paragraph macros for blank lines.

# File lib/rdoc/generator/mdoc/formatter.rb, line 59
def accept_blank_line(blank_line)
  parts << "\n.Pp\n"
end
accept_block_quote(block_quote) click to toggle source

Turn a large quoted section into a block display.

# File lib/rdoc/generator/mdoc/formatter.rb, line 51
def accept_block_quote(block_quote)
  parts << ".Bd -offset indent\n"
  block_quote.parts.each { |part| part.accept self }
  parts << "\n.Ed\n.Pp\n"
end
accept_heading(heading) click to toggle source

Turn a heading into a paragraph or a subsection header depending on wether we are currently processing a list or not. List items can’t contain subsection headers.

# File lib/rdoc/generator/mdoc/formatter.rb, line 34
def accept_heading(heading)
  if in_list?
    accept_paragraph(heading)
  else
    parts << ".Ss #{heading.text}\n"
  end
end
accept_list_end(list) click to toggle source

Close a list.

This works for all list types.

# File lib/rdoc/generator/mdoc/formatter.rb, line 84
def accept_list_end(list)
  list_types.pop
  parts << ".El\n"
end
accept_list_item_end(list_item) click to toggle source

Finish a list item.

This works for all list types.

# File lib/rdoc/generator/mdoc/formatter.rb, line 110
def accept_list_item_end(list_item)
end
accept_list_item_start(list_item) click to toggle source

Open a list item.

If the list has a label, that label is the list item. Otherwise, the list item has no content.

Also see accept_list_item_end.

# File lib/rdoc/generator/mdoc/formatter.rb, line 96
def accept_list_item_start(list_item)
  case current_list_type
  when :LABEL, :NOTE
    labels =  Array(list_item.label).join(", ")
    parts << ".It #{labels}\n"
  else
    parts << ".It\n"
  end
end
accept_list_start(list) click to toggle source

Open an enumerated, dictionary, or bulleted list.

The list must be closed using accept_list_start.

# File lib/rdoc/generator/mdoc/formatter.rb, line 67
def accept_list_start(list)
  list_types.push(list.type)

  case current_list_type
  when :NUMBER, :LALPHA, :UALPHA
    parts << ".Bl -enum\n"
  when :LABEL, :NOTE
    parts << ".Bl -hang -offset -indent\n"
  else
    parts << ".Bl -bullet\n"
  end
end
accept_paragraph(paragraph) click to toggle source

Output a paragraph macro for the escaped paragraph.

# File lib/rdoc/generator/mdoc/formatter.rb, line 44
def accept_paragraph(paragraph)
  parts << handle_inline_attributes(paragraph.text)
  parts << "\n.Pp\n"
end
accept_raw(raw) click to toggle source

Pass through all raw parts unparsed, separated by newlines.

# File lib/rdoc/generator/mdoc/formatter.rb, line 130
def accept_raw(raw)
  parts << raw.parts.join("\n")
end
accept_rule(rule) click to toggle source

Format a horizontal ruler.

This is a no-op.

# File lib/rdoc/generator/mdoc/formatter.rb, line 125
def accept_rule(rule)
end
accept_verbatim(verbatim) click to toggle source

Format code as an indented block.

# File lib/rdoc/generator/mdoc/formatter.rb, line 115
def accept_verbatim(verbatim)
  parts << ".Bd -literal -offset indent\n"
  parts << verbatim.text
  parts << "\n.Ed\n.Pp\n"
end
end_accepting() click to toggle source

Compile the parts together.

# File lib/rdoc/generator/mdoc/formatter.rb, line 26
def end_accepting
  handle_leading_punctuation parts.join.squeeze("\n")
end
start_accepting() click to toggle source

Initialize the formatter with an empty array of parts and lists.

# File lib/rdoc/generator/mdoc/formatter.rb, line 19
def start_accepting
  @parts = []
  @list_types = []
end

Private Instance Methods

attribute_manager() click to toggle source
# File lib/rdoc/generator/mdoc/formatter.rb, line 149
def attribute_manager
  @am
end
convert_string(string) click to toggle source
# File lib/rdoc/generator/mdoc/formatter.rb, line 161
def convert_string(string)
  escape(string.strip)
end
current_list_type() click to toggle source
# File lib/rdoc/generator/mdoc/formatter.rb, line 153
def current_list_type
  list_types.last
end
handle_inline_attributes(text) click to toggle source
# File lib/rdoc/generator/mdoc/formatter.rb, line 144
def handle_inline_attributes(text)
  flow = attribute_manager.flow(text.dup)
  convert_flow flow
end
handle_leading_punctuation(string) click to toggle source

Return a string with all new-lines immediately followed by one of:

.,:;()[]?!

immediately followed by whitespace or the end of line replaced with a space, the matched character and a space.

This is to prevent lines to start with ‘.` and to avoid whitespace between the last word of a sentence and the following punctuation character.

# File lib/rdoc/generator/mdoc/formatter.rb, line 176
def handle_leading_punctuation(string) # :doc:
  string.gsub(/\n([.,:;()\[\]?!])(\s|$)/, " \\1\n")
end
in_list?() click to toggle source
# File lib/rdoc/generator/mdoc/formatter.rb, line 157
def in_list?
  !list_types.empty?
end
init_attribute_manager_tags() click to toggle source
# File lib/rdoc/generator/mdoc/formatter.rb, line 138
def init_attribute_manager_tags
  add_tag :BOLD, "\n.Sy ", "\n"
  add_tag :EM, "\n.Em ", "\n"
  add_tag :TT, "\n.Li ", "\n"
end