Debian Codegraph @

src/odf/odf2xhtml.py

raw
1 #!/usr/bin/env python
2 # Copyright (C) 2006-2010 Søren Roug, European Environment Agency
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 #
18 # Contributor(s):
19 #
20 # import pdb
21 # pdb.set_trace()
22
23 from collections import defaultdict
24 from xml.dom import Node
25 from xml.sax import handler
26 from xml.sax.saxutils import escape, quoteattr
27
28 from .namespaces import (
29 ANIMNS,
30 CHARTNS,
31 CONFIGNS,
32 DCNS,
33 DR3DNS,
34 DRAWNS,
35 FONS,
36 FORMNS,
37 MATHNS,
38 METANS,
39 NUMBERNS,
40 OFFICENS,
41 PRESENTATIONNS,
42 SCRIPTNS,
43 SMILNS,
44 STYLENS,
45 SVGNS,
46 TABLENS,
47 TEXTNS,
48 XLINKNS,
49 )
50 from .opendocument import load
51
52 if False: # Added by Kovid
53 DR3DNS, MATHNS, CHARTNS, CONFIGNS, ANIMNS, FORMNS, SMILNS, SCRIPTNS
54
55 # Handling of styles
56 #
57 # First there are font face declarations. These set up a font style that will be
58 # referenced from a text-property. The declaration describes the font making
59 # it possible for the application to find a similar font should the system not
60 # have that particular one. The StyleToCSS stores these attributes to be used
61 # for the CSS2 font declaration.
62 #
63 # Then there are default-styles. These set defaults for various style types:
64 # "text", "paragraph", "section", "ruby", "table", "table-column", "table-row",
65 # "table-cell", "graphic", "presentation", "drawing-page", "chart".
66 # Since CSS2 can't refer to another style, ODF2XHTML add these to all
67 # styles unless overridden.
68 #
69 # The real styles are declared in the <style:style> element. They have a
70 # family referring to the default-styles, and may have a parent style.
71 #
72 # Styles have scope. The same name can be used for both paragraph and
73 # character etc. styles Since CSS2 has no scope we use a prefix. (Not elegant)
74 # In ODF a style can have a parent, these parents can be chained.
75
76
77 class StyleToCSS:
78 """The purpose of the StyleToCSS class is to contain the rules to convert
79 ODF styles to CSS2. Since it needs the generic fonts, it would probably
80 make sense to also contain the Styles in a dict as well..
81 """
82
83 def __init__(self):
84 # Font declarations
85 self.fontdict = {}
86
87 # Fill-images from presentations for backgrounds
88 self.fillimages = {}
89
90 self.ruleconversions = {
91 (DRAWNS, 'fill-image-name'): self.c_drawfillimage,
92 (FONS, 'background-color'): self.c_fo,
93 (FONS, 'border'): self.c_fo,
94 (FONS, 'border-bottom'): self.c_fo,
95 (FONS, 'border-left'): self.c_fo,
96 (FONS, 'border-right'): self.c_fo,
97 (FONS, 'border-top'): self.c_fo,
98 (FONS, 'break-after'): self.c_break, # Added by Kovid
99 (FONS, 'break-before'): self.c_break, # Added by Kovid
100 (FONS, 'color'): self.c_fo,
101 (FONS, 'font-family'): self.c_fo,
102 (FONS, 'font-size'): self.c_fo,
103 (FONS, 'font-style'): self.c_fo,
104 (FONS, 'font-variant'): self.c_fo,
105 (FONS, 'font-weight'): self.c_fo,
106 (FONS, 'line-height'): self.c_fo,
107 (FONS, 'margin'): self.c_fo,
108 (FONS, 'margin-bottom'): self.c_fo,
109 (FONS, 'margin-left'): self.c_fo,
110 (FONS, 'margin-right'): self.c_fo,
111 (FONS, 'margin-top'): self.c_fo,
112 (FONS, 'min-height'): self.c_fo,
113 (FONS, 'padding'): self.c_fo,
114 (FONS, 'padding-bottom'): self.c_fo,
115 (FONS, 'padding-left'): self.c_fo,
116 (FONS, 'padding-right'): self.c_fo,
117 (FONS, 'padding-top'): self.c_fo,
118 (FONS, 'page-width'): self.c_page_width,
119 (FONS, 'page-height'): self.c_page_height,
120 (FONS, 'text-align'): self.c_text_align,
121 (FONS, 'text-indent'): self.c_fo,
122 (TABLENS, 'border-model'): self.c_border_model,
123 (STYLENS, 'column-width'): self.c_width,
124 (STYLENS, 'font-name'): self.c_fn,
125 (STYLENS, 'horizontal-pos'): self.c_hp,
126 (STYLENS, 'text-position'): self.c_text_position,
127 (STYLENS, 'text-line-through-style'): self.c_text_line_through_style,
128 (STYLENS, 'text-underline-style'): self.c_text_underline_style,
129 (STYLENS, 'width'): self.c_width,
130 # FIXME Should do style:vertical-pos here
131 }
132
133 def save_font(self, name, family, generic):
134 """It is possible that the HTML browser doesn't know how to
135 show a particular font. Fortunately ODF provides generic fallbacks.
136 Unfortunately they are not the same as CSS2.
137 CSS2: serif, sans-serif, cursive, fantasy, monospace
138 ODF: roman, swiss, modern, decorative, script, system
139 This method put the font and fallback into a dictionary
140 """
141 htmlgeneric = 'sans-serif'
142 if generic == 'roman':
143 htmlgeneric = 'serif'
144 elif generic == 'swiss':
145 htmlgeneric = 'sans-serif'
146 elif generic == 'modern':
147 htmlgeneric = 'monospace'
148 elif generic == 'decorative':
149 htmlgeneric = 'sans-serif'
150 elif generic == 'script':
151 htmlgeneric = 'monospace'
152 elif generic == 'system':
153 htmlgeneric = 'serif'
154 self.fontdict[name] = (family, htmlgeneric)
155
156 def c_drawfillimage(self, ruleset, sdict, rule, val):
157 """Fill a figure with an image. Since CSS doesn't let you resize images
158 this should really be implemented as an absolutely position <img>
159 with a width and a height
160 """
161 sdict['background-image'] = f"url('{self.fillimages[val]}')"
162
163 def c_fo(self, ruleset, sdict, rule, val):
164 """XSL formatting attributes"""
165 selector = rule[1]
166 sdict[selector] = val
167
168 def c_break(self, ruleset, sdict, rule, val): # Added by Kovid
169 property = 'page-' + rule[1]
170 values = {
171 'auto': 'auto',
172 'column': 'always',
173 'page': 'always',
174 'even-page': 'left',
175 'odd-page': 'right',
176 'inherit': 'inherit',
177 }
178 sdict[property] = values.get(val, 'auto')
179
180 def c_border_model(self, ruleset, sdict, rule, val):
181 """Convert to CSS2 border model"""
182 if val == 'collapsing':
183 sdict['border-collapse'] = 'collapse'
184 else:
185 sdict['border-collapse'] = 'separate'
186
187 def c_width(self, ruleset, sdict, rule, val):
188 """Set width of box"""
189 sdict['width'] = val
190
191 def c_text_align(self, ruleset, sdict, rule, align):
192 """Text align"""
193 if align == 'start':
194 align = 'left'
195 if align == 'end':
196 align = 'right'
197 sdict['text-align'] = align
198
199 def c_fn(self, ruleset, sdict, rule, fontstyle):
200 """Generate the CSS font family
201 A generic font can be found in two ways. In a <style:font-face>
202 element or as a font-family-generic attribute in text-properties.
203 """
204 generic = ruleset.get((STYLENS, 'font-family-generic'))
205 if generic is not None:
206 self.save_font(fontstyle, fontstyle, generic)
207 family, htmlgeneric = self.fontdict.get(fontstyle, (fontstyle, 'serif'))
208 sdict['font-family'] = f'{family}, {htmlgeneric}'
209
210 def c_text_position(self, ruleset, sdict, rule, tp):
211 """Text position. This is used e.g. to make superscript and subscript
212 This attribute can have one or two values.
213
214 The first value must be present and specifies the vertical
215 text position as a percentage that relates to the current font
216 height or it takes one of the values sub or super. Negative
217 percentages or the sub value place the text below the
218 baseline. Positive percentages or the super value place
219 the text above the baseline. If sub or super is specified,
220 the application can choose an appropriate text position.
221
222 The second value is optional and specifies the font height
223 as a percentage that relates to the current font-height. If
224 this value is not specified, an appropriate font height is
225 used. Although this value may change the font height that
226 is displayed, it never changes the current font height that
227 is used for additional calculations.
228 """
229 textpos = tp.split(' ')
230 if len(textpos) == 2 and textpos[0] != '0%':
231 # Bug in OpenOffice. If vertical-align is 0% - ignore the text size.
232 sdict['font-size'] = textpos[1]
233 if textpos[0] == 'super':
234 sdict['vertical-align'] = '33%'
235 elif textpos[0] == 'sub':
236 sdict['vertical-align'] = '-33%'
237 else:
238 sdict['vertical-align'] = textpos[0]
239
240 def c_hp(self, ruleset, sdict, rule, hpos):
241 # FIXME: Frames wrap-style defaults to 'parallel', graphics to 'none'.
242 # It is properly set in the parent-styles, but the program doesn't
243 # collect the information.
244 wrap = ruleset.get((STYLENS, 'wrap'), 'parallel')
245 # Can have: from-left, left, center, right, from-inside, inside, outside
246 if hpos == 'center':
247 sdict['margin-left'] = 'auto'
248 sdict['margin-right'] = 'auto'
249 # else:
250 # # force it to be *something* then delete it
251 # sdict['margin-left'] = sdict['margin-right'] = ''
252 # del sdict['margin-left'], sdict['margin-right']
253
254 if hpos in ('right', 'outside'):
255 if wrap in ('left', 'parallel', 'dynamic'):
256 sdict['float'] = 'right'
257 elif wrap == 'run-through':
258 sdict['position'] = 'absolute' # Simulate run-through
259 sdict['top'] = '0'
260 sdict['right'] = '0'
261 else: # No wrapping
262 sdict['margin-left'] = 'auto'
263 sdict['margin-right'] = '0px'
264 elif hpos in ('left', 'inside'):
265 if wrap in ('right', 'parallel', 'dynamic'):
266 sdict['float'] = 'left'
267 elif wrap == 'run-through':
268 sdict['position'] = 'absolute' # Simulate run-through
269 sdict['top'] = '0'
270 sdict['left'] = '0'
271 else: # No wrapping
272 sdict['margin-left'] = '0px'
273 sdict['margin-right'] = 'auto'
274 elif hpos in ('from-left', 'from-inside'):
275 if wrap in ('right', 'parallel'):
276 sdict['float'] = 'left'
277 else:
278 sdict['position'] = 'relative' # No wrapping
279 if (SVGNS, 'x') in ruleset:
280 sdict['left'] = ruleset[(SVGNS, 'x')]
281
282 def c_page_width(self, ruleset, sdict, rule, val):
283 """Set width of box
284 HTML doesn't really have a page-width. It is always 100% of the browser width
285 """
286 sdict['width'] = val
287
288 def c_text_underline_style(self, ruleset, sdict, rule, val):
289 """Set underline decoration
290 HTML doesn't really have a page-width. It is always 100% of the browser width
291 """
292 if val and val != 'none':
293 sdict['text-decoration'] = 'underline'
294
295 def c_text_line_through_style(self, ruleset, sdict, rule, val):
296 """Set underline decoration
297 HTML doesn't really have a page-width. It is always 100% of the browser width
298 """
299 if val and val != 'none':
300 sdict['text-decoration'] = 'line-through'
301
302 def c_page_height(self, ruleset, sdict, rule, val):
303 """Set height of box"""
304 sdict['height'] = val
305
306 def convert_styles(self, ruleset):
307 """Rule is a tuple of (namespace, name). If the namespace is '' then
308 it is already CSS2
309 """
310 sdict = {}
311 for rule, val in ruleset.items():
312 if rule[0] == '':
313 sdict[rule[1]] = val
314 continue
315 method = self.ruleconversions.get(rule, None)
316 if method:
317 method(ruleset, sdict, rule, val)
318 return sdict
319
320
321 class TagStack:
322 def __init__(self):
323 self.stack = []
324
325 def push(self, tag, attrs):
326 self.stack.append((tag, attrs))
327
328 def pop(self):
329 item = self.stack.pop()
330 return item
331
332 def stackparent(self):
333 item = self.stack[-1]
334 return item[1]
335
336 def rfindattr(self, attr):
337 """Find a tag with the given attribute"""
338 for tag, attrs in self.stack:
339 if attr in attrs:
340 return attrs[attr]
341 return None
342
343 def count_tags(self, tag):
344 c = 0
345 for ttag, tattrs in self.stack:
346 if ttag == tag:
347 c = c + 1
348 return c
349
350
351 special_styles = {
352 'S-Emphasis': 'em',
353 'S-Citation': 'cite',
354 'S-Strong_20_Emphasis': 'strong',
355 'S-Variable': 'var',
356 'S-Definition': 'dfn',
357 'S-Teletype': 'tt',
358 'P-Heading_20_1': 'h1',
359 'P-Heading_20_2': 'h2',
360 'P-Heading_20_3': 'h3',
361 'P-Heading_20_4': 'h4',
362 'P-Heading_20_5': 'h5',
363 'P-Heading_20_6': 'h6',
364 # 'P-Caption':'caption',
365 'P-Addressee': 'address',
366 # 'P-List_20_Heading':'dt',
367 # 'P-List_20_Contents':'dd',
368 'P-Preformatted_20_Text': 'pre',
369 # 'P-Table_20_Heading':'th',
370 # 'P-Table_20_Contents':'td',
371 # 'P-Text_20_body':'p'
372 }
373
374 # -----------------------------------------------------------------------------
375 #
376 # ODFCONTENTHANDLER
377 #
378 # -----------------------------------------------------------------------------
379
380
381 class ODF2XHTML(handler.ContentHandler):
382 """The ODF2XHTML parses an ODF file and produces XHTML"""
383
384 def __init__(self, generate_css=True, embedable=False):
385 # Tags
386 self.generate_css = generate_css
387 self.frame_stack = []
388 self.list_number_map = defaultdict(lambda: 1)
389 self.list_id_map = {}
390 self.list_class_stack = []
391 self.elements = {
392 (DCNS, 'title'): (self.s_processcont, self.e_dc_title),
393 (DCNS, 'language'): (self.s_processcont, self.e_dc_contentlanguage),
394 (DCNS, 'creator'): (self.s_processcont, self.e_dc_creator),
395 (DCNS, 'description'): (self.s_processcont, self.e_dc_metatag),
396 (DCNS, 'date'): (self.s_processcont, self.e_dc_metatag),
397 (DRAWNS, 'custom-shape'): (self.s_custom_shape, self.e_custom_shape),
398 (DRAWNS, 'frame'): (self.s_draw_frame, self.e_draw_frame),
399 (DRAWNS, 'image'): (self.s_draw_image, None),
400 (DRAWNS, 'fill-image'): (self.s_draw_fill_image, None),
401 (DRAWNS, 'layer-set'): (self.s_ignorexml, None),
402 (DRAWNS, 'object'): (self.s_draw_object, None),
403 (DRAWNS, 'object-ole'): (self.s_draw_object_ole, None),
404 (DRAWNS, 'page'): (self.s_draw_page, self.e_draw_page),
405 (DRAWNS, 'text-box'): (self.s_draw_textbox, self.e_draw_textbox),
406 (METANS, 'creation-date'): (self.s_processcont, self.e_dc_metatag),
407 (METANS, 'generator'): (self.s_processcont, self.e_dc_metatag),
408 (METANS, 'initial-creator'): (self.s_processcont, self.e_dc_metatag),
409 (METANS, 'keyword'): (self.s_processcont, self.e_dc_metatag),
410 (NUMBERNS, 'boolean-style'): (self.s_ignorexml, None),
411 (NUMBERNS, 'currency-style'): (self.s_ignorexml, None),
412 (NUMBERNS, 'date-style'): (self.s_ignorexml, None),
413 (NUMBERNS, 'number-style'): (self.s_ignorexml, None),
414 (NUMBERNS, 'text-style'): (self.s_ignorexml, None),
415 (OFFICENS, 'annotation'): (self.s_ignorexml, None),
416 (OFFICENS, 'automatic-styles'): (self.s_office_automatic_styles, None),
417 (OFFICENS, 'document'): (self.s_office_document_content, self.e_office_document_content),
418 (OFFICENS, 'document-content'): (self.s_office_document_content, self.e_office_document_content),
419 (OFFICENS, 'forms'): (self.s_ignorexml, None),
420 (OFFICENS, 'master-styles'): (self.s_office_master_styles, None),
421 (OFFICENS, 'meta'): (self.s_ignorecont, None),
422 (OFFICENS, 'presentation'): (self.s_office_presentation, self.e_office_presentation),
423 (OFFICENS, 'spreadsheet'): (self.s_office_spreadsheet, self.e_office_spreadsheet),
424 (OFFICENS, 'styles'): (self.s_office_styles, None),
425 (OFFICENS, 'text'): (self.s_office_text, self.e_office_text),
426 (OFFICENS, 'scripts'): (self.s_ignorexml, None),
427 (OFFICENS, 'settings'): (self.s_ignorexml, None),
428 (PRESENTATIONNS, 'notes'): (self.s_ignorexml, None),
429 # (STYLENS, "default-page-layout"):(self.s_style_default_page_layout, self.e_style_page_layout),
430 (STYLENS, 'default-page-layout'): (self.s_ignorexml, None),
431 (STYLENS, 'default-style'): (self.s_style_default_style, self.e_style_default_style),
432 (STYLENS, 'drawing-page-properties'): (self.s_style_handle_properties, None),
433 (STYLENS, 'font-face'): (self.s_style_font_face, None),
434 # (STYLENS, "footer"):(self.s_style_footer, self.e_style_footer),
435 # (STYLENS, "footer-style"):(self.s_style_footer_style, None),
436 (STYLENS, 'graphic-properties'): (self.s_style_handle_properties, None),
437 (STYLENS, 'handout-master'): (self.s_ignorexml, None),
438 # (STYLENS, "header"):(self.s_style_header, self.e_style_header),
439 # (STYLENS, "header-footer-properties"):(self.s_style_handle_properties, None),
440 # (STYLENS, "header-style"):(self.s_style_header_style, None),
441 (STYLENS, 'master-page'): (self.s_style_master_page, None),
442 (STYLENS, 'page-layout-properties'): (self.s_style_handle_properties, None),
443 (STYLENS, 'page-layout'): (self.s_style_page_layout, self.e_style_page_layout),
444 # (STYLENS, "page-layout"):(self.s_ignorexml, None),
445 (STYLENS, 'paragraph-properties'): (self.s_style_handle_properties, None),
446 (STYLENS, 'style'): (self.s_style_style, self.e_style_style),
447 (STYLENS, 'table-cell-properties'): (self.s_style_handle_properties, None),
448 (STYLENS, 'table-column-properties'): (self.s_style_handle_properties, None),
449 (STYLENS, 'table-properties'): (self.s_style_handle_properties, None),
450 (STYLENS, 'text-properties'): (self.s_style_handle_properties, None),
451 (SVGNS, 'desc'): (self.s_ignorexml, None),
452 (TABLENS, 'covered-table-cell'): (self.s_ignorexml, None),
453 (TABLENS, 'table-cell'): (self.s_table_table_cell, self.e_table_table_cell),
454 (TABLENS, 'table-column'): (self.s_table_table_column, None),
455 (TABLENS, 'table-row'): (self.s_table_table_row, self.e_table_table_row),
456 (TABLENS, 'table'): (self.s_table_table, self.e_table_table),
457 (TEXTNS, 'a'): (self.s_text_a, self.e_text_a),
458 (TEXTNS, 'alphabetical-index-source'): (self.s_text_x_source, self.e_text_x_source),
459 (TEXTNS, 'bibliography-configuration'): (self.s_ignorexml, None),
460 (TEXTNS, 'bibliography-source'): (self.s_text_x_source, self.e_text_x_source),
461 (TEXTNS, 'bookmark'): (self.s_text_bookmark, None),
462 (TEXTNS, 'bookmark-start'): (self.s_text_bookmark, None),
463 (TEXTNS, 'reference-mark-start'): (self.s_text_bookmark, None), # Added by Kovid
464 (TEXTNS, 'bookmark-ref'): (self.s_text_bookmark_ref, self.e_text_a),
465 (TEXTNS, 'reference-ref'): (self.s_text_bookmark_ref, self.e_text_a), # Added by Kovid
466 (TEXTNS, 'bookmark-ref-start'): (self.s_text_bookmark_ref, None),
467 (TEXTNS, 'h'): (self.s_text_h, self.e_text_h),
468 (TEXTNS, 'illustration-index-source'): (self.s_text_x_source, self.e_text_x_source),
469 (TEXTNS, 'line-break'): (self.s_text_line_break, None),
470 (TEXTNS, 'linenumbering-configuration'): (self.s_ignorexml, None),
471 (TEXTNS, 'list'): (self.s_text_list, self.e_text_list),
472 (TEXTNS, 'list-item'): (self.s_text_list_item, self.e_text_list_item),
473 (TEXTNS, 'list-level-style-bullet'): (
474 self.s_text_list_level_style_bullet,
475 self.e_text_list_level_style_bullet,
476 ),
477 (TEXTNS, 'list-level-style-number'): (
478 self.s_text_list_level_style_number,
479 self.e_text_list_level_style_number,
480 ),
481 (TEXTNS, 'list-style'): (None, None),
482 (TEXTNS, 'note'): (self.s_text_note, None),
483 (TEXTNS, 'note-body'): (self.s_text_note_body, self.e_text_note_body),
484 (TEXTNS, 'note-citation'): (None, self.e_text_note_citation),
485 (TEXTNS, 'notes-configuration'): (self.s_ignorexml, None),
486 (TEXTNS, 'object-index-source'): (self.s_text_x_source, self.e_text_x_source),
487 (TEXTNS, 'p'): (self.s_text_p, self.e_text_p),
488 (TEXTNS, 's'): (self.s_text_s, None),
489 (TEXTNS, 'span'): (self.s_text_span, self.e_text_span),
490 (TEXTNS, 'tab'): (self.s_text_tab, None),
491 (TEXTNS, 'table-index-source'): (self.s_text_x_source, self.e_text_x_source),
492 (TEXTNS, 'table-of-content-source'): (self.s_text_x_source, self.e_text_x_source),
493 (TEXTNS, 'user-index-source'): (self.s_text_x_source, self.e_text_x_source),
494 }
495 if embedable:
496 self.set_embedable()
497 self._resetobject()
498
499 def set_plain(self):
500 """Tell the parser to not generate CSS"""
501 self.generate_css = False
502
503 def set_embedable(self):
504 """Tells the converter to only output the parts inside the <body>"""
505 self.elements[(OFFICENS, 'text')] = (None, None)
506 self.elements[(OFFICENS, 'spreadsheet')] = (None, None)
507 self.elements[(OFFICENS, 'presentation')] = (None, None)
508 self.elements[(OFFICENS, 'document-content')] = (None, None)
509
510 def add_style_file(self, stylefilename, media=None):
511 """Add a link to an external style file.
512 Also turns of the embedding of styles in the HTML
513 """
514 self.use_internal_css = False
515 self.stylefilename = stylefilename
516 if media:
517 self.metatags.append(f'<link rel="stylesheet" type="text/css" href="{stylefilename}" media="{media}"/>\n')
518 else:
519 self.metatags.append(f'<link rel="stylesheet" type="text/css" href="{stylefilename}"/>\n')
520
521 def _resetfootnotes(self):
522 # Footnotes and endnotes
523 self.notedict = {}
524 self.currentnote = 0
525 self.notebody: list[str] = []
526
527 def _resetobject(self):
528 self.lines = []
529 self._wfunc = self._wlines
530 self.xmlfile = ''
531 self.title = ''
532 self.language = ''
533 self.creator = ''
534 self.data = []
535 self.tagstack = TagStack()
536 self.htmlstack = []
537 self.pstack = []
538 self.processelem = True
539 self.processcont = True
540 self.listtypes = {}
541 self.headinglevels = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] # level 0 to 10
542 self.use_internal_css = True
543 self.cs = StyleToCSS()
544 self.anchors = {}
545
546 # Style declarations
547 self.stylestack = []
548 self.styledict = {}
549 self.currentstyle = None
550 self.list_starts = {}
551
552 self._resetfootnotes()
553
554 # Tags from meta.xml
555 self.metatags = []
556
557 def writeout(self, s):
558 if s != '':
559 self._wfunc(s)
560
561 def writedata(self):
562 d = ''.join(self.data)
563 if d != '':
564 self.writeout(escape(d))
565
566 def opentag(self, tag, attrs={}, block=False):
567 """Create an open HTML tag"""
568 self.htmlstack.append((tag, attrs, block))
569 a = []
570 for key, val in attrs.items():
571 a.append(f'''{key}={quoteattr(val)}''')
572 if len(a) == 0:
573 self.writeout(f'<{tag}>')
574 else:
575 self.writeout('<{} {}>'.format(tag, ' '.join(a)))
576 if block:
577 self.writeout('\n')
578
579 def closetag(self, tag, block=True):
580 """Close an open HTML tag"""
581 self.htmlstack.pop()
582 self.writeout(f'</{tag}>')
583 if block:
584 self.writeout('\n')
585
586 def emptytag(self, tag, attrs={}):
587 a = []
588 for key, val in attrs.items():
589 a.append(f'''{key}={quoteattr(val)}''')
590 self.writeout('<{} {}/>\n'.format(tag, ' '.join(a)))
591
592 # --------------------------------------------------
593 # Interface to parser
594 # --------------------------------------------------
595 def characters(self, content):
596 if self.processelem and self.processcont:
597 self.data.append(content)
598
599 def startElementNS(self, name, qname, attrs):
600 self.pstack.append((self.processelem, self.processcont))
601 if self.processelem:
602 method = self.elements.get(name, (None, None))[0]
603 if method:
604 self.handle_starttag(name, method, attrs)
605 else:
606 self.unknown_starttag(name, attrs)
607 self.tagstack.push(name, attrs)
608
609 def endElementNS(self, name, qname):
610 stag, attrs = self.tagstack.pop()
611 if self.processelem:
612 method = self.elements.get(name, (None, None))[1]
613 if method:
614 self.handle_endtag(name, attrs, method)
615 else:
616 self.unknown_endtag(name, attrs)
617 self.processelem, self.processcont = self.pstack.pop()
618
619 # --------------------------------------------------
620 def handle_starttag(self, tag, method, attrs):
621 method(tag, attrs)
622
623 def handle_endtag(self, tag, attrs, method):
624 method(tag, attrs)
625
626 def unknown_starttag(self, tag, attrs):
627 pass
628
629 def unknown_endtag(self, tag, attrs):
630 pass
631
632 def s_ignorexml(self, tag, attrs):
633 """Ignore this xml element and all children of it
634 It will automatically stop ignoring
635 """
636 self.processelem = False
637
638 def s_ignorecont(self, tag, attrs):
639 """Stop processing the text nodes"""
640 self.processcont = False
641
642 def s_processcont(self, tag, attrs):
643 """Start processing the text nodes"""
644 self.processcont = True
645
646 def classname(self, attrs):
647 """Generate a class name from a style name"""
648 c = attrs.get((TEXTNS, 'style-name'), '')
649 c = c.replace('.', '_')
650 return c
651
652 def get_anchor(self, name):
653 """Create a unique anchor id for a href name"""
654 if name not in self.anchors:
655 # Changed by Kovid
656 self.anchors[name] = f'anchor{len(self.anchors) + 1}'
657 return self.anchors.get(name)
658
659 def purgedata(self):
660 self.data = []
661
662 # -----------------------------------------------------------------------------
663 #
664 # Handle meta data
665 #
666 # -----------------------------------------------------------------------------
667 def e_dc_title(self, tag, attrs):
668 """Get the title from the meta data and create a HTML <title>"""
669 self.title = ''.join(self.data)
670 # self.metatags.append('<title>{}</title>\n'.format(escape(self.title)))
671 self.data = []
672
673 def e_dc_metatag(self, tag, attrs):
674 """Any other meta data is added as a <meta> element"""
675 self.metatags.append('<meta name="{}" content={}/>\n'.format(tag[1], quoteattr(''.join(self.data))))
676 self.data = []
677
678 def e_dc_contentlanguage(self, tag, attrs):
679 """Set the content language. Identifies the targeted audience"""
680 self.language = ''.join(self.data)
681 self.metatags.append(f'<meta http-equiv="content-language" content="{escape(self.language)}"/>\n')
682 self.data = []
683
684 def e_dc_creator(self, tag, attrs):
685 """Set the content creator. Identifies the targeted audience"""
686 self.creator = ''.join(self.data)
687 self.metatags.append(f'<meta http-equiv="creator" content="{escape(self.creator)}"/>\n')
688 self.data = []
689
690 def s_custom_shape(self, tag, attrs):
691 """A <draw:custom-shape> is made into a <div> in HTML which is then styled"""
692 anchor_type = attrs.get((TEXTNS, 'anchor-type'), 'notfound')
693 htmltag = 'div'
694 name = 'G-' + attrs.get((DRAWNS, 'style-name'), '')
695 if name == 'G-':
696 name = 'PR-' + attrs.get((PRESENTATIONNS, 'style-name'), '')
697 name = name.replace('.', '_')
698 if anchor_type == 'paragraph':
699 style = 'position:absolute;'
700 elif anchor_type == 'char':
701 style = 'position:absolute;'
702 elif anchor_type == 'as-char':
703 htmltag = 'div'
704 style = ''
705 else:
706 style = 'position: absolute;'
707 if (SVGNS, 'width') in attrs:
708 style = style + 'width:' + attrs[(SVGNS, 'width')] + ';'
709 if (SVGNS, 'height') in attrs:
710 style = style + 'height:' + attrs[(SVGNS, 'height')] + ';'
711 if (SVGNS, 'x') in attrs:
712 style = style + 'left:' + attrs[(SVGNS, 'x')] + ';'
713 if (SVGNS, 'y') in attrs:
714 style = style + 'top:' + attrs[(SVGNS, 'y')] + ';'
715 if self.generate_css:
716 self.opentag(htmltag, {'class': name, 'style': style})
717 else:
718 self.opentag(htmltag)
719
720 def e_custom_shape(self, tag, attrs):
721 """End the <draw:frame>"""
722 self.closetag('div')
723
724 def s_draw_frame(self, tag, attrs):
725 """A <draw:frame> is made into a <div> in HTML which is then styled"""
726 self.frame_stack.append([])
727 anchor_type = attrs.get((TEXTNS, 'anchor-type'), 'notfound')
728 htmltag = 'div'
729 name = 'G-' + attrs.get((DRAWNS, 'style-name'), '')
730 if name == 'G-':
731 name = 'PR-' + attrs.get((PRESENTATIONNS, 'style-name'), '')
732 name = name.replace('.', '_')
733 if anchor_type == 'paragraph':
734 style = 'position:relative;'
735 elif anchor_type == 'char':
736 style = 'position:relative;'
737 elif anchor_type == 'as-char':
738 htmltag = 'div'
739 style = ''
740 else:
741 style = 'position:absolute;'
742 if (SVGNS, 'width') in attrs:
743 style = style + 'width:' + attrs[(SVGNS, 'width')] + ';'
744 if (SVGNS, 'height') in attrs:
745 style = style + 'height:' + attrs[(SVGNS, 'height')] + ';'
746 if (SVGNS, 'x') in attrs:
747 style = style + 'left:' + attrs[(SVGNS, 'x')] + ';'
748 if (SVGNS, 'y') in attrs:
749 style = style + 'top:' + attrs[(SVGNS, 'y')] + ';'
750 if self.generate_css:
751 self.opentag(htmltag, {'class': name, 'style': style})
752 else:
753 self.opentag(htmltag)
754
755 def e_draw_frame(self, tag, attrs):
756 """End the <draw:frame>"""
757 self.closetag('div')
758 self.frame_stack.pop()
759
760 def s_draw_fill_image(self, tag, attrs):
761 name = attrs.get((DRAWNS, 'name'), 'NoName')
762 imghref = attrs[(XLINKNS, 'href')]
763 imghref = self.rewritelink(imghref)
764 self.cs.fillimages[name] = imghref
765
766 def rewritelink(self, imghref):
767 """Intended to be overloaded if you don't store your pictures
768 in a Pictures subfolder
769 """
770 return imghref
771
772 def s_draw_image(self, tag, attrs):
773 """A <draw:image> becomes an <img/> element"""
774 if self.frame_stack:
775 if self.frame_stack[-1]:
776 return
777 self.frame_stack[-1].append('img')
778 parent = self.tagstack.stackparent()
779 anchor_type = parent.get((TEXTNS, 'anchor-type'))
780 imghref = attrs[(XLINKNS, 'href')]
781 imghref = self.rewritelink(imghref)
782 htmlattrs = {'alt': '', 'src': imghref}
783 if self.generate_css:
784 if anchor_type != 'char':
785 htmlattrs['style'] = 'display: block;'
786 self.emptytag('img', htmlattrs)
787
788 def s_draw_object(self, tag, attrs):
789 """A <draw:object> is embedded object in the document (e.g. spreadsheet in presentation)."""
790 return # Added by Kovid
791 objhref = attrs[(XLINKNS, 'href')]
792 # Remove leading "./": from "./Object 1" to "Object 1"
793 # objhref = objhref [2:]
794
795 # Not using os.path.join since it fails to find the file on Windows.
796 # objcontentpath = '/'.join([objhref, 'content.xml'])
797
798 for c in self.document.childnodes:
799 if c.folder == objhref:
800 self._walknode(c.topnode)
801
802 def s_draw_object_ole(self, tag, attrs):
803 """A <draw:object-ole> is embedded OLE object in the document (e.g. MS Graph)."""
804 try:
805 class_id = attrs[(DRAWNS, 'class-id')]
806 except KeyError: # Added by Kovid to ignore <draw> without the right
807 return # attributes
808 if class_id and class_id.lower() == '00020803-0000-0000-c000-000000000046': # Microsoft Graph 97 Chart
809 tagattrs = {'name': 'object_ole_graph', 'class': 'ole-graph'}
810 self.opentag('a', tagattrs)
811 self.closetag('a', tagattrs)
812
813 def s_draw_page(self, tag, attrs):
814 """A <draw:page> is a slide in a presentation. We use a <fieldset> element in HTML.
815 Therefore if you convert a ODP file, you get a series of <fieldset>s.
816 Override this for your own purpose.
817 """
818 name = attrs.get((DRAWNS, 'name'), 'NoName')
819 stylename = attrs.get((DRAWNS, 'style-name'), '')
820 stylename = stylename.replace('.', '_')
821 masterpage = attrs.get((DRAWNS, 'master-page-name'), '')
822 masterpage = masterpage.replace('.', '_')
823 if self.generate_css:
824 self.opentag('fieldset', {'class': f'DP-{stylename} MP-{masterpage}'})
825 else:
826 self.opentag('fieldset')
827 self.opentag('legend')
828 self.writeout(escape(name))
829 self.closetag('legend')
830
831 def e_draw_page(self, tag, attrs):
832 self.closetag('fieldset')
833
834 def s_draw_textbox(self, tag, attrs):
835 style = ''
836 if (FONS, 'min-height') in attrs:
837 style = style + 'min-height:' + attrs[(FONS, 'min-height')] + ';'
838 self.opentag('div')
839 # self.opentag('div', {'style': style})
840
841 def e_draw_textbox(self, tag, attrs):
842 """End the <draw:text-box>"""
843 self.closetag('div')
844
845 def html_body(self, tag, attrs):
846 self.writedata()
847 if self.generate_css and self.use_internal_css:
848 self.opentag('style', {'type': 'text/css'}, True)
849 self.writeout('/*<![CDATA[*/\n')
850 self.generate_stylesheet()
851 self.writeout('/*]]>*/\n')
852 self.closetag('style')
853 self.purgedata()
854 self.closetag('head')
855 self.opentag('body', block=True)
856
857 # background-color: white removed by Kovid for #9118
858 # Specifying an explicit bg color prevents ebook readers
859 # from successfully inverting colors
860 # Added styling for endnotes
861 default_styles = '''
862 img { width: 100%; height: 100%; }
863 * { padding: 0; margin: 0; }
864 body { margin: 0 1em; }
865 ol, ul { padding-left: 2em; }
866 a.citation { text-decoration: none }
867 h1.notes-header { page-break-before: always }
868 dl.notes dt { font-size: large }
869 dl.notes dt a { text-decoration: none }
870 dl.notes dd { page-break-after: always }
871 dl.notes dd:last-of-type { page-break-after: avoid }
872 '''
873
874 def generate_stylesheet(self):
875 for name in self.stylestack:
876 styles = self.styledict.get(name)
877 assert styles is not None
878 # Preload with the family's default style
879 if '__style-family' in styles and styles['__style-family'] in self.styledict:
880 familystyle = self.styledict[styles['__style-family']].copy()
881 del styles['__style-family']
882 for style, val in styles.items():
883 familystyle[style] = val
884 styles = familystyle
885 # Resolve the remaining parent styles
886 while '__parent-style-name' in styles and styles['__parent-style-name'] in self.styledict:
887 parentstyle = self.styledict[styles['__parent-style-name']].copy()
888 del styles['__parent-style-name']
889 for style, val in styles.items():
890 parentstyle[style] = val
891 styles = parentstyle
892 self.styledict[name] = styles
893 # Write the styles to HTML
894 self.writeout(self.default_styles)
895 # Changed by Kovid to not write out endless copies of the same style
896 css_styles = {}
897 for name in self.stylestack:
898 styles = self.styledict.get(name)
899 css2 = tuple(self.cs.convert_styles(styles).items())
900 if css2 in css_styles:
901 css_styles[css2].append(name)
902 else:
903 css_styles[css2] = [name]
904
905 def filter_margins(css2):
906 names = {k for k, v in css2}
907 ignore = set()
908 if {'margin-left', 'margin-right', 'margin-top', 'margin-bottom'}.issubset(names):
909 # These come from XML and we cannot preserve XML attribute
910 # order so we assume that margin is to be overridden See
911 # https://bugs.launchpad.net/calibre/+bug/941134 and
912 # https://bugs.launchpad.net/calibre/+bug/1002702
913 ignore.add('margin')
914 css2 = sorted(css2, key=lambda x: {'margin': 0}.get(x[0], 1))
915 for k, v in css2:
916 if k not in ignore:
917 yield k, v
918
919 for css2, names in css_styles.items():
920 self.writeout('{} {{\n'.format(', '.join(names)))
921 for style, val in filter_margins(css2):
922 self.writeout(f'\t{style}: {val};\n')
923 self.writeout('}\n')
924
925 def generate_footnotes(self):
926 if self.currentnote == 0:
927 return
928 # Changed by Kovid to improve endnote functionality
929 from calibre.utils.localization import _
930
931 self.opentag('h1', {'class': 'notes-header'})
932 self.writeout(_('Notes'))
933 self.closetag('h1')
934 self.opentag('dl', {'class': 'notes'})
935 for key in range(1, self.currentnote + 1):
936 note = self.notedict[key]
937 # for key,note in self.notedict.items():
938 self.opentag('dt', {'id': f'footnote-{key}'})
939 # self.opentag('sup')
940 # self.writeout(escape(note['citation']))
941 # self.closetag('sup', False)
942 self.writeout('[')
943 self.opentag('a', {'href': f'#citation-{key}'})
944 self.writeout(f'←{key}')
945 self.closetag('a')
946 self.writeout(']\xa0')
947 self.closetag('dt')
948 self.opentag('dd')
949 self.writeout(note['body'])
950 self.closetag('dd')
951 self.closetag('dl')
952
953 def s_office_automatic_styles(self, tag, attrs):
954 if self.xmlfile == 'styles.xml':
955 self.autoprefix = 'A'
956 else:
957 self.autoprefix = ''
958
959 def s_office_document_content(self, tag, attrs):
960 """First tag in the content.xml file"""
961 self.writeout('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" ')
962 self.writeout('"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n')
963 self.opentag('html', {'xmlns': 'http://www.w3.org/1999/xhtml'}, True)
964 self.opentag('head', block=True)
965 self.emptytag('meta', {'http-equiv': 'Content-Type', 'content': 'text/html;charset=UTF-8'})
966 for metaline in self.metatags:
967 self.writeout(metaline)
968 self.writeout(f'<title>{escape(self.title)}</title>\n')
969
970 def e_office_document_content(self, tag, attrs):
971 """Last tag"""
972 self.closetag('html')
973
974 def s_office_master_styles(self, tag, attrs):
975 """ """
976
977 def s_office_presentation(self, tag, attrs):
978 """For some odd reason, OpenOffice Impress doesn't define a default-style
979 for the 'paragraph'. We therefore force a standard when we see
980 it is a presentation
981 """
982 self.styledict['p'] = {(FONS, 'font-size'): '24pt'}
983 self.styledict['presentation'] = {(FONS, 'font-size'): '24pt'}
984 self.html_body(tag, attrs)
985
986 def e_office_presentation(self, tag, attrs):
987 self.generate_footnotes()
988 self.closetag('body')
989
990 def s_office_spreadsheet(self, tag, attrs):
991 self.html_body(tag, attrs)
992
993 def e_office_spreadsheet(self, tag, attrs):
994 self.generate_footnotes()
995 self.closetag('body')
996
997 def s_office_styles(self, tag, attrs):
998 self.autoprefix = ''
999
1000 def s_office_text(self, tag, attrs):
1001 """OpenDocument text"""
1002 self.styledict['frame'] = {(STYLENS, 'wrap'): 'parallel'}
1003 self.html_body(tag, attrs)
1004
1005 def e_office_text(self, tag, attrs):
1006 self.generate_footnotes()
1007 self.closetag('body')
1008
1009 def s_style_handle_properties(self, tag, attrs):
1010 """Copy all attributes to a struct.
1011 We will later convert them to CSS2
1012 """
1013 if self.currentstyle is None: # Added by Kovid
1014 return
1015
1016 for key, attr in attrs.items():
1017 self.styledict[self.currentstyle][key] = attr
1018
1019 familymap = {
1020 'frame': 'frame',
1021 'paragraph': 'p',
1022 'presentation': 'presentation',
1023 'text': 'span',
1024 'section': 'div',
1025 'table': 'table',
1026 'table-cell': 'td',
1027 'table-column': 'col',
1028 'table-row': 'tr',
1029 'graphic': 'graphic',
1030 }
1031
1032 def s_style_default_style(self, tag, attrs):
1033 """A default style is like a style on an HTML tag"""
1034 family = attrs[(STYLENS, 'family')]
1035 htmlfamily = self.familymap.get(family, 'unknown')
1036 self.currentstyle = htmlfamily
1037 # self.stylestack.append(self.currentstyle)
1038 self.styledict[self.currentstyle] = {}
1039
1040 def e_style_default_style(self, tag, attrs):
1041 self.currentstyle = None
1042
1043 def s_style_font_face(self, tag, attrs):
1044 """It is possible that the HTML browser doesn't know how to
1045 show a particular font. Luckily ODF provides generic fallbacks
1046 Unfortunately they are not the same as CSS2.
1047 CSS2: serif, sans-serif, cursive, fantasy, monospace
1048 ODF: roman, swiss, modern, decorative, script, system
1049 """
1050 name = attrs[(STYLENS, 'name')]
1051 family = attrs[(SVGNS, 'font-family')]
1052 generic = attrs.get((STYLENS, 'font-family-generic'), '')
1053 self.cs.save_font(name, family, generic)
1054
1055 def s_style_footer(self, tag, attrs):
1056 self.opentag('div', {'id': 'footer'})
1057 self.purgedata()
1058
1059 def e_style_footer(self, tag, attrs):
1060 self.writedata()
1061 self.closetag('div')
1062 self.purgedata()
1063
1064 def s_style_footer_style(self, tag, attrs):
1065 self.currentstyle = '@print #footer'
1066 self.stylestack.append(self.currentstyle)
1067 self.styledict[self.currentstyle] = {}
1068
1069 def s_style_header(self, tag, attrs):
1070 self.opentag('div', {'id': 'header'})
1071 self.purgedata()
1072
1073 def e_style_header(self, tag, attrs):
1074 self.writedata()
1075 self.closetag('div')
1076 self.purgedata()
1077
1078 def s_style_header_style(self, tag, attrs):
1079 self.currentstyle = '@print #header'
1080 self.stylestack.append(self.currentstyle)
1081 self.styledict[self.currentstyle] = {}
1082
1083 def s_style_default_page_layout(self, tag, attrs):
1084 """Collect the formatting for the default page layout style."""
1085 self.currentstyle = '@page'
1086 self.stylestack.append(self.currentstyle)
1087 self.styledict[self.currentstyle] = {}
1088
1089 def s_style_page_layout(self, tag, attrs):
1090 """Collect the formatting for the page layout style.
1091 This won't work in CSS 2.1, as page identifiers are not allowed.
1092 It is legal in CSS3, but the rest of the application doesn't specify when to use what page layout
1093 """
1094 name = attrs[(STYLENS, 'name')]
1095 name = name.replace('.', '_')
1096 self.currentstyle = '.PL-' + name
1097 self.stylestack.append(self.currentstyle)
1098 self.styledict[self.currentstyle] = {}
1099
1100 def e_style_page_layout(self, tag, attrs):
1101 """End this style"""
1102 self.currentstyle = None
1103
1104 def s_style_master_page(self, tag, attrs):
1105 """Collect the formatting for the page layout style."""
1106 name = attrs[(STYLENS, 'name')]
1107 name = name.replace('.', '_')
1108
1109 self.currentstyle = '.MP-' + name
1110 self.stylestack.append(self.currentstyle)
1111 self.styledict[self.currentstyle] = {('', 'position'): 'relative'}
1112 # Then load the pagelayout style if we find it
1113 pagelayout = attrs.get((STYLENS, 'page-layout-name'), None)
1114 if pagelayout:
1115 pagelayout = '.PL-' + pagelayout
1116 if pagelayout in self.styledict:
1117 styles = self.styledict[pagelayout]
1118 for style, val in styles.items():
1119 self.styledict[self.currentstyle][style] = val
1120 else:
1121 self.styledict[self.currentstyle]['__parent-style-name'] = pagelayout
1122 self.s_ignorexml(tag, attrs)
1123
1124 # Short prefixes for class selectors
1125 _familyshort = {
1126 'drawing-page': 'DP',
1127 'paragraph': 'P',
1128 'presentation': 'PR',
1129 'text': 'S',
1130 'section': 'D',
1131 'table': 'T',
1132 'table-cell': 'TD',
1133 'table-column': 'TC',
1134 'table-row': 'TR',
1135 'graphic': 'G',
1136 }
1137
1138 def s_style_style(self, tag, attrs):
1139 """Collect the formatting for the style.
1140 Styles have scope. The same name can be used for both paragraph and
1141 character styles Since CSS has no scope we use a prefix. (Not elegant)
1142 In ODF a style can have a parent, these parents can be chained.
1143 We may not have encountered the parent yet, but if we have, we resolve it.
1144 """
1145 name = attrs[(STYLENS, 'name')]
1146 name = name.replace('.', '_')
1147 family = attrs[(STYLENS, 'family')]
1148 htmlfamily = self.familymap.get(family, 'unknown')
1149 sfamily = self._familyshort.get(family, 'X')
1150 name = f'{self.autoprefix}{sfamily}-{name}'
1151 parent = attrs.get((STYLENS, 'parent-style-name'))
1152 self.currentstyle = special_styles.get(name, '.' + name)
1153 self.stylestack.append(self.currentstyle)
1154 if self.currentstyle not in self.styledict:
1155 self.styledict[self.currentstyle] = {}
1156
1157 self.styledict[self.currentstyle]['__style-family'] = htmlfamily
1158
1159 # Then load the parent style if we find it
1160 if parent:
1161 parent = parent.replace('.', '_')
1162 parent = f'{sfamily}-{parent}'
1163 parent = special_styles.get(parent, '.' + parent)
1164 if parent in self.styledict:
1165 styles = self.styledict[parent]
1166 for style, val in styles.items():
1167 self.styledict[self.currentstyle][style] = val
1168 else:
1169 self.styledict[self.currentstyle]['__parent-style-name'] = parent
1170
1171 def e_style_style(self, tag, attrs):
1172 """End this style"""
1173 self.currentstyle = None
1174
1175 def s_table_table(self, tag, attrs):
1176 """Start a table"""
1177 c = attrs.get((TABLENS, 'style-name'), None)
1178 if c and self.generate_css:
1179 c = c.replace('.', '_')
1180 self.opentag('table', {'class': f'T-{c}'})
1181 else:
1182 self.opentag('table')
1183 self.purgedata()
1184
1185 def e_table_table(self, tag, attrs):
1186 """End a table"""
1187 self.writedata()
1188 self.closetag('table')
1189 self.purgedata()
1190
1191 def s_table_table_cell(self, tag, attrs):
1192 """Start a table cell"""
1193 # FIXME: number-columns-repeated § 8.1.3
1194 # repeated = int(attrs.get( (TABLENS,'number-columns-repeated'), 1))
1195 htmlattrs = {}
1196 rowspan = attrs.get((TABLENS, 'number-rows-spanned'))
1197 if rowspan:
1198 htmlattrs['rowspan'] = rowspan
1199 colspan = attrs.get((TABLENS, 'number-columns-spanned'))
1200 if colspan:
1201 htmlattrs['colspan'] = colspan
1202
1203 c = attrs.get((TABLENS, 'style-name'))
1204 if c:
1205 htmlattrs['class'] = 'TD-{}'.format(c.replace('.', '_'))
1206 self.opentag('td', htmlattrs)
1207 self.purgedata()
1208
1209 def e_table_table_cell(self, tag, attrs):
1210 """End a table cell"""
1211 self.writedata()
1212 self.closetag('td')
1213 self.purgedata()
1214
1215 def s_table_table_column(self, tag, attrs):
1216 """Start a table column"""
1217 c = attrs.get((TABLENS, 'style-name'), None)
1218 repeated = int(attrs.get((TABLENS, 'number-columns-repeated'), 1))
1219 htmlattrs = {}
1220 if c:
1221 htmlattrs['class'] = 'TC-{}'.format(c.replace('.', '_'))
1222 for x in range(repeated):
1223 self.emptytag('col', htmlattrs)
1224 self.purgedata()
1225
1226 def s_table_table_row(self, tag, attrs):
1227 """Start a table row"""
1228 # FIXME: table:number-rows-repeated
1229 c = attrs.get((TABLENS, 'style-name'), None)
1230 htmlattrs = {}
1231 if c:
1232 htmlattrs['class'] = 'TR-{}'.format(c.replace('.', '_'))
1233 self.opentag('tr', htmlattrs)
1234 self.purgedata()
1235
1236 def e_table_table_row(self, tag, attrs):
1237 """End a table row"""
1238 self.writedata()
1239 self.closetag('tr')
1240 self.purgedata()
1241
1242 def s_text_a(self, tag, attrs):
1243 """Anchors start"""
1244 self.writedata()
1245 href = attrs[(XLINKNS, 'href')].split('|')[0]
1246 if href[:1] == '#': # Changed by Kovid
1247 href = '#' + self.get_anchor(href[1:])
1248 self.opentag('a', {'href': href})
1249 self.purgedata()
1250
1251 def e_text_a(self, tag, attrs):
1252 """End an anchor or bookmark reference"""
1253 self.writedata()
1254 self.closetag('a', False)
1255 self.purgedata()
1256
1257 def s_text_bookmark(self, tag, attrs):
1258 """Bookmark definition"""
1259 name = attrs[(TEXTNS, 'name')]
1260 html_id = self.get_anchor(name)
1261 self.writedata()
1262 self.opentag('span', {'id': html_id})
1263 self.closetag('span', False)
1264 self.purgedata()
1265
1266 def s_text_bookmark_ref(self, tag, attrs):
1267 """Bookmark reference"""
1268 name = attrs[(TEXTNS, 'ref-name')]
1269 html_id = '#' + self.get_anchor(name)
1270 self.writedata()
1271 self.opentag('a', {'href': html_id})
1272 self.purgedata()
1273
1274 def s_text_h(self, tag, attrs):
1275 """Headings start"""
1276 level = int(attrs[(TEXTNS, 'outline-level')])
1277 level = min(max(level, 1), 6) # Heading levels go only to 6 in XHTML
1278 self.headinglevels[level] = self.headinglevels[level] + 1
1279 name = self.classname(attrs)
1280 for x in range(level + 1, 10):
1281 self.headinglevels[x] = 0
1282 special = special_styles.get('P-' + name)
1283 if special or not self.generate_css:
1284 self.opentag(f'h{level}')
1285 else:
1286 self.opentag(f'h{level}', {'class': f'P-{name}'})
1287 self.purgedata()
1288
1289 def e_text_h(self, tag, attrs):
1290 """Headings end
1291 Side-effect: If there is no title in the metadata, then it is taken
1292 from the first heading of any level.
1293 """
1294 self.writedata()
1295 level = int(attrs[(TEXTNS, 'outline-level')])
1296 level = min(level, 6) # Heading levels go only to 6 in XHTML
1297 level = max(level, 1)
1298 lev = self.headinglevels[1 : level + 1]
1299 outline = '.'.join(map(str, lev))
1300 heading = ''.join(self.data)
1301 if self.title == '':
1302 self.title = heading
1303 # Changed by Kovid
1304 tail = ''.join(self.data)
1305 anchor = self.get_anchor(f'{outline}.{tail}')
1306 anchor2 = self.get_anchor(tail) # Added by kovid to fix #7506
1307 self.opentag('a', {'id': anchor})
1308 self.closetag('a', False)
1309 self.opentag('a', {'id': anchor2})
1310 self.closetag('a', False)
1311 self.closetag(f'h{level}')
1312 self.purgedata()
1313
1314 def s_text_line_break(self, tag, attrs):
1315 """Force a line break (<br/>)"""
1316 self.writedata()
1317 self.emptytag('br')
1318 self.purgedata()
1319
1320 def s_text_list(self, tag, attrs):
1321 """Start a list (<ul> or <ol>)
1322 To know which level we're at, we have to count the number
1323 of <text:list> elements on the tagstack.
1324 """
1325 name = attrs.get((TEXTNS, 'style-name'))
1326 continue_numbering = attrs.get((TEXTNS, 'continue-numbering')) == 'true'
1327 continue_list = attrs.get((TEXTNS, 'continue-list'))
1328 list_id = attrs.get(('http://www.w3.org/XML/1998/namespace', 'id'))
1329 level = self.tagstack.count_tags(tag) + 1
1330 if name:
1331 name = name.replace('.', '_')
1332 else:
1333 # FIXME: If a list is contained in a table cell or text box,
1334 # the list level must return to 1, even though the table or
1335 # textbox itself may be nested within another list.
1336 name = self.tagstack.rfindattr((TEXTNS, 'style-name'))
1337 list_class = f'{name}_{level}'
1338 tag_name = self.listtypes.get(list_class, 'ul')
1339 number_class = tag_name + list_class
1340 if list_id:
1341 self.list_id_map[list_id] = number_class
1342 if continue_list:
1343 if continue_list in self.list_id_map:
1344 tglc = self.list_id_map[continue_list]
1345 self.list_number_map[number_class] = self.list_number_map[tglc]
1346 else:
1347 self.list_number_map.pop(number_class, None)
1348 elif not continue_numbering:
1349 self.list_number_map.pop(number_class, None)
1350 self.list_class_stack.append(number_class)
1351 attrs = {}
1352 if tag_name == 'ol' and self.list_number_map[number_class] != 1:
1353 attrs = {'start': str(self.list_number_map[number_class])}
1354 if self.generate_css:
1355 attrs['class'] = list_class
1356 self.opentag(str(tag_name), attrs)
1357 self.purgedata()
1358
1359 def e_text_list(self, tag, attrs):
1360 """End a list"""
1361 self.writedata()
1362 if self.list_class_stack:
1363 self.list_class_stack.pop()
1364 name = attrs.get((TEXTNS, 'style-name'))
1365 level = self.tagstack.count_tags(tag) + 1
1366 if name:
1367 name = name.replace('.', '_')
1368 else:
1369 # FIXME: If a list is contained in a table cell or text box,
1370 # the list level must return to 1, even though the table or
1371 # textbox itself may be nested within another list.
1372 name = self.tagstack.rfindattr((TEXTNS, 'style-name'))
1373 list_class = f'{name}_{level}'
1374 self.closetag(self.listtypes.get(list_class, 'ul'))
1375 self.purgedata()
1376
1377 def s_text_list_item(self, tag, attrs):
1378 """Start list item"""
1379 number_class = self.list_class_stack[-1] if self.list_class_stack else None
1380 if number_class:
1381 self.list_number_map[number_class] += 1
1382 self.opentag('li')
1383 self.purgedata()
1384
1385 def e_text_list_item(self, tag, attrs):
1386 """End list item"""
1387 self.writedata()
1388 self.closetag('li')
1389 self.purgedata()
1390
1391 def s_text_list_level_style_bullet(self, tag, attrs):
1392 """CSS doesn't have the ability to set the glyph
1393 to a particular character, so we just go through
1394 the available glyphs
1395 """
1396 name = self.tagstack.rfindattr((STYLENS, 'name'))
1397 level = attrs[(TEXTNS, 'level')]
1398 self.prevstyle = self.currentstyle
1399 list_class = f'{name}_{level}'
1400 self.listtypes[list_class] = 'ul'
1401 self.currentstyle = '.{}_{}'.format(name.replace('.', '_'), level)
1402 self.stylestack.append(self.currentstyle)
1403 self.styledict[self.currentstyle] = {}
1404
1405 level = int(level)
1406 listtype = ('square', 'disc', 'circle')[level % 3]
1407 self.styledict[self.currentstyle][('', 'list-style-type')] = listtype
1408
1409 def e_text_list_level_style_bullet(self, tag, attrs):
1410 self.currentstyle = self.prevstyle
1411 del self.prevstyle
1412
1413 def s_text_list_level_style_number(self, tag, attrs):
1414 name = self.tagstack.stackparent()[(STYLENS, 'name')]
1415 level = attrs[(TEXTNS, 'level')]
1416 num_format = attrs.get((STYLENS, 'num-format'), '1')
1417 start_value = attrs.get((TEXTNS, 'start-value'), '1')
1418 list_class = f'{name}_{level}'
1419 self.prevstyle = self.currentstyle
1420 self.currentstyle = '.{}_{}'.format(name.replace('.', '_'), level)
1421 if start_value != '1':
1422 self.list_starts[self.currentstyle] = start_value
1423 self.listtypes[list_class] = 'ol'
1424 self.stylestack.append(self.currentstyle)
1425 self.styledict[self.currentstyle] = {}
1426 if num_format == '1':
1427 listtype = 'decimal'
1428 elif num_format == 'I':
1429 listtype = 'upper-roman'
1430 elif num_format == 'i':
1431 listtype = 'lower-roman'
1432 elif num_format == 'A':
1433 listtype = 'upper-alpha'
1434 elif num_format == 'a':
1435 listtype = 'lower-alpha'
1436 else:
1437 listtype = 'decimal'
1438 self.styledict[self.currentstyle][('', 'list-style-type')] = listtype
1439
1440 def e_text_list_level_style_number(self, tag, attrs):
1441 self.currentstyle = self.prevstyle
1442 del self.prevstyle
1443
1444 def s_text_note(self, tag, attrs):
1445 self.writedata()
1446 self.purgedata()
1447 self.currentnote = self.currentnote + 1
1448 self.notedict[self.currentnote] = {}
1449 self.notebody = []
1450
1451 def e_text_note(self, tag, attrs):
1452 pass
1453
1454 def collectnote(self, s):
1455 if s != '':
1456 self.notebody.append(s)
1457
1458 def s_text_note_body(self, tag, attrs):
1459 self._orgwfunc = self._wfunc
1460 self._wfunc = self.collectnote
1461
1462 def e_text_note_body(self, tag, attrs):
1463 self._wfunc = self._orgwfunc
1464 self.notedict[self.currentnote]['body'] = ''.join(self.notebody)
1465 self.notebody = []
1466 del self._orgwfunc
1467
1468 def e_text_note_citation(self, tag, attrs):
1469 # Changed by Kovid to improve formatting and enable backlinks
1470 mark = ''.join(self.data)
1471 self.notedict[self.currentnote]['citation'] = mark
1472 self.opentag('sup')
1473 self.opentag(
1474 'a',
1475 {
1476 'href': f'#footnote-{self.currentnote}',
1477 'class': 'citation',
1478 'id': f'citation-{self.currentnote}',
1479 },
1480 )
1481 # self.writeout( escape(mark) )
1482 # Since HTML only knows about endnotes, there is too much risk that the
1483 # marker is reused in the source. Therefore we force numeric markers
1484 self.writeout(str(self.currentnote))
1485 self.closetag('a')
1486 self.closetag('sup')
1487
1488 def s_text_p(self, tag, attrs):
1489 """Paragraph"""
1490 htmlattrs = {}
1491 specialtag = 'p'
1492 c = attrs.get((TEXTNS, 'style-name'), None)
1493 if c:
1494 c = c.replace('.', '_')
1495 specialtag = special_styles.get('P-' + c)
1496 if specialtag is None:
1497 specialtag = 'p'
1498 if self.generate_css:
1499 htmlattrs['class'] = f'P-{c}'
1500 self.opentag(specialtag, htmlattrs)
1501 self.purgedata()
1502
1503 def e_text_p(self, tag, attrs):
1504 """End Paragraph"""
1505 specialtag = 'p'
1506 c = attrs.get((TEXTNS, 'style-name'), None)
1507 if c:
1508 c = c.replace('.', '_')
1509 specialtag = special_styles.get('P-' + c)
1510 if specialtag is None:
1511 specialtag = 'p'
1512 self.writedata()
1513 if not self.data: # Added by Kovid
1514 # Give substance to empty paragraphs, as rendered by OOo
1515 self.writeout('&#160;')
1516 self.closetag(specialtag)
1517 self.purgedata()
1518
1519 def s_text_s(self, tag, attrs):
1520 # Changed by Kovid to fix non breaking spaces being prepended to
1521 # element instead of being part of the text flow.
1522 # We don't use an entity for the nbsp as the contents of self.data will
1523 # be escaped on writeout.
1524 """Generate a number of spaces. We use the non breaking space for
1525 the text:s ODF element.
1526 """
1527 try:
1528 c = int(attrs.get((TEXTNS, 'c'), 1))
1529 except Exception:
1530 c = 0
1531 if c > 0:
1532 self.data.append('\u00a0' * c)
1533
1534 def s_text_span(self, tag, attrs):
1535 """The <text:span> element matches the <span> element in HTML. It is
1536 typically used to properties of the text.
1537 """
1538 self.writedata()
1539 c = attrs.get((TEXTNS, 'style-name'), None)
1540 htmlattrs = {}
1541 # Changed by Kovid to handle inline special styles defined on <text:span> tags.
1542 # Apparently LibreOffice does this.
1543 special = 'span'
1544 if c:
1545 c = c.replace('.', '_')
1546 special = special_styles.get('S-' + c)
1547 if special is None:
1548 special = 'span'
1549 if self.generate_css:
1550 htmlattrs['class'] = f'S-{c}'
1551
1552 self.opentag(special, htmlattrs)
1553 self.purgedata()
1554
1555 def e_text_span(self, tag, attrs):
1556 """End the <text:span>"""
1557 self.writedata()
1558 c = attrs.get((TEXTNS, 'style-name'), None)
1559 # Changed by Kovid to handle inline special styles defined on <text:span> tags.
1560 # Apparently LibreOffice does this.
1561 special = 'span'
1562 if c:
1563 c = c.replace('.', '_')
1564 special = special_styles.get('S-' + c)
1565 if special is None:
1566 special = 'span'
1567
1568 self.closetag(special, False)
1569 self.purgedata()
1570
1571 def s_text_tab(self, tag, attrs):
1572 """Move to the next tabstop. We ignore this in HTML"""
1573 self.writedata()
1574 self.writeout(' ')
1575 self.purgedata()
1576
1577 def s_text_x_source(self, tag, attrs):
1578 """Various indexes and tables of contents. We ignore those."""
1579 self.writedata()
1580 self.purgedata()
1581 self.s_ignorexml(tag, attrs)
1582
1583 def e_text_x_source(self, tag, attrs):
1584 """Various indexes and tables of contents. We ignore those."""
1585 self.writedata()
1586 self.purgedata()
1587
1588 # -----------------------------------------------------------------------------
1589 #
1590 # Reading the file
1591 #
1592 # -----------------------------------------------------------------------------
1593
1594 def load(self, odffile):
1595 """Loads a document into the parser and parses it.
1596 The argument can either be a filename or a document in memory.
1597 """
1598 self.lines = []
1599 self._wfunc = self._wlines
1600 if isinstance(odffile, (bytes, str)) or hasattr(odffile, 'read'): # Added by Kovid
1601 self.document = load(odffile)
1602 else:
1603 self.document = odffile
1604 self._walknode(self.document.topnode)
1605
1606 def _walknode(self, node):
1607 if node.nodeType == Node.ELEMENT_NODE:
1608 self.startElementNS(node.qname, node.tagName, node.attributes)
1609 for c in node.childNodes:
1610 self._walknode(c)
1611 self.endElementNS(node.qname, node.tagName)
1612 if node.nodeType in (Node.TEXT_NODE, Node.CDATA_SECTION_NODE):
1613 self.characters(str(node))
1614
1615 def odf2xhtml(self, odffile):
1616 """Load a file and return the XHTML"""
1617 self.load(odffile)
1618 return self.xhtml()
1619
1620 def _wlines(self, s):
1621 if s:
1622 self.lines.append(s)
1623
1624 def xhtml(self):
1625 """Returns the xhtml"""
1626 return ''.join(self.lines)
1627
1628 def _writecss(self, s):
1629 if s:
1630 self._csslines.append(s)
1631
1632 def _writenothing(self, s):
1633 pass
1634
1635 def css(self):
1636 """Returns the CSS content"""
1637 self._csslines = []
1638 self._wfunc = self._writecss
1639 self.generate_stylesheet()
1640 res = ''.join(self._csslines)
1641 self._wfunc = self._wlines
1642 del self._csslines
1643 return res
1644
1645 def save(self, outputfile, addsuffix=False):
1646 """Save the HTML under the filename.
1647 If the filename is '-' then save to stdout
1648 We have the last style filename in self.stylefilename
1649 """
1650 if outputfile == '-':
1651 import sys # Added by Kovid
1652
1653 outputfp = sys.stdout
1654 else:
1655 if addsuffix:
1656 outputfile = outputfile + '.html'
1657 outputfp = open(outputfile, 'wb')
1658 outputfp.write(self.xhtml().encode('us-ascii', 'xmlcharrefreplace'))
1659 outputfp.close()
1660
1661
1662 class ODF2XHTMLembedded(ODF2XHTML):
1663 """The ODF2XHTML parses an ODF file and produces XHTML"""
1664
1665 def __init__(self, lines, generate_css=True, embedable=False):
1666 self._resetobject()
1667 self.lines = lines
1668
1669 # Tags
1670 self.generate_css = generate_css
1671 self.elements = {
1672 # (DCNS, 'title'): (self.s_processcont, self.e_dc_title),
1673 # (DCNS, 'language'): (self.s_processcont, self.e_dc_contentlanguage),
1674 # (DCNS, 'creator'): (self.s_processcont, self.e_dc_metatag),
1675 # (DCNS, 'description'): (self.s_processcont, self.e_dc_metatag),
1676 # (DCNS, 'date'): (self.s_processcont, self.e_dc_metatag),
1677 (DRAWNS, 'frame'): (self.s_draw_frame, self.e_draw_frame),
1678 (DRAWNS, 'image'): (self.s_draw_image, None),
1679 (DRAWNS, 'fill-image'): (self.s_draw_fill_image, None),
1680 (DRAWNS, 'layer-set'): (self.s_ignorexml, None),
1681 (DRAWNS, 'page'): (self.s_draw_page, self.e_draw_page),
1682 (DRAWNS, 'object'): (self.s_draw_object, None),
1683 (DRAWNS, 'object-ole'): (self.s_draw_object_ole, None),
1684 (DRAWNS, 'text-box'): (self.s_draw_textbox, self.e_draw_textbox),
1685 # (METANS, 'creation-date'):(self.s_processcont, self.e_dc_metatag),
1686 # (METANS, 'generator'):(self.s_processcont, self.e_dc_metatag),
1687 # (METANS, 'initial-creator'): (self.s_processcont, self.e_dc_metatag),
1688 # (METANS, 'keyword'): (self.s_processcont, self.e_dc_metatag),
1689 (NUMBERNS, 'boolean-style'): (self.s_ignorexml, None),
1690 (NUMBERNS, 'currency-style'): (self.s_ignorexml, None),
1691 (NUMBERNS, 'date-style'): (self.s_ignorexml, None),
1692 (NUMBERNS, 'number-style'): (self.s_ignorexml, None),
1693 (NUMBERNS, 'text-style'): (self.s_ignorexml, None),
1694 # (OFFICENS, "automatic-styles"):(self.s_office_automatic_styles, None),
1695 # (OFFICENS, "document-content"):(self.s_office_document_content, self.e_office_document_content),
1696 (OFFICENS, 'forms'): (self.s_ignorexml, None),
1697 # (OFFICENS, "master-styles"):(self.s_office_master_styles, None),
1698 (OFFICENS, 'meta'): (self.s_ignorecont, None),
1699 # (OFFICENS, "presentation"):(self.s_office_presentation, self.e_office_presentation),
1700 # (OFFICENS, "spreadsheet"):(self.s_office_spreadsheet, self.e_office_spreadsheet),
1701 # (OFFICENS, "styles"):(self.s_office_styles, None),
1702 # (OFFICENS, "text"):(self.s_office_text, self.e_office_text),
1703 (OFFICENS, 'scripts'): (self.s_ignorexml, None),
1704 (PRESENTATIONNS, 'notes'): (self.s_ignorexml, None),
1705 # (STYLENS, "default-page-layout"):(self.s_style_default_page_layout, self.e_style_page_layout),
1706 # (STYLENS, "default-page-layout"):(self.s_ignorexml, None),
1707 # (STYLENS, "default-style"):(self.s_style_default_style, self.e_style_default_style),
1708 # (STYLENS, "drawing-page-properties"):(self.s_style_handle_properties, None),
1709 # (STYLENS, "font-face"):(self.s_style_font_face, None),
1710 # (STYLENS, "footer"):(self.s_style_footer, self.e_style_footer),
1711 # (STYLENS, "footer-style"):(self.s_style_footer_style, None),
1712 # (STYLENS, "graphic-properties"):(self.s_style_handle_properties, None),
1713 # (STYLENS, "handout-master"):(self.s_ignorexml, None),
1714 # (STYLENS, "header"):(self.s_style_header, self.e_style_header),
1715 # (STYLENS, "header-footer-properties"):(self.s_style_handle_properties, None),
1716 # (STYLENS, "header-style"):(self.s_style_header_style, None),
1717 # (STYLENS, "master-page"):(self.s_style_master_page, None),
1718 # (STYLENS, "page-layout-properties"):(self.s_style_handle_properties, None),
1719 # (STYLENS, "page-layout"):(self.s_style_page_layout, self.e_style_page_layout),
1720 # (STYLENS, "page-layout"):(self.s_ignorexml, None),
1721 # (STYLENS, "paragraph-properties"):(self.s_style_handle_properties, None),
1722 # (STYLENS, "style"):(self.s_style_style, self.e_style_style),
1723 # (STYLENS, "table-cell-properties"):(self.s_style_handle_properties, None),
1724 # (STYLENS, "table-column-properties"):(self.s_style_handle_properties, None),
1725 # (STYLENS, "table-properties"):(self.s_style_handle_properties, None),
1726 # (STYLENS, "text-properties"):(self.s_style_handle_properties, None),
1727 (SVGNS, 'desc'): (self.s_ignorexml, None),
1728 (TABLENS, 'covered-table-cell'): (self.s_ignorexml, None),
1729 (TABLENS, 'table-cell'): (self.s_table_table_cell, self.e_table_table_cell),
1730 (TABLENS, 'table-column'): (self.s_table_table_column, None),
1731 (TABLENS, 'table-row'): (self.s_table_table_row, self.e_table_table_row),
1732 (TABLENS, 'table'): (self.s_table_table, self.e_table_table),
1733 (TEXTNS, 'a'): (self.s_text_a, self.e_text_a),
1734 (TEXTNS, 'alphabetical-index-source'): (self.s_text_x_source, self.e_text_x_source),
1735 (TEXTNS, 'bibliography-configuration'): (self.s_ignorexml, None),
1736 (TEXTNS, 'bibliography-source'): (self.s_text_x_source, self.e_text_x_source),
1737 (TEXTNS, 'h'): (self.s_text_h, self.e_text_h),
1738 (TEXTNS, 'illustration-index-source'): (self.s_text_x_source, self.e_text_x_source),
1739 (TEXTNS, 'line-break'): (self.s_text_line_break, None),
1740 (TEXTNS, 'linenumbering-configuration'): (self.s_ignorexml, None),
1741 (TEXTNS, 'list'): (self.s_text_list, self.e_text_list),
1742 (TEXTNS, 'list-item'): (self.s_text_list_item, self.e_text_list_item),
1743 (TEXTNS, 'list-level-style-bullet'): (
1744 self.s_text_list_level_style_bullet,
1745 self.e_text_list_level_style_bullet,
1746 ),
1747 (TEXTNS, 'list-level-style-number'): (
1748 self.s_text_list_level_style_number,
1749 self.e_text_list_level_style_number,
1750 ),
1751 (TEXTNS, 'list-style'): (None, None),
1752 (TEXTNS, 'note'): (self.s_text_note, None),
1753 (TEXTNS, 'note-body'): (self.s_text_note_body, self.e_text_note_body),
1754 (TEXTNS, 'note-citation'): (None, self.e_text_note_citation),
1755 (TEXTNS, 'notes-configuration'): (self.s_ignorexml, None),
1756 (TEXTNS, 'object-index-source'): (self.s_text_x_source, self.e_text_x_source),
1757 (TEXTNS, 'p'): (self.s_text_p, self.e_text_p),
1758 (TEXTNS, 's'): (self.s_text_s, None),
1759 (TEXTNS, 'span'): (self.s_text_span, self.e_text_span),
1760 (TEXTNS, 'tab'): (self.s_text_tab, None),
1761 (TEXTNS, 'table-index-source'): (self.s_text_x_source, self.e_text_x_source),
1762 (TEXTNS, 'table-of-content-source'): (self.s_text_x_source, self.e_text_x_source),
1763 (TEXTNS, 'user-index-source'): (self.s_text_x_source, self.e_text_x_source),
1764 (TEXTNS, 'page-number'): (None, None),
1765 }