Class: RDoc::Markup::ToRdoc

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

Overview

Outputs RDoc markup as RDoc markup! (mostly)

Constant Summary collapse

DEFAULT_HEADINGS =
{
    1 => ['= ',      ''],
    2 => ['== ',     ''],
    3 => ['=== ',    ''],
    4 => ['==== ',   ''],
    5 => ['===== ',  ''],
    6 => ['====== ', '']
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ ToRdoc

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



57
58
59
60
61
62
63
64
65
# File 'lib/rdoc/markup/to_rdoc.rb', line 57

def initialize
  super

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

  @headings = DEFAULT_HEADINGS.dup
  @hard_break = "\n"
end

Instance Attribute Details

#indent ⇒ Object

Current indent amount for output in characters



22
23
24
# File 'lib/rdoc/markup/to_rdoc.rb', line 22

def indent
  @indent
end

#list_index ⇒ Object (readonly)

Stack of current list indexes for alphabetic and numeric lists



32
33
34
# File 'lib/rdoc/markup/to_rdoc.rb', line 32

def list_index
  @list_index
end

#list_type ⇒ Object (readonly)

Stack of list types



37
38
39
# File 'lib/rdoc/markup/to_rdoc.rb', line 37

def list_type
  @list_type
end

#list_width ⇒ Object (readonly)

Stack of list widths for indentation



42
43
44
# File 'lib/rdoc/markup/to_rdoc.rb', line 42

def list_width
  @list_width
end

#prefix ⇒ Object (readonly)

Prefix for the next list item. See #use_prefix



47
48
49
# File 'lib/rdoc/markup/to_rdoc.rb', line 47

def prefix
  @prefix
end

#res ⇒ Object (readonly)

Output accumulator



52
53
54
# File 'lib/rdoc/markup/to_rdoc.rb', line 52

def res
  @res
end

#width ⇒ Object

Output width in characters



27
28
29
# File 'lib/rdoc/markup/to_rdoc.rb', line 27

def width
  @width
end

Instance Method Details

#accept_blank_line(blank_line) ⇒ Object

Adds blank_line to the output



70
71
72
# File 'lib/rdoc/markup/to_rdoc.rb', line 70

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

#accept_block_quote(block_quote) ⇒ Object

Adds paragraph to the output



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rdoc/markup/to_rdoc.rb', line 77

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



92
93
94
95
96
97
98
# File 'lib/rdoc/markup/to_rdoc.rb', line 92

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



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

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



103
104
105
106
107
# File 'lib/rdoc/markup/to_rdoc.rb', line 103

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rdoc/markup/to_rdoc.rb', line 112

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



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

def accept_list_item_start(list_item)
  type = @list_type.last

  case type
  when :NOTE, :LABEL
    stripped_labels = Array(list_item.label).map do |label|
      attributes(label).strip
    end

    bullets = case type
    when :NOTE
      stripped_labels.map { |b| "#{b}::" }
    when :LABEL
      stripped_labels.map { |b| "[#{b}]" }
    end

    bullets = bullets.join("\n")
    bullets << "\n" unless stripped_labels.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



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

def accept_list_start(list)
  case list.type
  when :BULLET
    @list_index << nil
    @list_width << 1
  when :LABEL, :NOTE
    @list_index << nil
    @list_width << 2
  when :LALPHA
    @list_index << 'a'
    @list_width << list.items.length.to_s.length
  when :NUMBER
    @list_index << 1
    @list_width << list.items.length.to_s.length
  when :UALPHA
    @list_index << 'A'
    @list_width << list.items.length.to_s.length
  else
    raise Error, "invalid list type #{list.type}"
  end

  @list_type << list.type
end

#accept_paragraph(paragraph) ⇒ Object

Adds paragraph to the output



196
197
198
199
# File 'lib/rdoc/markup/to_rdoc.rb', line 196

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

#accept_raw(raw) ⇒ Object

Adds raw to the output



214
215
216
# File 'lib/rdoc/markup/to_rdoc.rb', line 214

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

#accept_rule(rule) ⇒ Object

Adds rule to the output



221
222
223
224
225
# File 'lib/rdoc/markup/to_rdoc.rb', line 221

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

#accept_table(header, body, aligns) ⇒ Object

Adds table to the output



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/rdoc/markup/to_rdoc.rb', line 244

def accept_table(header, body, aligns)
  header = header.map { |h| attributes h }
  body = body.map { |row| row.map { |t| attributes t } }
  widths = header.zip(*body).map do |cols|
    cols.compact.map { |col| calculate_text_width(col) }.max
  end
  aligns = aligns.map do |a|
    case a
    when nil, :center
      :center
    when :left
      :ljust
    when :right
      :rjust
    end
  end
  @res << header.zip(widths, aligns).map do |h, w, a|
    extra_width = h.size - calculate_text_width(h)
    h.__send__(a, w + extra_width)
  end.join("|").rstrip << "\n"
  @res << widths.map {|w| "-" * w }.join("|") << "\n"
  body.each do |row|
    @res << widths.zip(aligns).each_with_index.map do |(w, a), i|
      t = row[i] || ""
      extra_width = t.size - calculate_text_width(t)
      t.__send__(a, w + extra_width)
    end.join("|").rstrip << "\n"
  end
end

#accept_verbatim(verbatim) ⇒ Object

Outputs verbatim indented 2 columns



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

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

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

  @res << "\n"
end

#add_text(text) ⇒ Object



349
350
351
# File 'lib/rdoc/markup/to_rdoc.rb', line 349

def add_text(text)
  emit_inline(text)
end

#attributes(text) ⇒ Object

Applies attribute-specific markup to text using RDoc::Markup::InlineParser



360
361
362
# File 'lib/rdoc/markup/to_rdoc.rb', line 360

def attributes(text)
  handle_inline(text)
end

#calculate_text_width(text) ⇒ Object



274
275
276
# File 'lib/rdoc/markup/to_rdoc.rb', line 274

def calculate_text_width(text)
  text.size
end

#emit_inline(text) ⇒ Object



353
354
355
# File 'lib/rdoc/markup/to_rdoc.rb', line 353

def emit_inline(text)
  @inline_output << text
end

#end_accepting ⇒ Object

Returns the generated output



367
368
369
# File 'lib/rdoc/markup/to_rdoc.rb', line 367

def end_accepting
  @res.join
end

#handle_BOLD(target) ⇒ Object



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

def handle_BOLD(target)
  on(:BOLD)
  super
  off(:BOLD)
end

#handle_BOLD_WORD(word) ⇒ Object



298
299
300
301
302
# File 'lib/rdoc/markup/to_rdoc.rb', line 298

def handle_BOLD_WORD(word)
  on(:BOLD)
  super
  off(:BOLD)
end

#handle_EM(target) ⇒ Object



292
293
294
295
296
# File 'lib/rdoc/markup/to_rdoc.rb', line 292

def handle_EM(target)
  on(:EM)
  super
  off(:EM)
end

#handle_EM_WORD(word) ⇒ Object



304
305
306
307
308
# File 'lib/rdoc/markup/to_rdoc.rb', line 304

def handle_EM_WORD(word)
  on(:EM)
  super
  off(:EM)
end

#handle_HARD_BREAK ⇒ Object



322
323
324
# File 'lib/rdoc/markup/to_rdoc.rb', line 322

def handle_HARD_BREAK
  add_text("\n")
end

#handle_inline(text, initial_attributes = []) ⇒ Object



331
332
333
334
335
336
337
338
# File 'lib/rdoc/markup/to_rdoc.rb', line 331

def handle_inline(text, initial_attributes = [])
  @attributes = Hash.new(0)
  initial_attributes.each { |attr| on(attr) }
  out = @inline_output = +''
  super(text)
  @inline_output = nil
  out
end

#handle_PLAIN_TEXT(text) ⇒ Object



278
279
280
# File 'lib/rdoc/markup/to_rdoc.rb', line 278

def handle_PLAIN_TEXT(text)
  add_text(text)
end

#handle_REGEXP_HANDLING_TEXT(text) ⇒ Object



282
283
284
# File 'lib/rdoc/markup/to_rdoc.rb', line 282

def handle_REGEXP_HANDLING_TEXT(text)
  add_text(text)
end

#handle_regexp_SUPPRESSED_CROSSREF(text) ⇒ Object

Removes preceding \ from the suppressed crossref target



374
375
376
# File 'lib/rdoc/markup/to_rdoc.rb', line 374

def handle_regexp_SUPPRESSED_CROSSREF(text)
  text.sub('\\', '')
end

#handle_STRIKE(target) ⇒ Object



316
317
318
319
320
# File 'lib/rdoc/markup/to_rdoc.rb', line 316

def handle_STRIKE(target)
  on(:STRIKE)
  super
  off(:STRIKE)
end


326
327
328
329
# File 'lib/rdoc/markup/to_rdoc.rb', line 326

def handle_TIDYLINK(label_part, url)
  super
  add_text("( #{url} )")
end

#handle_TT(code) ⇒ Object



310
311
312
313
314
# File 'lib/rdoc/markup/to_rdoc.rb', line 310

def handle_TT(code)
  on(:TT)
  super
  off(:TT)
end

#off(attr) ⇒ Object



344
345
346
347
# File 'lib/rdoc/markup/to_rdoc.rb', line 344

def off(attr)
  @attributes[attr] -= 1
  @attributes.delete(attr) if @attributes[attr] == 0
end

#on(attr) ⇒ Object



340
341
342
# File 'lib/rdoc/markup/to_rdoc.rb', line 340

def on(attr)
  @attributes[attr] += 1
end

#start_accepting ⇒ Object

Prepares the visitor for text generation



381
382
383
384
385
386
387
388
389
# File 'lib/rdoc/markup/to_rdoc.rb', line 381

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

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

#use_prefix ⇒ Object

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



395
396
397
398
399
400
# File 'lib/rdoc/markup/to_rdoc.rb', line 395

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

  prefix
end

#wrap(text) ⇒ Object

Wraps text to #width



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/rdoc/markup/to_rdoc.rb', line 405

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

  text_len = @width - @indent

  text_len = 20 if text_len < 20

  next_prefix = ' ' * @indent

  prefix = @prefix || next_prefix
  @prefix = nil

  text.scan(/\G(?:([^ \n]{#{text_len}})(?=[^ \n])|(.{1,#{text_len}})(?:[ \n]|\z))/) do
    @res << prefix << ($1 || $2) << "\n"
    prefix = next_prefix
  end
end