manuskript/manuskript/exporter/pandoc/__init__.py

116 lines
3.6 KiB
Python
Raw Normal View History

2016-04-02 06:01:27 +13:00
#!/usr/bin/env python
# --!-- coding: utf8 --!--
2016-04-15 21:58:09 +12:00
import subprocess
2016-04-15 00:30:31 +12:00
2016-04-15 21:58:09 +12:00
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QCursor
from PyQt5.QtWidgets import qApp, QMessageBox
2016-04-02 06:01:27 +13:00
2016-04-15 21:58:09 +12:00
from manuskript.exporter.basic import basicExporter, basicFormat
from manuskript.exporter.pandoc.HTML import HTML
from manuskript.exporter.pandoc.PDF import PDF
from manuskript.exporter.pandoc.outputFormats import ePub, OpenDocument, DocX
2017-10-12 19:36:10 +13:00
from manuskript.exporter.pandoc.plainText import reST, markdown, latex, OPML
from manuskript.functions import mainWindow, safeTranslate
2016-04-02 06:01:27 +13:00
import logging
LOGGER = logging.getLogger(__name__)
2016-04-02 06:01:27 +13:00
class pandocExporter(basicExporter):
name = "Pandoc"
description = safeTranslate(qApp, "Export", """<p>A universal document converter. Can be used to convert Markdown to a wide range of other
2016-04-02 06:01:27 +13:00
formats.</p>
<p>Website: <a href="http://www.pandoc.org">http://pandoc.org/</a></p>
2016-04-08 00:27:51 +12:00
""")
2016-04-02 06:01:27 +13:00
cmd = "pandoc"
absentTip = "Install pandoc to benefit from a wide range of export formats (DocX, ePub, PDF, etc.)"
absentURL = "http://pandoc.org/installing.html"
2016-04-02 06:01:27 +13:00
2016-04-12 19:15:01 +12:00
def __init__(self):
basicExporter.__init__(self)
2016-04-15 21:58:09 +12:00
self.exportTo = [
markdown(self),
latex(self),
HTML(self),
ePub(self),
OpenDocument(self),
DocX(self),
PDF(self),
reST(self),
2017-10-12 19:36:10 +13:00
OPML(self),
2016-04-15 21:58:09 +12:00
]
2016-04-12 19:15:01 +12:00
def version(self):
if self.isValid():
2017-11-04 20:26:42 +13:00
r = self.run(["--version"])
2016-04-02 06:01:27 +13:00
return r.split("\n")[0]
else:
return ""
2016-04-15 21:58:09 +12:00
def convert(self, src, args, outputfile=None):
if self.isValid() == 2:
run = self.cmd
elif self.isValid() == 1:
run = self.customPath
else:
LOGGER.error("No command for pandoc.")
return None
args = [run] + args
2016-04-15 21:58:09 +12:00
if outputfile:
args.append("--output={}".format(outputfile))
for name, col, var in [
("Title", 0, "title"),
("Subtitle", 1, "subtitle"),
("Serie", 2, ""),
("Volume", 3, ""),
("Genre", 4, ""),
("License", 5, ""),
("Author", 6, "author"),
("Email", 7, ""),
]:
item = mainWindow().mdlFlatData.item(0, col)
if var and item and item.text().strip():
args.append("--variable={}:{}".format(var, item.text().strip()))
# Add title metadata required for pandoc >= 2.x
title = "Untitled"
if mainWindow().mdlFlatData.item(0, 0):
title = mainWindow().mdlFlatData.item(0, 0).text().strip()
args.append("--metadata=title:{}".format(title))
2016-04-15 21:58:09 +12:00
qApp.setOverrideCursor(QCursor(Qt.WaitCursor))
p = subprocess.Popen(
args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
if not type(src) == bytes:
src = src.encode("utf-8") # assumes utf-8
stdout, stderr = p.communicate(src)
qApp.restoreOverrideCursor()
if stderr or p.returncode != 0:
err_type = "ERROR" if p.returncode != 0 else "WARNING"
err = "%s on export\n" % err_type \
+ "Return code: %d\n" % p.returncode \
+ "Command and parameters:\n%s\n" % p.args \
+ "Stderr content:\n" + stderr.decode("utf-8")
if p.returncode != 0:
LOGGER.error(err)
QMessageBox.critical(mainWindow().dialog, safeTranslate(qApp, "Export", "Error"), err)
else:
LOGGER.warning(err)
2016-04-15 21:58:09 +12:00
return None
2016-04-02 06:01:27 +13:00
2016-04-15 21:58:09 +12:00
return stdout.decode("utf-8")
2016-04-02 06:01:27 +13:00