Class: RDoc::Markup::ToRdoc

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

Overview

Outputs RDoc markup as RDoc markup! (mostly)

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Formatter

#add_tag, #annotate, #convert, #convert_flow, #convert_special, #convert_string, #in_tt?, #off_tags, #on_tags, #tt?

Constructor Details

- (ToRdoc) initialize

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



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

def initialize
  super

  @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] = ['====== ', '']
end

Instance Attribute Details

- (Object) indent

Current indent amount for output in characters



12
13
14
# File 'lib/rdoc/markup/to_rdoc.rb', line 12

def indent
  @indent
end

- (Object) list_index (readonly)

Stack of current list indexes for alphabetic and numeric lists



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

def list_index
  @list_index
end

- (Object) list_type (readonly)

Stack of list types



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

def list_type
  @list_type
end

- (Object) list_width (readonly)

Stack of list widths for indentation



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

def list_width
  @list_width
end

- (Object) prefix (readonly)

Prefix for the next list item. See #use_prefix



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

def prefix
  @prefix
end

- (Object) res (readonly)

Output accumulator



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

def res
  @res
end

- (Object) width

Output width in characters



17
18
19
# File 'lib/rdoc/markup/to_rdoc.rb', line 17

def width
  @width
end

Instance Method Details

- (Object) accept_blank_line(blank_line)

Adds blank_line to the output



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

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

- (Object) accept_heading(heading)

Adds heading to the output



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

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

- (Object) accept_list_end(list)

Finishes consumption of list



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

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

- (Object) accept_list_item_end(list_item)

Finishes consumption of list_item



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rdoc/markup/to_rdoc.rb', line 104

def accept_list_item_end list_item
  width = case @list_type.last
          when :BULLET then
            2
          when :NOTE, :LABEL then
            @res << "\n"
            2
          else
            bullet = @list_index.last.to_s
            @list_index[-1] = @list_index.last.succ
            bullet.length + 2
          end

  @indent -= width
end

- (Object) accept_list_item_start(list_item)

Prepares the visitor for consuming list_item



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

def accept_list_item_start list_item
  type = @list_type.last

  case type
  when :NOTE, :LABEL then
    bullet = attributes(list_item.label) + ":\n"
    @prefix = ' ' * @indent
    @indent += 2
    @prefix << bullet + (' ' * @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

- (Object) accept_list_start(list)

Prepares the visitor for consuming list



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

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

- (Object) accept_paragraph(paragraph)

Adds paragraph to the output



170
171
172
# File 'lib/rdoc/markup/to_rdoc.rb', line 170

def accept_paragraph paragraph
  wrap attributes(paragraph.text)
end

- (Object) accept_raw(raw)

Adds raw to the output



177
178
179
# File 'lib/rdoc/markup/to_rdoc.rb', line 177

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

- (Object) accept_rule(rule)

Adds rule to the output



184
185
186
187
188
# File 'lib/rdoc/markup/to_rdoc.rb', line 184

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

- (Object) accept_verbatim(verbatim)

Outputs verbatim indented 2 columns



193
194
195
196
197
198
199
200
201
202
# File 'lib/rdoc/markup/to_rdoc.rb', line 193

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

- (Object) attributes(text)

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



207
208
209
210
# File 'lib/rdoc/markup/to_rdoc.rb', line 207

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

- (Object) end_accepting

Returns the generated output



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

def end_accepting
  @res.join
end

- (Object) handle_special_SUPPRESSED_CROSSREF(special)

Removes preceding \ from the suppressed crossref special



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

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

- (Object) init_tags

Maps attributes to HTML sequences



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

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

- (Object) start_accepting

Prepares the visitor for text generation



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

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

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

- (Object) use_prefix

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



245
246
247
248
249
250
251
# File 'lib/rdoc/markup/to_rdoc.rb', line 245

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

  prefix
end

- (Object) wrap(text)

Wraps text to #width



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/rdoc/markup/to_rdoc.rb', line 256

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