diff --git a/nw/convert/tohtml.py b/nw/convert/tohtml.py new file mode 100644 index 00000000..313ecb4c --- /dev/null +++ b/nw/convert/tohtml.py @@ -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 : "", + self.FMT_B_E : "", + self.FMT_I_B : "", + self.FMT_I_E : "", + self.FMT_U_B : "", + self.FMT_U_E : "", + } + + self.theResult = "" + thisPar = [] + for tType, tText, tFormat in self.theTokens: + + if tType == "empty": + if len(thisPar) > 0: + self.theResult += "

%s

\n" % " ".join(thisPar) + thisPar = [] + elif tType == "header1": + self.theResult += "

%s

\n" % tText + elif tType == "header2": + self.theResult += "

%s

\n" % tText + elif tType == "header3": + self.theResult += "

%s

\n" % tText + elif tType == "header4": + self.theResult += "

%s

\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 diff --git a/nw/convert/tokenizer.py b/nw/convert/tokenizer.py index 85596ebb..5fe3cf9e 100644 --- a/nw/convert/tokenizer.py +++ b/nw/convert/tokenizer.py @@ -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 diff --git a/nw/gui/winmain.py b/nw/gui/winmain.py index e5678d42..986e6cd3 100644 --- a/nw/gui/winmain.py +++ b/nw/gui/winmain.py @@ -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