class RDoc::Generator::Mdoc::Formatter
Format an RDoc::Document into mdoc.
Attributes
Public Class Methods
Instantiate a mdoc formatter that escapes special mdoc characters.
# File lib/rdoc/generator/mdoc/formatter.rb, line 12 def initialize(options=nil, markup = nil) super init_attribute_manager_tags end
Public Instance Methods
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
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
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
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
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
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
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
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
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
Format a horizontal ruler.
This is a no-op.
# File lib/rdoc/generator/mdoc/formatter.rb, line 125 def accept_rule(rule) end
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
Compile the parts together.
# File lib/rdoc/generator/mdoc/formatter.rb, line 26 def end_accepting handle_leading_punctuation parts.join.squeeze("\n") end
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
# File lib/rdoc/generator/mdoc/formatter.rb, line 149 def attribute_manager @am end
# File lib/rdoc/generator/mdoc/formatter.rb, line 161 def convert_string(string) escape(string.strip) end
# File lib/rdoc/generator/mdoc/formatter.rb, line 153 def current_list_type list_types.last end
# File lib/rdoc/generator/mdoc/formatter.rb, line 144 def handle_inline_attributes(text) flow = attribute_manager.flow(text.dup) convert_flow flow end
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
# File lib/rdoc/generator/mdoc/formatter.rb, line 157 def in_list? !list_types.empty? end