| 1 |
# Copyright (C) 2006-2008 Søren Roug, European Environment Agency |
| 2 |
# |
| 3 |
# This library is free software; you can redistribute it and/or |
| 4 |
# modify it under the terms of the GNU Lesser General Public |
| 5 |
# License as published by the Free Software Foundation; either |
| 6 |
# version 2.1 of the License, or (at your option) any later version. |
| 7 |
# |
| 8 |
# This library is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
# Lesser General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU Lesser General Public |
| 14 |
# License along with this library; if not, write to the Free Software |
| 15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 |
# |
| 17 |
# See http://trac.edgewall.org/wiki/WikiFormatting |
| 18 |
# |
| 19 |
# Contributor(s): |
| 20 |
# |
| 21 |
|
| 22 |
|
| 23 |
import xml.dom.minidom |
| 24 |
import zipfile |
| 25 |
from collections.abc import Callable |
| 26 |
|
| 27 |
from .elementtypes import empty_elements, inline_elements |
| 28 |
from .namespaces import nsdict |
| 29 |
|
| 30 |
IGNORED_TAGS = [ |
| 31 |
'draw:adraw:g', |
| 32 |
'draw:line', |
| 33 |
'draw:object-ole', |
| 34 |
'office:annotation', |
| 35 |
'presentation:notes', |
| 36 |
'svg:desc', |
| 37 |
] + [nsdict[item[0]] + ':' + item[1] for item in empty_elements] |
| 38 |
|
| 39 |
INLINE_TAGS = [nsdict[item[0]] + ':' + item[1] for item in inline_elements] |
| 40 |
|
| 41 |
|
| 42 |
class TextProps: |
| 43 |
"""Holds properties for a text style.""" |
| 44 |
|
| 45 |
def __init__(self): |
| 46 |
|
| 47 |
self.italic = False |
| 48 |
self.bold = False |
| 49 |
self.fixed = False |
| 50 |
self.underlined = False |
| 51 |
self.strikethrough = False |
| 52 |
self.superscript = False |
| 53 |
self.subscript = False |
| 54 |
|
| 55 |
def setItalic(self, value): |
| 56 |
if value == 'italic': |
| 57 |
self.italic = True |
| 58 |
elif value == 'normal': |
| 59 |
self.italic = False |
| 60 |
|
| 61 |
def setBold(self, value): |
| 62 |
if value == 'bold': |
| 63 |
self.bold = True |
| 64 |
elif value == 'normal': |
| 65 |
self.bold = False |
| 66 |
|
| 67 |
def setFixed(self, value): |
| 68 |
self.fixed = value |
| 69 |
|
| 70 |
def setUnderlined(self, value): |
| 71 |
if value and value != 'none': |
| 72 |
self.underlined = True |
| 73 |
|
| 74 |
def setStrikethrough(self, value): |
| 75 |
if value and value != 'none': |
| 76 |
self.strikethrough = True |
| 77 |
|
| 78 |
def setPosition(self, value): |
| 79 |
if value is None or value == '': |
| 80 |
return |
| 81 |
posisize = value.split(' ') |
| 82 |
textpos = posisize[0] |
| 83 |
if textpos.find('%') == -1: |
| 84 |
if textpos == 'sub': |
| 85 |
self.superscript = False |
| 86 |
self.subscript = True |
| 87 |
elif textpos == 'super': |
| 88 |
self.superscript = True |
| 89 |
self.subscript = False |
| 90 |
else: |
| 91 |
itextpos = int(textpos[: textpos.find('%')]) |
| 92 |
if itextpos > 10: |
| 93 |
self.superscript = False |
| 94 |
self.subscript = True |
| 95 |
elif itextpos < -10: |
| 96 |
self.superscript = True |
| 97 |
self.subscript = False |
| 98 |
|
| 99 |
def __unicode__(self): |
| 100 |
|
| 101 |
return f'[italic={self.italic!s}, bold=i{self.bold!s}, fixed={self.fixed!s}]' |
| 102 |
|
| 103 |
__str__ = __unicode__ |
| 104 |
|
| 105 |
|
| 106 |
class ParagraphProps: |
| 107 |
"""Holds properties of a paragraph style.""" |
| 108 |
|
| 109 |
def __init__(self): |
| 110 |
|
| 111 |
self.blockquote = False |
| 112 |
self.headingLevel = 0 |
| 113 |
self.code = False |
| 114 |
self.title = False |
| 115 |
self.indented = 0 |
| 116 |
|
| 117 |
def setIndented(self, value): |
| 118 |
self.indented = value |
| 119 |
|
| 120 |
def setHeading(self, level): |
| 121 |
self.headingLevel = level |
| 122 |
|
| 123 |
def setTitle(self, value): |
| 124 |
self.title = value |
| 125 |
|
| 126 |
def setCode(self, value): |
| 127 |
self.code = value |
| 128 |
|
| 129 |
def __unicode__(self): |
| 130 |
|
| 131 |
return f'[bq={self.blockquote!s}, h={self.headingLevel}, code={self.code!s}]' |
| 132 |
|
| 133 |
__str__ = __unicode__ |
| 134 |
|
| 135 |
|
| 136 |
class ListProperties: |
| 137 |
"""Holds properties for a list style.""" |
| 138 |
|
| 139 |
def __init__(self): |
| 140 |
self.ordered = False |
| 141 |
|
| 142 |
def setOrdered(self, value): |
| 143 |
self.ordered = value |
| 144 |
|
| 145 |
|
| 146 |
class ODF2MoinMoin: |
| 147 |
def __init__(self, filepath): |
| 148 |
self.footnotes = [] |
| 149 |
self.footnoteCounter = 0 |
| 150 |
self.textStyles = {'Standard': TextProps()} |
| 151 |
self.paragraphStyles = {'Standard': ParagraphProps()} |
| 152 |
self.listStyles = {} |
| 153 |
self.fixedFonts = [] |
| 154 |
self.hasTitle = 0 |
| 155 |
self.lastsegment = None |
| 156 |
|
| 157 |
# Tags |
| 158 |
self.elements: dict[str, Callable[..., str]] = { |
| 159 |
'draw:page': self.textToString, |
| 160 |
'draw:frame': self.textToString, |
| 161 |
'draw:image': self.draw_image, |
| 162 |
'draw:text-box': self.textToString, |
| 163 |
'text:a': self.text_a, |
| 164 |
'text:note': self.text_note, |
| 165 |
} |
| 166 |
for tag in IGNORED_TAGS: |
| 167 |
self.elements[tag] = self.do_nothing |
| 168 |
|
| 169 |
for tag in INLINE_TAGS: |
| 170 |
self.elements[tag] = self.inline_markup |
| 171 |
self.elements['text:line-break'] = self.text_line_break |
| 172 |
self.elements['text:s'] = self.text_s |
| 173 |
self.elements['text:tab'] = self.text_tab |
| 174 |
|
| 175 |
self.load(filepath) |
| 176 |
|
| 177 |
def processFontDeclarations(self, fontDecl): |
| 178 |
"""Extracts necessary font information from a font-declaration |
| 179 |
element. |
| 180 |
""" |
| 181 |
for fontFace in fontDecl.getElementsByTagName('style:font-face'): |
| 182 |
if fontFace.getAttribute('style:font-pitch') == 'fixed': |
| 183 |
self.fixedFonts.append(fontFace.getAttribute('style:name')) |
| 184 |
|
| 185 |
def extractTextProperties(self, style, parent=None): |
| 186 |
"""Extracts text properties from a style element.""" |
| 187 |
|
| 188 |
textProps = TextProps() |
| 189 |
|
| 190 |
textPropEl = style.getElementsByTagName('style:text-properties') |
| 191 |
if not textPropEl: |
| 192 |
return textProps |
| 193 |
|
| 194 |
textPropEl = textPropEl[0] |
| 195 |
|
| 196 |
textProps.setItalic(textPropEl.getAttribute('fo:font-style')) |
| 197 |
textProps.setBold(textPropEl.getAttribute('fo:font-weight')) |
| 198 |
textProps.setUnderlined(textPropEl.getAttribute('style:text-underline-style')) |
| 199 |
textProps.setStrikethrough(textPropEl.getAttribute('style:text-line-through-style')) |
| 200 |
textProps.setPosition(textPropEl.getAttribute('style:text-position')) |
| 201 |
|
| 202 |
if textPropEl.getAttribute('style:font-name') in self.fixedFonts: |
| 203 |
textProps.setFixed(True) |
| 204 |
|
| 205 |
return textProps |
| 206 |
|
| 207 |
def extractParagraphProperties(self, style, parent=None): |
| 208 |
"""Extracts paragraph properties from a style element.""" |
| 209 |
|
| 210 |
paraProps = ParagraphProps() |
| 211 |
|
| 212 |
name = style.getAttribute('style:name') |
| 213 |
|
| 214 |
if name.startswith('Heading_20_'): |
| 215 |
level = name[11:] |
| 216 |
try: |
| 217 |
level = int(level) |
| 218 |
paraProps.setHeading(level) |
| 219 |
except Exception: |
| 220 |
level = 0 |
| 221 |
|
| 222 |
if name == 'Title': |
| 223 |
paraProps.setTitle(True) |
| 224 |
|
| 225 |
paraPropEl = style.getElementsByTagName('style:paragraph-properties') |
| 226 |
if paraPropEl: |
| 227 |
paraPropEl = paraPropEl[0] |
| 228 |
leftMargin = paraPropEl.getAttribute('fo:margin-left') |
| 229 |
if leftMargin: |
| 230 |
try: |
| 231 |
leftMargin = float(leftMargin[:-2]) |
| 232 |
if leftMargin > 0.01: |
| 233 |
paraProps.setIndented(True) |
| 234 |
except Exception: |
| 235 |
pass |
| 236 |
|
| 237 |
textProps = self.extractTextProperties(style) |
| 238 |
if textProps.fixed: |
| 239 |
paraProps.setCode(True) |
| 240 |
|
| 241 |
return paraProps |
| 242 |
|
| 243 |
def processStyles(self, styleElements): |
| 244 |
'''Runs through "style" elements extracting necessary information.''' |
| 245 |
|
| 246 |
for style in styleElements: |
| 247 |
name = style.getAttribute('style:name') |
| 248 |
|
| 249 |
if name == 'Standard': |
| 250 |
continue |
| 251 |
|
| 252 |
family = style.getAttribute('style:family') |
| 253 |
parent = style.getAttribute('style:parent-style-name') |
| 254 |
|
| 255 |
if family == 'text': |
| 256 |
self.textStyles[name] = self.extractTextProperties(style, parent) |
| 257 |
|
| 258 |
elif family == 'paragraph': |
| 259 |
self.paragraphStyles[name] = self.extractParagraphProperties(style, parent) |
| 260 |
self.textStyles[name] = self.extractTextProperties(style, parent) |
| 261 |
|
| 262 |
def processListStyles(self, listStyleElements): |
| 263 |
|
| 264 |
for style in listStyleElements: |
| 265 |
name = style.getAttribute('style:name') |
| 266 |
|
| 267 |
prop = ListProperties() |
| 268 |
if style.hasChildNodes(): |
| 269 |
subitems = [el for el in style.childNodes if el.nodeType == xml.dom.Node.ELEMENT_NODE and el.tagName == 'text:list-level-style-number'] |
| 270 |
if len(subitems) > 0: |
| 271 |
prop.setOrdered(True) |
| 272 |
|
| 273 |
self.listStyles[name] = prop |
| 274 |
|
| 275 |
def load(self, filepath): |
| 276 |
"""Loads an ODT file.""" |
| 277 |
|
| 278 |
zip = zipfile.ZipFile(filepath) |
| 279 |
|
| 280 |
styles_doc = xml.dom.minidom.parseString(zip.read('styles.xml')) |
| 281 |
fontfacedecls = styles_doc.getElementsByTagName('office:font-face-decls') |
| 282 |
if fontfacedecls: |
| 283 |
self.processFontDeclarations(fontfacedecls[0]) |
| 284 |
self.processStyles(styles_doc.getElementsByTagName('style:style')) |
| 285 |
self.processListStyles(styles_doc.getElementsByTagName('text:list-style')) |
| 286 |
|
| 287 |
self.content = xml.dom.minidom.parseString(zip.read('content.xml')) |
| 288 |
fontfacedecls = self.content.getElementsByTagName('office:font-face-decls') |
| 289 |
if fontfacedecls: |
| 290 |
self.processFontDeclarations(fontfacedecls[0]) |
| 291 |
|
| 292 |
self.processStyles(self.content.getElementsByTagName('style:style')) |
| 293 |
self.processListStyles(self.content.getElementsByTagName('text:list-style')) |
| 294 |
|
| 295 |
def compressCodeBlocks(self, text): |
| 296 |
"""Removes extra blank lines from code blocks.""" |
| 297 |
|
| 298 |
return text |
| 299 |
lines = text.split('\n') |
| 300 |
buffer = [] |
| 301 |
numLines = len(lines) |
| 302 |
for i in range(numLines): |
| 303 |
if lines[i].strip() or i == numLines - 1 or i == 0 or not (lines[i - 1].startswith(' ') and lines[i + 1].startswith(' ')): |
| 304 |
buffer.append('\n' + lines[i]) |
| 305 |
|
| 306 |
return ''.join(buffer) |
| 307 |
|
| 308 |
# ----------------------------------- |
| 309 |
def do_nothing(self, node): |
| 310 |
return '' |
| 311 |
|
| 312 |
def draw_image(self, node): |
| 313 |
""" """ |
| 314 |
|
| 315 |
link = node.getAttribute('xlink:href') |
| 316 |
if link and link[:2] == './': # Indicates a sub-object, which isn't supported |
| 317 |
return f'{link}\n' |
| 318 |
if link and link[:9] == 'Pictures/': |
| 319 |
link = link[9:] |
| 320 |
return f'[[Image({link})]]\n' |
| 321 |
|
| 322 |
def text_a(self, node): |
| 323 |
text = self.textToString(node) |
| 324 |
link = node.getAttribute('xlink:href') |
| 325 |
if link.strip() == text.strip(): |
| 326 |
return f'[{link.strip()}] ' |
| 327 |
else: |
| 328 |
return f'[{link.strip()} {text.strip()}] ' |
| 329 |
|
| 330 |
def text_line_break(self, node): |
| 331 |
return '[[BR]]' |
| 332 |
|
| 333 |
def text_note(self, node): |
| 334 |
cite = node.getElementsByTagName('text:note-citation')[0].childNodes[0].nodeValue |
| 335 |
body = node.getElementsByTagName('text:note-body')[0].childNodes[0] |
| 336 |
self.footnotes.append((cite, self.textToString(body))) |
| 337 |
return f'^{cite}^' |
| 338 |
|
| 339 |
def text_s(self, node): |
| 340 |
try: |
| 341 |
num = int(node.getAttribute('text:c')) |
| 342 |
return ' ' * num |
| 343 |
except Exception: |
| 344 |
return ' ' |
| 345 |
|
| 346 |
def text_tab(self, node): |
| 347 |
return ' ' |
| 348 |
|
| 349 |
def inline_markup(self, node): |
| 350 |
text = self.textToString(node) |
| 351 |
|
| 352 |
if not text.strip(): |
| 353 |
return '' # don't apply styles to white space |
| 354 |
|
| 355 |
styleName = node.getAttribute('text:style-name') |
| 356 |
style = self.textStyles.get(styleName, TextProps()) |
| 357 |
|
| 358 |
if style.fixed: |
| 359 |
return '`' + text + '`' |
| 360 |
|
| 361 |
mark = [] |
| 362 |
if style: |
| 363 |
if style.italic: |
| 364 |
mark.append("''") |
| 365 |
if style.bold: |
| 366 |
mark.append("'''") |
| 367 |
if style.underlined: |
| 368 |
mark.append('__') |
| 369 |
if style.strikethrough: |
| 370 |
mark.append('~~') |
| 371 |
if style.superscript: |
| 372 |
mark.append('^') |
| 373 |
if style.subscript: |
| 374 |
mark.append(',,') |
| 375 |
revmark = mark[:] |
| 376 |
revmark.reverse() |
| 377 |
return '{}{}{}'.format(''.join(mark), text, ''.join(revmark)) |
| 378 |
|
| 379 |
# ----------------------------------- |
| 380 |
def listToString(self, listElement, indent=0): |
| 381 |
|
| 382 |
self.lastsegment = listElement.tagName |
| 383 |
buffer = [] |
| 384 |
|
| 385 |
styleName = listElement.getAttribute('text:style-name') |
| 386 |
props = self.listStyles.get(styleName, ListProperties()) |
| 387 |
|
| 388 |
i = 0 |
| 389 |
for item in listElement.childNodes: |
| 390 |
buffer.append(' ' * indent) |
| 391 |
i += 1 |
| 392 |
if props.ordered: |
| 393 |
number = str(i) |
| 394 |
number = ' ' + number + '. ' |
| 395 |
buffer.append(' 1. ') |
| 396 |
else: |
| 397 |
buffer.append(' * ') |
| 398 |
subitems = [el for el in item.childNodes if el.tagName in ['text:p', 'text:h', 'text:list']] |
| 399 |
for subitem in subitems: |
| 400 |
if subitem.tagName == 'text:list': |
| 401 |
buffer.append('\n') |
| 402 |
buffer.append(self.listToString(subitem, indent + 3)) |
| 403 |
else: |
| 404 |
buffer.append(self.paragraphToString(subitem, indent + 3)) |
| 405 |
self.lastsegment = subitem.tagName |
| 406 |
self.lastsegment = item.tagName |
| 407 |
buffer.append('\n') |
| 408 |
|
| 409 |
return ''.join(buffer) |
| 410 |
|
| 411 |
def tableToString(self, tableElement): |
| 412 |
"""MoinMoin uses || to delimit table cells""" |
| 413 |
|
| 414 |
self.lastsegment = tableElement.tagName |
| 415 |
buffer = [] |
| 416 |
|
| 417 |
for item in tableElement.childNodes: |
| 418 |
self.lastsegment = item.tagName |
| 419 |
if item.tagName == 'table:table-header-rows': |
| 420 |
buffer.append(self.tableToString(item)) |
| 421 |
if item.tagName == 'table:table-row': |
| 422 |
buffer.append('\n||') |
| 423 |
for cell in item.childNodes: |
| 424 |
buffer.append(self.inline_markup(cell)) |
| 425 |
buffer.append('||') |
| 426 |
self.lastsegment = cell.tagName |
| 427 |
return ''.join(buffer) |
| 428 |
|
| 429 |
def toString(self): |
| 430 |
"""Converts the document to a string. |
| 431 |
FIXME: Result from second call differs from first call |
| 432 |
""" |
| 433 |
body = self.content.getElementsByTagName('office:body')[0] |
| 434 |
text = body.childNodes[0] |
| 435 |
|
| 436 |
buffer = [] |
| 437 |
|
| 438 |
paragraphs = [ |
| 439 |
el |
| 440 |
for el in text.childNodes |
| 441 |
if isinstance(el, xml.dom.minidom.Element) and el.tagName in ['draw:page', 'text:p', 'text:h', 'text:section', 'text:list', 'table:table'] |
| 442 |
] |
| 443 |
|
| 444 |
for paragraph in paragraphs: |
| 445 |
if paragraph.tagName == 'text:list': |
| 446 |
text = self.listToString(paragraph) |
| 447 |
elif paragraph.tagName == 'text:section': |
| 448 |
text = self.textToString(paragraph) |
| 449 |
elif paragraph.tagName == 'table:table': |
| 450 |
text = self.tableToString(paragraph) |
| 451 |
else: |
| 452 |
text = self.paragraphToString(paragraph) |
| 453 |
if text: |
| 454 |
buffer.append(text) |
| 455 |
|
| 456 |
if self.footnotes: |
| 457 |
buffer.append('----') |
| 458 |
for cite, body in self.footnotes: |
| 459 |
buffer.append(f'{cite}: {body}') |
| 460 |
|
| 461 |
buffer.append('') |
| 462 |
return self.compressCodeBlocks('\n'.join(buffer)) |
| 463 |
|
| 464 |
def textToString(self, element): |
| 465 |
|
| 466 |
buffer = [] |
| 467 |
|
| 468 |
for node in element.childNodes: |
| 469 |
if node.nodeType == xml.dom.Node.TEXT_NODE: |
| 470 |
buffer.append(node.nodeValue) |
| 471 |
|
| 472 |
elif node.nodeType == xml.dom.Node.ELEMENT_NODE: |
| 473 |
tag = node.tagName |
| 474 |
|
| 475 |
if tag in ('draw:text-box', 'draw:frame'): |
| 476 |
buffer.append(self.textToString(node)) |
| 477 |
|
| 478 |
elif tag in ('text:p', 'text:h'): |
| 479 |
text = self.paragraphToString(node) |
| 480 |
if text: |
| 481 |
buffer.append(text) |
| 482 |
elif tag == 'text:list': |
| 483 |
buffer.append(self.listToString(node)) |
| 484 |
else: |
| 485 |
method = self.elements.get(tag) |
| 486 |
if method: |
| 487 |
buffer.append(method(node)) |
| 488 |
else: |
| 489 |
buffer.append(' {' + tag + '} ') |
| 490 |
|
| 491 |
return ''.join(buffer) |
| 492 |
|
| 493 |
def paragraphToString(self, paragraph, indent=0): |
| 494 |
|
| 495 |
dummyParaProps = ParagraphProps() |
| 496 |
|
| 497 |
style_name = paragraph.getAttribute('text:style-name') |
| 498 |
paraProps = self.paragraphStyles.get(style_name, dummyParaProps) |
| 499 |
text = self.inline_markup(paragraph) |
| 500 |
|
| 501 |
if paraProps and not paraProps.code: |
| 502 |
text = text.strip() |
| 503 |
|
| 504 |
if paragraph.tagName == 'text:p' and self.lastsegment == 'text:p': |
| 505 |
text = '\n' + text |
| 506 |
|
| 507 |
self.lastsegment = paragraph.tagName |
| 508 |
|
| 509 |
if paraProps.title: |
| 510 |
self.hasTitle = 1 |
| 511 |
return '= ' + text + ' =\n' |
| 512 |
|
| 513 |
outlinelevel = paragraph.getAttribute('text:outline-level') |
| 514 |
if outlinelevel: |
| 515 |
level = int(outlinelevel) |
| 516 |
if self.hasTitle: |
| 517 |
level += 1 |
| 518 |
|
| 519 |
if level >= 1: |
| 520 |
return '=' * level + ' ' + text + ' ' + '=' * level + '\n' |
| 521 |
|
| 522 |
elif paraProps.code: |
| 523 |
return '{{{\n' + text + '\n}}}\n' |
| 524 |
|
| 525 |
if paraProps.indented: |
| 526 |
return self.wrapParagraph(text, indent=indent, blockquote=True) |
| 527 |
|
| 528 |
else: |
| 529 |
return self.wrapParagraph(text, indent=indent) |
| 530 |
|
| 531 |
def wrapParagraph(self, text, indent=0, blockquote=False): |
| 532 |
|
| 533 |
counter = 0 |
| 534 |
buffer = [] |
| 535 |
LIMIT = 50 |
| 536 |
|
| 537 |
if blockquote: |
| 538 |
buffer.append(' ') |
| 539 |
|
| 540 |
return ''.join(buffer) + text |
| 541 |
# Unused from here |
| 542 |
for token in text.split(): |
| 543 |
if counter > LIMIT - indent: |
| 544 |
buffer.append('\n' + ' ' * indent) |
| 545 |
if blockquote: |
| 546 |
buffer.append(' ') |
| 547 |
counter = 0 |
| 548 |
|
| 549 |
buffer.append(token + ' ') |
| 550 |
counter += len(token) |
| 551 |
|
| 552 |
return ''.join(buffer) |