2008-02-09 22:59:08 -05:00
|
|
|
require 'rdoc/markup/formatter'
|
2008-01-13 22:34:05 -05:00
|
|
|
require 'rdoc/markup/inline'
|
|
|
|
|
|
|
|
require 'cgi'
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Outputs RDoc markup as HTML
|
|
|
|
|
2008-02-09 22:59:08 -05:00
|
|
|
class RDoc::Markup::ToHtml < RDoc::Markup::Formatter
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
include RDoc::Text
|
|
|
|
|
2011-02-05 01:20:57 -05:00
|
|
|
# :section: Utilities
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Maps RDoc::Markup::Parser::LIST_TOKENS types to HTML tags
|
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
LIST_TYPE_TO_HTML = {
|
2010-04-01 03:45:16 -04:00
|
|
|
:BULLET => ['<ul>', '</ul>'],
|
2011-06-16 00:59:24 -04:00
|
|
|
:LABEL => ['<dl class="rdoc-list">', '</dl>'],
|
2010-04-01 03:45:16 -04:00
|
|
|
:LALPHA => ['<ol style="display: lower-alpha">', '</ol>'],
|
2010-12-19 22:22:49 -05:00
|
|
|
:NOTE => ['<table class="rdoc-list">', '</table>'],
|
2010-04-01 03:45:16 -04:00
|
|
|
:NUMBER => ['<ol>', '</ol>'],
|
|
|
|
:UALPHA => ['<ol style="display: upper-alpha">', '</ol>'],
|
2008-01-13 22:34:05 -05:00
|
|
|
}
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
attr_reader :res # :nodoc:
|
|
|
|
attr_reader :in_list_entry # :nodoc:
|
|
|
|
attr_reader :list # :nodoc:
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2010-12-28 17:08:56 -05:00
|
|
|
##
|
|
|
|
# Path to this document for relative links
|
|
|
|
|
|
|
|
attr_accessor :from_path
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
##
|
|
|
|
# Converts a target url to one that is relative to a given path
|
|
|
|
|
|
|
|
def self.gen_relative_url(path, target)
|
|
|
|
from = File.dirname path
|
|
|
|
to, to_file = File.split target
|
|
|
|
|
|
|
|
from = from.split "/"
|
|
|
|
to = to.split "/"
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
from.delete '.'
|
|
|
|
to.delete '.'
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
while from.size > 0 and to.size > 0 and from[0] == to[0] do
|
|
|
|
from.shift
|
|
|
|
to.shift
|
|
|
|
end
|
|
|
|
|
|
|
|
from.fill ".."
|
|
|
|
from.concat to
|
|
|
|
from << to_file
|
|
|
|
File.join(*from)
|
|
|
|
end
|
|
|
|
|
2011-02-05 01:20:57 -05:00
|
|
|
# :section:
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Creates a new formatter that will output HTML
|
|
|
|
|
2011-06-16 00:59:24 -04:00
|
|
|
def initialize markup = nil
|
2010-04-01 03:45:16 -04:00
|
|
|
super
|
|
|
|
|
|
|
|
@th = nil
|
|
|
|
@in_list_entry = nil
|
|
|
|
@list = nil
|
2010-12-28 17:08:56 -05:00
|
|
|
@from_path = ''
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
# external links
|
2010-04-01 03:45:16 -04:00
|
|
|
@markup.add_special(/((link:|https?:|mailto:|ftp:|www\.)\S+\w)/, :HYPERLINK)
|
|
|
|
|
|
|
|
# and links of the form <text>[<url>]
|
2011-08-11 14:31:49 -04:00
|
|
|
@markup.add_special(/(((\{.*?\})|\b\S+?)\[\S+?\])/, :TIDYLINK)
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
init_tags
|
|
|
|
end
|
|
|
|
|
2011-02-05 01:20:57 -05:00
|
|
|
# :section: Special Handling
|
|
|
|
#
|
|
|
|
# These methods handle special markup added by RDoc::Markup#add_special.
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2008-02-12 17:23:00 -05:00
|
|
|
##
|
2011-07-30 20:19:00 -04:00
|
|
|
# +special+ is a potential link. The following schemes are handled:
|
2011-02-05 01:20:57 -05:00
|
|
|
#
|
|
|
|
# <tt>mailto:</tt>::
|
|
|
|
# Inserted as-is.
|
|
|
|
# <tt>http:</tt>::
|
|
|
|
# Links are checked to see if they reference an image. If so, that image
|
|
|
|
# gets inserted using an <tt><img></tt> tag. Otherwise a conventional
|
|
|
|
# <tt><a href></tt> is used.
|
|
|
|
# <tt>link:</tt>::
|
|
|
|
# Reference to a local file relative to the output directory.
|
2008-02-12 17:23:00 -05:00
|
|
|
|
|
|
|
def handle_special_HYPERLINK(special)
|
|
|
|
url = special.text
|
2011-07-30 20:19:00 -04:00
|
|
|
|
2008-02-12 17:23:00 -05:00
|
|
|
gen_url url, url
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2011-07-30 20:19:00 -04:00
|
|
|
# This +special+ is a link where the label is different from the URL
|
|
|
|
# <tt>label[url]</tt> or <tt>{long label}[url]</tt>
|
2008-02-12 17:23:00 -05:00
|
|
|
|
|
|
|
def handle_special_TIDYLINK(special)
|
|
|
|
text = special.text
|
|
|
|
|
|
|
|
return text unless text =~ /\{(.*?)\}\[(.*?)\]/ or text =~ /(\S+)\[(.*?)\]/
|
|
|
|
|
|
|
|
label = $1
|
|
|
|
url = $2
|
|
|
|
gen_url url, label
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
# :section: Visitor
|
2011-02-05 01:20:57 -05:00
|
|
|
#
|
|
|
|
# These methods implement the HTML visitor.
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Prepares the visitor for HTML generation
|
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
def start_accepting
|
2010-04-01 03:45:16 -04:00
|
|
|
@res = []
|
2008-01-13 22:34:05 -05:00
|
|
|
@in_list_entry = []
|
2010-04-01 03:45:16 -04:00
|
|
|
@list = []
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Returns the generated output
|
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
def end_accepting
|
2010-04-01 03:45:16 -04:00
|
|
|
@res.join
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Adds +paragraph+ to the output
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def accept_paragraph(paragraph)
|
2010-12-19 22:22:49 -05:00
|
|
|
@res << "\n<p>"
|
|
|
|
@res << wrap(to_html(paragraph.text))
|
|
|
|
@res << "</p>\n"
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Adds +verbatim+ to the output
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def accept_verbatim(verbatim)
|
2010-12-19 22:22:49 -05:00
|
|
|
@res << "\n<pre>"
|
|
|
|
@res << CGI.escapeHTML(verbatim.text.rstrip)
|
|
|
|
@res << "</pre>\n"
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Adds +rule+ to the output
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def accept_rule(rule)
|
|
|
|
size = rule.weight
|
2008-01-13 22:34:05 -05:00
|
|
|
size = 10 if size > 10
|
2010-12-19 22:22:49 -05:00
|
|
|
@res << "<hr style=\"height: #{size}px\">\n"
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Prepares the visitor for consuming +list+
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def accept_list_start(list)
|
|
|
|
@list << list.type
|
2010-12-19 22:22:49 -05:00
|
|
|
@res << html_list_name(list.type, true)
|
2008-01-13 22:34:05 -05:00
|
|
|
@in_list_entry.push false
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Finishes consumption of +list+
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def accept_list_end(list)
|
|
|
|
@list.pop
|
2008-01-13 22:34:05 -05:00
|
|
|
if tag = @in_list_entry.pop
|
2010-12-19 22:22:49 -05:00
|
|
|
@res << tag
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
@res << html_list_name(list.type, false) << "\n"
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Prepares the visitor for consuming +list_item+
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def accept_list_item_start(list_item)
|
2008-01-13 22:34:05 -05:00
|
|
|
if tag = @in_list_entry.last
|
2010-12-19 22:22:49 -05:00
|
|
|
@res << tag
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
2008-02-09 22:59:08 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
@res << list_item_start(list_item, @list.last)
|
|
|
|
end
|
2008-02-09 22:59:08 -05:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Finishes consumption of +list_item+
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def accept_list_item_end(list_item)
|
|
|
|
@in_list_entry[-1] = list_end_for(@list.last)
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Adds +blank_line+ to the output
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
def accept_blank_line(blank_line)
|
|
|
|
# @res << annotate("<p />") << "\n"
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Adds +heading+ to the output
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
def accept_heading(heading)
|
|
|
|
@res << "\n<h#{heading.level}>"
|
|
|
|
@res << to_html(heading.text)
|
|
|
|
@res << "</h#{heading.level}>\n"
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Adds +raw+ to the output
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
def accept_raw raw
|
|
|
|
@res << raw.parts.join("\n")
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
|
2011-02-05 01:20:57 -05:00
|
|
|
# :section: Utilities
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# CGI escapes +text+
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
def convert_string(text)
|
|
|
|
CGI.escapeHTML text
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
|
2011-02-05 01:20:57 -05:00
|
|
|
##
|
2011-07-30 20:19:00 -04:00
|
|
|
# Generate a link for +url+, labeled with +text+. Handles the special cases
|
|
|
|
# for img: and link: described under handle_special_HYPERLINK
|
2011-02-05 01:20:57 -05:00
|
|
|
|
|
|
|
def gen_url(url, text)
|
|
|
|
if url =~ /([A-Za-z]+):(.*)/ then
|
|
|
|
type = $1
|
|
|
|
path = $2
|
|
|
|
else
|
|
|
|
type = "http"
|
|
|
|
path = url
|
|
|
|
url = "http://#{url}"
|
|
|
|
end
|
|
|
|
|
|
|
|
if type == "link" then
|
|
|
|
url = if path[0, 1] == '#' then # is this meaningful?
|
|
|
|
path
|
|
|
|
else
|
|
|
|
self.class.gen_relative_url @from_path, path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-23 19:53:49 -04:00
|
|
|
if (type == "http" or type == "https" or type == "link") and
|
2011-02-05 01:20:57 -05:00
|
|
|
url =~ /\.(gif|png|jpg|jpeg|bmp)$/ then
|
|
|
|
"<img src=\"#{url}\" />"
|
|
|
|
else
|
|
|
|
"<a href=\"#{url}\">#{text.sub(%r{^#{type}:/*}, '')}</a>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Determines the HTML list element for +list_type+ and +open_tag+
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def html_list_name(list_type, open_tag)
|
2010-04-02 00:40:47 -04:00
|
|
|
tags = LIST_TYPE_TO_HTML[list_type]
|
2010-04-01 03:45:16 -04:00
|
|
|
raise RDoc::Error, "Invalid list type: #{list_type.inspect}" unless tags
|
2010-12-19 22:22:49 -05:00
|
|
|
tags[open_tag ? 0 : 1]
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2011-02-05 01:20:57 -05:00
|
|
|
##
|
|
|
|
# Maps attributes to HTML tags
|
|
|
|
|
|
|
|
def init_tags
|
|
|
|
add_tag :BOLD, "<b>", "</b>"
|
|
|
|
add_tag :TT, "<tt>", "</tt>"
|
|
|
|
add_tag :EM, "<em>", "</em>"
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Returns the HTML tag for +list_type+, possible using a label from
|
|
|
|
# +list_item+
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def list_item_start(list_item, list_type)
|
|
|
|
case list_type
|
|
|
|
when :BULLET, :LALPHA, :NUMBER, :UALPHA then
|
2010-12-19 22:22:49 -05:00
|
|
|
"<li>"
|
2010-04-01 03:45:16 -04:00
|
|
|
when :LABEL then
|
2010-12-19 22:22:49 -05:00
|
|
|
"<dt>#{to_html list_item.label}</dt>\n<dd>"
|
2008-01-18 19:06:19 -05:00
|
|
|
when :NOTE then
|
2010-12-19 22:22:49 -05:00
|
|
|
"<tr><td class=\"rdoc-term\"><p>#{to_html list_item.label}</p></td>\n<td>"
|
2008-01-13 22:34:05 -05:00
|
|
|
else
|
2010-04-01 03:45:16 -04:00
|
|
|
raise RDoc::Error, "Invalid list type: #{list_type.inspect}"
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Returns the HTML end-tag for +list_type+
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
def list_end_for(list_type)
|
|
|
|
case list_type
|
|
|
|
when :BULLET, :LALPHA, :NUMBER, :UALPHA then
|
2008-01-13 22:34:05 -05:00
|
|
|
"</li>"
|
2010-04-01 03:45:16 -04:00
|
|
|
when :LABEL then
|
2008-01-13 22:34:05 -05:00
|
|
|
"</dd>"
|
2008-01-18 19:06:19 -05:00
|
|
|
when :NOTE then
|
2008-01-13 22:34:05 -05:00
|
|
|
"</td></tr>"
|
|
|
|
else
|
2010-04-01 03:45:16 -04:00
|
|
|
raise RDoc::Error, "Invalid list type: #{list_type.inspect}"
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Converts +item+ to HTML using RDoc::Text#to_html
|
|
|
|
|
|
|
|
def to_html item
|
|
|
|
super convert_flow @am.flow item
|
|
|
|
end
|
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
|