2008-01-13 22:34:05 -05:00
|
|
|
require 'cgi'
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
2012-11-26 23:28:14 -05:00
|
|
|
# Outputs RDoc markup as HTML.
|
2010-04-01 03:45:16 -04:00
|
|
|
|
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 = {
|
2012-11-26 23:28:14 -05:00
|
|
|
:BULLET => ['<ul>', '</ul>'],
|
|
|
|
:LABEL => ['<dl class="rdoc-list label-list">', '</dl>'],
|
|
|
|
:LALPHA => ['<ol style="list-style-type: lower-alpha">', '</ol>'],
|
|
|
|
:NOTE => ['<dl class="rdoc-list note-list">', '</dl>'],
|
|
|
|
:NUMBER => ['<ol>', '</ol>'],
|
|
|
|
:UALPHA => ['<ol style="list-style-type: 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
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# The RDoc::CodeObject HTML is being generated for. This is used to
|
|
|
|
# generate namespaced URI fragments
|
|
|
|
|
|
|
|
attr_accessor :code_object
|
|
|
|
|
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
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def initialize options, markup = nil
|
2010-04-01 03:45:16 -04:00
|
|
|
super
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
@code_object = nil
|
|
|
|
@from_path = ''
|
2010-04-01 03:45:16 -04:00
|
|
|
@in_list_entry = nil
|
|
|
|
@list = nil
|
2012-11-26 23:28:14 -05:00
|
|
|
@th = nil
|
|
|
|
@hard_break = "<br>\n"
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
# external links
|
2012-11-26 23:28:14 -05:00
|
|
|
@markup.add_special(/(?:link:|https?:|mailto:|ftp:|irc:|www\.)\S+\w/,
|
|
|
|
:HYPERLINK)
|
|
|
|
|
|
|
|
# internal links
|
|
|
|
@markup.add_special(/rdoc-[a-z]+:\S+/, :RDOCLINK)
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
# and links of the form <text>[<url>]
|
2012-11-26 23:28:14 -05:00
|
|
|
@markup.add_special(/(?:
|
|
|
|
\{.*?\} | # multi-word label
|
|
|
|
\b[^\s{}]+? # single-word label
|
|
|
|
)
|
|
|
|
|
|
|
|
\[\S+?\] # link target
|
|
|
|
/x, :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
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# +special+ is a <code><br></code>
|
|
|
|
|
|
|
|
def handle_special_HARD_BREAK special
|
|
|
|
'<br>'
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# +special+ is an rdoc-schemed link that will be converted into a hyperlink.
|
|
|
|
#
|
|
|
|
# For the +rdoc-ref+ scheme the named reference will be returned without
|
|
|
|
# creating a link.
|
|
|
|
#
|
|
|
|
# For the +rdoc-label+ scheme the footnote and label prefixes are stripped
|
|
|
|
# when creating a link. All other contents will be linked verbatim.
|
|
|
|
|
|
|
|
def handle_special_RDOCLINK special
|
|
|
|
url = special.text
|
|
|
|
|
|
|
|
case url
|
|
|
|
when /\Ardoc-ref:/
|
|
|
|
$'
|
|
|
|
when /\Ardoc-label:/
|
|
|
|
text = $'
|
|
|
|
|
|
|
|
text = case text
|
|
|
|
when /\Alabel-/ then $'
|
|
|
|
when /\Afootmark-/ then "^#{$'}"
|
|
|
|
when /\Afoottext-/ then "*#{$'}"
|
|
|
|
else text
|
|
|
|
end
|
|
|
|
|
|
|
|
gen_url url, text
|
|
|
|
else
|
|
|
|
url =~ /\Ardoc-[a-z]+:/
|
|
|
|
|
|
|
|
$'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-02-12 17:23:00 -05:00
|
|
|
##
|
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
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# Adds +block_quote+ to the output
|
|
|
|
|
|
|
|
def accept_block_quote block_quote
|
|
|
|
@res << "\n<blockquote>"
|
|
|
|
|
|
|
|
block_quote.parts.each do |part|
|
|
|
|
part.accept self
|
|
|
|
end
|
|
|
|
|
|
|
|
@res << "</blockquote>\n"
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Adds +paragraph+ to the output
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def accept_paragraph paragraph
|
2010-12-19 22:22:49 -05:00
|
|
|
@res << "\n<p>"
|
2012-11-26 23:28:14 -05:00
|
|
|
text = paragraph.text @hard_break
|
|
|
|
@res << wrap(to_html(text))
|
2010-12-19 22:22:49 -05:00
|
|
|
@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
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def accept_verbatim verbatim
|
|
|
|
text = verbatim.text.rstrip
|
|
|
|
|
|
|
|
@res << if verbatim.ruby? or parseable? text then
|
|
|
|
begin
|
|
|
|
tokens = RDoc::RubyLex.tokenize text, @options
|
|
|
|
|
|
|
|
html = RDoc::TokenStream.to_html tokens
|
|
|
|
|
|
|
|
"\n<pre class=\"ruby\">#{html}</pre>\n"
|
|
|
|
rescue RDoc::RubyLex::Error
|
|
|
|
"\n<pre>#{CGI.escapeHTML text}</pre>\n"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
"\n<pre>#{CGI.escapeHTML text}</pre>\n"
|
|
|
|
end
|
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
|
|
|
##
|
2012-11-26 23:28:14 -05:00
|
|
|
# Adds +heading+ to the output. The headings greater than 6 are trimmed to
|
|
|
|
# level 6.
|
|
|
|
|
|
|
|
def accept_heading heading
|
|
|
|
level = [6, heading.level].min
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
label = heading.aref
|
|
|
|
label = [@code_object.aref, label].compact.join '-' if
|
|
|
|
@code_object and @code_object.respond_to? :aref
|
|
|
|
|
|
|
|
@res << "\n<h#{level} id=\"#{label}\">"
|
2010-12-19 22:22:49 -05:00
|
|
|
@res << to_html(heading.text)
|
2012-11-26 23:28:14 -05:00
|
|
|
@res << "</h#{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
|
|
|
##
|
2012-11-26 23:28:14 -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
|
|
|
##
|
2012-11-26 23:28:14 -05:00
|
|
|
# Generate a link to +url+ with content +text+. Handles the special cases
|
2011-07-30 20:19:00 -04:00
|
|
|
# for img: and link: described under handle_special_HYPERLINK
|
2011-02-05 01:20:57 -05:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def gen_url url, text
|
|
|
|
if url =~ /^rdoc-label:([^:]*)(?::(.*))?/ then
|
|
|
|
type = "link"
|
|
|
|
path = "##{$1}"
|
|
|
|
id = " id=\"#{$2}\"" if $2
|
|
|
|
elsif url =~ /([A-Za-z]+):(.*)/ then
|
2011-02-05 01:20:57 -05:00
|
|
|
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
|
2012-11-26 23:28:14 -05:00
|
|
|
"<a#{id} href=\"#{url}\">#{text.sub(%r{^#{type}:/*}, '')}</a>"
|
2011-02-05 01:20:57 -05:00
|
|
|
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
|
2012-11-26 23:28:14 -05:00
|
|
|
add_tag :BOLD, "<strong>", "</strong>"
|
|
|
|
add_tag :TT, "<code>", "</code>"
|
|
|
|
add_tag :EM, "<em>", "</em>"
|
2011-02-05 01:20:57 -05:00
|
|
|
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>"
|
2012-11-26 23:28:14 -05:00
|
|
|
when :LABEL, :NOTE then
|
|
|
|
Array(list_item.label).map do |label|
|
|
|
|
"<dt>#{to_html label}\n"
|
|
|
|
end.join << "<dd>"
|
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>"
|
2012-11-26 23:28:14 -05:00
|
|
|
when :LABEL, :NOTE then
|
2008-01-13 22:34:05 -05:00
|
|
|
"</dd>"
|
|
|
|
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
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# Returns true if Ripper is available it can create a sexp from +text+
|
|
|
|
|
|
|
|
def parseable? text
|
|
|
|
text =~ /\b(def|class|module|require) |=>|\{\s?\||do \|/ and
|
|
|
|
text !~ /<%|%>/
|
|
|
|
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
|
|
|
|
|