Class: RDoc::Markup::ToMarkdown

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

Overview

Outputs parsed markup as Markdown

Constant Summary

Constants inherited from ToRdoc

RDoc::Markup::ToRdoc::DEFAULT_HEADINGS

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, #add_text, #attributes, #calculate_text_width, #emit_inline, #end_accepting, #handle_PLAIN_TEXT, #handle_REGEXP_HANDLING_TEXT, #handle_inline, #handle_regexp_SUPPRESSED_CROSSREF, #off, #on, #start_accepting, #use_prefix, #wrap

Methods inherited from Formatter

#accept_document, #add_regexp_handling_RDOCLINK, #annotate, #apply_regexp_handling, #convert, #convert_string, gen_relative_url, #handle_PLAIN_TEXT, #handle_REGEXP_HANDLING_TEXT, #handle_TEXT, #handle_inline, #ignore, #parse_url, #traverse_inline_nodes, #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
# 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

  @hard_break = "  \n"
end

Instance Method Details

#accept_list_end(list) ⇒ Object

Finishes consumption of list



30
31
32
# File 'lib/rdoc/markup/to_markdown.rb', line 30

def accept_list_end(list)
  super
end

#accept_list_item_end(list_item) ⇒ Object

Finishes consumption of list_item



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rdoc/markup/to_markdown.rb', line 37

def accept_list_item_end(list_item)
  width = case @list_type.last
          when :BULLET then
            4
          when :NOTE, :LABEL then
            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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rdoc/markup/to_markdown.rb', line 58

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" 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



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/rdoc/markup/to_markdown.rb', line 142

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



159
160
161
162
163
# File 'lib/rdoc/markup/to_markdown.rb', line 159

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

#accept_verbatim(verbatim) ⇒ Object

Outputs verbatim indented 4 columns



168
169
170
171
172
173
174
175
176
177
# File 'lib/rdoc/markup/to_markdown.rb', line 168

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



80
81
82
83
84
85
86
# File 'lib/rdoc/markup/to_markdown.rb', line 80

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.



182
183
184
185
186
# File 'lib/rdoc/markup/to_markdown.rb', line 182

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

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

#handle_BOLD(nodes) ⇒ Object



111
112
113
# File 'lib/rdoc/markup/to_markdown.rb', line 111

def handle_BOLD(nodes)
  handle_tag(nodes, '**', 'strong')
end

#handle_BOLD_WORD(word) ⇒ Object



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

def handle_BOLD_WORD(word)
  add_tag('strong', '**', convert_string(word))
end

#handle_EM(nodes) ⇒ Object



115
116
117
# File 'lib/rdoc/markup/to_markdown.rb', line 115

def handle_EM(nodes)
  handle_tag(nodes, '*', 'em')
end

#handle_EM_WORD(word) ⇒ Object



123
124
125
# File 'lib/rdoc/markup/to_markdown.rb', line 123

def handle_EM_WORD(word)
  add_tag('em', '*', convert_string(word))
end

#handle_HARD_BREAKObject



135
136
137
# File 'lib/rdoc/markup/to_markdown.rb', line 135

def handle_HARD_BREAK
  emit_inline("  \n")
end

Handles rdoc- type links for footnotes.



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/rdoc/markup/to_markdown.rb', line 191

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

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



211
212
213
# File 'lib/rdoc/markup/to_markdown.rb', line 211

def handle_regexp_RDOCLINK(text)
  handle_rdoc_link text
end

#handle_STRIKE(nodes) ⇒ Object



131
132
133
# File 'lib/rdoc/markup/to_markdown.rb', line 131

def handle_STRIKE(nodes)
  handle_tag(nodes, '~~', 's')
end

#handle_tag(nodes, simple_tag, tag) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rdoc/markup/to_markdown.rb', line 88

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


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

def handle_TIDYLINK(label_part, url)
  if url =~ /^rdoc-label:foot/ then
    emit_inline(handle_rdoc_link(url))
  else
    emit_inline('[')
    traverse_inline_nodes(label_part)
    emit_inline("](#{url})")
  end
end

#handle_TT(text) ⇒ Object



127
128
129
# File 'lib/rdoc/markup/to_markdown.rb', line 127

def handle_TT(text)
  add_tag('code', '`', convert_string(text))
end