Outputs RDoc markup as HTML
Maps RDoc::Markup::Parser::LIST_TOKENS types to HTML tags
Converts a target url to one that is relative to a given path
# File lib/rdoc/markup/to_html.rb, line 30
30: def self.gen_relative_url(path, target)
31: from = File.dirname path
32: to, to_file = File.split target
33:
34: from = from.split "/"
35: to = to.split "/"
36:
37: from.delete '.'
38: to.delete '.'
39:
40: while from.size > 0 and to.size > 0 and from[0] == to[0] do
41: from.shift
42: to.shift
43: end
44:
45: from.fill ".."
46: from.concat to
47: from << to_file
48: File.join(*from)
49: end
# File lib/rdoc/markup/to_html.rb, line 51
51: def initialize
52: super
53:
54: @th = nil
55: @in_list_entry = nil
56: @list = nil
57:
58: # external hyperlinks
59: @markup.add_special(/((link:|https?:|mailto:|ftp:|www\.)\S+\w)/, :HYPERLINK)
60:
61: # and links of the form <text>[<url>]
62: @markup.add_special(/(((\{.*?\})|\b\S+?)\[\S+?\.\S+?\])/, :TIDYLINK)
63:
64: init_tags
65: end
# File lib/rdoc/markup/to_html.rb, line 222
222: def accept_blank_line(blank_line)
223: # @res << annotate("<p />") << "\n"
224: end
# File lib/rdoc/markup/to_html.rb, line 226
226: def accept_heading(heading)
227: @res << convert_heading(heading.level, @am.flow(heading.text))
228: end
# File lib/rdoc/markup/to_html.rb, line 202
202: def accept_list_end(list)
203: @list.pop
204: if tag = @in_list_entry.pop
205: @res << annotate(tag) << "\n"
206: end
207: @res << html_list_name(list.type, false) << "\n"
208: end
# File lib/rdoc/markup/to_html.rb, line 218
218: def accept_list_item_end(list_item)
219: @in_list_entry[1] = list_end_for(@list.last)
220: end
# File lib/rdoc/markup/to_html.rb, line 210
210: def accept_list_item_start(list_item)
211: if tag = @in_list_entry.last
212: @res << annotate(tag) << "\n"
213: end
214:
215: @res << list_item_start(list_item, @list.last)
216: end
# File lib/rdoc/markup/to_html.rb, line 196
196: def accept_list_start(list)
197: @list << list.type
198: @res << html_list_name(list.type, true) << "\n"
199: @in_list_entry.push false
200: end
# File lib/rdoc/markup/to_html.rb, line 178
178: def accept_paragraph(paragraph)
179: @res << annotate("<p>") + "\n"
180: @res << wrap(convert_flow(@am.flow(paragraph.text)))
181: @res << annotate("</p>") + "\n"
182: end
# File lib/rdoc/markup/to_html.rb, line 230
230: def accept_raw raw
231: @res << raw.parts.join("\n")
232: end
# File lib/rdoc/markup/to_html.rb, line 190
190: def accept_rule(rule)
191: size = rule.weight
192: size = 10 if size > 10
193: @res << "<hr style=\"height: #{size}px\"></hr>"
194: end
# File lib/rdoc/markup/to_html.rb, line 184
184: def accept_verbatim(verbatim)
185: @res << annotate("<pre>") << "\n"
186: @res << CGI.escapeHTML(verbatim.text)
187: @res << annotate("</pre>") << "\n"
188: end
# File lib/rdoc/markup/to_html.rb, line 174
174: def end_accepting
175: @res.join
176: end
Generate a hyperlink for url, labeled with text. Handle the special cases for img: and link: described under handle_special_HYPERLINK
# File lib/rdoc/markup/to_html.rb, line 80
80: def gen_url(url, text)
81: if url =~ /([A-Za-z]+):(.*)/ then
82: type = $1
83: path = $2
84: else
85: type = "http"
86: path = url
87: url = "http://#{url}"
88: end
89:
90: if type == "link" then
91: url = if path[0, 1] == '#' then # is this meaningful?
92: path
93: else
94: self.class.gen_relative_url @from_path, path
95: end
96: end
97:
98: if (type == "http" or type == "link") and
99: url =~ /\.(gif|png|jpg|jpeg|bmp)$/ then
100: "<img src=\"#{url}\" />"
101: else
102: "<a href=\"#{url}\">#{text.sub(%r{^#{type}:/*}, '')}</a>"
103: end
104: end
And we’re invoked with a potential external hyperlink mailto: just
gets inserted. http: links are checked to see if they reference an image.
If so, that image gets inserted using an tag. Otherwise a
conventional is used. We also support a special type of
hyperlink, link:, which is a reference to a local file whose path is
relative to the —op directory.
# File lib/rdoc/markup/to_html.rb, line 114
114: def handle_special_HYPERLINK(special)
115: url = special.text
116: gen_url url, url
117: end
Here’s a hypedlink where the label is different to the URL
<label>[url] or {long label}[url]
# File lib/rdoc/markup/to_html.rb, line 123
123: def handle_special_TIDYLINK(special)
124: text = special.text
125:
126: return text unless text =~ /\{(.*?)\}\[(.*?)\]/ or text =~ /(\S+)\[(.*?)\]/
127:
128: label = $1
129: url = $2
130: gen_url url, label
131: end
# File lib/rdoc/markup/to_html.rb, line 168
168: def start_accepting
169: @res = []
170: @in_list_entry = []
171: @list = []
172: end
This is a higher speed (if messier) version of wrap
# File lib/rdoc/markup/to_html.rb, line 136
136: def wrap(txt, line_len = 76)
137: res = []
138: sp = 0
139: ep = txt.length
140:
141: while sp < ep
142: # scan back for a space
143: p = sp + line_len - 1
144: if p >= ep
145: p = ep
146: else
147: while p > sp and txt[p] != \s\
148: p -= 1
149: end
150: if p <= sp
151: p = sp + line_len
152: while p < ep and txt[p] != \s\
153: p += 1
154: end
155: end
156: end
157: res << txt[sp...p] << "\n"
158: sp = p
159: sp += 1 while sp < ep and txt[sp] == \s\
160: end
161:
162: res.join
163: end
Converts headings to hN elements
# File lib/rdoc/markup/to_html.rb, line 287
287: def convert_heading(level, flow)
288: [annotate("<h#{level}>"),
289: convert_flow(flow),
290: annotate("</h#{level}>\n")].join
291: end
Converts string item
# File lib/rdoc/markup/to_html.rb, line 239
239: def convert_string(item)
240: in_tt? ? convert_string_simple(item) : convert_string_fancy(item)
241: end
Converts ampersand, dashes, elipsis, quotes, copyright and registered trademark symbols to HTML escaped Unicode.
# File lib/rdoc/markup/to_html.rb, line 254
254: def convert_string_fancy(item)
255: # convert ampersand before doing anything else
256: item.gsub(/&/, '&').
257:
258: # convert -- to em-dash, (-- to en-dash)
259: gsub(/---?/, '—'). #gsub(/--/, '–').
260:
261: # convert ... to elipsis (and make sure .... becomes .<elipsis>)
262: gsub(/\.\.\.\./, '.…').gsub(/\.\.\./, '…').
263:
264: # convert single closing quote
265: gsub(%{([^ \t\r\n\[\{\(])\'}, '\1’'). # }
266: gsub(%{\'(?=\W|s\b)}, '’').
267:
268: # convert single opening quote
269: gsub(/'/, '‘').
270:
271: # convert double closing quote
272: gsub(%{([^ \t\r\n\[\{\(])\"(?=\W)}, '\1”'). # }
273:
274: # convert double opening quote
275: gsub(/"/, '“').
276:
277: # convert copyright
278: gsub(/\(c\)/, '©').
279:
280: # convert registered trademark
281: gsub(/\(r\)/, '®')
282: end
Escapes HTML in item
# File lib/rdoc/markup/to_html.rb, line 246
246: def convert_string_simple(item)
247: CGI.escapeHTML item
248: end
Determins the HTML list element for list_type and open_tag
# File lib/rdoc/markup/to_html.rb, line 296
296: def html_list_name(list_type, open_tag)
297: tags = LIST_TYPE_TO_HTML[list_type]
298: raise RDoc::Error, "Invalid list type: #{list_type.inspect}" unless tags
299: annotate tags[open_tag ? 0 : 1]
300: end
Ends a list item
# File lib/rdoc/markup/to_html.rb, line 330
330: def list_end_for(list_type)
331: case list_type
332: when :BULLET, :LALPHA, :NUMBER, :UALPHA then
333: "</li>"
334: when :LABEL then
335: "</dd>"
336: when :NOTE then
337: "</td></tr>"
338: else
339: raise RDoc::Error, "Invalid list type: #{list_type.inspect}"
340: end
341: end
Starts a list item
# File lib/rdoc/markup/to_html.rb, line 305
305: def list_item_start(list_item, list_type)
306: case list_type
307: when :BULLET, :LALPHA, :NUMBER, :UALPHA then
308: annotate("<li>")
309:
310: when :LABEL then
311: annotate("<dt>") +
312: convert_flow(@am.flow(list_item.label)) +
313: annotate("</dt>") +
314: annotate("<dd>")
315:
316: when :NOTE then
317: annotate("<tr>") +
318: annotate("<td valign=\"top\">") +
319: convert_flow(@am.flow(list_item.label)) +
320: annotate("</td>") +
321: annotate("<td>")
322: else
323: raise RDoc::Error, "Invalid list type: #{list_type.inspect}"
324: end
325: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.