Class: OdtRenderer
Constant Summary
collapse
- LONG_DASH_CODE =
0xe1.chr + 0x80.chr + 0x93.chr
- PARAGRAPH_STYLES =
{
:body => 'FT',
:body2 => 'IT',
:title => 'HA',
:subtitle => 'HB',
:section => 'HC',
:sec => 'HC',
:subsec => 'HD',
:first_code => 'CDT1',
:middle_code => 'CDT',
:end_code => 'CDTX',
:author => 'AU',
:quote => 'Quotation',
:attribution => 'Quotation Attribution',
:single_code => 'C1',
:ltitle => 'LH',
:first_listing => 'LC',
:middle_listing => 'LC2',
:end_listing => 'LX',
:pn => 'PN',
:pt => 'PT',
:cn => 'HA',
:chapter => 'HA',
:ct => 'HB',
:fc => 'FC'}
1
Instance Method Summary
collapse
Methods included from Tokenize
#parse_link, #token_text, #token_type, #tokenize_body_text
Instance Method Details
#add_body_text(text, element) ⇒ Object
165
166
167
168
|
# File 'lib/notroff/odt_renderer.rb', line 165
def add_body_text( text, element )
tokens = tokenize_body_text( text )
tokens.each {|token| add_span( token, element ) }
end
|
#add_code_text(text, element) ⇒ Object
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/notroff/odt_renderer.rb', line 170
def add_code_text( text, element )
text = text.dup
re = /\S+|\s+/
until text.empty?
chunk = text.slice!( re )
if chunk !~ /^ /
element.add_text( chunk.string )
else
space_element = Element.new( 'text:s' )
space_element.attributes['text:c'] = chunk.size.to_s
element.add( space_element )
end
end
end
|
#add_span(token, element) ⇒ Object
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/notroff/odt_renderer.rb', line 185
def add_span( token, element )
case token[:type]
when :italic
element.add( span_for( token.string, "T1" ))
when :code
element.add( span_for( token.string, "CD1" ))
when :bold
element.add( span_for( token.string, "T2" ))
when :normal
element.add_text( token.string )
when :footnote
element.add( ( token.string ) )
when :link
text, url = parse_link(token)
add_body_text(text, element)
element.add_text( " (#{url}) " )
else
raise "Dont know what to do with #{token}"
end
end
|
#bullet_item_style_for(items, i) ⇒ Object
153
154
155
156
157
|
# File 'lib/notroff/odt_renderer.rb', line 153
def bullet_item_style_for(items, i)
return 'List_20_1_20_Start' if i == 0
return 'List_20_1_20_End' if i == (items.size-1)
'List_20_1_20_Cont.'
end
|
#bullet_type?(para) ⇒ Boolean
99
100
101
|
# File 'lib/notroff/odt_renderer.rb', line 99
def bullet_type?(para)
group?(para) and para[:kid_type] == :bullet
end
|
#code_type?(type) ⇒ Boolean
103
104
105
106
|
# File 'lib/notroff/odt_renderer.rb', line 103
def code_type?( type )
[ :first_code, :middle_code, :end_code, :single_code,
:listing, :first_listing, :middle_listing, :end_listing ].include?(type)
end
|
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/notroff/odt_renderer.rb', line 234
def (text )
note_element = Element.new( "text:note" )
note_element.attributes["text:id"] ="ftn#{@@footnote_number}"
note_element.attributes["text:note-class"] ="footnote"
cit = Element.new( "text:note-citation" )
cit.add_text( "#{@@footnote_number}" )
note_element.add( cit )
note_body = Element.new( "text:note-body" )
note_paragraph = Element.new( "text:p" )
note_paragraph.attributes['text:style-name'] = 'FTN'
add_body_text(text, note_paragraph)
note_body.add( note_paragraph )
note_element.add( note_body )
@@footnote_number += 1
note_element
end
|
48
49
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
|
# File 'lib/notroff/odt_renderer.rb', line 48
def format( para )
Logger.log "Format: #{para.inspect}"
type = para[:type]
text = para
return nil if text.empty? and ! code_type?( type )
result = nil
if [:body, :body2].include?(type)
Logger.log("Rendering bodyish para of type ", type )
result = new_text_element( type )
add_body_text( text, result )
elsif quote_type?(para)
result = new_quote_element(para[:kid_type], para[:kids])
elsif list_type?(para)
result = new_list_element(para[:kids])
elsif bullet_type?(para)
result = new_bullet_element(para[:kids])
elsif code_type?(type)
result = new_text_element( type )
add_code_text( text, result )
elsif PARAGRAPH_STYLES[type]
Logger.log("Rendering simple para of type ", type )
result = new_text_element( type )
result.add_text( text.string )
else
raise "Dont know what to do with type [#{type}]"
end
result
end
|
#group?(para) ⇒ Boolean
87
88
89
|
# File 'lib/notroff/odt_renderer.rb', line 87
def group?(para)
para[:type] == :group
end
|
#list_type?(para) ⇒ Boolean
95
96
97
|
# File 'lib/notroff/odt_renderer.rb', line 95
def list_type?(para)
group?(para) and para[:kid_type] == :list
end
|
#new_bullet_element(items) ⇒ Object
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/notroff/odt_renderer.rb', line 139
def new_bullet_element(items)
list = Element.new('text:list')
list.attributes['text:style-name'] = 'L1'
items.each_with_index do |item, i|
list_item_element = Element.new('text:list-item')
text_element = Element.new('text:p')
text_element.attributes['text:style-name'] = bullet_item_style_for(items, i)
add_body_text(item.string, text_element)
list_item_element.add(text_element)
list.add(list_item_element)
end
list
end
|
#new_list_element(items) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/notroff/odt_renderer.rb', line 119
def new_list_element(items)
list = Element.new('text:list')
list.attributes['text:style-name'] = 'L2'
items.each_with_index do |item, i|
list_item_element = Element.new('text:list-item')
text_element = Element.new('text:p')
text_element.attributes['text:style-name'] = numbered_item_style_for(items, i)
add_body_text(item.string, text_element)
list_item_element.add(text_element)
list.add(list_item_element)
end
list
end
|
#new_quote_element(type, items) ⇒ Object
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/notroff/odt_renderer.rb', line 108
def new_quote_element(type, items)
p = new_text_element(type)
items.each_with_index do |item, i|
p.add( Element.new('text:line-break')) unless i == 0
el = Element.new('text:span')
add_body_text(item.string, el)
p.add(el)
end
p
end
|
#new_text_element(type) ⇒ Object
159
160
161
162
163
|
# File 'lib/notroff/odt_renderer.rb', line 159
def new_text_element( type )
result = Element.new( "text:p" )
result.attributes["text:style-name"] = PARAGRAPH_STYLES[type]
result
end
|
#numbered_item_style_for(items, i) ⇒ Object
133
134
135
136
137
|
# File 'lib/notroff/odt_renderer.rb', line 133
def numbered_item_style_for(items, i)
return 'P7' if i == 0
return 'P4' if i == (items.size-1)
'P3'
end
|
#process(paragraphs) ⇒ Object
39
40
41
42
43
44
45
46
|
# File 'lib/notroff/odt_renderer.rb', line 39
def process( paragraphs )
elements = []
paragraphs.each do |paragraph|
new_element = format( paragraph )
elements << new_element if new_element
end
{:body => elements}
end
|
#quote_type?(para) ⇒ Boolean
91
92
93
|
# File 'lib/notroff/odt_renderer.rb', line 91
def quote_type?(para)
group?(para) and [:quote, :attribution].include?(para[:kid_type])
end
|
#remove_escapes(text) ⇒ Object
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
# File 'lib/notroff/odt_renderer.rb', line 213
def remove_escapes( text )
text = text.clone
results = ''
until text.empty?
match = /\\(.)/.match( text )
if match.nil?
results << text
text = ''
else
unless match.pre_match.empty?
results << match.pre_match
end
results << match[1]
text = match.post_match
end
end
results
end
|
#span_for(text, style) ⇒ Object
206
207
208
209
210
211
|
# File 'lib/notroff/odt_renderer.rb', line 206
def span_for( text, style )
span = Element.new( "text:span" )
span.attributes['text:style-name'] = style
span.text = remove_escapes(text)
span
end
|