manuskript/manuskript/exporter/manuskript/plainText.py

164 lines
4.6 KiB
Python
Raw Normal View History

2016-04-05 06:00:19 +12:00
#!/usr/bin/env python
# --!-- coding: utf8 --!--
import re
from PyQt5.QtGui import QFont, QTextCharFormat
2016-04-08 00:27:51 +12:00
from PyQt5.QtWidgets import QPlainTextEdit, qApp, QFrame
2016-04-05 06:00:19 +12:00
from manuskript.exporter.basic import basicFormat
2016-04-06 03:21:07 +12:00
from manuskript.functions import mainWindow
from manuskript.models.outlineModel import outlineItem
from manuskript.ui.exporters.manuskript.plainTextSettings import exporterSettings
2016-04-06 03:21:07 +12:00
2016-04-05 06:00:19 +12:00
class plainText(basicFormat):
2016-04-08 00:47:29 +12:00
name = qApp.translate("Export", "Plain text")
description = qApp.translate("Export", """Simplest export to plain text. Allows you to use your own markup not understood
2016-04-08 00:27:51 +12:00
by manuskript, for example <a href='www.fountain.io'>Fountain</a>.""")
2016-04-06 03:21:07 +12:00
implemented = True
2016-04-05 06:00:19 +12:00
requires = {
"Settings": True,
2016-04-06 03:21:07 +12:00
"Preview": True,
2016-04-05 06:00:19 +12:00
}
@classmethod
def settingsWidget(cls):
w = exporterSettings(cls)
2016-04-07 23:17:14 +12:00
w.loadSettings()
2016-04-06 03:21:07 +12:00
return w
@classmethod
def previewWidget(cls):
w = QPlainTextEdit()
2016-04-08 00:27:51 +12:00
w.setFrameShape(QFrame.NoFrame)
2016-04-06 03:21:07 +12:00
w.setReadOnly(True)
return w
2016-04-08 00:27:51 +12:00
@classmethod
def output(cls, settings):
return cls.concatenate(mainWindow().mdlOutline.rootItem, settings)
2016-04-06 03:21:07 +12:00
@classmethod
def preview(cls, settingsWidget, previewWidget):
settings = settingsWidget.getSettings()
2016-04-07 23:17:14 +12:00
# Save settings
settingsWidget.writeSettings()
2016-04-06 03:21:07 +12:00
2016-04-08 00:27:51 +12:00
r = cls.output(settings)
# Set preview font
2016-04-08 00:27:51 +12:00
cls.preparesTextEditView(previewWidget, settings["Preview"]["PreviewFont"])
previewWidget.setPlainText(r)
@classmethod
def preparesTextEditView(cls, view, textFont):
cf = QTextCharFormat()
f = QFont()
2016-04-08 00:27:51 +12:00
f.fromString(textFont)
cf.setFont(f)
2016-04-08 00:27:51 +12:00
view.setCurrentCharFormat(cf)
2016-04-06 03:21:07 +12:00
@classmethod
2016-04-07 23:17:14 +12:00
def concatenate(cls, item: outlineItem, settings) -> str:
s = settings
2016-04-06 03:21:07 +12:00
r = ""
# Do we include item
if not item.compile() or s["Content"]["IgnoreCompile"]:
2016-04-06 03:21:07 +12:00
return ""
# What do we include
l = item.level()
if l >= 0: # item is not root
if item.isFolder():
if not s["Content"]["More"] and s["Content"]["FolderTitle"] or\
s["Content"]["More"] and s["Content"]["FolderTitle"][l]:
2016-04-07 23:17:14 +12:00
r += cls.processTitle(item.title(), l, settings)
2016-04-06 03:21:07 +12:00
elif item.isText():
if not s["Content"]["More"] and s["Content"]["TextTitle"] or \
s["Content"]["More"] and s["Content"]["TextTitle"][l]:
2016-04-06 03:21:07 +12:00
2016-04-07 23:17:14 +12:00
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]:
r += cls.processText(item.text(), settings)
2016-04-06 03:21:07 +12:00
content = ""
# Add item children
last = None
for c in item.children():
# Separator
if last:
# Between folder
if last == c.type() == "folder":
content += s["Separator"]["FF"]
2016-04-06 03:21:07 +12:00
elif last == c.type() == "md":
content += s["Separator"]["TT"]
2016-04-06 03:21:07 +12:00
elif last == "folder" and c.type() == "md":
content += s["Separator"]["FT"]
2016-04-06 03:21:07 +12:00
elif last == "md" and c.type() == "folder":
content += s["Separator"]["TF"]
2016-04-06 03:21:07 +12:00
2016-04-07 23:17:14 +12:00
content += cls.concatenate(c, settings)
2016-04-06 03:21:07 +12:00
last = c.type()
# r += cls.processContent(content, settings)
r += content
return r
2016-04-07 23:17:14 +12:00
@classmethod
def processTitle(cls, text, level, settings):
return text + "\n"
@classmethod
def processText(cls, content, settings):
s = settings["Transform"]
if s["Dash"]:
content = content.replace("---", "")
if s["Ellipse"]:
content = content.replace("...", "")
if s["Spaces"]:
o = ""
while o != content:
o = content
content = content.replace(" ", " ")
if s["DoubleQuotes"]:
q = s["DoubleQuotes"].split("___")
s["Custom"].append([True, '"(.*?)"', "{}\\1{}".format(q[0], q[1]), True])
if s["SingleQuote"]:
q = s["SingleQuote"].split("___")
s["Custom"].append([True, "'(.*?)'", "{}\\1{}".format(q[0], q[1]), True])
for enabled, A, B, reg in s["Custom"]:
if not enabled:
continue
if not reg:
content = content.replace(A, B)
else:
content = re.sub(A, B, content)
content += "\n"
return content
2016-04-06 03:21:07 +12:00