Class: RDoc::Markup::ToMarkdown

Inherits:
ToRdoc show all
Defined in:
lib/rdoc/markup/to_markdown.rb

Overview

Outputs parsed markup as Markdown

Instance Attribute Summary

Attributes inherited from ToRdoc

#indent, #list_index, #list_type, #list_width, #prefix, #res, #width

Instance Method Summary collapse

Methods inherited from ToRdoc

#accept_blank_line, #accept_block_quote, #accept_heading, #accept_indented_paragraph, #accept_paragraph, #accept_raw, #accept_table, #attributes, #end_accepting, #handle_regexp_SUPPRESSED_CROSSREF, #start_accepting, #use_prefix, #wrap

Methods inherited from Formatter

#accept_document, #add_regexp_handling_RDOCLINK, #add_regexp_handling_TIDYLINK, #add_tag, #annotate, #convert, #convert_flow, #convert_regexp_handling, #convert_string, gen_relative_url, #ignore, #in_tt?, #off_tags, #on_tags, #parse_url, #tt?

Constructor Details

#initialize(markup = nil) ⇒ ToMarkdown

Creates a new formatter that will output Markdown format text



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rdoc/markup/to_markdown.rb', line 12

def initialize markup = nil
  super

  @headings[1] = ['# ',      '']
  @headings[2] = ['## ',     '']
  @headings[3] = ['### ',    '']
  @headings[4] = ['#### ',   '']
  @headings[5] = ['##### ',  '']
  @headings[6] = ['###### ', '']

  add_regexp_handling_RDOCLINK
  add_regexp_handling_TIDYLINK

  @hard_break = "  \n"
end

Instance Method Details

#accept_list_end(list) ⇒ Object

Finishes consumption of ‘list`



47
48
49
50
51
# File 'lib/rdoc/markup/to_markdown.rb', line 47

def accept_list_end list
  @res << "\n"

  super
end

#accept_list_item_end(list_item) ⇒ Object

Finishes consumption of ‘list_item`



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rdoc/markup/to_markdown.rb', line 56

def accept_list_item_end list_item
  width = case @list_type.last
          when :BULLET then
            4
          when :NOTE, :LABEL then
            use_prefix

            4
          else
            @list_index[-1] = @list_index.last.succ
            4
          end

  @indent -= width
end

#accept_list_item_start(list_item) ⇒ Object

Prepares the visitor for consuming ‘list_item`



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rdoc/markup/to_markdown.rb', line 75

def accept_list_item_start list_item
  type = @list_type.last

  case type
  when :NOTE, :LABEL then
    bullets = Array(list_item.label).map do |label|
      attributes(label).strip
    end.join "\n"

    bullets << "\n:"

    @prefix = ' ' * @indent
    @indent += 4
    @prefix << bullets + (' ' * (@indent - 1))
  else
    bullet = type == :BULLET ? '*' : @list_index.last.to_s + '.'
    @prefix = (' ' * @indent) + bullet.ljust(4)

    @indent += 4
  end
end

#accept_list_start(list) ⇒ Object

Prepares the visitor for consuming ‘list`



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rdoc/markup/to_markdown.rb', line 100

def accept_list_start list
  case list.type
  when :BULLET, :LABEL, :NOTE then
    @list_index << nil
  when :LALPHA, :NUMBER, :UALPHA then
    @list_index << 1
  else
    raise RDoc::Error, "invalid list type #{list.type}"
  end

  @list_width << 4
  @list_type << list.type
end

#accept_rule(rule) ⇒ Object

Adds ‘rule` to the output



117
118
119
120
121
# File 'lib/rdoc/markup/to_markdown.rb', line 117

def accept_rule rule
  use_prefix or @res << ' ' * @indent
  @res << '-' * 3
  @res << "\n"
end

#accept_verbatim(verbatim) ⇒ Object

Outputs ‘verbatim` indented 4 columns



126
127
128
129
130
131
132
133
134
135
# File 'lib/rdoc/markup/to_markdown.rb', line 126

def accept_verbatim verbatim
  indent = ' ' * (@indent + 4)

  verbatim.parts.each do |part|
    @res << indent unless part == "\n"
    @res << part
  end

  @res << "\n"
end

#gen_url(url, text) ⇒ Object

Creates a Markdown-style URL from url with text.



140
141
142
143
144
# File 'lib/rdoc/markup/to_markdown.rb', line 140

def gen_url url, text
  scheme, url, = parse_url url

  "[#{text.sub(%r{^#{scheme}:/*}i, '')}](#{url})"
end

Handles rdoc- type links for footnotes.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rdoc/markup/to_markdown.rb', line 149

def handle_rdoc_link url
  case url
  when /^rdoc-ref:/ then
    $'
  when /^rdoc-label:footmark-(\d+)/ then
    "[^#{$1}]:"
  when /^rdoc-label:foottext-(\d+)/ then
    "[^#{$1}]"
  when /^rdoc-label:label-/ then
    gen_url url, $'
  when /^rdoc-image:/ then
    "![](#{$'})"
  when /^rdoc-[a-z]+:/ then
    $'
  end
end

#handle_regexp_HARD_BREAK(target) ⇒ Object

Adds a newline to the output



40
41
42
# File 'lib/rdoc/markup/to_markdown.rb', line 40

def handle_regexp_HARD_BREAK target
  "  \n"
end

Converts the rdoc-…: links into a Markdown.style links.



187
188
189
# File 'lib/rdoc/markup/to_markdown.rb', line 187

def handle_regexp_RDOCLINK target
  handle_rdoc_link target.text
end

Converts the RDoc markup tidylink into a Markdown.style link.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rdoc/markup/to_markdown.rb', line 169

def handle_regexp_TIDYLINK target
  text = target.text

  return text unless text =~ /\{(.*?)\}\[(.*?)\]/ or text =~ /(\S+)\[(.*?)\]/

  label = $1
  url   = $2

  if url =~ /^rdoc-label:foot/ then
    handle_rdoc_link url
  else
    gen_url url, label
  end
end

#init_tagsObject

Maps attributes to HTML sequences



31
32
33
34
35
# File 'lib/rdoc/markup/to_markdown.rb', line 31

def init_tags
  add_tag :BOLD, '**', '**'
  add_tag :EM,   '*',  '*'
  add_tag :TT,   '`',  '`'
end