Implemented the auto-replace code in the doc viewer.
This commit is contained in:
+16
-2
@@ -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 : "</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 = ""
|
||||
|
||||
@@ -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
@@ -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)
|
||||
|
||||
@@ -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 <A>, and this C with <C>. While <E> is just <E>.
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?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>
|
||||
<name>Sample Project</name>
|
||||
<title>Sample Project</title>
|
||||
@@ -9,8 +9,8 @@
|
||||
<settings>
|
||||
<spellCheck>True</spellCheck>
|
||||
<lastEdited>636b6aa9b697b</lastEdited>
|
||||
<lastViewed>None</lastViewed>
|
||||
<lastWordCount>0</lastWordCount>
|
||||
<lastViewed>636b6aa9b697b</lastViewed>
|
||||
<lastWordCount>581</lastWordCount>
|
||||
<autoReplace>
|
||||
<A>B</A>
|
||||
<C>D</C>
|
||||
@@ -77,10 +77,10 @@
|
||||
<status>Notes</status>
|
||||
<expanded>False</expanded>
|
||||
<layout>SCENE</layout>
|
||||
<charCount>656</charCount>
|
||||
<wordCount>121</wordCount>
|
||||
<paraCount>5</paraCount>
|
||||
<cursorPos>442</cursorPos>
|
||||
<charCount>736</charCount>
|
||||
<wordCount>137</wordCount>
|
||||
<paraCount>6</paraCount>
|
||||
<cursorPos>725</cursorPos>
|
||||
</item>
|
||||
<item handle="bc0cbd2a407f3" order="2" parent="e7ded148d6e4a">
|
||||
<name>New File</name>
|
||||
|
||||
Reference in New Issue
Block a user