class RDoc::Markup::ToRdoc
def accept_blank_line(blank_line)
def accept_blank_line(blank_line) @res << "\n" end
def accept_block_quote(block_quote)
def accept_block_quote(block_quote) @indent += 2 block_quote.parts.each do |part| @prefix = '> ' part.accept self end @indent -= 2 end
def accept_heading(heading)
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
def accept_indented_paragraph(paragraph)
def accept_indented_paragraph(paragraph) @indent += paragraph.indent text = paragraph.text @hard_break wrap attributes text @indent -= paragraph.indent end
def accept_list_end(list)
def accept_list_end(list) @list_index.pop @list_type.pop @list_width.pop end
def accept_list_item_end(list_item)
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
def accept_list_item_start(list_item)
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
def accept_list_start(list)
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
def accept_paragraph(paragraph)
def accept_paragraph(paragraph) text = paragraph.text @hard_break wrap attributes text end
def accept_raw(raw)
def accept_raw(raw) @res << raw.parts.join("\n") end
def accept_rule(rule)
def accept_rule(rule) use_prefix or @res << ' ' * @indent @res << '-' * (@width - @indent) @res << "\n" end
def accept_table(header, body, aligns)
def accept_table(header, body, aligns) header = header.map { |h| attributes h } body = body.map { |row| row.map { |t| attributes t } } widths = header.zip(*body).map do |cols| cols.compact.map { |col| calculate_text_width(col) }.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| extra_width = h.size - calculate_text_width(h) h.__send__(a, w + extra_width) end.join("|").rstrip << "\n" @res << widths.map {|w| "-" * w }.join("|") << "\n" body.each do |row| @res << widths.zip(aligns).each_with_index.map do |(w, a), i| t = row[i] || "" extra_width = t.size - calculate_text_width(t) t.__send__(a, w + extra_width) end.join("|").rstrip << "\n" end end
def accept_verbatim(verbatim)
def accept_verbatim(verbatim) indent = ' ' * (@indent + 2) verbatim.parts.each do |part| @res << indent unless part == "\n" @res << part end @res << "\n" end
def add_text(text)
def add_text(text) emit_inline(text) end
def attributes(text)
def attributes(text) handle_inline(text) end
def calculate_text_width(text)
def calculate_text_width(text) text.size end
def emit_inline(text)
def emit_inline(text) @inline_output << text end
def end_accepting
def end_accepting @res.join end
def handle_BOLD(target)
def handle_BOLD(target) on(:BOLD) super off(:BOLD) end
def handle_BOLD_WORD(word)
def handle_BOLD_WORD(word) on(:BOLD) super off(:BOLD) end
def handle_EM(target)
def handle_EM(target) on(:EM) super off(:EM) end
def handle_EM_WORD(word)
def handle_EM_WORD(word) on(:EM) super off(:EM) end
def handle_HARD_BREAK
def handle_HARD_BREAK add_text("\n") end
def handle_PLAIN_TEXT(text)
def handle_PLAIN_TEXT(text) add_text(text) end
def handle_REGEXP_HANDLING_TEXT(text)
def handle_REGEXP_HANDLING_TEXT(text) add_text(text) end
def handle_STRIKE(target)
def handle_STRIKE(target) on(:STRIKE) super off(:STRIKE) end
def handle_TIDYLINK(label_part, url)
def handle_TIDYLINK(label_part, url) super add_text("( #{url} )") end
def handle_TT(code)
def handle_TT(code) on(:TT) super off(:TT) end
def handle_inline(text, initial_attributes = [])
def handle_inline(text, initial_attributes = []) @attributes = Hash.new(0) initial_attributes.each { |attr| on(attr) } out = @inline_output = +'' super(text) @inline_output = nil out end
def handle_regexp_SUPPRESSED_CROSSREF(text)
def handle_regexp_SUPPRESSED_CROSSREF(text) text.sub('\\', '') end
def initialize(markup = nil)
def initialize(markup = nil) super nil, markup @markup.add_regexp_handling(/\\\S/, :SUPPRESSED_CROSSREF) @width = 78 @headings = DEFAULT_HEADINGS.dup @hard_break = "\n" end
def off(attr)
def off(attr) @attributes[attr] -= 1 @attributes.delete(attr) if @attributes[attr] == 0 end
def on(attr)
def on(attr) @attributes[attr] += 1 end
def start_accepting
def start_accepting @res = [""] @indent = 0 @prefix = nil @list_index = [] @list_type = [] @list_width = [] end
def use_prefix
def use_prefix prefix, @prefix = @prefix, nil @res << prefix if prefix prefix end
def wrap(text)
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