2017-11-27 05:45:24 -05:00
|
|
|
# frozen_string_literal: true
|
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
|
|
|
|
|
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)
|
|
|
|
|
2012-12-16 18:07:49 -05:00
|
|
|
add_special_RDOCLINK
|
|
|
|
add_special_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
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
def handle_RDOCLINK url # :nodoc:
|
|
|
|
case url
|
|
|
|
when /^rdoc-ref:/
|
|
|
|
$'
|
|
|
|
when /^rdoc-label:/
|
|
|
|
text = $'
|
|
|
|
|
|
|
|
text = case text
|
|
|
|
when /\Alabel-/ then $'
|
|
|
|
when /\Afootmark-/ then $'
|
|
|
|
when /\Afoottext-/ then $'
|
|
|
|
else text
|
|
|
|
end
|
|
|
|
|
|
|
|
gen_url url, text
|
|
|
|
when /^rdoc-image:/
|
|
|
|
"<img src=\"#{$'}\">"
|
|
|
|
else
|
|
|
|
url =~ /\Ardoc-[a-z]+:/
|
|
|
|
|
|
|
|
$'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
2013-09-18 19:33:36 -04:00
|
|
|
handle_RDOCLINK special.text
|
2012-11-26 23:28:14 -05:00
|
|
|
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
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
return text unless
|
|
|
|
text =~ /^\{(.*)\}\[(.*?)\]$/ or text =~ /^(\S+)\[(.*?)\]$/
|
2008-02-12 17:23:00 -05:00
|
|
|
|
|
|
|
label = $1
|
|
|
|
url = $2
|
2013-09-18 19:33:36 -04:00
|
|
|
|
|
|
|
label = handle_RDOCLINK label if /^rdoc-image:/ =~ label
|
|
|
|
|
2008-02-12 17:23:00 -05:00
|
|
|
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
|
2013-09-18 19:33:36 -04:00
|
|
|
text = text.gsub(/\r?\n/, ' ')
|
2012-11-26 23:28:14 -05:00
|
|
|
@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
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
klass = nil
|
2012-11-26 23:28:14 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
content = if verbatim.ruby? or parseable? text then
|
|
|
|
begin
|
2018-08-26 21:30:18 -04:00
|
|
|
tokens = RDoc::Parser::RipperStateLex.parse text
|
2013-09-18 19:33:36 -04:00
|
|
|
klass = ' class="ruby"'
|
2012-11-26 23:28:14 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
result = RDoc::TokenStream.to_html tokens
|
|
|
|
result = result + "\n" unless "\n" == result[-1]
|
|
|
|
result
|
2017-10-09 22:01:00 -04:00
|
|
|
rescue
|
2013-09-18 19:33:36 -04:00
|
|
|
CGI.escapeHTML text
|
|
|
|
end
|
|
|
|
else
|
|
|
|
CGI.escapeHTML text
|
2012-11-26 23:28:14 -05:00
|
|
|
end
|
2013-09-18 19:33:36 -04:00
|
|
|
|
|
|
|
if @options.pipe then
|
2017-09-11 23:42:54 -04:00
|
|
|
@res << "\n<pre><code>#{CGI.escapeHTML text}\n</code></pre>\n"
|
2013-09-18 19:33:36 -04:00
|
|
|
else
|
|
|
|
@res << "\n<pre#{klass}>#{content}</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
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
def accept_rule rule
|
|
|
|
@res << "<hr>\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
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
label = heading.label @code_object
|
2012-11-26 23:28:14 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
@res << if @options.output_decoration
|
|
|
|
"\n<h#{level} id=\"#{label}\">"
|
|
|
|
else
|
|
|
|
"\n<h#{level}>"
|
|
|
|
end
|
2010-12-19 22:22:49 -05:00
|
|
|
@res << to_html(heading.text)
|
2012-12-19 22:57:08 -05:00
|
|
|
unless @options.pipe then
|
|
|
|
@res << "<span><a href=\"##{label}\">¶</a>"
|
2014-12-06 20:22:37 -05:00
|
|
|
@res << " <a href=\"#top\">↑</a></span>"
|
2012-12-19 22:57:08 -05:00
|
|
|
end
|
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
|
2012-12-16 18:07:49 -05:00
|
|
|
scheme, url, id = parse_url url
|
2011-02-05 01:20:57 -05:00
|
|
|
|
2012-12-16 18:07:49 -05:00
|
|
|
if %w[http https link].include?(scheme) and
|
2011-02-05 01:20:57 -05:00
|
|
|
url =~ /\.(gif|png|jpg|jpeg|bmp)$/ then
|
|
|
|
"<img src=\"#{url}\" />"
|
|
|
|
else
|
2013-09-18 19:33:36 -04:00
|
|
|
text = text.sub %r%^#{scheme}:/*%i, ''
|
|
|
|
text = text.sub %r%^[*\^](\d+)$%, '\1'
|
|
|
|
|
|
|
|
link = "<a#{id} href=\"#{url}\">#{text}</a>"
|
|
|
|
|
|
|
|
link = "<sup>#{link}</sup>" if /"foot/ =~ id
|
|
|
|
|
|
|
|
link
|
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
|
|
|
##
|
2014-12-06 20:22:37 -05:00
|
|
|
# Returns true if text is valid ruby syntax
|
2012-11-26 23:28:14 -05:00
|
|
|
|
|
|
|
def parseable? text
|
2017-02-05 21:35:48 -05:00
|
|
|
verbose, $VERBOSE = $VERBOSE, nil
|
2014-12-06 20:22:37 -05:00
|
|
|
eval("BEGIN {return true}\n#{text}")
|
|
|
|
rescue SyntaxError
|
|
|
|
false
|
2017-02-05 21:35:48 -05:00
|
|
|
ensure
|
|
|
|
$VERBOSE = verbose
|
2012-11-26 23:28:14 -05:00
|
|
|
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
|
|
|
|
|