Class: RDoc::Markup::ToMarkdown
- Inherits:
-
Markup::ToRdoc
- Object
- Markup::ToRdoc
- RDoc::Markup::ToMarkdown
- Defined in:
- lib/rdoc/markup/to_markdown.rb
Overview
Outputs parsed markup as Markdown
Instance Method Summary collapse
-
#accept_list_end(list) ⇒ Object
Finishes consumption of
list. -
#accept_list_item_end(list_item) ⇒ Object
Finishes consumption of
list_item. -
#accept_list_item_start(list_item) ⇒ Object
Prepares the visitor for consuming
list_item. -
#accept_list_start(list) ⇒ Object
Prepares the visitor for consuming
list. -
#accept_rule(rule) ⇒ Object
Adds
ruleto the output. -
#accept_verbatim(verbatim) ⇒ Object
Outputs
verbatimindented 4 columns. - #add_tag(tag, simple_tag, content) ⇒ Object
-
#gen_url(url, text) ⇒ Object
Creates a Markdown-style URL from
urlwithtext. - #handle_BOLD(nodes) ⇒ Object
- #handle_BOLD_WORD(word) ⇒ Object
- #handle_EM(nodes) ⇒ Object
- #handle_EM_WORD(word) ⇒ Object
- #handle_HARD_BREAK ⇒ Object
-
#handle_rdoc_link(url) ⇒ Object
Handles rdoc- type links for footnotes.
-
#handle_regexp_RDOCLINK(text) ⇒ Object
Converts the rdoc-...: links into a Markdown.style links.
- #handle_STRIKE(nodes) ⇒ Object
- #handle_tag(nodes, simple_tag, tag) ⇒ Object
- #handle_TIDYLINK(label_part, url) ⇒ Object
- #handle_TT(text) ⇒ Object
-
#initialize ⇒ ToMarkdown
constructor
Creates a new formatter that will output Markdown format text.
Constructor Details
#initialize ⇒ ToMarkdown
Creates a new formatter that will output Markdown format text
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rdoc/markup/to_markdown.rb', line 14 def initialize super @headings[1] = ['# ', ''] @headings[2] = ['## ', ''] @headings[3] = ['### ', ''] @headings[4] = ['#### ', ''] @headings[5] = ['##### ', ''] @headings[6] = ['###### ', ''] add_regexp_handling_RDOCLINK @hard_break = " \n" end |
Instance Method Details
#accept_list_end(list) ⇒ Object
Finishes consumption of list
32 33 34 |
# File 'lib/rdoc/markup/to_markdown.rb', line 32 def accept_list_end(list) super end |
#accept_list_item_end(list_item) ⇒ Object
Finishes consumption of list_item
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rdoc/markup/to_markdown.rb', line 39 def accept_list_item_end(list_item) width = case @list_type.last when :BULLET 4 when :NOTE, :LABEL use_prefix @res << "\n" 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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rdoc/markup/to_markdown.rb', line 60 def accept_list_item_start(list_item) type = @list_type.last case type when :NOTE, :LABEL bullets = Array(list_item.label).map do |label| attributes(label).strip end.join "\n" bullets << "\n" unless bullets.empty? @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
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/rdoc/markup/to_markdown.rb', line 144 def accept_list_start(list) case list.type when :BULLET, :LABEL, :NOTE @list_index << nil when :LALPHA, :NUMBER, :UALPHA @list_index << 1 else raise Error, "invalid list type #{list.type}" end @list_width << 4 @list_type << list.type end |
#accept_rule(rule) ⇒ Object
Adds rule to the output
161 162 163 164 165 |
# File 'lib/rdoc/markup/to_markdown.rb', line 161 def accept_rule(rule) use_prefix or @res << ' ' * @indent @res << '-' * 3 @res << "\n" end |
#accept_verbatim(verbatim) ⇒ Object
Outputs verbatim indented 4 columns
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/rdoc/markup/to_markdown.rb', line 170 def accept_verbatim(verbatim) indent = ' ' * (@indent + 4) verbatim.parts.each do |part| @res << indent unless part == "\n" @res << part end @res << "\n" end |
#add_tag(tag, simple_tag, content) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/rdoc/markup/to_markdown.rb', line 82 def add_tag(tag, simple_tag, content) if content.match?(/\A[\w\s]+\z/) emit_inline("#{simple_tag}#{content}#{simple_tag}") else emit_inline("<#{tag}>#{content}</#{tag}>") end end |
#gen_url(url, text) ⇒ Object
Creates a Markdown-style URL from url with text.
184 185 186 187 188 |
# File 'lib/rdoc/markup/to_markdown.rb', line 184 def gen_url(url, text) scheme, url, = parse_url url "[#{text.sub(%r{^#{scheme}:/*}i, '')}](#{url})" end |
#handle_BOLD(nodes) ⇒ Object
113 114 115 |
# File 'lib/rdoc/markup/to_markdown.rb', line 113 def handle_BOLD(nodes) handle_tag(nodes, '**', 'strong') end |
#handle_BOLD_WORD(word) ⇒ Object
121 122 123 |
# File 'lib/rdoc/markup/to_markdown.rb', line 121 def handle_BOLD_WORD(word) add_tag('strong', '**', convert_string(word)) end |
#handle_EM(nodes) ⇒ Object
117 118 119 |
# File 'lib/rdoc/markup/to_markdown.rb', line 117 def handle_EM(nodes) handle_tag(nodes, '*', 'em') end |
#handle_EM_WORD(word) ⇒ Object
125 126 127 |
# File 'lib/rdoc/markup/to_markdown.rb', line 125 def handle_EM_WORD(word) add_tag('em', '*', convert_string(word)) end |
#handle_HARD_BREAK ⇒ Object
137 138 139 |
# File 'lib/rdoc/markup/to_markdown.rb', line 137 def handle_HARD_BREAK emit_inline(" \n") end |
#handle_rdoc_link(url) ⇒ Object
Handles rdoc- type links for footnotes.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/rdoc/markup/to_markdown.rb', line 193 def handle_rdoc_link(url) case url when /^rdoc-ref:/ $' when /^rdoc-label:footmark-(\d+)/ "[^#{$1}]:" when /^rdoc-label:foottext-(\d+)/ "[^#{$1}]" when /^rdoc-label:label-/ gen_url url, $' when /^rdoc-image:/ "" when /^rdoc-[a-z]+:/ $' end end |
#handle_regexp_RDOCLINK(text) ⇒ Object
Converts the rdoc-...: links into a Markdown.style links.
213 214 215 |
# File 'lib/rdoc/markup/to_markdown.rb', line 213 def handle_regexp_RDOCLINK(text) handle_rdoc_link text end |
#handle_STRIKE(nodes) ⇒ Object
133 134 135 |
# File 'lib/rdoc/markup/to_markdown.rb', line 133 def handle_STRIKE(nodes) handle_tag(nodes, '~~', 's') end |
#handle_tag(nodes, simple_tag, tag) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rdoc/markup/to_markdown.rb', line 90 def handle_tag(nodes, simple_tag, tag) if nodes.size == 1 && String === nodes[0] content = apply_regexp_handling(nodes[0]).map do |text, converted| converted ? text : convert_string(text) end.join add_tag(tag, simple_tag, content) else emit_inline("<#{tag}>") traverse_inline_nodes(nodes) emit_inline("</#{tag}>") end end |
#handle_TIDYLINK(label_part, url) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/rdoc/markup/to_markdown.rb', line 103 def handle_TIDYLINK(label_part, url) if url =~ /^rdoc-label:foot/ emit_inline(handle_rdoc_link(url)) else emit_inline('[') traverse_inline_nodes(label_part) emit_inline("](#{url})") end end |
#handle_TT(text) ⇒ Object
129 130 131 |
# File 'lib/rdoc/markup/to_markdown.rb', line 129 def handle_TT(text) add_tag('code', '`', convert_string(text)) end |