Class: RDocF95::Markup::ToHtml

Inherits:
Formatter show all
Defined in:
lib/rdoc-f95/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-f95/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



175
176
177
# File 'lib/rdoc-f95/markup/to_html.rb', line 175

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

#accept_heading(am, fragment) ⇒ Object



179
180
181
# File 'lib/rdoc-f95/markup/to_html.rb', line 179

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

#accept_list_end(am, fragment) ⇒ Object



156
157
158
159
160
161
# File 'lib/rdoc-f95/markup/to_html.rb', line 156

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



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rdoc-f95/markup/to_html.rb', line 163

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



151
152
153
154
# File 'lib/rdoc-f95/markup/to_html.rb', line 151

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



133
134
135
136
137
# File 'lib/rdoc-f95/markup/to_html.rb', line 133

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



145
146
147
148
149
# File 'lib/rdoc-f95/markup/to_html.rb', line 145

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

#accept_verbatim(am, fragment) ⇒ Object



139
140
141
142
143
# File 'lib/rdoc-f95/markup/to_html.rb', line 139

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.



108
109
110
# File 'lib/rdoc-f95/markup/to_html.rb', line 108

def add_tag(name, start, stop)
  @attr_tags << InlineTag.new(RDocF95::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.



117
118
119
# File 'lib/rdoc-f95/markup/to_html.rb', line 117

def annotate(tag)
  tag
end

#end_acceptingObject



129
130
131
# File 'lib/rdoc-f95/markup/to_html.rb', line 129

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
62
63
64
# File 'lib/rdoc-f95/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
            RDocF95::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}\" />"

  elsif type == "link"
    "<a href=\"#{url}\">#{text.sub(%r{^#{type}:/*}, '')}</a>"
  else
    "<a href=\"#{url}\" target=\"_top\">#{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.



74
75
76
77
# File 'lib/rdoc-f95/markup/to_html.rb', line 74

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]


83
84
85
86
87
88
89
90
91
# File 'lib/rdoc-f95/markup/to_html.rb', line 83

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



96
97
98
99
100
101
102
# File 'lib/rdoc-f95/markup/to_html.rb', line 96

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

#start_acceptingObject

Here’s the client side of the visitor pattern



124
125
126
127
# File 'lib/rdoc-f95/markup/to_html.rb', line 124

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

#wrap(txt, line_len = 76) ⇒ Object

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



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

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