manuskript/manuskript/exporter/odt_old.py

60 lines
1.5 KiB
Python
Raw Normal View History

2015-07-10 22:29:25 +12:00
#!/usr/bin/env python
2016-02-07 00:34:22 +13:00
# --!-- coding: utf8 --!--
from PyQt5.QtGui import QTextDocument, QTextCursor, QTextDocumentWriter
from manuskript.exporter.basic import basicExporter
from manuskript.functions import mainWindow
2015-07-10 22:29:25 +12:00
class odtExporter(basicExporter):
requires = ["filename"]
2016-02-07 00:34:22 +13:00
2015-07-10 22:29:25 +12:00
def __init__(self):
pass
2016-02-07 00:34:22 +13:00
2015-07-10 22:29:25 +12:00
def doCompile(self, filename):
mw = mainWindow()
root = mw.mdlOutline.rootItem
2016-02-07 00:34:22 +13:00
2015-07-10 22:29:25 +12:00
doc = QTextDocument()
cursor = QTextCursor(doc)
2016-02-07 00:34:22 +13:00
2015-07-10 22:29:25 +12:00
def appendItem(item):
if item.isFolder():
2016-02-07 00:34:22 +13:00
2015-07-10 22:29:25 +12:00
cursor.setPosition(doc.characterCount() - 1)
title = "<h{l}>{t}</h{l}><br>\n".format(
2016-02-07 00:34:22 +13:00
l=str(item.level() + 1),
t=item.title())
2015-07-10 22:29:25 +12:00
cursor.insertHtml(title)
2016-02-07 00:34:22 +13:00
2015-07-10 22:29:25 +12:00
for c in item.children():
appendItem(c)
2016-02-07 00:34:22 +13:00
2015-07-10 22:29:25 +12:00
else:
text = self.formatText(item.text(), item.type())
cursor.setPosition(doc.characterCount() - 1)
cursor.insertHtml(text)
2016-02-07 00:34:22 +13:00
2015-07-10 22:29:25 +12:00
for c in root.children():
appendItem(c)
2016-02-07 00:34:22 +13:00
2015-07-10 22:29:25 +12:00
dw = QTextDocumentWriter(filename, "odt")
dw.write(doc)
2016-02-07 00:34:22 +13:00
2015-07-10 22:29:25 +12:00
def formatText(self, text, _type):
2016-02-07 00:34:22 +13:00
2015-07-10 22:29:25 +12:00
if not text:
return text
2016-02-07 00:34:22 +13:00
2015-07-10 22:29:25 +12:00
if _type == "t2t":
text = self.runT2T(text)
2016-02-07 00:34:22 +13:00
2015-07-10 22:29:25 +12:00
elif _type == "txt":
text = text.replace("\n", "<br>")
2016-02-07 00:34:22 +13:00
2015-07-10 22:29:25 +12:00
elif _type == "html":
text = self.htmlBody(text)
2016-02-07 00:34:22 +13:00
2015-07-10 22:29:25 +12:00
return text + "<br>"