2017-11-27 05:45:24 -05:00
|
|
|
# frozen_string_literal: true
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Outputs RDoc markup as RDoc markup! (mostly)
|
|
|
|
|
|
|
|
class RDoc::Markup::ToRdoc < RDoc::Markup::Formatter
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Current indent amount for output in characters
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
attr_accessor :indent
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# Output width in characters
|
|
|
|
|
|
|
|
attr_accessor :width
|
|
|
|
|
|
|
|
##
|
|
|
|
# Stack of current list indexes for alphabetic and numeric lists
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
attr_reader :list_index
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# Stack of list types
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
attr_reader :list_type
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# Stack of list widths for indentation
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
attr_reader :list_width
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# Prefix for the next list item. See #use_prefix
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
attr_reader :prefix
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# Output accumulator
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
attr_reader :res
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Creates a new formatter that will output (mostly) \RDoc markup
|
|
|
|
|
2011-06-16 00:59:24 -04:00
|
|
|
def initialize markup = nil
|
2012-11-26 23:28:14 -05:00
|
|
|
super nil, markup
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2018-10-17 02:28:20 -04:00
|
|
|
@markup.add_regexp_handling(/\\\S/, :SUPPRESSED_CROSSREF)
|
2010-04-01 03:45:16 -04:00
|
|
|
@width = 78
|
|
|
|
init_tags
|
|
|
|
|
|
|
|
@headings = {}
|
|
|
|
@headings.default = []
|
|
|
|
|
|
|
|
@headings[1] = ['= ', '']
|
|
|
|
@headings[2] = ['== ', '']
|
|
|
|
@headings[3] = ['=== ', '']
|
|
|
|
@headings[4] = ['==== ', '']
|
|
|
|
@headings[5] = ['===== ', '']
|
|
|
|
@headings[6] = ['====== ', '']
|
2012-11-26 23:28:14 -05:00
|
|
|
|
|
|
|
@hard_break = "\n"
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Maps attributes to HTML sequences
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
def init_tags
|
|
|
|
add_tag :BOLD, "<b>", "</b>"
|
|
|
|
add_tag :TT, "<tt>", "</tt>"
|
|
|
|
add_tag :EM, "<em>", "</em>"
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Adds +blank_line+ to the output
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def accept_blank_line blank_line
|
|
|
|
@res << "\n"
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# Adds +paragraph+ to the output
|
|
|
|
|
|
|
|
def accept_block_quote block_quote
|
|
|
|
@indent += 2
|
|
|
|
|
|
|
|
block_quote.parts.each do |part|
|
|
|
|
@prefix = '> '
|
|
|
|
|
|
|
|
part.accept self
|
|
|
|
end
|
|
|
|
|
|
|
|
@indent -= 2
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Adds +heading+ to the output
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
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
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Finishes consumption of +list+
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def accept_list_end list
|
|
|
|
@list_index.pop
|
|
|
|
@list_type.pop
|
|
|
|
@list_width.pop
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Finishes consumption of +list_item+
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def accept_list_item_end list_item
|
|
|
|
width = case @list_type.last
|
|
|
|
when :BULLET then
|
|
|
|
2
|
|
|
|
when :NOTE, :LABEL then
|
2012-11-26 23:28:14 -05:00
|
|
|
if @prefix then
|
|
|
|
@res << @prefix.strip
|
|
|
|
@prefix = nil
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
@res << "\n"
|
|
|
|
2
|
|
|
|
else
|
|
|
|
bullet = @list_index.last.to_s
|
|
|
|
@list_index[-1] = @list_index.last.succ
|
|
|
|
bullet.length + 2
|
|
|
|
end
|
|
|
|
|
|
|
|
@indent -= width
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Prepares the visitor for consuming +list_item+
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def accept_list_item_start list_item
|
2010-12-19 22:22:49 -05:00
|
|
|
type = @list_type.last
|
|
|
|
|
|
|
|
case type
|
2010-04-01 03:45:16 -04:00
|
|
|
when :NOTE, :LABEL then
|
2012-11-26 23:28:14 -05:00
|
|
|
bullets = Array(list_item.label).map do |label|
|
|
|
|
attributes(label).strip
|
|
|
|
end.join "\n"
|
|
|
|
|
|
|
|
bullets << ":\n" unless bullets.empty?
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
@prefix = ' ' * @indent
|
2010-04-01 03:45:16 -04:00
|
|
|
@indent += 2
|
2012-11-26 23:28:14 -05:00
|
|
|
@prefix << bullets + (' ' * @indent)
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
2010-12-19 22:22:49 -05:00
|
|
|
bullet = type == :BULLET ? '*' : @list_index.last.to_s + '.'
|
2010-04-01 03:45:16 -04:00
|
|
|
@prefix = (' ' * @indent) + bullet.ljust(bullet.length + 1)
|
|
|
|
width = bullet.length + 1
|
|
|
|
@indent += width
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Prepares the visitor for consuming +list+
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
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
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Adds +paragraph+ to the output
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def accept_paragraph paragraph
|
2012-11-26 23:28:14 -05:00
|
|
|
text = paragraph.text @hard_break
|
|
|
|
wrap attributes text
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
2010-04-26 23:45:22 -04:00
|
|
|
|
2011-06-16 00:59:24 -04:00
|
|
|
##
|
|
|
|
# Adds +paragraph+ to the output
|
|
|
|
|
|
|
|
def accept_indented_paragraph paragraph
|
|
|
|
@indent += paragraph.indent
|
2012-11-26 23:28:14 -05:00
|
|
|
text = paragraph.text @hard_break
|
|
|
|
wrap attributes text
|
2011-06-16 00:59:24 -04:00
|
|
|
@indent -= paragraph.indent
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Adds +raw+ to the output
|
|
|
|
|
2010-04-26 23:45:22 -04:00
|
|
|
def accept_raw raw
|
|
|
|
@res << raw.parts.join("\n")
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Adds +rule+ to the output
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def accept_rule rule
|
|
|
|
use_prefix or @res << ' ' * @indent
|
|
|
|
@res << '-' * (@width - @indent)
|
|
|
|
@res << "\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Outputs +verbatim+ indented 2 columns
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
def accept_verbatim verbatim
|
|
|
|
indent = ' ' * (@indent + 2)
|
|
|
|
|
|
|
|
verbatim.parts.each do |part|
|
2010-12-19 22:22:49 -05:00
|
|
|
@res << indent unless part == "\n"
|
|
|
|
@res << part
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2018-11-26 00:32:50 -05:00
|
|
|
@res << "\n"
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2021-01-25 10:31:00 -05:00
|
|
|
##
|
|
|
|
# Adds +table+ to the output
|
|
|
|
|
|
|
|
def accept_table header, body, aligns
|
|
|
|
widths = header.zip(body) do |h, b|
|
|
|
|
[h.size, b.size].max
|
|
|
|
end
|
|
|
|
aligns = aligns.map do |a|
|
|
|
|
case a
|
|
|
|
when nil
|
|
|
|
:center
|
|
|
|
when :left
|
|
|
|
:ljust
|
|
|
|
when :right
|
|
|
|
:rjust
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@res << header.zip(widths, aligns) 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) do |t, w, a|
|
|
|
|
t.__send__(a, w)
|
|
|
|
end.join("|").rstrip << "\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Applies attribute-specific markup to +text+ using RDoc::AttributeManager
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def attributes text
|
|
|
|
flow = @am.flow text.dup
|
|
|
|
convert_flow flow
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Returns the generated output
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def end_accepting
|
|
|
|
@res.join
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
2018-10-17 02:28:20 -04:00
|
|
|
# Removes preceding \\ from the suppressed crossref +target+
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2018-10-17 02:28:20 -04:00
|
|
|
def handle_regexp_SUPPRESSED_CROSSREF target
|
|
|
|
text = target.text
|
2010-12-19 22:22:49 -05:00
|
|
|
text = text.sub('\\', '') unless in_tt?
|
|
|
|
text
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# Adds a newline to the output
|
|
|
|
|
2018-10-17 02:28:20 -04:00
|
|
|
def handle_regexp_HARD_BREAK target
|
2012-11-26 23:28:14 -05:00
|
|
|
"\n"
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Prepares the visitor for text generation
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def start_accepting
|
|
|
|
@res = [""]
|
|
|
|
@indent = 0
|
|
|
|
@prefix = nil
|
|
|
|
|
|
|
|
@list_index = []
|
|
|
|
@list_type = []
|
|
|
|
@list_width = []
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Adds the stored #prefix to the output and clears it. Lists generate a
|
|
|
|
# prefix for later consumption.
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def use_prefix
|
2012-11-26 23:28:14 -05:00
|
|
|
prefix, @prefix = @prefix, nil
|
2010-04-01 03:45:16 -04:00
|
|
|
@res << prefix if prefix
|
|
|
|
|
|
|
|
prefix
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Wraps +text+ to #width
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def wrap text
|
|
|
|
return unless text && !text.empty?
|
|
|
|
|
|
|
|
text_len = @width - @indent
|
|
|
|
|
|
|
|
text_len = 20 if text_len < 20
|
|
|
|
|
|
|
|
re = /^(.{0,#{text_len}})[ \n]/
|
|
|
|
next_prefix = ' ' * @indent
|
|
|
|
|
|
|
|
prefix = @prefix || next_prefix
|
|
|
|
@prefix = nil
|
|
|
|
|
|
|
|
@res << prefix
|
|
|
|
|
|
|
|
while text.length > text_len
|
|
|
|
if text =~ re then
|
|
|
|
@res << $1
|
|
|
|
text.slice!(0, $&.length)
|
|
|
|
else
|
|
|
|
@res << text.slice!(0, text_len)
|
|
|
|
end
|
|
|
|
|
|
|
|
@res << "\n" << next_prefix
|
|
|
|
end
|
|
|
|
|
|
|
|
if text.empty? then
|
|
|
|
@res.pop
|
|
|
|
@res.pop
|
|
|
|
else
|
|
|
|
@res << text
|
|
|
|
@res << "\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|