manuskript/manuskript/exporter/arbo.py

49 lines
1.2 KiB
Python
Raw Normal View History

2015-07-01 23:14:03 +12:00
#!/usr/bin/env python
2016-02-07 00:34:22 +13:00
# --!-- coding: utf8 --!--
import os
from manuskript.functions import mainWindow
2015-07-01 23:14:03 +12:00
class arboExporter():
requires = ["path"]
2016-02-07 00:34:22 +13:00
2015-07-01 23:14:03 +12:00
def __init__(self):
pass
2016-02-07 00:34:22 +13:00
2015-07-01 23:14:03 +12:00
def doCompile(self, path):
2016-02-07 00:34:22 +13:00
# FIXME: overwrites when items have identical names
2015-07-01 23:14:03 +12:00
mw = mainWindow()
root = mw.mdlOutline.rootItem
2016-02-07 00:34:22 +13:00
2015-07-01 23:14:03 +12:00
def writeItem(item, path):
if item.isFolder():
path2 = os.path.join(path, item.title())
2016-02-07 00:34:22 +13:00
2015-07-01 23:14:03 +12:00
try:
os.mkdir(path2)
except FileExistsError:
pass
2016-02-07 00:34:22 +13:00
2015-07-01 23:14:03 +12:00
for c in item.children():
writeItem(c, path2)
2016-02-07 00:34:22 +13:00
2015-07-01 23:14:03 +12:00
else:
ext = ".t2t" if item.isT2T() else \
2016-02-07 00:34:22 +13:00
".html" if item.isHTML() else \
".txt"
2015-07-01 23:14:03 +12:00
path2 = os.path.join(path, item.title() + ext)
f = open(path2, "w")
text = self.formatText(item.text(), item.type())
f.write(text)
2016-02-07 00:34:22 +13:00
2015-07-01 23:14:03 +12:00
for c in root.children():
writeItem(c, path)
2016-02-07 00:34:22 +13:00
2015-07-01 23:14:03 +12:00
def formatText(self, text, _type):
if _type == "t2t":
# Empty lines for headers
text = "\n\n\n" + text
2016-02-07 00:34:22 +13:00
2015-07-01 23:14:03 +12:00
return text