require 'rdoc/markup/formatter' require 'rdoc/markup/inline' require 'cgi' ## # Outputs RDoc markup as HTML class RDoc::Markup::ToHtml < RDoc::Markup::Formatter include RDoc::Text ## # Maps RDoc::Markup::Parser::LIST_TOKENS types to HTML tags LIST_TYPE_TO_HTML = { :BULLET => [''], :LABEL => ['
', '
'], :LALPHA => ['
    ', '
'], :NOTE => ['', '
'], :NUMBER => ['
    ', '
'], :UALPHA => ['
    ', '
'], } attr_reader :res # :nodoc: attr_reader :in_list_entry # :nodoc: attr_reader :list # :nodoc: ## # Path to this document for relative links attr_accessor :from_path ## # 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 "/" from.delete '.' to.delete '.' 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 ## # Creates a new formatter that will output HTML def initialize super @th = nil @in_list_entry = nil @list = nil @from_path = '' # external hyperlinks @markup.add_special(/((link:|https?:|mailto:|ftp:|www\.)\S+\w)/, :HYPERLINK) # and links of the form [] @markup.add_special(/(((\{.*?\})|\b\S+?)\[\S+?\.\S+?\])/, :TIDYLINK) init_tags end ## # Maps attributes to HTML tags def init_tags add_tag :BOLD, "", "" add_tag :TT, "", "" add_tag :EM, "", "" end ## # Generate a hyperlink for +url+, labeled with +text+. Handles the special # cases for img: and link: described under handle_special_HYPERLINK 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 if (type == "http" or type == "link") and url =~ /\.(gif|png|jpg|jpeg|bmp)$/ then "" else "#{text.sub(%r{^#{type}:/*}, '')}" end end # :section: Special handling ## # 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. def handle_special_HYPERLINK(special) url = special.text gen_url url, url end ## # Here's a hypedlink where the label is different to the URL #