diff --git a/nw/convert/tohtml.py b/nw/convert/tohtml.py index 280c6d0c..3225c4bb 100644 --- a/nw/convert/tohtml.py +++ b/nw/convert/tohtml.py @@ -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 = { + "<" : "<", + ">" : ">", + "&" : "&", + } + 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 : "", self.FMT_I_B : "", self.FMT_I_E : "", - self.FMT_U_B : "", - self.FMT_U_E : "", + self.FMT_U_B : "", + self.FMT_U_E : "", } self.theResult = "" diff --git a/nw/convert/tokenizer.py b/nw/convert/tokenizer.py index 5fe3cf9e..59365ec1 100644 --- a/nw/convert/tokenizer.py +++ b/nw/convert/tokenizer.py @@ -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 diff --git a/nw/gui/winmain.py b/nw/gui/winmain.py index c6963083..c13d2d01 100644 --- a/nw/gui/winmain.py +++ b/nw/gui/winmain.py @@ -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) diff --git a/sample/sampleNovel/data_6/36b6aa9b697b_main.nwd b/sample/sampleNovel/data_6/36b6aa9b697b_main.nwd index a084c76f..e4c4beb8 100644 --- a/sample/sampleNovel/data_6/36b6aa9b697b_main.nwd +++ b/sample/sampleNovel/data_6/36b6aa9b697b_main.nwd @@ -18,3 +18,6 @@ This paragraph is also meaningless. At least a bit. It’s also very short. But This one is a bit longer. “It also has some dialogue in it” she said, before she moved on to check if the spellchecker worked. It did. “Cool,” she concluded. +Let’s auto-replace this A with , and this C with . While is just . + + diff --git a/sample/sampleNovel/meta/sessionInfo.log b/sample/sampleNovel/meta/sessionInfo.log index 7d118aff..4111f856 100644 --- a/sample/sampleNovel/meta/sessionInfo.log +++ b/sample/sampleNovel/meta/sessionInfo.log @@ -182,3 +182,8 @@ Start: 2019-06-08 19:20:15 End: 2019-06-08 19:20:41 Words: 0 Start: 2019-06-08 19:54:27 End: 2019-06-08 19:54:38 Words: 0 Start: 2019-06-08 19:58:46 End: 2019-06-08 19:58:50 Words: 0 Start: 2019-06-08 20:01:07 End: 2019-06-08 20:01:18 Words: 0 +Start: 2019-06-08 20:26:26 End: 2019-06-08 20:28:18 Words: 572 +Start: 2019-06-08 20:30:24 End: 2019-06-08 20:42:10 Words: 9 +Start: 2019-06-08 20:42:14 End: 2019-06-08 20:42:37 Words: 0 +Start: 2019-06-08 20:42:41 End: 2019-06-08 20:43:25 Words: 0 +Start: 2019-06-08 20:43:29 End: 2019-06-08 20:43:42 Words: 0 diff --git a/sample/sampleNovel/nwProject.nwx b/sample/sampleNovel/nwProject.nwx index 0d012aa1..7fc31a8e 100644 --- a/sample/sampleNovel/nwProject.nwx +++ b/sample/sampleNovel/nwProject.nwx @@ -1,5 +1,5 @@ - + Sample Project Sample Project @@ -9,8 +9,8 @@ True 636b6aa9b697b - None - 0 + 636b6aa9b697b + 581 B D @@ -77,10 +77,10 @@ Notes False SCENE - 656 - 121 - 5 - 442 + 736 + 137 + 6 + 725 New File