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 logging
import re
import nw import nw
from nw.convert.tokenizer import Tokenizer from nw.convert.tokenizer import Tokenizer
@@ -24,6 +25,19 @@ class ToHtml(Tokenizer):
return 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): def doConvert(self):
htmlTags = { htmlTags = {
@@ -31,8 +45,8 @@ class ToHtml(Tokenizer):
self.FMT_B_E : "</strong>", self.FMT_B_E : "</strong>",
self.FMT_I_B : "<em>", self.FMT_I_B : "<em>",
self.FMT_I_E : "</em>", self.FMT_I_E : "</em>",
self.FMT_U_B : "<mark>", self.FMT_U_B : "<u>",
self.FMT_U_E : "</mark>", self.FMT_U_E : "</u>",
} }
self.theResult = "" self.theResult = ""
+10
View File
@@ -11,6 +11,7 @@
""" """
import logging import logging
import re
import nw import nw
from operator import itemgetter from operator import itemgetter
@@ -57,6 +58,15 @@ class Tokenizer():
return 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): def tokenizeText(self):
"""Scan the text for either lines starting with specific characters that indicate headers, """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 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.docEditor.setSpellCheck(self.theProject.spellCheck)
self.statusBar.setRefTime(self.theProject.projOpened) self.statusBar.setRefTime(self.theProject.projOpened)
self.mainMenu.updateMenu() self.mainMenu.updateMenu()
self.hasProject = True
# Restore previously open documents, if any # Restore previously open documents, if any
if self.theProject.lastEdited is not None: if self.theProject.lastEdited is not None:
@@ -253,8 +254,6 @@ class GuiMain(QMainWindow):
if self.theProject.lastViewed is not None: if self.theProject.lastViewed is not None:
self.viewDocument(self.theProject.lastViewed) self.viewDocument(self.theProject.lastViewed)
self.hasProject = True
return True return True
def saveProject(self): def saveProject(self):
@@ -329,6 +328,7 @@ class GuiMain(QMainWindow):
logger.debug("Generating preview for item %s" % tHandle) logger.debug("Generating preview for item %s" % tHandle)
aDoc = ToHtml(self.theProject, self) aDoc = ToHtml(self.theProject, self)
aDoc.setText(tHandle) aDoc.setText(tHandle)
aDoc.doAutoReplace()
aDoc.tokenizeText() aDoc.tokenizeText()
aDoc.doConvert() aDoc.doConvert()
self.docViewer.setHtml(aDoc.theResult) self.docViewer.setHtml(aDoc.theResult)
@@ -18,3 +18,6 @@ This paragraph is also meaningless. At least a bit. Its 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. 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.
Lets auto-replace this A with <A>, and this C with <C>. While <E> is just <E>.
+5
View File
@@ -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: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 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: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
+7 -7
View File
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?> <?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="0.1.4" fileVersion="1.0" timeStamp="2019-06-08 20:01:18"> <novelWriterXML appVersion="0.1.4" fileVersion="1.0" timeStamp="2019-06-08 20:43:42">
<project> <project>
<name>Sample Project</name> <name>Sample Project</name>
<title>Sample Project</title> <title>Sample Project</title>
@@ -9,8 +9,8 @@
<settings> <settings>
<spellCheck>True</spellCheck> <spellCheck>True</spellCheck>
<lastEdited>636b6aa9b697b</lastEdited> <lastEdited>636b6aa9b697b</lastEdited>
<lastViewed>None</lastViewed> <lastViewed>636b6aa9b697b</lastViewed>
<lastWordCount>0</lastWordCount> <lastWordCount>581</lastWordCount>
<autoReplace> <autoReplace>
<A>B</A> <A>B</A>
<C>D</C> <C>D</C>
@@ -77,10 +77,10 @@
<status>Notes</status> <status>Notes</status>
<expanded>False</expanded> <expanded>False</expanded>
<layout>SCENE</layout> <layout>SCENE</layout>
<charCount>656</charCount> <charCount>736</charCount>
<wordCount>121</wordCount> <wordCount>137</wordCount>
<paraCount>5</paraCount> <paraCount>6</paraCount>
<cursorPos>442</cursorPos> <cursorPos>725</cursorPos>
</item> </item>
<item handle="bc0cbd2a407f3" order="2" parent="e7ded148d6e4a"> <item handle="bc0cbd2a407f3" order="2" parent="e7ded148d6e4a">
<name>New File</name> <name>New File</name>