Updated tokenizer, and added html converter as a subclass
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""novelWriter HTML Text Converter
|
||||
|
||||
novelWriter – HTML Text Converter
|
||||
===================================
|
||||
Extends the Tokenizer class to write HTML
|
||||
|
||||
File History:
|
||||
Created: 2019-05-07 [0.0.1]
|
||||
|
||||
"""
|
||||
|
||||
import logging
|
||||
import nw
|
||||
|
||||
from nw.convert.tokenizer import Tokenizer
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class ToHtml(Tokenizer):
|
||||
|
||||
def __init__(self, theProject, theParent):
|
||||
Tokenizer.__init__(self, theProject, theParent)
|
||||
|
||||
return
|
||||
|
||||
def doConvert(self):
|
||||
|
||||
htmlTags = {
|
||||
self.FMT_B_B : "<strong>",
|
||||
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.theResult = ""
|
||||
thisPar = []
|
||||
for tType, tText, tFormat in self.theTokens:
|
||||
|
||||
if tType == "empty":
|
||||
if len(thisPar) > 0:
|
||||
self.theResult += "<p>%s</p>\n" % " ".join(thisPar)
|
||||
thisPar = []
|
||||
elif tType == "header1":
|
||||
self.theResult += "<h1>%s</h1>\n" % tText
|
||||
elif tType == "header2":
|
||||
self.theResult += "<h2>%s</h2>\n" % tText
|
||||
elif tType == "header3":
|
||||
self.theResult += "<h3>%s</h3>\n" % tText
|
||||
elif tType == "header4":
|
||||
self.theResult += "<h4>%s</h4>\n" % tText
|
||||
elif tType == "text":
|
||||
tTemp = tText
|
||||
for xPos, xLen, xFmt in reversed(tFormat):
|
||||
tTemp = tTemp[:xPos]+htmlTags[xFmt]+tTemp[xPos+xLen:]
|
||||
thisPar.append(tTemp)
|
||||
|
||||
print(self.theResult)
|
||||
|
||||
return
|
||||
|
||||
# END Class ToHtml
|
||||
@@ -13,8 +13,8 @@
|
||||
import logging
|
||||
import nw
|
||||
|
||||
from operator import itemgetter
|
||||
from PyQt5.QtCore import QRegularExpression
|
||||
|
||||
from nw.project.document import NWDoc
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -38,6 +38,7 @@ class Tokenizer():
|
||||
self.theHandle = None
|
||||
self.theItem = None
|
||||
self.theTokens = None
|
||||
self.theResult = None
|
||||
|
||||
return
|
||||
|
||||
@@ -107,9 +108,13 @@ class Tokenizer():
|
||||
xPos = rxMatch.capturedStart(n)
|
||||
xLen = rxMatch.capturedLength(n)
|
||||
fmtPos.append([xPos,xLen,theKeys[n]])
|
||||
# Save the line as is, but append the array of formatting locations
|
||||
|
||||
# Save the line as is, but append the array of formatting locations sorted by position
|
||||
fmtPos = sorted(fmtPos,key=itemgetter(0))
|
||||
self.theTokens.append(("text",aLine,fmtPos))
|
||||
|
||||
# Always add an empty line at the end
|
||||
self.theTokens.append(("empty","",None))
|
||||
# print(self.theTokens)
|
||||
|
||||
return
|
||||
|
||||
+3
-1
@@ -29,6 +29,7 @@ from nw.project.project import NWProject
|
||||
from nw.project.document import NWDoc
|
||||
from nw.project.item import NWItem
|
||||
from nw.convert.tokenizer import Tokenizer
|
||||
from nw.convert.tohtml import ToHtml
|
||||
from nw.enum import nwItemType
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -201,9 +202,10 @@ class GuiMain(QMainWindow):
|
||||
for tHandle in self.theProject.treeOrder:
|
||||
if tHandle not in theHandles:
|
||||
continue
|
||||
aDoc = Tokenizer(self.theProject, self)
|
||||
aDoc = ToHtml(self.theProject, self)
|
||||
aDoc.setText(tHandle)
|
||||
aDoc.tokenizeText()
|
||||
aDoc.doConvert()
|
||||
|
||||
return
|
||||
|
||||
|
||||
Reference in New Issue
Block a user