Minor fixes and updates (#1026)
* Make the Tokenizer class an abstract class * Fix error message when opening non-file project items
This commit is contained in:
committed by
GitHub
parent
fbff421de8
commit
6e3262f78e
@@ -183,9 +183,9 @@ class NWItem():
|
|||||||
|
|
||||||
self.setParent(xItem.attrib.get("parent", None))
|
self.setParent(xItem.attrib.get("parent", None))
|
||||||
self.setOrder(xItem.attrib.get("order", 0))
|
self.setOrder(xItem.attrib.get("order", 0))
|
||||||
self.setType(xItem.attrib.get("type", None))
|
self.setType(xItem.attrib.get("type", nwItemType.NO_TYPE))
|
||||||
self.setClass(xItem.attrib.get("class", None))
|
self.setClass(xItem.attrib.get("class", nwItemClass.NO_CLASS))
|
||||||
self.setLayout(xItem.attrib.get("layout", None))
|
self.setLayout(xItem.attrib.get("layout", nwItemLayout.NO_LAYOUT))
|
||||||
|
|
||||||
for xValue in xItem:
|
for xValue in xItem:
|
||||||
if xValue.tag == "meta":
|
if xValue.tag == "meta":
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
"""
|
"""
|
||||||
novelWriter – Text Tokenizer
|
novelWriter – Text Tokenizer
|
||||||
============================
|
============================
|
||||||
Splits a piece of novelWriter markdown text into its elements
|
Split novelWriter plain text into its elements
|
||||||
|
|
||||||
File History:
|
File History:
|
||||||
Created: 2019-05-05 [0.0.1]
|
Created: 2019-05-05 [0.0.1]
|
||||||
@@ -27,6 +27,7 @@ import re
|
|||||||
import logging
|
import logging
|
||||||
import novelwriter
|
import novelwriter
|
||||||
|
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
@@ -40,7 +41,7 @@ from novelwriter.core.document import NWDoc
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Tokenizer():
|
class Tokenizer(ABC):
|
||||||
|
|
||||||
# In-Text Format
|
# In-Text Format
|
||||||
FMT_B_B = 1 # Begin bold
|
FMT_B_B = 1 # Begin bold
|
||||||
@@ -267,6 +268,10 @@ class Tokenizer():
|
|||||||
# Class Methods
|
# Class Methods
|
||||||
##
|
##
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def doConvert(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
def addRootHeading(self, theHandle):
|
def addRootHeading(self, theHandle):
|
||||||
"""Add a heading at the start of a new root folder.
|
"""Add a heading at the start of a new root folder.
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -28,12 +28,17 @@ from novelwriter.core import NWProject, NWDoc
|
|||||||
from novelwriter.core.tokenizer import Tokenizer
|
from novelwriter.core.tokenizer import Tokenizer
|
||||||
|
|
||||||
|
|
||||||
|
class BareTokenizer(Tokenizer):
|
||||||
|
def doConvert(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.core
|
@pytest.mark.core
|
||||||
def testCoreToken_Setters(mockGUI):
|
def testCoreToken_Setters(mockGUI):
|
||||||
"""Test all the setters for the Tokenizer class.
|
"""Test all the setters for the Tokenizer class.
|
||||||
"""
|
"""
|
||||||
theProject = NWProject(mockGUI)
|
theProject = NWProject(mockGUI)
|
||||||
theToken = Tokenizer(theProject)
|
theToken = BareTokenizer(theProject)
|
||||||
|
|
||||||
# Verify defaults
|
# Verify defaults
|
||||||
assert theToken._fmtTitle == "%title%"
|
assert theToken._fmtTitle == "%title%"
|
||||||
@@ -135,7 +140,7 @@ def testCoreToken_TextOps(monkeypatch, nwMinimal, mockGUI):
|
|||||||
theProject.projLang = "en"
|
theProject.projLang = "en"
|
||||||
theProject._loadProjectLocalisation()
|
theProject._loadProjectLocalisation()
|
||||||
|
|
||||||
theToken = Tokenizer(theProject)
|
theToken = BareTokenizer(theProject)
|
||||||
theToken.setKeepMarkdown(True)
|
theToken.setKeepMarkdown(True)
|
||||||
|
|
||||||
assert theProject.openProject(nwMinimal)
|
assert theProject.openProject(nwMinimal)
|
||||||
@@ -222,7 +227,7 @@ def testCoreToken_HeaderFormat(mockGUI):
|
|||||||
"""Test the tokenization of header formats in the Tokenizer class.
|
"""Test the tokenization of header formats in the Tokenizer class.
|
||||||
"""
|
"""
|
||||||
theProject = NWProject(mockGUI)
|
theProject = NWProject(mockGUI)
|
||||||
theToken = Tokenizer(theProject)
|
theToken = BareTokenizer(theProject)
|
||||||
theToken.setKeepMarkdown(True)
|
theToken.setKeepMarkdown(True)
|
||||||
|
|
||||||
# Title
|
# Title
|
||||||
@@ -426,7 +431,7 @@ def testCoreToken_MetaFormat(mockGUI):
|
|||||||
"""Test the tokenization of meta formats in the Tokenizer class.
|
"""Test the tokenization of meta formats in the Tokenizer class.
|
||||||
"""
|
"""
|
||||||
theProject = NWProject(mockGUI)
|
theProject = NWProject(mockGUI)
|
||||||
theToken = Tokenizer(theProject)
|
theToken = BareTokenizer(theProject)
|
||||||
theToken.setKeepMarkdown(True)
|
theToken.setKeepMarkdown(True)
|
||||||
|
|
||||||
# Comment
|
# Comment
|
||||||
@@ -495,7 +500,7 @@ def testCoreToken_MarginFormat(mockGUI):
|
|||||||
"""Test the tokenization of margin formats in the Tokenizer class.
|
"""Test the tokenization of margin formats in the Tokenizer class.
|
||||||
"""
|
"""
|
||||||
theProject = NWProject(mockGUI)
|
theProject = NWProject(mockGUI)
|
||||||
theToken = Tokenizer(theProject)
|
theToken = BareTokenizer(theProject)
|
||||||
theToken.setKeepMarkdown(True)
|
theToken.setKeepMarkdown(True)
|
||||||
|
|
||||||
# Alignment and Indentation
|
# Alignment and Indentation
|
||||||
@@ -550,7 +555,7 @@ def testCoreToken_TextFormat(mockGUI):
|
|||||||
"""Test the tokenization of text formats in the Tokenizer class.
|
"""Test the tokenization of text formats in the Tokenizer class.
|
||||||
"""
|
"""
|
||||||
theProject = NWProject(mockGUI)
|
theProject = NWProject(mockGUI)
|
||||||
theToken = Tokenizer(theProject)
|
theToken = BareTokenizer(theProject)
|
||||||
theToken.setKeepMarkdown(True)
|
theToken.setKeepMarkdown(True)
|
||||||
|
|
||||||
# Text
|
# Text
|
||||||
@@ -672,7 +677,7 @@ def testCoreToken_SpecialFormat(mockGUI):
|
|||||||
"""Test the tokenization of special formats in the Tokenizer class.
|
"""Test the tokenization of special formats in the Tokenizer class.
|
||||||
"""
|
"""
|
||||||
theProject = NWProject(mockGUI)
|
theProject = NWProject(mockGUI)
|
||||||
theToken = Tokenizer(theProject)
|
theToken = BareTokenizer(theProject)
|
||||||
|
|
||||||
theToken._isNovel = True
|
theToken._isNovel = True
|
||||||
|
|
||||||
@@ -877,7 +882,7 @@ def testCoreToken_ProcessHeaders(mockGUI):
|
|||||||
theProject = NWProject(mockGUI)
|
theProject = NWProject(mockGUI)
|
||||||
theProject.projLang = "en"
|
theProject.projLang = "en"
|
||||||
theProject._loadProjectLocalisation()
|
theProject._loadProjectLocalisation()
|
||||||
theToken = Tokenizer(theProject)
|
theToken = BareTokenizer(theProject)
|
||||||
|
|
||||||
# Nothing
|
# Nothing
|
||||||
theToken._theText = "Some text ...\n"
|
theToken._theText = "Some text ...\n"
|
||||||
|
|||||||
Reference in New Issue
Block a user