Class: BayeuxHTMLGen

Inherits:
Object
  • Object
show all
Defined in:
lib/bayeux/html_gen.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(syntax_tree) ⇒ BayeuxHTMLGen

Default Constructor



10
11
12
13
# File 'lib/bayeux/html_gen.rb', line 10

def initialize(syntax_tree)
  @syntax_tree = syntax_tree
  @html_string = String.new
end

Class Method Details

.header_id(header_text) ⇒ Object

Return the header id, given a string of text



463
464
465
# File 'lib/bayeux/html_gen.rb', line 463

def self.header_id(header_text)
  return header_text.gsub(" ", "_").downcase
end

Instance Method Details

#doc_tocObject

Return the table of contents, extract from the AST



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/bayeux/html_gen.rb', line 60

def doc_toc
  
  # ToC to return to the caller
  toc = String.new
  
  # The current heading level
  current_level = 0

  # The last heading level
  # Note: The very top level is provided
  # by the enclosing template. Effectivly
  # we are skipping the enclosures for the L1
  # headers
  last_level = 2
  
  # Set containing valid header tags (ignore level 1 tags)
  header_tags = Set.new [:h2, :h3, :h4, :h5, :h6]
  
  # Walk the forest
  @syntax_tree.block_forest.each{|tree|
    
    # Ignore if not a header
    if header_tags.include?(tree.content.type) then
    
      # Set the current level based on the header
      case tree.content.type
        when :h1
          current_level = 1
        when :h2
          current_level = 2
        when :h3
          current_level = 3
        when :h4
          current_level = 4
        when :h5
          current_level = 5
        when :h6
          current_level = 6        
      end
      
      # Write out the toc
      if current_level < last_level then
        
        # Moving up: so close off the last level,
        # create a new level, and record the current
        # item
        
        unless last_level == 0 then
          toc << "</ul>"
        end
        
        toc << "#{indent(last_level)}<ul>\n"
        toc << "#{indent(current_level)}<li><a href=\"##{header_id(tree.content.content)}\">#{tree.content.content}</a></li>\n"
        
      elsif current_level > last_level then
        
        # Moving down: so add a new list as a sub-list of
        # the current list, and record the current item
        
        toc << "#{indent(last_level)}<ul>\n"
        toc << "#{indent(current_level)}<li><a href=\"##{header_id(tree.content.content)}\">#{tree.content.content}</a></li>\n"
        
      else
        # This header is at the same level, so just output the
        # list item
        toc << "#{indent(current_level)}<li><a href=\"##{header_id(tree.content.content)}\">#{tree.content.content}</a></li>\n"
      
      end
      
      # Record the current level
      last_level = current_level
    end
  }
  
  # Close off the lists
  current_level.downto(3){|index|
    toc << "#{indent(index)}</ul>\n"
  }
      
  return toc
end

#generateObject

Return the AST as an HTML string (stored in @html_string)



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bayeux/html_gen.rb', line 16

def generate
  
  # Set-up the logger
  @html_gen_log = Logger.new('bayex_html_gen')
  @html_gen_log.outputters = Outputter.stdout
  
  # Create a walker for the trees in the forest
  walker = TreeWalker.new
  
  walker.on_before_down = self.method(:generate_node_start)
  walker.on_after_up = self.method(:generate_node_end)
  
  walker.on_no_siblings = self.method(:generate_full_node)
  walker.on_no_children = self.method(:generate_full_node)
      
  # Clear the internal HTML representation
  @html_string.clear
  
  # Walk the forest
  @syntax_tree.block_forest.each{|tree|
    walker.walk_tree(tree)
  }

  #@html_gen_log.debug {@syntax_tree.to_s}
  #@html_gen_log.info {@html_string}
  
  # Remove extra spaces between entities and tags
  @html_string.gsub!(/;[\s]+</,';<')
  
  # Return the generated string to the caller
  tidy = TidyFFI::Tidy.new(@html_string)

  tidy.options.fix_uri = 1
  tidy.options.indent = 1
  tidy.options.join_classes = 1
  tidy.options.markup = 1
  tidy.options.output_html = 1
  tidy.options.punctuation_wrap = 1
  tidy.options.show_body_only = 1  
      
  return tidy.clean  
end

#generate_full_node(block) ⇒ Object

Output the full contents of the node, properly bracketed as an HTML expression. This is only called if we have no sub-nodes to deal with



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/bayeux/html_gen.rb', line 150

def generate_full_node(block)
  
  # Check if we need to add a space
  # Check if we need to add a space
  if not block.content[0] =~ /\s|[:;.,?!]/
    @html_string << " "
  end
              
  case block.type
    
    # Headers
    when :h1
      @html_string << "<h1 id=\"#{header_id(block.content)}\">#{block.content}</h1>"
    when :h2
      @html_string << "<h2 id=\"#{header_id(block.content)}\">#{block.content}</h2>"
    when :h3
      @html_string << "<h3 id=\"#{header_id(block.content)}\">#{block.content}</h3>"
    when :h4
      @html_string << "<h4 id=\"#{header_id(block.content)}\">#{block.content}</h4>"
    when :h5
      @html_string << "<h5 id=\"#{header_id(block.content)}\">#{block.content}</h5>"
    when :h5
      @html_string << "<h6 id=\"#{header_id(block.content)}\">#{block.content}</h6>"
    
    # Ordinary paragraphs
    when :paragraph
      @html_string << "<p>#{typeset(block.content)}</p>"

    when :none
      @html_string << "#{typeset(block.content)}"
      
    # Special paragraphs
    when :block_quote
      @html_string << "<blockquote>#{typeset(block.content)}</blockquote>"  
    when :single_quote
      @html_string << "&lsquo;&#8202;#{typeset(block.content)}&#8202;&rsquo;"
    when :double_quote
      @html_string << "&ldquo;&#8202;#{typeset(block.content)}&#8202;&rdquo;"
      
    when :note
      @html_string << "<p class=\"note\"><span class=\"note_header\">Note:</span>#{typeset(block.content)}</span></p>"
      
    when :command
      @html_string << "<p class=\"command\">#{block.content}</span>"
      
    when :code_language
      @code_language = block.content
    when :code_start_number
      @code_start_number = block.content  
      
    when :file
      @html_string << "<pre class=\"file\">#{block.content}</pre>"
    when :output
      @html_string << "<pre class=\"output\">#{block.content}</pre>"
      
    # Special Characters
    when :em_dash
      @html_string << "&mdash;"
    when :en_dash
      @html_string << "&ndash;"
    when :elipses
      @html_string << "&#8202;.&thinsp;&#8202;.&thinsp;&#8202;.&thinsp;"
    when :elipses_stop
      @html_string << "&#8202;.&thinsp;&#8202;.&thinsp;&#8202;.&thinsp;."
      
    # Links
    when :link_target
      @link_target = block.content
    when :link_text
      @link_text = block.content
      
    # Lists
    when :item
      @html_string << "<li>#{typeset(block.content)}</li>"
    
    when :dl_header
      @html_string << "<dt>#{block.content}</dt>"
    when :dl_text
      @html_string << "<dd>#{block.content}</dd>"
            
    # Tags
    when :ac
      # Do we know anything about this acronymn?
      if $reference.acronym_list.include?(block.content) then
        
        # Assemble from the reference list        
        if $reference.acronym_list[block.content].include?('text_html') then
          acronym = $reference.acronym_list[block.content]['text_html']
        else
          acronym = $reference.acronym_list[block.content]['text']
        end
        
        definition = $reference.acronym_list[block.content]['def']
        
        @html_string << "<acronym title=\"#{definition}\">#{acronym}</acronym>"
      else
        # Do what we can
        @html_string << "<acronym>#{block.content}</acronym>"
      end
          
    when :emph 
      @html_string << "<em>#{block.content}</em>"
    
    when :tt
      @html_string << "<tt>#{block.content}</tt>"
      
  end
end

#generate_node_end(block) ⇒ Object



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/bayeux/html_gen.rb', line 369

def generate_node_end(block)
      
  case block.type
    
    # Headers
    when :h1
      @html_string << "</h1>"
    when :h2
      @html_string << "</h2>"
    when :h3
      @html_string << "</h3>"
    when :h4
      @html_string << "</h4>"
    when :h5
      @html_string << "</h5>"
    when :h6
      @html_string << "</h6>"
    
    # Ordinary paragraphs
    when :paragraph
      @html_string << "</p>"
    
    # Special paragraphs
    when :block_quote
      @html_string << "</blockquote>"
    when :single_quote
      @html_string << "&#8202;&rsquo;"
    when :double_quote
      @html_string << "&#8202;&rdquo;"
      
    when :note, :command
      @html_string << "</p>"
      
    when :code
      # Assemble the code block from the sub-tree nodes
      begin
        pretty_code = Uv.parse(block.content, "xhtml", @code_language, @code_start_number.to_i, "dawn")
        @html_string << pretty_code
      rescue
        pretty_code = Uv.parse(block.content, "xhtml", "plain_text", @code_start_number.to_i, "dawn")
        @html_string << pretty_code          
      end
              
    # Special Characters
    when :em_dash
      @html_string << "&mdash;"
    when :en_dash
      @html_string << "&ndash;"
    when :elipses
      @html_string << "&#8202;.&thinsp;&#8202;.&thinsp;&#8202;.&thinsp;"
    when :elipses_stop
      @html_string << "&#8202;.&thinsp;&#8202;.&thinsp;&#8202;.&thinsp;."
      
    # Links
    when :link
      # Assemble the link from the sub-tree nodes
      @html_string << "<a href=\"#{@link_target}\">#{@link_text}</a>"
    when :link_target
      @link_target = block.content
    when :link_text
      @link_text = block.content
        
    # Lists
    when :ol
      @html_string << "</ol>" 
    when :ul
      @html_string << "</ul>" 
    when :item
      @html_string << "</li>"
    
    when :dl
      @html_string << "</dl>" 
    when :dl_header
      @html_string << "</dt>"
    when :dl_text
      @html_string << "</dd>"
    
    # Tags
    when :ac
      @html_string << "</acronym>"
          
    when :emph 
      @html_string << "</em>"
    
    when :tt
      @html_string << "</tt>"
  end
end

#generate_node_start(block) ⇒ Object

Output only the start of a node



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/bayeux/html_gen.rb', line 260

def generate_node_start(block)
  
  # Check if we need to add a space
  if not block.content[0] =~ /\s|[:;.,?!]/
    @html_string << " "
  end
  
  case block.type
    
    # Headers
    when :h1
      @html_string << "<h1 id=\"#{header_id(block.content)}\">"
    when :h2
      @html_string << "<h2 id=\"#{header_id(block.content)}\">"
    when :h3
      @html_string << "<h3 id=\"#{header_id(block.content)}\">"
    when :h4
      @html_string << "<h4 id=\"#{header_id(block.content)}\">"
    when :h5
      @html_string << "<h5 id=\"#{header_id(block.content)}\">"
    when :h5
      @html_string << "<h6 id=\"#{header_id(block.content)}\">"
    
    # Ordinary paragraphs
    when :paragraph
      @html_string << "<p>#{typeset(block.content)}"
      
    when :none
      @html_string << "#{block.content}"
      
    # Special paragraphs
    when :block_quote
      @html_string << "<blockquote>#{typeset(block.content)}"
    when :single_quote
      @html_string << "&lsquo;&#8202;#{typeset(block.content)}"
    when :double_quote
      @html_string << "&ldquo;&#8202;#{typeset(block.content)}"
      
    when :note
      @html_string << "<p class=\"note\"><span class=\"note_header\">Note:</span>#{typeset(block.content)}"
      
    when :command
      @html_string << "<p class=\"command\">#{typeset(block.content)}"
      
    when :code_language
      @code_language = block.content
    when :code_start_number
      @code_start_number = block.content
      
    # Special Characters
    when :em_dash
      @html_string << "&mdash;"
    when :en_dash
      @html_string << "&ndash;"
    when :elipses
      @html_string << "&#8202;.&thinsp;&#8202;.&thinsp;&#8202;.&thinsp;"
    when :elipses_stop
      @html_string << "&#8202;.&thinsp;&#8202;.&thinsp;&#8202;.&thinsp;."
      
    # Links
    when :link_target
      @link_target = block.content
    when :link_text
      @link_text = block.content
      
    # Lists
    when :ol
      @html_string << "<ol>" 
    when :ul
      @html_string << "<ul>" 
    when :item
      @html_string << "<li>#{block.content}"
    
    when :dl
      @html_string << "<dl>" 
    when :dl_header
      @html_string << "<dt>#{block.content}"
    when :dl_text
      @html_string << "<dd>#{block.content}"
    
    # Tags        
    when :ac
      # Do we know anything about this acronymn?
      if $reference.acronym_list.include?(block.content) then
      
        # Assemble from the reference list        
        if $reference.acronym_list[block.content].include?('text_html') then
          acronym = $reference.acronym_list[block.content]['text_html']
        else
          acronym = $reference.acronym_list[block.content]['text']
        end
      
        definition = $reference.acronym_list[block.content]['def']
      
        @html_string << "<acronym title=\"#{definition}\">#{acronym}"
      else
        # Do what we can
        @html_string << "<acronym>#{block.content}"
      end
              
    when :emph 
      @html_string << "<em>#{block.content}"
      
    when :tt
      @html_string << "<tt>#{block.content}"
      
  end
end

#indent(level) ⇒ Object

Return the correct indent for the given level



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/bayeux/html_gen.rb', line 487

def indent(level)
  case level
    when 1
      return ""
    when 2
      return "  "
    when 3
      return "    "
    when 4
      return "      "
    when 5
      return "        "
    when 6
      return "          "
  else
    return ""
  end
end

#typeset(string) ⇒ Object

Fix Typography, according to HTML standards



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/bayeux/html_gen.rb', line 468

def typeset(string)
  return_str = string
      
  return_str.gsub!(';','&#8202;:')
  return_str.gsub!(':','&#8202;:')
  
  return_str.gsub!("'", "&rsquo;")

  return_str.gsub!('.','&#8202;.&thinsp;')
  return_str.gsub!('?','&#8202;?&thinsp;')
  return_str.gsub!('!','&#8202;!&thinsp;')

  return_str.gsub!('e&#8202;.&thinsp;g&#8202;.&thinsp;','e.g. ')
  return_str.gsub!('i&#8202;.&thinsp;e&#8202;.&thinsp;','i.e. ')
  
  return return_str
end