Adds: import from many formats using pandoc. #200

This commit is contained in:
Olivier Keshavjee 2017-11-07 20:30:39 +01:00
parent 9eb1402613
commit ccf33b3ccf
6 changed files with 160 additions and 16 deletions

View file

@ -129,7 +129,7 @@ class basicFormat:
@classmethod
def isValid(cls):
return True
@classmethod
def projectPath(cls):
return os.path.dirname(os.path.abspath(mainWindow().currentProject))

View file

@ -4,9 +4,22 @@
from manuskript.importer.folderImporter import folderImporter
from manuskript.importer.markdownImporter import markdownImporter
from manuskript.importer.opmlImporter import opmlImporter
from manuskript.importer.pandocImporters import markdownPandocImporter, \
odtPandocImporter, ePubPandocImporter, docXPandocImporter, HTMLPandocImporter, \
rstPandocImporter, LaTeXPandocImporter
importers = [
#markdownImporter,
# Internal
opmlImporter,
#folderImporter,
folderImporter,
markdownImporter,
# Pandoc
markdownPandocImporter,
odtPandocImporter,
ePubPandocImporter,
docXPandocImporter,
HTMLPandocImporter,
rstPandocImporter,
LaTeXPandocImporter,
]

View file

@ -22,7 +22,8 @@ class abstractImporter:
# For folder, use "<<folder>>"
icon = ""
def startImport(self, filePath, settingsWidget):
@classmethod
def startImport(cls, filePath, parentItem, settingsWidget):
"""
Takes a str path to the file/folder to import, and the settingsWidget
returnend by `self.settingsWidget()` containing the user set settings,
@ -37,4 +38,8 @@ class abstractImporter:
"""
return None
@classmethod
def isValid(cls):
return False

View file

@ -16,22 +16,36 @@ class opmlImporter(abstractImporter):
icon = "text-x-opml+xml"
@classmethod
def startImport(cls, filePath, parentItem, settingsWidget):
def isValid(cls):
return True
@classmethod
def startImport(cls, filePath, parentItem, settingsWidget, fromString=None):
"""
Import/export outline cards in OPML format.
"""
ret = False
try:
with open(filePath, 'rb') as opmlFile:
#opmlContent = cls.saveNewlines(opmlFile.read())
opmlContent = opmlFile.read()
except:
QMessageBox.critical(settingsWidget,
qApp.translate("Import", "OPML Import"),
qApp.translate("Import", "File open failed."))
if filePath != "":
# We have a filePath, so we read the file
try:
with open(filePath, 'rb') as opmlFile:
#opmlContent = cls.saveNewlines(opmlFile.read())
opmlContent = opmlFile.read()
except:
QMessageBox.critical(settingsWidget,
qApp.translate("Import", "OPML Import"),
qApp.translate("Import", "File open failed."))
return None
elif fromString == "":
# We have neither filePath nor fromString, so we leave
return None
else:
# We load from string
opmlContent = bytes(fromString, "utf-8")
parsed = ET.fromstring(opmlContent)
opmlNode = parsed

View file

@ -0,0 +1,91 @@
#!/usr/bin/env python
# --!-- coding: utf8 --!--
from manuskript.importer.abstractImporter import abstractImporter
from manuskript.exporter.pandoc import pandocExporter
from manuskript.importer.opmlImporter import opmlImporter
class pandocImporter(abstractImporter):
formatFrom = ""
@classmethod
def isValid(cls):
return pandocExporter().isValid()
@classmethod
def startImport(cls, filePath, parentItem, settingsWidget):
# pandoc --from=markdown filename --to=opml --standalone
args = [
"--from={}".format(cls.formatFrom),
filePath,
"--to=opml",
"--standalone"
]
r = pandocExporter().run(args)
return opmlImporter.startImport("", parentItem,
settingsWidget, fromString=r)
class markdownPandocImporter(pandocImporter):
name = "Markdown (pandoc)"
description = "Markdown, using pandoc"
fileFormat = "Markdown files (*.md *.txt *)"
icon = "text-x-markdown"
formatFrom = "markdown"
class ePubPandocImporter(pandocImporter):
name = "ePub (pandoc)"
description = ""
fileFormat = "ePub files (*.epub)"
icon = "application-epub+zip"
formatFrom = "epub"
class docXPandocImporter(pandocImporter):
name = "DocX (pandoc)"
description = ""
fileFormat = "DocX files (*.docx)"
icon = "application-vnd.openxmlformats-officedocument.wordprocessingml.document"
formatFrom = "docx"
class odtPandocImporter(pandocImporter):
name = "ODT (pandoc)"
description = ""
fileFormat = "Open Document files (*.odt)"
icon = "application-vnd.oasis.opendocument.text"
formatFrom = "odt"
class rstPandocImporter(pandocImporter):
name = "reStructuredText (pandoc)"
description = ""
fileFormat = "reStructuredText files (*.rst)"
icon = "text-plain"
formatFrom = "rst"
class HTMLPandocImporter(pandocImporter):
name = "HTML (pandoc)"
description = ""
fileFormat = "HTML files (*.htm *.html)"
icon = "text-html"
formatFrom = "html"
class LaTeXPandocImporter(pandocImporter):
name = "LaTeX (pandoc)"
description = ""
fileFormat = "LaTeX files (*.tex)"
icon = "text-x-tex"
formatFrom = "latex"

View file

@ -3,9 +3,9 @@
import json
import os
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QBrush, QColor, QIcon
from PyQt5.QtWidgets import QWidget, QFileDialog, QMessageBox
from PyQt5.QtCore import Qt, QTimer, QUrl
from PyQt5.QtGui import QBrush, QColor, QIcon, QDesktopServices
from PyQt5.QtWidgets import QWidget, QFileDialog, QMessageBox, QStyle
from manuskript.functions import lightBlue, writablePath, appPath
from manuskript.ui.importers.importer_ui import Ui_importer
@ -14,6 +14,7 @@ from manuskript.ui import style
from manuskript import importer
from manuskript.models.outlineModel import outlineModel
from manuskript.enums import Outline
from manuskript.exporter.pandoc import pandocExporter
class importerDialog(QWidget, Ui_importer):
@ -68,6 +69,15 @@ class importerDialog(QWidget, Ui_importer):
for f in self.importers:
addFormat(f.name, f.icon)
if not f.isValid():
item = self.cmbImporters.model().item(self.cmbImporters.count() - 1)
item.setFlags(Qt.NoItemFlags)
if not pandocExporter().isValid():
self.cmbImporters.addItem(
self.style().standardIcon(QStyle.SP_MessageBoxWarning),
"Install pandoc to import from much more formats",
"::URL::http://pandoc.org/installing.html")
def currentFormat(self):
formatName = self.cmbImporters.currentText()
@ -141,7 +151,15 @@ class importerDialog(QWidget, Ui_importer):
settings widget using the current format provided settings widget.
"""
# We check if we have to open an URL
data = self.cmbImporters.currentData()
if data and data[:7] == "::URL::" and data[7:]:
# FIXME: use functions.openURL after merge with feature/Exporters
QDesktopServices.openUrl(QUrl(data[7:]))
return
F = self.currentFormat()
self.settingsWidget = generalSettings()
self.setGroupWidget(self.grpSettings, self.settingsWidget)
self.grpSettings.setMinimumWidth(200)
@ -152,6 +170,9 @@ class importerDialog(QWidget, Ui_importer):
#toolBox.insertItem(toolBox.count(), w, "Pandoc")
#See pandoc's abstractPlainText
# Clear file name
self.setFileName("")
def setGroupWidget(self, group, widget):
"""
Sets the given widget as main widget for QGroupBox group.