Class: RD::RD2HTMLVisitor

Inherits:
RDVisitor show all
Includes:
MethodParse
Defined in:
lib/rd/rd2html-lib.rb

Direct Known Subclasses

RD2HTMLExtVisitor

Constant Summary collapse

SYSTEM_NAME =
"RDtool -- RD2HTMLVisitor"
SYSTEM_VERSION =
"$Version: "+ RD::VERSION+"$"
VERSION =
Version.new_from_version_string(SYSTEM_NAME, SYSTEM_VERSION)
OUTPUT_SUFFIX =

must-have constants

"html"
INCLUDE_SUFFIX =
["html"]
METACHAR =
{ "<" => "&lt;", ">" => "&gt;", "&" => "&amp;" }

Constants included from MethodParse

MethodParse::KIND2NUM

Instance Attribute Summary collapse

Attributes inherited from OutputFormatVisitor

#filename, #include_suffix, #input_filename

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MethodParse

analize_method, kind2num, kind2str, make_method_index, make_mindex_label, str2kind, tr_method

Methods inherited from RDVisitor

#apply_to_DescListItemTerm, #apply_to_MethodListItemTerm, #prepare_labels, #refer_external

Methods inherited from OutputFormatVisitor

#apply_to_Include

Methods included from SearchFile

#search_file

Methods inherited from Visitor

define_visit_Nonterminal, define_visit_Terminal, #visit_DescListItem, #visit_MethodListItem, #visit_Reference, #visit_children

Constructor Details

#initializeRD2HTMLVisitor

Returns a new instance of RD2HTMLVisitor.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rd/rd2html-lib.rb', line 42

def initialize
  @css = nil
  @charset = nil
  @lang = nil
  @title = nil
  @html_link_rel = {}
  @html_link_rev = {}

  @footnotes = []
  @index = {}

  #      @use_old_anchor = nil
  @use_old_anchor = true # MUST -> nil
  @output_rbl = nil
  super
end

Instance Attribute Details

#charsetObject Also known as: charcode

Returns the value of attribute charset.



28
29
30
# File 'lib/rd/rd2html-lib.rb', line 28

def charset
  @charset
end

#cssObject

Returns the value of attribute css.



27
28
29
# File 'lib/rd/rd2html-lib.rb', line 27

def css
  @css
end

#footnotesObject (readonly)

Returns the value of attribute footnotes.



39
40
41
# File 'lib/rd/rd2html-lib.rb', line 39

def footnotes
  @footnotes
end

#foottextsObject (readonly)

Returns the value of attribute foottexts.



40
41
42
# File 'lib/rd/rd2html-lib.rb', line 40

def foottexts
  @foottexts
end

Returns the value of attribute html_link_rel.



33
34
35
# File 'lib/rd/rd2html-lib.rb', line 33

def html_link_rel
  @html_link_rel
end

Returns the value of attribute html_link_rev.



34
35
36
# File 'lib/rd/rd2html-lib.rb', line 34

def html_link_rev
  @html_link_rev
end

#langObject

Returns the value of attribute lang.



31
32
33
# File 'lib/rd/rd2html-lib.rb', line 31

def lang
  @lang
end

#output_rblObject

output external Label file.



37
38
39
# File 'lib/rd/rd2html-lib.rb', line 37

def output_rbl
  @output_rbl
end

#titleObject

Returns the value of attribute title.



32
33
34
# File 'lib/rd/rd2html-lib.rb', line 32

def title
  @title
end

#use_old_anchorObject

Returns the value of attribute use_old_anchor.



35
36
37
# File 'lib/rd/rd2html-lib.rb', line 35

def use_old_anchor
  @use_old_anchor
end

Class Method Details

.versionObject



17
18
19
# File 'lib/rd/rd2html-lib.rb', line 17

def self.version
  VERSION
end

Instance Method Details

#add_foottext(num, foottext) ⇒ Object

Raises:

  • (ArgumentError)


393
394
395
396
397
# File 'lib/rd/rd2html-lib.rb', line 393

def add_foottext(num, foottext)
  raise ArgumentError, "[BUG] footnote ##{num} isn't here." unless
	footnotes[num - 1]
  @foottexts[num - 1] = foottext
end

#apply_to_Code(element, content) ⇒ Object



287
288
289
# File 'lib/rd/rd2html-lib.rb', line 287

def apply_to_Code(element, content)
  %Q[<code>#{content.join("")}</code>]
end

#apply_to_DescList(element, items) ⇒ Object



230
231
232
# File 'lib/rd/rd2html-lib.rb', line 230

def apply_to_DescList(element, items)
  %Q[<dl>\n#{items.join("\n").chomp}\n</dl>]
end

#apply_to_DescListItem(element, term, description) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/rd/rd2html-lib.rb', line 251

def apply_to_DescListItem(element, term, description)
  anchor = get_anchor(element.term)
  label = hyphen_escape(element.label)
  term = term.join("")
  if description.empty?
	%Q[<dt><a name="#{anchor}" id="#{anchor}">#{term}</a></dt>] +
	%Q[<!-- RDLabel: "#{label}" -->]
  else
    %Q[<dt><a name="#{anchor}" id="#{anchor}">#{term}</a></dt>] +
    %Q[<!-- RDLabel: "#{label}" -->\n] +
    %Q[<dd>\n#{description.join("\n").chomp}\n</dd>]
  end
end

#apply_to_DocumentElement(element, content) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/rd/rd2html-lib.rb', line 67

def apply_to_DocumentElement(element, content)
  ret = ""
  ret << xml_decl + "\n"
  ret << doctype_decl + "\n"
  ret << html_open_tag + "\n"
  ret << html_head + "\n"
  ret << html_body(content) + "\n"
  ret << "</html>\n"
  ret
end

#apply_to_Emphasis(element, content) ⇒ Object



283
284
285
# File 'lib/rd/rd2html-lib.rb', line 283

def apply_to_Emphasis(element, content)
  %Q[<em>#{content.join("")}</em>]
end

#apply_to_EnumList(element, items) ⇒ Object



226
227
228
# File 'lib/rd/rd2html-lib.rb', line 226

def apply_to_EnumList(element, items)
  %Q[<ol>\n#{items.join("\n").chomp}\n</ol>]
end

#apply_to_EnumListItem(element, content) ⇒ Object



242
243
244
# File 'lib/rd/rd2html-lib.rb', line 242

def apply_to_EnumListItem(element, content)
  %Q[<li>#{content.join("\n").chomp}</li>]
end

#apply_to_Footnote(element, content) ⇒ Object

Raises:

  • (ArgumentError)


354
355
356
357
358
359
360
361
362
363
364
# File 'lib/rd/rd2html-lib.rb', line 354

def apply_to_Footnote(element, content)
  num = get_footnote_num(element)
  raise ArgumentError, "[BUG?] #{element} is not registered." unless num
  
  add_foottext(num, content)
  anchor = a_name("footmark", num)
  href = a_name("foottext", num)
  %Q|<a name="#{anchor}" id="#{anchor}" | +
	%Q|href="##{href}"><sup><small>| +
  %Q|*#{num}</small></sup></a>|
end

#apply_to_Foottext(element, content) ⇒ Object

Raises:

  • (ArgumentError)


382
383
384
385
386
387
388
389
390
391
# File 'lib/rd/rd2html-lib.rb', line 382

def apply_to_Foottext(element, content)
  num = get_footnote_num(element)
  raise ArgumentError, "[BUG] #{element} isn't registered." unless num
  anchor = a_name("foottext", num)
  href = a_name("footmark", num)
  content = content.join("")
  %|<a name="#{anchor}" id="#{anchor}" href="##{href}">|+
	%|<sup><small>*#{num}</small></sup></a>| +
	%|<small>#{content}</small><br />|
end

#apply_to_Headline(element, title) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/rd/rd2html-lib.rb', line 163

def apply_to_Headline(element, title)
  anchor = get_anchor(element)
  label = hyphen_escape(element.label)
  title = title.join("")
  %Q[<h#{element.level}><a name="#{anchor}" id="#{anchor}">#{title}</a>] +
  %Q[</h#{element.level}><!-- RDLabel: "#{label}" -->]
end

#apply_to_Index(element, content) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/rd/rd2html-lib.rb', line 299

def apply_to_Index(element, content)
  tmp = []
  element.each do |i|
	tmp.push(i) if i.is_a?(String)
  end
  key = meta_char_escape(tmp.join(""))
  if @index.has_key?(key)
	# warning?
	%Q[<!-- Index, but conflict -->#{content.join("")}<!-- Index end -->]
  else
	num = @index[key] = @index.size
	anchor = a_name("index", num)
	%Q[<a name="#{anchor}" id="#{anchor}">#{content.join("")}</a>]
  end
end

#apply_to_ItemList(element, items) ⇒ Object



222
223
224
# File 'lib/rd/rd2html-lib.rb', line 222

def apply_to_ItemList(element, items)
  %Q[<ul>\n#{items.join("\n").chomp}\n</ul>]
end

#apply_to_ItemListItem(element, content) ⇒ Object



238
239
240
# File 'lib/rd/rd2html-lib.rb', line 238

def apply_to_ItemListItem(element, content)
  %Q[<li>#{content.join("\n").chomp}</li>]
end

#apply_to_Keyboard(element, content) ⇒ Object



295
296
297
# File 'lib/rd/rd2html-lib.rb', line 295

def apply_to_Keyboard(element, content)
  %Q[<kbd>#{content.join("")}</kbd>]
end

#apply_to_MethodList(element, items) ⇒ Object



234
235
236
# File 'lib/rd/rd2html-lib.rb', line 234

def apply_to_MethodList(element, items)
  %Q[<dl>\n#{items.join("\n").chomp}\n</dl>]
end

#apply_to_MethodListItem(element, term, description) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/rd/rd2html-lib.rb', line 265

def apply_to_MethodListItem(element, term, description)
  term = parse_method(term)  # maybe: term -> element.term
  anchor = get_anchor(element.term)
  label = hyphen_escape(element.label)
  if description.empty?
	%Q[<dt><a name="#{anchor}" id="#{anchor}"><code>#{term}] +
    %Q[</code></a></dt><!-- RDLabel: "#{label}" -->]
  else
    %Q[<dt><a name="#{anchor}" id="#{anchor}"><code>#{term}] +
	%Q[</code></a></dt><!-- RDLabel: "#{label}" -->\n] +
	%Q[<dd>\n#{description.join("\n")}</dd>]
  end
end

#apply_to_Reference_with_RDLabel(element, content) ⇒ Object



315
316
317
318
319
320
321
# File 'lib/rd/rd2html-lib.rb', line 315

def apply_to_Reference_with_RDLabel(element, content)
  if element.label.filename
	apply_to_RefToOtherFile(element, content)
  else
	apply_to_RefToElement(element, content)
  end
end

#apply_to_Reference_with_URL(element, content) ⇒ Object



323
324
325
326
# File 'lib/rd/rd2html-lib.rb', line 323

def apply_to_Reference_with_URL(element, content)
  %Q[<a href="#{meta_char_escape(element.label.url)}">] +
	%Q[#{content.join("")}</a>]
end

#apply_to_RefToElement(element, content) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/rd/rd2html-lib.rb', line 328

def apply_to_RefToElement(element, content)
  content = content.join("")
  if anchor = refer(element)
	content = content.sub(/^function#/, "")
	%Q[<a href="\##{anchor}">#{content}</a>]
  else
	# warning?
	label = hyphen_escape(element.to_label)
	%Q[<!-- Reference, RDLabel "#{label}" doesn't exist -->] +
	  %Q[<em class="label-not-found">#{content}</em><!-- Reference end -->]
	#' 
  end
end

#apply_to_RefToOtherFile(element, content) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
# File 'lib/rd/rd2html-lib.rb', line 342

def apply_to_RefToOtherFile(element, content)
  content = content.join("")
  filename = element.label.filename.sub(/\.(rd|rb)(\.\w+)?$/, "." +
	    OUTPUT_SUFFIX)
  anchor = refer_external(element)
  if anchor
	%Q[<a href="#{filename}\##{anchor}">#{content}</a>]
  else
	%Q[<a href="#{filename}">#{content}</a>]
  end
end

#apply_to_String(element) ⇒ Object



409
410
411
# File 'lib/rd/rd2html-lib.rb', line 409

def apply_to_String(element)
  meta_char_escape(element)
end

#apply_to_StringElement(element) ⇒ Object



279
280
281
# File 'lib/rd/rd2html-lib.rb', line 279

def apply_to_StringElement(element)
  apply_to_String(element.content)
end

#apply_to_TextBlock(element, content) ⇒ Object

RDVisitor#apply_to_Include



173
174
175
176
177
178
179
180
181
# File 'lib/rd/rd2html-lib.rb', line 173

def apply_to_TextBlock(element, content)
  content = content.join("")
  if (is_this_textblock_only_one_block_of_parent_listitem?(element) or
	  is_this_textblock_only_one_block_other_than_sublists_in_parent_listitem?(element))
	content.chomp
  else
	%Q[<p>#{content.chomp}</p>]
  end
end

#apply_to_Var(element, content) ⇒ Object



291
292
293
# File 'lib/rd/rd2html-lib.rb', line 291

def apply_to_Var(element, content)
  %Q[<var>#{content.join("")}</var>]
end

#apply_to_Verb(element) ⇒ Object



399
400
401
402
# File 'lib/rd/rd2html-lib.rb', line 399

def apply_to_Verb(element)
  content = apply_to_String(element.content)
  %Q[#{content}]
end

#apply_to_Verbatim(element) ⇒ Object



214
215
216
217
218
219
220
# File 'lib/rd/rd2html-lib.rb', line 214

def apply_to_Verbatim(element)
  content = []
  element.each_line do |i|
	content.push(apply_to_String(i))
  end
  %Q[<pre>#{content.join("").chomp}</pre>]
end

#consist_of_one_textblock_and_sublists(element) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/rd/rd2html-lib.rb', line 201

def consist_of_one_textblock_and_sublists(element)
  i = 0
  element.each_child do |child|
	if i == 0
	  return false unless child.is_a?(TextBlock)
	else
	  return false unless child.is_a?(List)
	end
	i += 1
  end
  return true
end

#get_footnote_num(fn) ⇒ Object

Raises:

  • (ArgumentError)


366
367
368
369
370
371
372
373
374
# File 'lib/rd/rd2html-lib.rb', line 366

def get_footnote_num(fn)
  raise ArgumentError, "#{fn} must be Footnote." unless fn.is_a? Footnote
  i = @footnotes.index(fn)
  if i
	i + 1
  else
	nil
  end
end

#hyphen_escape(str) ⇒ Object



461
462
463
# File 'lib/rd/rd2html-lib.rb', line 461

def hyphen_escape(str)
  str.gsub(/--/, "&shy;&shy;")
end

#is_this_textblock_only_one_block_of_parent_listitem?(element) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
186
187
188
189
190
# File 'lib/rd/rd2html-lib.rb', line 183

def is_this_textblock_only_one_block_of_parent_listitem?(element)
  parent = element.parent
  (parent.is_a?(ItemListItem) or
   parent.is_a?(EnumListItem) or
   parent.is_a?(DescListItem) or
   parent.is_a?(MethodListItem)) and
	consist_of_one_textblock?(parent)
end

#is_this_textblock_only_one_block_other_than_sublists_in_parent_listitem?(element) ⇒ Boolean

Returns:

  • (Boolean)


192
193
194
195
196
197
198
199
# File 'lib/rd/rd2html-lib.rb', line 192

def is_this_textblock_only_one_block_other_than_sublists_in_parent_listitem?(element)
  parent = element.parent
  (parent.is_a?(ItemListItem) or
   parent.is_a?(EnumListItem) or
   parent.is_a?(DescListItem) or
   parent.is_a?(MethodListItem)) and
	consist_of_one_textblock_and_sublists(element.parent)
end

#visit(tree) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/rd/rd2html-lib.rb', line 59

def visit(tree)
  prepare_labels(tree, "label-")
  prepare_footnotes(tree)
  tmp = super(tree)
  make_rbl_file(@filename) if @output_rbl and @filename
  tmp
end