Class: HtmlRenderer

Inherits:
Processor show all
Includes:
REXML, Tokenize
Defined in:
lib/notroff/html_renderer.rb

Instance Method Summary collapse

Methods included from Tokenize

#parse_link, #remove_escapes, #token_text, #token_type, #tokenize_body_text

Instance Method Details

#add_body_text(text, element) ⇒ Object



75
76
77
78
# File 'lib/notroff/html_renderer.rb', line 75

def add_body_text( text, element )
  tokens = tokenize_body_text( text )
  tokens.each {|token| add_span( token, element ) }
end

#add_span(token, element) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/notroff/html_renderer.rb', line 80

def add_span( token, element )
  case token[:type]
  when :italic
    element.add(span_for(token.string, "em"))
  when :code
    element.add(span_for(token.string, "code"))
  when :bold
    element.add(span_for(token.string, "b"))
  when :normal
    element.add_text(token.string)
  when :footnote
    add_body_text(" [#{token.string}] ", element)
  when :link
    element.add(anchor_element_for(token))
  else
    raise "Dont know what to do with type #{token[:type]} - #{token}"
  end
end

#anchor_element_for(link_token) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/notroff/html_renderer.rb', line 99

def anchor_element_for(link_token)
  text, url = parse_link(link_token)
  anchor = Element.new('a')
  add_body_text(text, anchor)
  anchor.add_attribute('href', url)
  anchor
end

#code_element(type, text) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/notroff/html_renderer.rb', line 107

def code_element(type, text)
  element = Element.new('code')
  pre_element = Element.new('pre')
  element.add pre_element
  pre_element.add_text text
  element
end

#format(p) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/notroff/html_renderer.rb', line 17

def format( p )
  type = p[:type]
  text = p.string

  return nil if text.empty? and :type != :code

  if type == :code
    code_element(type, text)
  elsif type == :group and p[:kid_type] == :list
    list_element('ol', p[:kids])
  elsif type == :group and p[:kid_type] == :bullet
    list_element('ul', p[:kids])
  else
    text_element(type, text)
  end
end

#list_element(type, paras) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/notroff/html_renderer.rb', line 34

def list_element(type, paras)
  list = Element.new(type)
  paras.each do |para|
    item = Element.new('li')
    add_body_text(para.string, item)
    list.add(item)
  end
  list
end

#process(paragraphs) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/notroff/html_renderer.rb', line 8

def process( paragraphs )
  body = Element.new('body')
  paragraphs.each do |paragraph|
    new_element = format( paragraph )
    body.add new_element if new_element
  end
  body
end

#span_for(text, style) ⇒ Object



115
116
117
118
119
# File 'lib/notroff/html_renderer.rb', line 115

def span_for( text, style )
  span = Element.new( style )
  span.text = remove_escapes(text)
  span
end

#tag_for(type) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/notroff/html_renderer.rb', line 52

def tag_for(type)
  case type
  when :body
    'p'
  when :text
    'p'
  when :author
    'h3'
  when :section
    'h3'
  when :sec
    'h3'
  when :chapter
    'h2'
  when :quote
    'blockquote'
  when :title
    'h1'
  else
      raise "Dont know what to do with #{type}"
  end
end

#text_element(type, text) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/notroff/html_renderer.rb', line 44

def text_element(type, text)
  puts "*** text element: #{type}"
  puts "*** text #{text}"
  element = Element.new(tag_for(type))
  add_body_text(text, element)
  element
end