From e0936b5f33305fcfbf70907ed5ec14046ba938ba Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Tue, 7 May 2019 18:01:19 +0200 Subject: [PATCH] Text tokenizer done. --- nw/convert/tokenizer.py | 84 ++++++++++++++++++++++++++++++++++++++--- nw/gui/dochighlight.py | 8 ++-- nw/gui/winmain.py | 3 +- 3 files changed, 84 insertions(+), 11 deletions(-) diff --git a/nw/convert/tokenizer.py b/nw/convert/tokenizer.py index e6ca8c05..85596ebb 100644 --- a/nw/convert/tokenizer.py +++ b/nw/convert/tokenizer.py @@ -13,32 +13,104 @@ import logging import nw +from PyQt5.QtCore import QRegularExpression + from nw.project.document import NWDoc logger = logging.getLogger(__name__) class Tokenizer(): + FMT_B_B = 1 # Begin Bold + FMT_B_E = 2 # End Bold + FMT_I_B = 3 # Begin Italics + FMT_I_E = 4 # End Italics + FMT_U_B = 5 # Begin Underline + FMT_U_E = 6 # End Underline + def __init__(self, theProject, theParent): self.mainConf = nw.CONFIG self.theProject = theProject self.theParent = theParent + self.theText = None self.theHandle = None self.theItem = None self.theTokens = None return - def tokenizeText(self, tHandle): + def setText(self, theHandle, theText=None): - self.theItem = self.theProject.getItem(tHandle) - theDoc = NWDoc(self.theProject, self.theParent) - theText = theDoc.openDocument(tHandle) + self.theHandle = theHandle + self.theItem = self.theProject.getItem(theHandle) - for aLine in theText.splitlines(): - print(aLine) + if theText is not None: + # If the text is set, just use that + self.theText = theText + else: + # Otherwise, load it from file + theDocument = NWDoc(self.theProject, self.theParent) + self.theText = theDocument.openDocument(theHandle) + + 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 + same RegExes that the syntax highlighter uses and save the locations of these formatting + tags into the token array. + """ + + # RegExes for adding formatting tags within text lines + # Keep in sync with the DocHighlighter class + rxFormats = [( + QRegularExpression(r"(?