Implemented the auto-replace code in the doc viewer.

This commit is contained in:
Veronica K. B. Olsen
2019-06-08 20:44:10 +02:00
parent 2d55ec06cc
commit dcf4903c64
6 changed files with 43 additions and 11 deletions
+16 -2
View File
@@ -11,6 +11,7 @@
"""
import logging
import re
import nw
from nw.convert.tokenizer import Tokenizer
@@ -24,6 +25,19 @@ class ToHtml(Tokenizer):
return
def doAutoReplace(self):
Tokenizer.doAutoReplace(self)
theDict = {
"<" : "&lt;",
">" : "&gt;",
"&" : "&amp;",
}
xRep = re.compile("|".join([re.escape(k) for k in theDict.keys()]), flags=re.DOTALL)
self.theText = xRep.sub(lambda x: theDict[x.group(0)], self.theText)
return
def doConvert(self):
htmlTags = {
@@ -31,8 +45,8 @@ class ToHtml(Tokenizer):
self.FMT_B_E : "</strong>",
self.FMT_I_B : "<em>",
self.FMT_I_E : "</em>",
self.FMT_U_B : "<mark>",
self.FMT_U_E : "</mark>",
self.FMT_U_B : "<u>",
self.FMT_U_E : "</u>",
}
self.theResult = ""
+10
View File
@@ -11,6 +11,7 @@
"""
import logging
import re
import nw
from operator import itemgetter
@@ -57,6 +58,15 @@ class Tokenizer():
return
def doAutoReplace(self):
if len(self.theProject.autoReplace) > 0:
theDict = {}
for aKey, aVal in self.theProject.autoReplace.items():
theDict["<%s>" % aKey] = aVal
xRep = re.compile("|".join([re.escape(k) for k in theDict.keys()]), flags=re.DOTALL)
self.theText = xRep.sub(lambda x: theDict[x.group(0)], self.theText)
return
def tokenizeText(self):
"""Scan the text for either lines starting with specific characters that indicate headers,
comments, commands etc, or just contains plain text. in the case of plain text, apply the
+2 -2
View File
@@ -246,6 +246,7 @@ class GuiMain(QMainWindow):
self.docEditor.setSpellCheck(self.theProject.spellCheck)
self.statusBar.setRefTime(self.theProject.projOpened)
self.mainMenu.updateMenu()
self.hasProject = True
# Restore previously open documents, if any
if self.theProject.lastEdited is not None:
@@ -253,8 +254,6 @@ class GuiMain(QMainWindow):
if self.theProject.lastViewed is not None:
self.viewDocument(self.theProject.lastViewed)
self.hasProject = True
return True
def saveProject(self):
@@ -329,6 +328,7 @@ class GuiMain(QMainWindow):
logger.debug("Generating preview for item %s" % tHandle)
aDoc = ToHtml(self.theProject, self)
aDoc.setText(tHandle)
aDoc.doAutoReplace()
aDoc.tokenizeText()
aDoc.doConvert()
self.docViewer.setHtml(aDoc.theResult)