manuskript/src/exporter/html.py
2015-07-01 13:14:03 +02:00

87 lines
2.3 KiB
Python

#!/usr/bin/env python
#--!-- coding: utf8 --!--
from qt import *
from enums import *
from functions import *
from exporter.basic import basicExporter
import subprocess
class htmlExporter(basicExporter):
requires = ["filename"]
def __init__(self):
pass
def doCompile(self, filename):
mw = mainWindow()
root = mw.mdlOutline.rootItem
html = ""
def appendItem(item):
if item.isFolder():
html = ""
title = "<h{l}>{t}</h{l}>\n".format(
l = str(item.level() + 1),
t = item.title())
html += title
for c in item.children():
html += appendItem(c)
return html
else:
text = self.formatText(item.text(), item.type())
return text
for c in root.children():
html += appendItem(c)
template = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>{TITLE}</title>
</head>
<body>
{BODY}
</body>
</html>"""
f = open(filename, "w")
f.write(template.format(
TITLE="FIXME",
BODY=html))
def formatText(self, text, _type):
if not text:
return text
if _type == "t2t":
text = self.runT2T(text)
elif _type == "txt":
text = text.replace("\n", "<br>")
return text + "<br>"
def runT2T(self, text, target="html"):
cmdl = ['txt2tags', '-t', target, '--enc=utf-8', '--no-headers', '-o', '-', '-']
cmd = subprocess.Popen(('echo', text), stdout=subprocess.PIPE)
try:
output = subprocess.check_output(cmdl, stdin=cmd.stdout, stderr=subprocess.STDOUT) # , cwd="/tmp"
except subprocess.CalledProcessError as e:
print("Error!")
return text
cmd.wait()
return output.decode("utf-8")