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
|
2016-04-15 21:58:09 +12:00
|
|
|
from manuskript.functions import mainWindow
|
2016-04-02 06:01:27 +13:00
|
|
|
|
|
|
|
|
|
|
|
class pandocExporter(basicExporter):
|
|
|
|
|
|
|
|
name = "Pandoc"
|
2019-08-31 04:31:50 +12:00
|
|
|
description = qApp.translate("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"
|
2017-11-06 22:05:20 +13:00
|
|
|
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):
|
2019-05-10 06:24:52 +12:00
|
|
|
if self.isValid() == 2:
|
|
|
|
run = self.cmd
|
|
|
|
elif self.isValid() == 1:
|
|
|
|
run = self.customPath
|
|
|
|
else:
|
|
|
|
print("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))
|
|
|
|
|
2017-10-21 12:01:26 +13:00
|
|
|
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()))
|
|
|
|
|
2019-05-23 04:07:33 +12:00
|
|
|
# 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))
|
2018-11-20 07:29:36 +13:00
|
|
|
|
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()
|
|
|
|
|
2018-09-13 07:03:26 +12:00
|
|
|
if stderr or p.returncode != 0:
|
|
|
|
err = "ERROR on export" + "\n" \
|
|
|
|
+ "Return code" + ": %d\n" % (p.returncode) \
|
|
|
|
+ "Command and parameters" + ":\n%s\n" % (p.args) \
|
|
|
|
+ "Stderr content" + ":\n" + stderr.decode("utf-8")
|
2016-04-15 21:58:09 +12:00
|
|
|
print(err)
|
|
|
|
QMessageBox.critical(mainWindow().dialog, qApp.translate("Export", "Error"), err)
|
|
|
|
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
|
|
|
|