Class: DocbookRenderer
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(element, text) ⇒ Object
181
182
183
184
|
# File 'lib/notroff/docbook_renderer.rb', line 181
def add_body_text( element, text )
tokens = tokenize_body_text( text )
tokens.each {|token| add_span( token, element ) }
end
|
#add_content_element(el) ⇒ Object
103
104
105
106
107
108
109
|
# File 'lib/notroff/docbook_renderer.rb', line 103
def add_content_element(el)
if @section
@section.add_element(el)
else
current_chapter.add_element(el)
end
end
|
#add_span(token, element) ⇒ Object
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/notroff/docbook_renderer.rb', line 186
def add_span( token, element )
Logger.log "Add span: token: #{token} element: #{element}"
case token[:type]
when :italic
element.add( span_for( token.string, "emphasis" ))
when :code
element.add( span_for( token.string, "code" ))
when :bold
element.add( span_for( token.string, "emphasis" ))
when :normal
element.add_text( token.string )
when :footnote
element.add((token.string))
when :link
element.add(link_element_for(token.string))
else
raise "Dont know what to do with #{token}"
end
end
|
#code_element(type, text) ⇒ Object
221
222
223
224
225
226
227
|
# File 'lib/notroff/docbook_renderer.rb', line 221
def code_element(type, text)
element = Element.new('informalexample')
prog_element = element_for('programlisting', text)
prog_element.add_attribute('xml:space', 'preserve')
element.add_element(prog_element)
element
end
|
#current_chapter ⇒ Object
33
34
35
|
# File 'lib/notroff/docbook_renderer.rb', line 33
def current_chapter
@chapter || new_chapter
end
|
#element_for(type, text) ⇒ Object
44
45
46
47
48
|
# File 'lib/notroff/docbook_renderer.rb', line 44
def element_for(type, text)
result = Element.new(type)
result.add_text(text)
result
end
|
215
216
217
218
219
|
# File 'lib/notroff/docbook_renderer.rb', line 215
def ( text )
fn = Element.new('footnote')
fn.add_element(element_for('para', text))
fn
end
|
50
51
52
53
54
55
56
57
58
59
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
|
# File 'lib/notroff/docbook_renderer.rb', line 50
def format( p )
type = p[:type]
text = p.string
return nil if text.empty? and :type != :code
case type
when :title
@title = text
when :author
@author = text
when :chapter
Logger.log "adding chapter #{text}"
new_chapter
title_element = Element.new('title')
add_body_text(title_element, text)
@chapter.add_element(title_element)
when :section
Logger.log "adding section #{text}"
@section = Element.new('section')
current_chapter.add(@section)
title_element = Element.new('title')
add_body_text(title_element, text)
@section.add_element(title_element)
when :body
Logger.log "adding body #{text[0..5]}"
paragraph = Element.new('para')
add_body_text(paragraph, text)
add_content_element(paragraph)
when :quote
Logger.log "adding blockquote #{text[0..9]}"
quote = Element.new('blockquote')
paragraph = Element.new('para')
add_body_text(paragraph, text)
quote.add(paragraph)
add_content_element(quote)
when :code
add_content_element(code_element(type, text))
when :group
add_content_element(group_element(p))
else
raise "Dont know what to do with #{type}"
end
end
|
#group_element(para) ⇒ Object
<itemizedlist mark=‘opencircle’>
<listitem>
6 <para>TeX and LaTeX
</para>
8 </listitem>
<listitem override='bullet'>
10 <para>Troff
</para>
12 </listitem>
<listitem>
14 <para>Lout
</para>
16 </listitem>
</itemizedlist>
126
127
128
129
130
131
132
|
# File 'lib/notroff/docbook_renderer.rb', line 126
def group_element(para)
group_type = para[:kid_type]
kids = para[:kids]
list_element = list_element_for(group_type)
kids.each {|k| list_element.add(list_item_element_for(k))}
list_element
end
|
#link_element_for(link_token) ⇒ Object
206
207
208
209
210
211
212
|
# File 'lib/notroff/docbook_renderer.rb', line 206
def link_element_for(link_token)
text, url = parse_link(link_token)
link = Element.new('link')
add_body_text(link, text)
link.add_attribute('xl:href', url)
link
end
|
#list_element_for(type) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/notroff/docbook_renderer.rb', line 134
def list_element_for(type)
el = nil
if type == :bullet
el = Element.new('itemizedlist')
el.add_attribute('mark', 'opencircle')
elsif type == :list
el = Element.new('orderedlist')
el.add_attribute('numeration', 'arabic')
else
raise "Don't know what to do with list type #{type}"
end
el
end
|
#list_item_element_for(para) ⇒ Object
148
149
150
151
152
|
# File 'lib/notroff/docbook_renderer.rb', line 148
def list_item_element_for(para)
el = Element.new('listitem')
add_body_text(Element.new('para', el), para.string)
el
end
|
#new_chapter ⇒ Object
37
38
39
40
41
42
|
# File 'lib/notroff/docbook_renderer.rb', line 37
def new_chapter
@chapter = Element.new('chapter')
@chapters << @chapter
@section = nil
@chapter
end
|
#process(paragraphs) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/notroff/docbook_renderer.rb', line 8
def process( paragraphs )
@author = 'John Smith'
@title = 'Wealth of Nations'
@chapters = []
@chapter = nil
@section = nil
paragraphs.each do |paragraph|
format( paragraph )
end
@book = Element.new('book')
@book.add_namespace( 'http://docbook.org/ns/docbook')
@book.add_namespace('xl', 'http://www.w3.org/1999/xlink')
@book.add_attribute('version', '5.0')
@book.add_element element_for('title', @title)
@book.add_element element_for('author', @author)
@chapters.each {|ch| @book << ch}
doc = Document.new
decl = XMLDecl.new
decl.version = '1.0'
doc << decl
doc << @book
doc
end
|
#span_for(text, style) ⇒ Object
229
230
231
232
233
|
# File 'lib/notroff/docbook_renderer.rb', line 229
def span_for( text, style )
span = Element.new(style)
span.text = remove_escapes(text)
span
end
|
#tag_for(type) ⇒ Object
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/notroff/docbook_renderer.rb', line 160
def tag_for(type)
case type
when :body
'para'
when :text
'p'
when :author
'h3'
when :section
'h3'
when :chapter
'chapter'
when :sec
'h3'
when :title
'h2'
else
raise "Dont know what to do with #{type}"
end
end
|
#text_element(type, text) ⇒ Object
154
155
156
157
158
|
# File 'lib/notroff/docbook_renderer.rb', line 154
def text_element(type, text)
element = [ tag_for(type) ]
add_body_text(element, text)
element
end
|