Class: RDoc::Markup::ToHtml

Inherits:
Formatter show all
Defined in:
lib/rdoc/markup/to_html.rb

Direct Known Subclasses

ToHtmlCrossref

Defined Under Namespace

Classes: InlineTag

Constant Summary collapse

LIST_TYPE_TO_HTML =
{
  :BULLET =>     %w[<ul> </ul>],
  :NUMBER =>     %w[<ol> </ol>],
  :UPPERALPHA => %w[<ol> </ol>],
  :LOWERALPHA => %w[<ol> </ol>],
  :LABELED =>    %w[<dl> </dl>],
  :NOTE    =>    %w[<table> </table>],
}

Instance Method Summary collapse

Methods inherited from Formatter

#convert

Constructor Details

#initializeToHtml

Returns a new instance of ToHtml.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rdoc/markup/to_html.rb', line 21

def initialize
  super

  # external hyperlinks
  @markup.add_special(/((link:|https?:|mailto:|ftp:|www\.)\S+\w)/, :HYPERLINK)

  # and links of the form  <text>[<url>]
  @markup.add_special(/(((\{.*?\})|\b\S+?)\[\S+?\.\S+?\])/, :TIDYLINK)

  init_tags
end

Instance Method Details

#accept_blank_line(am, fragment) ⇒ Object



172
173
174
# File 'lib/rdoc/markup/to_html.rb', line 172

def accept_blank_line(am, fragment)
  # @res << annotate("<p />") << "\n"
end

#accept_heading(am, fragment) ⇒ Object



176
177
178
# File 'lib/rdoc/markup/to_html.rb', line 176

def accept_heading(am, fragment)
  @res << convert_heading(fragment.head_level, am.flow(fragment.txt))
end

#accept_list_end(am, fragment) ⇒ Object



153
154
155
156
157
158
# File 'lib/rdoc/markup/to_html.rb', line 153

def accept_list_end(am, fragment)
  if tag = @in_list_entry.pop
    @res << annotate(tag) << "\n"
  end
  @res << html_list_name(fragment.type, false) << "\n"
end

#accept_list_item(am, fragment) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rdoc/markup/to_html.rb', line 160

def accept_list_item(am, fragment)
  if tag = @in_list_entry.last
    @res << annotate(tag) << "\n"
  end

  @res << list_item_start(am, fragment)

  @res << wrap(convert_flow(am.flow(fragment.txt))) << "\n"

  @in_list_entry[-1] = list_end_for(fragment.type)
end

#accept_list_start(am, fragment) ⇒ Object



148
149
150
151
# File 'lib/rdoc/markup/to_html.rb', line 148

def accept_list_start(am, fragment)
  @res << html_list_name(fragment.type, true) << "\n"
  @in_list_entry.push false
end

#accept_paragraph(am, fragment) ⇒ Object



130
131
132
133
134
# File 'lib/rdoc/markup/to_html.rb', line 130

def accept_paragraph(am, fragment)
  @res << annotate("<p>") + "\n"
  @res << wrap(convert_flow(am.flow(fragment.txt)))
  @res << annotate("</p>") + "\n"
end

#accept_rule(am, fragment) ⇒ Object



142
143
144
145
146
# File 'lib/rdoc/markup/to_html.rb', line 142

def accept_rule(am, fragment)
  size = fragment.param
  size = 10 if size > 10
  @res << "<hr size=\"#{size}\"></hr>"
end

#accept_verbatim(am, fragment) ⇒ Object



136
137
138
139
140
# File 'lib/rdoc/markup/to_html.rb', line 136

def accept_verbatim(am, fragment)
  @res << annotate("<pre>") + "\n"
  @res << CGI.escapeHTML(fragment.txt)
  @res << annotate("</pre>") << "\n"
end

#add_tag(name, start, stop) ⇒ Object

Add a new set of HTML tags for an attribute. We allow separate start and end tags for flexibility.



105
106
107
# File 'lib/rdoc/markup/to_html.rb', line 105

def add_tag(name, start, stop)
  @attr_tags << InlineTag.new(RDoc::Markup::Attribute.bitmap_for(name), start, stop)
end

#annotate(tag) ⇒ Object

Given an HTML tag, decorate it with class information and the like if required. This is a no-op in the base class, but is overridden in HTML output classes that implement style sheets.



114
115
116
# File 'lib/rdoc/markup/to_html.rb', line 114

def annotate(tag)
  tag
end

#end_acceptingObject



126
127
128
# File 'lib/rdoc/markup/to_html.rb', line 126

def end_accepting
  @res
end

#gen_url(url, text) ⇒ Object

Generate a hyperlink for url, labeled with text. Handle the special cases for img: and link: described under handle_special_HYPEDLINK



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rdoc/markup/to_html.rb', line 37

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
            RDoc::Generator.gen_url @from_path, path
          end
  end

  if (type == "http" or type == "link") and
     url =~ /\.(gif|png|jpg|jpeg|bmp)$/ then
    "<img src=\"#{url}\" />"
  else
    "<a href=\"#{url}\">#{text.sub(%r{^#{type}:/*}, '')}</a>"
  end
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 <img> tag. Otherwise a conventional <a href> 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.



71
72
73
74
# File 'lib/rdoc/markup/to_html.rb', line 71

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

<label>[url] or {long label}[url]


80
81
82
83
84
85
86
87
88
# File 'lib/rdoc/markup/to_html.rb', line 80

def handle_special_TIDYLINK(special)
  text = special.text

  return text unless text =~ /\{(.*?)\}\[(.*?)\]/ or text =~ /(\S+)\[(.*?)\]/

  label = $1
  url   = $2
  gen_url url, label
end

#init_tagsObject

Set up the standard mapping of attributes to HTML tags



93
94
95
96
97
98
99
# File 'lib/rdoc/markup/to_html.rb', line 93

def init_tags
  @attr_tags = [
    InlineTag.new(RDoc::Markup::Attribute.bitmap_for(:BOLD), "<b>", "</b>"),
    InlineTag.new(RDoc::Markup::Attribute.bitmap_for(:TT),   "<tt>", "</tt>"),
    InlineTag.new(RDoc::Markup::Attribute.bitmap_for(:EM),   "<em>", "</em>"),
  ]
end

#start_acceptingObject

Here’s the client side of the visitor pattern



121
122
123
124
# File 'lib/rdoc/markup/to_html.rb', line 121

def start_accepting
  @res = ""
  @in_list_entry = []
end

#wrap(txt, line_len = 76) ⇒ Object

This is a higher speed (if messier) version of wrap



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/rdoc/markup/to_html.rb', line 183

def wrap(txt, line_len = 76)
  res = ""
  sp = 0
  ep = txt.length
  while sp < ep
    # scan back for a space
    p = sp + line_len - 1
    if p >= ep
      p = ep
    else
      while p > sp and txt[p] != ?\s
        p -= 1
      end
      if p <= sp
        p = sp + line_len
        while p < ep and txt[p] != ?\s
          p += 1
        end
      end
    end
    res << txt[sp...p] << "\n"
    sp = p
    sp += 1 while sp < ep and txt[sp] == ?\s
  end
  res
end