Class: RDoc::Markup::ToMarkdown
- 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
-
#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 ‘rule` to the output.
-
#accept_verbatim(verbatim) ⇒ Object
Outputs ‘verbatim` indented 4 columns.
-
#gen_url(url, text) ⇒ Object
Creates a Markdown-style URL from
url
withtext
. -
#handle_rdoc_link(url) ⇒ Object
Handles
rdoc-
type links for footnotes. -
#handle_special_HARD_BREAK(special) ⇒ Object
Adds a newline to the output.
-
#handle_special_RDOCLINK(special) ⇒ Object
Converts the rdoc-…: links into a Markdown.style links.
-
#handle_special_TIDYLINK(special) ⇒ Object
Converts the RDoc markup tidylink into a Markdown.style link.
-
#init_tags ⇒ Object
Maps attributes to HTML sequences.
-
#initialize(markup = nil) ⇒ ToMarkdown
constructor
Creates a new formatter that will output Markdown format text.
Methods inherited from ToRdoc
#accept_blank_line, #accept_block_quote, #accept_heading, #accept_indented_paragraph, #accept_paragraph, #accept_raw, #attributes, #end_accepting, #handle_special_SUPPRESSED_CROSSREF, #start_accepting, #use_prefix, #wrap
Methods inherited from Formatter
#accept_document, #add_special_RDOCLINK, #add_special_TIDYLINK, #add_tag, #annotate, #convert, #convert_flow, #convert_special, #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
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/rdoc/markup/to_markdown.rb', line 11 def initialize markup = nil super @headings[1] = ['# ', ''] @headings[2] = ['## ', ''] @headings[3] = ['### ', ''] @headings[4] = ['#### ', ''] @headings[5] = ['##### ', ''] @headings[6] = ['###### ', ''] add_special_RDOCLINK add_special_TIDYLINK @hard_break = " \n" end |
Instance Method Details
#accept_list_end(list) ⇒ Object
Finishes consumption of ‘list`
46 47 48 49 50 |
# File 'lib/rdoc/markup/to_markdown.rb', line 46 def accept_list_end list @res << "\n" super end |
#accept_list_item_end(list_item) ⇒ Object
Finishes consumption of ‘list_item`
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rdoc/markup/to_markdown.rb', line 55 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`
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rdoc/markup/to_markdown.rb', line 74 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`
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/rdoc/markup/to_markdown.rb', line 99 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
116 117 118 119 120 |
# File 'lib/rdoc/markup/to_markdown.rb', line 116 def accept_rule rule use_prefix or @res << ' ' * @indent @res << '-' * 3 @res << "\n" end |
#accept_verbatim(verbatim) ⇒ Object
Outputs ‘verbatim` indented 4 columns
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/rdoc/markup/to_markdown.rb', line 125 def accept_verbatim verbatim indent = ' ' * (@indent + 4) verbatim.parts.each do |part| @res << indent unless part == "\n" @res << part end @res << "\n" unless @res =~ /\n\z/ end |
#gen_url(url, text) ⇒ Object
Creates a Markdown-style URL from url
with text
.
139 140 141 142 143 |
# File 'lib/rdoc/markup/to_markdown.rb', line 139 def gen_url url, text scheme, url, = parse_url url "[#{text.sub(%r{^#{scheme}:/*}i, '')}](#{url})" end |
#handle_rdoc_link(url) ⇒ Object
Handles rdoc-
type links for footnotes.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/rdoc/markup/to_markdown.rb', line 148 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_special_HARD_BREAK(special) ⇒ Object
Adds a newline to the output
39 40 41 |
# File 'lib/rdoc/markup/to_markdown.rb', line 39 def handle_special_HARD_BREAK special " \n" end |
#handle_special_RDOCLINK(special) ⇒ Object
Converts the rdoc-…: links into a Markdown.style links.
186 187 188 |
# File 'lib/rdoc/markup/to_markdown.rb', line 186 def handle_special_RDOCLINK special handle_rdoc_link special.text end |
#handle_special_TIDYLINK(special) ⇒ Object
Converts the RDoc markup tidylink into a Markdown.style link.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/rdoc/markup/to_markdown.rb', line 168 def handle_special_TIDYLINK special text = special.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_tags ⇒ Object
Maps attributes to HTML sequences
30 31 32 33 34 |
# File 'lib/rdoc/markup/to_markdown.rb', line 30 def add_tag :BOLD, '**', '**' add_tag :EM, '*', '*' add_tag :TT, '`', '`' end |