mirror of
https://github.com/olivierkes/manuskript.git
synced 2024-05-21 21:32:26 +12:00
Adds markdown exporter
This commit is contained in:
parent
16585c4a2a
commit
a956856da9
|
@ -1,17 +1,19 @@
|
|||
#!/usr/bin/env python
|
||||
# --!-- coding: utf8 --!--
|
||||
from PyQt5.QtWidgets import QTextEdit
|
||||
from PyQt5.QtWidgets import QTextEdit, qApp
|
||||
|
||||
from manuskript.exporter.basic import basicExporter, basicFormat
|
||||
from manuskript.exporter.manuskript.markdown import markdown
|
||||
from manuskript.exporter.manuskript.plainText import plainText
|
||||
|
||||
|
||||
class manuskriptExporter(basicExporter):
|
||||
|
||||
name = "Manuskript"
|
||||
description = "Default exporter, provides basic formats used by other exporters."
|
||||
description = qApp.tr("Default exporter, provides basic formats used by other exporters.")
|
||||
exportTo = [
|
||||
plainText,
|
||||
markdown,
|
||||
basicFormat("HTML"),
|
||||
basicFormat("OPML")
|
||||
]
|
||||
|
|
92
manuskript/exporter/manuskript/markdown.py
Normal file
92
manuskript/exporter/manuskript/markdown.py
Normal file
|
@ -0,0 +1,92 @@
|
|||
#!/usr/bin/env python
|
||||
# --!-- coding: utf8 --!--
|
||||
from PyQt5.QtGui import QTextCharFormat, QFont
|
||||
from PyQt5.QtWidgets import QPlainTextEdit, QGroupBox, qApp, QVBoxLayout, QCheckBox
|
||||
|
||||
from manuskript.exporter.manuskript.plainText import plainText
|
||||
from manuskript.functions import mainWindow
|
||||
from manuskript.ui.editors.MMDHighlighter import MMDHighlighter
|
||||
from manuskript.ui.exporters.manuskript.plainTextSettings import exporterSettings
|
||||
|
||||
|
||||
class markdown(plainText):
|
||||
name = qApp.tr("Markdown")
|
||||
description = qApp.tr("Just like plain text, excepts adds markdown titles. \
|
||||
Presupposes that texts are formatted in markdown.")
|
||||
|
||||
@classmethod
|
||||
def settingsWidget(cls):
|
||||
w = markdownSettings(cls)
|
||||
w.loadSettings()
|
||||
return w
|
||||
|
||||
@classmethod
|
||||
def preview(cls, settingsWidget, previewWidget):
|
||||
# plainText.preview(settingsWidget, previewWidget)
|
||||
# Don't know why, if I call superclass here, then concatenate does not call this processTitle function,
|
||||
# But that of of plainText.
|
||||
|
||||
# ---------------------------
|
||||
# From plainText:
|
||||
|
||||
settings = settingsWidget.getSettings()
|
||||
|
||||
# Save settings
|
||||
settingsWidget.writeSettings()
|
||||
|
||||
# ---------------------------
|
||||
# Specific to markdown
|
||||
|
||||
s = settingsWidget.settings
|
||||
if s["Preview"]["MarkdownHighlighter"]:
|
||||
cls.highlighter = MMDHighlighter(previewWidget)
|
||||
else:
|
||||
cls.highlighter = None
|
||||
|
||||
# ---------------------------
|
||||
# From plainText:
|
||||
|
||||
r = cls.concatenate(mainWindow().mdlOutline.rootItem, settings)
|
||||
|
||||
# Set preview font
|
||||
cf = QTextCharFormat()
|
||||
f = QFont()
|
||||
f.fromString(settings["Preview"]["PreviewFont"])
|
||||
cf.setFont(f)
|
||||
previewWidget.setCurrentCharFormat(cf)
|
||||
|
||||
previewWidget.setPlainText(r)
|
||||
|
||||
@classmethod
|
||||
def processTitle(cls, text, level, settings):
|
||||
return "{} {}\n".format(
|
||||
"#" * (level + 1),
|
||||
text
|
||||
)
|
||||
|
||||
|
||||
class markdownSettings(exporterSettings):
|
||||
def __init__(self, _format, parent=None):
|
||||
exporterSettings.__init__(self, _format, parent)
|
||||
|
||||
# Adds markdown syntax highlighter setting
|
||||
w = self.toolBox.widget(self.toolBox.count() - 1)
|
||||
self.grpMarkdown = QGroupBox(self.tr("Markdown"))
|
||||
self.grpMarkdown.setLayout(QVBoxLayout())
|
||||
self.chkMarkdownHighlighter = QCheckBox(self.tr("Preview with highlighter."))
|
||||
self.grpMarkdown.layout().addWidget(self.chkMarkdownHighlighter)
|
||||
|
||||
w.layout().insertWidget(w.layout().count() - 1, self.grpMarkdown)
|
||||
|
||||
def updateFromSettings(self):
|
||||
exporterSettings.updateFromSettings(self)
|
||||
|
||||
s = self.settings["Preview"]
|
||||
val = s.get("MarkdownHighlighter", False)
|
||||
self.chkMarkdownHighlighter.setChecked(val)
|
||||
|
||||
def getSettings(self):
|
||||
self.settings = exporterSettings.getSettings(self)
|
||||
self.settings["Preview"]["MarkdownHighlighter"] = self.chkMarkdownHighlighter.isChecked()
|
||||
|
||||
return self.settings
|
|
@ -2,7 +2,7 @@
|
|||
# --!-- coding: utf8 --!--
|
||||
import re
|
||||
from PyQt5.QtGui import QFont, QTextCharFormat
|
||||
from PyQt5.QtWidgets import QPlainTextEdit
|
||||
from PyQt5.QtWidgets import QPlainTextEdit, qApp
|
||||
|
||||
from manuskript.exporter.basic import basicFormat
|
||||
from manuskript.functions import mainWindow
|
||||
|
@ -11,9 +11,9 @@ from manuskript.ui.exporters.manuskript.plainTextSettings import exporterSetting
|
|||
|
||||
|
||||
class plainText(basicFormat):
|
||||
name = "Plain text"
|
||||
description = "Simplest export to plain text. Allows you to use your own markup not understood " \
|
||||
"by manuskript, for example <a href='www.fountain.io'>Fountain</a>."
|
||||
name = qApp.tr("Plain text")
|
||||
description = qApp.tr("Simplest export to plain text. Allows you to use your own markup not understood " \
|
||||
"by manuskript, for example <a href='www.fountain.io'>Fountain</a>.")
|
||||
implemented = True
|
||||
requires = {
|
||||
"Settings": True,
|
||||
|
@ -23,6 +23,7 @@ class plainText(basicFormat):
|
|||
@classmethod
|
||||
def settingsWidget(cls):
|
||||
w = exporterSettings(cls)
|
||||
w.loadSettings()
|
||||
return w
|
||||
|
||||
@classmethod
|
||||
|
@ -34,7 +35,9 @@ class plainText(basicFormat):
|
|||
@classmethod
|
||||
def preview(cls, settingsWidget, previewWidget):
|
||||
settings = settingsWidget.getSettings()
|
||||
print(settings)
|
||||
|
||||
# Save settings
|
||||
settingsWidget.writeSettings()
|
||||
|
||||
r = cls.concatenate(mainWindow().mdlOutline.rootItem, settings)
|
||||
|
||||
|
@ -48,8 +51,7 @@ class plainText(basicFormat):
|
|||
previewWidget.setPlainText(r)
|
||||
|
||||
@classmethod
|
||||
def concatenate(cls, item: outlineItem, settings,
|
||||
processTitle=lambda x: x + "\n") -> str:
|
||||
def concatenate(cls, item: outlineItem, settings) -> str:
|
||||
s = settings
|
||||
r = ""
|
||||
|
||||
|
@ -65,13 +67,13 @@ class plainText(basicFormat):
|
|||
if not s["Content"]["More"] and s["Content"]["FolderTitle"] or\
|
||||
s["Content"]["More"] and s["Content"]["FolderTitle"][l]:
|
||||
|
||||
r += processTitle(item.title())
|
||||
r += cls.processTitle(item.title(), l, settings)
|
||||
|
||||
elif item.isText():
|
||||
if not s["Content"]["More"] and s["Content"]["TextTitle"] or \
|
||||
s["Content"]["More"] and s["Content"]["TextTitle"][l]:
|
||||
|
||||
r += processTitle(item.title())
|
||||
r += cls.processTitle(item.title(), l, settings)
|
||||
|
||||
if not s["Content"]["More"] and s["Content"]["TextText"] or \
|
||||
s["Content"]["More"] and s["Content"]["TextText"][l]:
|
||||
|
@ -99,7 +101,7 @@ class plainText(basicFormat):
|
|||
elif last == "md" and c.type() == "folder":
|
||||
content += s["Separator"]["TF"]
|
||||
|
||||
content += cls.concatenate(c, settings, processTitle)
|
||||
content += cls.concatenate(c, settings)
|
||||
|
||||
last = c.type()
|
||||
|
||||
|
@ -108,6 +110,10 @@ class plainText(basicFormat):
|
|||
|
||||
return r
|
||||
|
||||
@classmethod
|
||||
def processTitle(cls, text, level, settings):
|
||||
return text + "\n"
|
||||
|
||||
@classmethod
|
||||
def processText(cls, content, settings):
|
||||
s = settings["Transform"]
|
||||
|
|
|
@ -75,7 +75,7 @@ class basicHighlighter(QSyntaxHighlighter):
|
|||
# Spell checking
|
||||
# Based on http://john.nachtimwald.com/2009/08/22/qplaintextedit-with-in-line-spell-check/
|
||||
WORDS = '(?iu)[\w\']+'
|
||||
if self.editor.spellcheck:
|
||||
if hasattr(self.editor, "spellcheck") and self.editor.spellcheck:
|
||||
for word_object in re.finditer(WORDS, text):
|
||||
if self.editor._dict and not self.editor._dict.check(word_object.group()):
|
||||
format = self.format(word_object.start())
|
||||
|
|
|
@ -118,7 +118,6 @@ class exporterSettings(QWidget, Ui_exporterSettings):
|
|||
# Final stuff
|
||||
|
||||
self.toolBox.setCurrentIndex(0)
|
||||
self.loadSettings()
|
||||
|
||||
###################################################################################################################
|
||||
# SETTINGS
|
||||
|
@ -126,7 +125,6 @@ class exporterSettings(QWidget, Ui_exporterSettings):
|
|||
|
||||
def loadSettings(self):
|
||||
filename = self.getSettingsPath()
|
||||
print(filename)
|
||||
if os.path.exists(filename):
|
||||
with open(filename) as f:
|
||||
self.settings = json.load(f)
|
||||
|
@ -229,7 +227,6 @@ class exporterSettings(QWidget, Ui_exporterSettings):
|
|||
s["TextTitle"] = []
|
||||
s["TextText"] = []
|
||||
nLevel = int((self.tblContent.rowCount() - 2) / 2)
|
||||
print(nLevel)
|
||||
for row in range(nLevel):
|
||||
s["FolderTitle"].append(self.tblContent.item(2 + row, 1).checkState() == Qt.Checked)
|
||||
s["TextTitle"].append(self.tblContent.item(2 + row + nLevel, 1).checkState() == Qt.Checked)
|
||||
|
@ -278,7 +275,6 @@ class exporterSettings(QWidget, Ui_exporterSettings):
|
|||
|
||||
s["Custom"] = []
|
||||
for i in range(self.tblReplacements.rowCount()):
|
||||
print(self.getTableRowValues(self.tblReplacements, i))
|
||||
s["Custom"].append(self.getTableRowValues(self.tblReplacements, i))
|
||||
self.settings["Transform"] = s
|
||||
|
||||
|
@ -289,9 +285,6 @@ class exporterSettings(QWidget, Ui_exporterSettings):
|
|||
s["PreviewFont"] = f.toString()
|
||||
self.settings["Preview"] = s
|
||||
|
||||
# Save settings
|
||||
self.writeSettings()
|
||||
|
||||
return self.settings
|
||||
|
||||
def sepCmbChanged(self, index):
|
||||
|
|
|
@ -471,6 +471,7 @@ class Ui_exporterSettings(object):
|
|||
self.verticalLayout_6.addItem(spacerItem10)
|
||||
self.toolBox.addItem(self.transformations, "")
|
||||
self.preview = QtWidgets.QWidget()
|
||||
self.preview.setStyleSheet("QGroupBox{font-weight:bold;}")
|
||||
self.preview.setObjectName("preview")
|
||||
self.verticalLayout_11 = QtWidgets.QVBoxLayout(self.preview)
|
||||
self.verticalLayout_11.setObjectName("verticalLayout_11")
|
||||
|
|
|
@ -1145,6 +1145,9 @@ QToolBox::tab:selected, QToolBox::tab:hover{
|
|||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="preview">
|
||||
<property name="styleSheet">
|
||||
<string notr="true">QGroupBox{font-weight:bold;}</string>
|
||||
</property>
|
||||
<attribute name="label">
|
||||
<string>Preview</string>
|
||||
</attribute>
|
||||
|
|
Loading…
Reference in a new issue