RDoc::Markup::ToRdoc

Outputs RDoc markup as RDoc markup! (mostly)

Attributes

indent[RW]
list_index[R]
list_type[R]
list_width[R]
prefix[R]
res[R]

Public Class Methods

new() click to toggle source
    # File lib/rdoc/markup/to_rdoc.rb, line 15
15:   def initialize
16:     super
17: 
18:     @markup.add_special(/\\[^\s]/, :SUPPRESSED_CROSSREF)
19: 
20:     @width = 78
21:     @prefix = ''
22: 
23:     init_tags
24: 
25:     @headings = {}
26:     @headings.default = []
27: 
28:     @headings[1] = ['= ',      '']
29:     @headings[2] = ['== ',     '']
30:     @headings[3] = ['=== ',    '']
31:     @headings[4] = ['==== ',   '']
32:     @headings[5] = ['===== ',  '']
33:     @headings[6] = ['====== ', '']
34:   end

Public Instance Methods

accept_blank_line(blank_line) click to toggle source
    # File lib/rdoc/markup/to_rdoc.rb, line 45
45:   def accept_blank_line blank_line
46:     @res << "\n"
47:   end
accept_heading(heading) click to toggle source
    # File lib/rdoc/markup/to_rdoc.rb, line 49
49:   def accept_heading heading
50:     use_prefix or @res << ' ' * @indent
51:     @res << @headings[heading.level][0]
52:     @res << attributes(heading.text)
53:     @res << @headings[heading.level][1]
54:     @res << "\n"
55:   end
accept_list_end(list) click to toggle source
    # File lib/rdoc/markup/to_rdoc.rb, line 57
57:   def accept_list_end list
58:     @list_index.pop
59:     @list_type.pop
60:     @list_width.pop
61:   end
accept_list_item_end(list_item) click to toggle source
    # File lib/rdoc/markup/to_rdoc.rb, line 63
63:   def accept_list_item_end list_item
64:     width = case @list_type.last
65:             when :BULLET then
66:               2
67:             when :NOTE, :LABEL then
68:               @res << "\n"
69:               2
70:             else
71:               bullet = @list_index.last.to_s
72:               @list_index[1] = @list_index.last.succ
73:               bullet.length + 2
74:             end
75: 
76:     @indent -= width
77:   end
accept_list_item_start(list_item) click to toggle source
     # File lib/rdoc/markup/to_rdoc.rb, line 79
 79:   def accept_list_item_start list_item
 80:     bullet = case @list_type.last
 81:              when :BULLET then
 82:                '*'
 83:              when :NOTE, :LABEL then
 84:                attributes(list_item.label) + ":\n"
 85:              else
 86:                @list_index.last.to_s + '.'
 87:              end
 88: 
 89:     case @list_type.last
 90:     when :NOTE, :LABEL then
 91:       @indent += 2
 92:       @prefix = bullet + (' ' * @indent)
 93:     else
 94:       @prefix = (' ' * @indent) + bullet.ljust(bullet.length + 1)
 95: 
 96:       width = bullet.length + 1
 97: 
 98:       @indent += width
 99:     end
100:   end
accept_list_start(list) click to toggle source
     # File lib/rdoc/markup/to_rdoc.rb, line 102
102:   def accept_list_start list
103:     case list.type
104:     when :BULLET then
105:       @list_index << nil
106:       @list_width << 1
107:     when :LABEL, :NOTE then
108:       @list_index << nil
109:       @list_width << 2
110:     when :LALPHA then
111:       @list_index << 'a'
112:       @list_width << list.items.length.to_s.length
113:     when :NUMBER then
114:       @list_index << 1
115:       @list_width << list.items.length.to_s.length
116:     when :UALPHA then
117:       @list_index << 'A'
118:       @list_width << list.items.length.to_s.length
119:     else
120:       raise RDoc::Error, "invalid list type #{list.type}"
121:     end
122: 
123:     @list_type << list.type
124:   end
accept_paragraph(paragraph) click to toggle source
     # File lib/rdoc/markup/to_rdoc.rb, line 126
126:   def accept_paragraph paragraph
127:     wrap attributes(paragraph.text)
128:   end
accept_raw(raw) click to toggle source
     # File lib/rdoc/markup/to_rdoc.rb, line 130
130:   def accept_raw raw
131:     @res << raw.parts.join("\n")
132:   end
accept_rule(rule) click to toggle source
     # File lib/rdoc/markup/to_rdoc.rb, line 134
134:   def accept_rule rule
135:     use_prefix or @res << ' ' * @indent
136:     @res << '-' * (@width - @indent)
137:     @res << "\n"
138:   end
accept_verbatim(verbatim) click to toggle source

Outputs verbatim flush left and indented 2 columns

     # File lib/rdoc/markup/to_rdoc.rb, line 143
143:   def accept_verbatim verbatim
144:     indent = ' ' * (@indent + 2)
145: 
146:     lines = []
147:     current_line = []
148: 
149:     # split into lines
150:     verbatim.parts.each do |part|
151:       current_line << part
152: 
153:       if part == "\n" then
154:         lines << current_line
155:         current_line = []
156:       end
157:     end
158: 
159:     lines << current_line unless current_line.empty?
160: 
161:     # calculate margin
162:     indented = lines.select { |line| line != ["\n"] }
163:     margin = indented.map { |line| line.first.length }.min
164: 
165:     # flush left
166:     indented.each { |line| line[0][0...margin] = '' }
167: 
168:     # output
169:     use_prefix or @res << indent # verbatim is unlikely to have prefix
170:     @res << lines.shift.join
171: 
172:     lines.each do |line|
173:       @res << indent unless line == ["\n"]
174:       @res << line.join
175:     end
176: 
177:     @res << "\n"
178:   end
attributes(text) click to toggle source
     # File lib/rdoc/markup/to_rdoc.rb, line 180
180:   def attributes text
181:     flow = @am.flow text.dup
182:     convert_flow flow
183:   end
end_accepting() click to toggle source
     # File lib/rdoc/markup/to_rdoc.rb, line 185
185:   def end_accepting
186:     @res.join
187:   end
handle_special_SUPPRESSED_CROSSREF(special) click to toggle source
     # File lib/rdoc/markup/to_rdoc.rb, line 189
189:   def handle_special_SUPPRESSED_CROSSREF special
190:     special.text.sub(/\\/, '')
191:   end
init_tags() click to toggle source

Maps attributes to ANSI sequences

    # File lib/rdoc/markup/to_rdoc.rb, line 39
39:   def init_tags
40:     add_tag :BOLD, "<b>", "</b>"
41:     add_tag :TT,   "<tt>", "</tt>"
42:     add_tag :EM,   "<em>", "</em>"
43:   end
start_accepting() click to toggle source
     # File lib/rdoc/markup/to_rdoc.rb, line 193
193:   def start_accepting
194:     @res = [""]
195:     @indent = 0
196:     @prefix = nil
197: 
198:     @list_index = []
199:     @list_type  = []
200:     @list_width = []
201:   end
use_prefix() click to toggle source
     # File lib/rdoc/markup/to_rdoc.rb, line 203
203:   def use_prefix
204:     prefix = @prefix
205:     @prefix = nil
206:     @res << prefix if prefix
207: 
208:     prefix
209:   end
wrap(text) click to toggle source
     # File lib/rdoc/markup/to_rdoc.rb, line 211
211:   def wrap text
212:     return unless text && !text.empty?
213: 
214:     text_len = @width - @indent
215: 
216:     text_len = 20 if text_len < 20
217: 
218:     re = /^(.{0,#{text_len}})[ \n]/
219:     next_prefix = ' ' * @indent
220: 
221:     prefix = @prefix || next_prefix
222:     @prefix = nil
223: 
224:     @res << prefix
225: 
226:     while text.length > text_len
227:       if text =~ re then
228:         @res << $1
229:         text.slice!(0, $&.length)
230:       else
231:         @res << text.slice!(0, text_len)
232:       end
233: 
234:       @res << "\n" << next_prefix
235:     end
236: 
237:     if text.empty? then
238:       @res.pop
239:       @res.pop
240:     else
241:       @res << text
242:       @res << "\n"
243:     end
244:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.