Class: RDoc::Markup::ToRdoc

Inherits:
Formatter show all
Defined in:
lib/rdoc/markup/to_rdoc.rb

Overview

Outputs RDoc markup as RDoc markup! (mostly)

Direct Known Subclasses

ToAnsi, ToBs, ToMarkdown

Instance Attribute Summary collapse

Instance Method Summary collapse

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) ⇒ ToRdoc

Creates a new formatter that will output (mostly) RDoc markup


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rdoc/markup/to_rdoc.rb', line 44

def initialize markup = nil
  super nil, markup

  @markup.add_special(/\\\S/, :SUPPRESSED_CROSSREF)
  @width = 78
  init_tags

  @headings = {}
  @headings.default = []

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

  @hard_break = "\n"
end

Instance Attribute Details

#indentObject

Current indent amount for output in characters


9
10
11
# File 'lib/rdoc/markup/to_rdoc.rb', line 9

def indent
  @indent
end

#list_indexObject (readonly)

Stack of current list indexes for alphabetic and numeric lists


19
20
21
# File 'lib/rdoc/markup/to_rdoc.rb', line 19

def list_index
  @list_index
end

#list_typeObject (readonly)

Stack of list types


24
25
26
# File 'lib/rdoc/markup/to_rdoc.rb', line 24

def list_type
  @list_type
end

#list_widthObject (readonly)

Stack of list widths for indentation


29
30
31
# File 'lib/rdoc/markup/to_rdoc.rb', line 29

def list_width
  @list_width
end

#prefixObject (readonly)

Prefix for the next list item. See #use_prefix


34
35
36
# File 'lib/rdoc/markup/to_rdoc.rb', line 34

def prefix
  @prefix
end

#resObject (readonly)

Output accumulator


39
40
41
# File 'lib/rdoc/markup/to_rdoc.rb', line 39

def res
  @res
end

#widthObject

Output width in characters


14
15
16
# File 'lib/rdoc/markup/to_rdoc.rb', line 14

def width
  @width
end

Instance Method Details

#accept_blank_line(blank_line) ⇒ Object

Adds blank_line to the output


76
77
78
# File 'lib/rdoc/markup/to_rdoc.rb', line 76

def accept_blank_line blank_line
  @res << "\n"
end

#accept_block_quote(block_quote) ⇒ Object

Adds paragraph to the output


83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rdoc/markup/to_rdoc.rb', line 83

def accept_block_quote block_quote
  @indent += 2

  block_quote.parts.each do |part|
    @prefix = '> '

    part.accept self
  end

  @indent -= 2
end

#accept_heading(heading) ⇒ Object

Adds heading to the output


98
99
100
101
102
103
104
# File 'lib/rdoc/markup/to_rdoc.rb', line 98

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

#accept_indented_paragraph(paragraph) ⇒ Object

Adds paragraph to the output


202
203
204
205
206
207
# File 'lib/rdoc/markup/to_rdoc.rb', line 202

def accept_indented_paragraph paragraph
  @indent += paragraph.indent
  text = paragraph.text @hard_break
  wrap attributes text
  @indent -= paragraph.indent
end

#accept_list_end(list) ⇒ Object

Finishes consumption of list


109
110
111
112
113
# File 'lib/rdoc/markup/to_rdoc.rb', line 109

def accept_list_end list
  @list_index.pop
  @list_type.pop
  @list_width.pop
end

#accept_list_item_end(list_item) ⇒ Object

Finishes consumption of list_item


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rdoc/markup/to_rdoc.rb', line 118

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

#accept_list_item_start(list_item) ⇒ Object

Prepares the visitor for consuming list_item


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rdoc/markup/to_rdoc.rb', line 142

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 += 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

#accept_list_start(list) ⇒ Object

Prepares the visitor for consuming list


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/rdoc/markup/to_rdoc.rb', line 167

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

#accept_paragraph(paragraph) ⇒ Object

Adds paragraph to the output


194
195
196
197
# File 'lib/rdoc/markup/to_rdoc.rb', line 194

def accept_paragraph paragraph
  text = paragraph.text @hard_break
  wrap attributes text
end

#accept_raw(raw) ⇒ Object

Adds raw to the output


212
213
214
# File 'lib/rdoc/markup/to_rdoc.rb', line 212

def accept_raw raw
  @res << raw.parts.join("\n")
end

#accept_rule(rule) ⇒ Object

Adds rule to the output


219
220
221
222
223
# File 'lib/rdoc/markup/to_rdoc.rb', line 219

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

#accept_verbatim(verbatim) ⇒ Object

Outputs verbatim indented 2 columns


228
229
230
231
232
233
234
235
236
237
# File 'lib/rdoc/markup/to_rdoc.rb', line 228

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

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

  @res << "\n" unless @res =~ /\n\z/
end

#attributes(text) ⇒ Object

Applies attribute-specific markup to text using RDoc::AttributeManager


242
243
244
245
# File 'lib/rdoc/markup/to_rdoc.rb', line 242

def attributes text
  flow = @am.flow text.dup
  convert_flow flow
end

#end_acceptingObject

Returns the generated output


250
251
252
# File 'lib/rdoc/markup/to_rdoc.rb', line 250

def end_accepting
  @res.join
end

#handle_special_HARD_BREAK(special) ⇒ Object

Adds a newline to the output


266
267
268
# File 'lib/rdoc/markup/to_rdoc.rb', line 266

def handle_special_HARD_BREAK special
  "\n"
end

#handle_special_SUPPRESSED_CROSSREF(special) ⇒ Object

Removes preceding \ from the suppressed crossref special


257
258
259
260
261
# File 'lib/rdoc/markup/to_rdoc.rb', line 257

def handle_special_SUPPRESSED_CROSSREF special
  text = special.text
  text = text.sub('\\', '') unless in_tt?
  text
end

#init_tagsObject

Maps attributes to HTML sequences


67
68
69
70
71
# File 'lib/rdoc/markup/to_rdoc.rb', line 67

def init_tags
  add_tag :BOLD, "<b>", "</b>"
  add_tag :TT,   "<tt>", "</tt>"
  add_tag :EM,   "<em>", "</em>"
end

#start_acceptingObject

Prepares the visitor for text generation


273
274
275
276
277
278
279
280
281
# File 'lib/rdoc/markup/to_rdoc.rb', line 273

def start_accepting
  @res = [""]
  @indent = 0
  @prefix = nil

  @list_index = []
  @list_type  = []
  @list_width = []
end

#use_prefixObject

Adds the stored #prefix to the output and clears it. Lists generate a prefix for later consumption.


287
288
289
290
291
292
# File 'lib/rdoc/markup/to_rdoc.rb', line 287

def use_prefix
  prefix, @prefix = @prefix, nil
  @res << prefix if prefix

  prefix
end

#wrap(text) ⇒ Object

Wraps text to #width


297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/rdoc/markup/to_rdoc.rb', line 297

def wrap text
  return unless text && !text.empty?

  text_len = @width - @indent

  text_len = 20 if text_len < 20

  re = /^(.{0,#{text_len}})[ \n]/
  next_prefix = ' ' * @indent

  prefix = @prefix || next_prefix
  @prefix = nil

  @res << prefix

  while text.length > text_len
    if text =~ re then
      @res << $1
      text.slice!(0, $&.length)
    else
      @res << text.slice!(0, text_len)
    end

    @res << "\n" << next_prefix
  end

  if text.empty? then
    @res.pop
    @res.pop
  else
    @res << text
    @res << "\n"
  end
end