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