From a1837d8153472191063fb75cb710477d25005255 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Wed, 24 May 2023 00:50:05 +0200 Subject: [PATCH] Clean up old heading format usage --- novelwriter/constants.py | 6 ++- novelwriter/core/buildsettings.py | 8 ++-- novelwriter/core/tokenizer.py | 32 +++++++-------- novelwriter/tools/manussettings.py | 34 +++++----------- tests/test_core/test_core_docbuild.py | 22 ----------- tests/test_core/test_core_tokenizer.py | 54 +++++++++++++------------- 6 files changed, 61 insertions(+), 95 deletions(-) diff --git a/novelwriter/constants.py b/novelwriter/constants.py index 717f3cdb..e0d6e053 100644 --- a/novelwriter/constants.py +++ b/novelwriter/constants.py @@ -208,7 +208,7 @@ class nwLabels: # END Class nwLabels -class nwHeadingFormats: +class nwHeadFmt: TITLE = "{Title}" CH_NUM = "{Chapter}" @@ -218,7 +218,9 @@ class nwHeadingFormats: SC_NUM = "{Scene}" SC_ABS = "{Scene:Abs}" -# END Class nwHeadingFormats + ALL = [TITLE, CH_NUM, CH_WORD, CH_ROMU, CH_ROML, SC_NUM, SC_ABS] + +# END Class nwHeadFmt class nwQuotes: diff --git a/novelwriter/core/buildsettings.py b/novelwriter/core/buildsettings.py index 5704fd75..845634d6 100644 --- a/novelwriter/core/buildsettings.py +++ b/novelwriter/core/buildsettings.py @@ -36,7 +36,7 @@ from pathlib import Path from PyQt5.QtCore import QT_TRANSLATE_NOOP from novelwriter.common import checkUuid, isHandle, jsonEncode -from novelwriter.constants import nwConst, nwFiles, nwHeadingFormats +from novelwriter.constants import nwConst, nwFiles, nwHeadFmt from novelwriter.core.item import NWItem from novelwriter.core.project import NWProject from novelwriter.error import logException @@ -52,9 +52,9 @@ SETTINGS_TEMPLATE = { "filter.includeNovel": (bool, True), "filter.includeNotes": (bool, False), "filter.includeInactive": (bool, False), - "headings.fmtTitle": (str, nwHeadingFormats.TITLE), - "headings.fmtChapter": (str, nwHeadingFormats.TITLE), - "headings.fmtUnnumbered": (str, nwHeadingFormats.TITLE), + "headings.fmtTitle": (str, nwHeadFmt.TITLE), + "headings.fmtChapter": (str, nwHeadFmt.TITLE), + "headings.fmtUnnumbered": (str, nwHeadFmt.TITLE), "headings.fmtScene": (str, "* * *"), "headings.fmtSection": (str, ""), "headings.hideScene": (bool, False), diff --git a/novelwriter/core/tokenizer.py b/novelwriter/core/tokenizer.py index 0cc4bf4a..fe5bb06d 100644 --- a/novelwriter/core/tokenizer.py +++ b/novelwriter/core/tokenizer.py @@ -35,7 +35,7 @@ from PyQt5.QtCore import QCoreApplication, QRegularExpression from novelwriter.enum import nwItemLayout, nwItemType from novelwriter.common import numberToRoman, checkInt -from novelwriter.constants import nwConst, nwHeadingFormats, nwRegEx, nwUnicode +from novelwriter.constants import nwConst, nwHeadFmt, nwRegEx, nwUnicode from novelwriter.core.project import NWProject logger = logging.getLogger(__name__) @@ -126,11 +126,11 @@ class Tokenizer(ABC): self._marginMeta = (0.000, 0.584) # Title Formats - self._fmtTitle = "%title%" # Formatting for titles - self._fmtChapter = "%title%" # Formatting for numbered chapters - self._fmtUnNum = "%title%" # Formatting for unnumbered chapters - self._fmtScene = "%title%" # Formatting for scenes - self._fmtSection = "%title%" # Formatting for sections + self._fmtTitle = nwHeadFmt.TITLE # Formatting for titles + self._fmtChapter = nwHeadFmt.TITLE # Formatting for numbered chapters + self._fmtUnNum = nwHeadFmt.TITLE # Formatting for unnumbered chapters + self._fmtScene = nwHeadFmt.TITLE # Formatting for scenes + self._fmtSection = nwHeadFmt.TITLE # Formatting for sections self._hideScene = False # Do not include scene headers self._hideSection = False # Do not include section headers @@ -751,19 +751,19 @@ class HeadingFormatter: def apply(self, hFormat: str, text: str): """Apply formatting to a specific heading. """ - hFormat = hFormat.replace(nwHeadingFormats.TITLE, text) - hFormat = hFormat.replace(nwHeadingFormats.CH_NUM, str(self._chCount)) - hFormat = hFormat.replace(nwHeadingFormats.SC_NUM, str(self._scChCount)) - hFormat = hFormat.replace(nwHeadingFormats.SC_ABS, str(self._scAbsCount)) - if nwHeadingFormats.CH_WORD in hFormat: + hFormat = hFormat.replace(nwHeadFmt.TITLE, text) + hFormat = hFormat.replace(nwHeadFmt.CH_NUM, str(self._chCount)) + hFormat = hFormat.replace(nwHeadFmt.SC_NUM, str(self._scChCount)) + hFormat = hFormat.replace(nwHeadFmt.SC_ABS, str(self._scAbsCount)) + if nwHeadFmt.CH_WORD in hFormat: chWord = self._project.localLookup(self._chCount) - hFormat = hFormat.replace(nwHeadingFormats.CH_WORD, chWord) - if nwHeadingFormats.CH_ROML in hFormat: + hFormat = hFormat.replace(nwHeadFmt.CH_WORD, chWord) + if nwHeadFmt.CH_ROML in hFormat: chRom = numberToRoman(self._chCount, True) - hFormat = hFormat.replace(nwHeadingFormats.CH_ROML, chRom) - if nwHeadingFormats.CH_ROMU in hFormat: + hFormat = hFormat.replace(nwHeadFmt.CH_ROML, chRom) + if nwHeadFmt.CH_ROMU in hFormat: chRom = numberToRoman(self._chCount, False) - hFormat = hFormat.replace(nwHeadingFormats.CH_ROMU, chRom) + hFormat = hFormat.replace(nwHeadFmt.CH_ROMU, chRom) return hFormat diff --git a/novelwriter/tools/manussettings.py b/novelwriter/tools/manussettings.py index fecf764e..7ed048a7 100644 --- a/novelwriter/tools/manussettings.py +++ b/novelwriter/tools/manussettings.py @@ -41,7 +41,7 @@ from PyQt5.QtWidgets import ( ) from novelwriter import CONFIG -from novelwriter.constants import nwConst, nwHeadingFormats +from novelwriter.constants import nwConst, nwHeadFmt from novelwriter.core.buildsettings import BuildSettings, FilterMode from novelwriter.extensions.switch import NSwitch from novelwriter.extensions.switchbox import NSwitchBox @@ -706,13 +706,13 @@ class GuiBuildHeadingsTab(QWidget): self.aInsScNum = self.menuInsert.addAction(self.tr("Scene Number (In Chapter)")) self.aInsScAbs = self.menuInsert.addAction(self.tr("Scene Number (Absolute)")) - self.aInsTitle.triggered.connect(lambda: self._insertIntoForm(nwHeadingFormats.TITLE)) - self.aInsChNum.triggered.connect(lambda: self._insertIntoForm(nwHeadingFormats.CH_NUM)) - self.aInsChWord.triggered.connect(lambda: self._insertIntoForm(nwHeadingFormats.CH_WORD)) - self.aInsChRomU.triggered.connect(lambda: self._insertIntoForm(nwHeadingFormats.CH_ROMU)) - self.aInsChRomL.triggered.connect(lambda: self._insertIntoForm(nwHeadingFormats.CH_ROML)) - self.aInsScNum.triggered.connect(lambda: self._insertIntoForm(nwHeadingFormats.SC_NUM)) - self.aInsScAbs.triggered.connect(lambda: self._insertIntoForm(nwHeadingFormats.SC_ABS)) + self.aInsTitle.triggered.connect(lambda: self._insertIntoForm(nwHeadFmt.TITLE)) + self.aInsChNum.triggered.connect(lambda: self._insertIntoForm(nwHeadFmt.CH_NUM)) + self.aInsChWord.triggered.connect(lambda: self._insertIntoForm(nwHeadFmt.CH_WORD)) + self.aInsChRomU.triggered.connect(lambda: self._insertIntoForm(nwHeadFmt.CH_ROMU)) + self.aInsChRomL.triggered.connect(lambda: self._insertIntoForm(nwHeadFmt.CH_ROML)) + self.aInsScNum.triggered.connect(lambda: self._insertIntoForm(nwHeadFmt.SC_NUM)) + self.aInsScAbs.triggered.connect(lambda: self._insertIntoForm(nwHeadFmt.SC_ABS)) self.btnInsert = QPushButton(self.tr("Insert")) self.btnInsert.setMenu(self.menuInsert) @@ -845,31 +845,17 @@ class GuiHeadingSyntax(QSyntaxHighlighter): def __init__(self, document: QTextDocument, mainTheme: GuiTheme): super().__init__(document) - - self._valid = [ - nwHeadingFormats.TITLE.lower(), - nwHeadingFormats.CH_NUM.lower(), - nwHeadingFormats.CH_WORD.lower(), - nwHeadingFormats.CH_ROMU.lower(), - nwHeadingFormats.CH_ROML.lower(), - nwHeadingFormats.SC_NUM.lower(), - nwHeadingFormats.SC_ABS.lower(), - ] - self._fmtSymbol = QTextCharFormat() self._fmtSymbol.setForeground(QColor(*mainTheme.colHead)) - self._fmtFormat = QTextCharFormat() self._fmtFormat.setForeground(QColor(*mainTheme.colEmph)) - return def highlightBlock(self, text: str): """Add syntax highlighting to the text block. """ - check = text.lower() - for heading in self._valid: - pos = check.find(heading) + for heading in nwHeadFmt.ALL: + pos = text.find(heading) if pos >= 0: chars = len(heading) self.setFormat(pos, chars, self._fmtSymbol) diff --git a/tests/test_core/test_core_docbuild.py b/tests/test_core/test_core_docbuild.py index 5b56f317..dde00264 100644 --- a/tests/test_core/test_core_docbuild.py +++ b/tests/test_core/test_core_docbuild.py @@ -30,28 +30,6 @@ from tools import ODT_IGNORE, cmpFiles from novelwriter.core.project import NWProject from novelwriter.core.docbuild import NWBuildDocument -# BUILD_CONF = { -# "format.fmtTitle": "Title: %title%", -# "format.fmtChapter": "Chapter: %title%", -# "format.fmtUnnumbered": "%title%", -# "format.fmtScene": "Scene: %title%", -# "format.fmtSection": "Section: %title%", -# "format.buildLang": "en_GB", -# "format.hideScene": False, -# "format.hideSection": False, -# "format.textFont": "Arial", -# "format.textSize": 12, -# "format.lineHeight": 1.5, -# "format.justifyText": True, -# "format.noStyling": False, -# "format.replaceUCode": False, -# "filter.includeSynopsis": True, -# "filter.includeComments": True, -# "filter.includeKeywords": True, -# "filter.includeBody": True, -# "process.replaceTabs": True, -# } - BUILD_CONF = { "name": "Test Build", "uuid": "f8796eee-e234-4e8a-8355-b2709177e53c", diff --git a/tests/test_core/test_core_tokenizer.py b/tests/test_core/test_core_tokenizer.py index 053daf90..39bbc8fc 100644 --- a/tests/test_core/test_core_tokenizer.py +++ b/tests/test_core/test_core_tokenizer.py @@ -20,10 +20,10 @@ along with this program. If not, see . """ import pytest -from novelwriter.constants import nwHeadingFormats from tools import C, buildTestProject, readFile +from novelwriter.constants import nwHeadFmt from novelwriter.core.project import NWProject from novelwriter.core.tokenizer import Tokenizer, stripEscape @@ -41,11 +41,11 @@ def testCoreToken_Setters(mockGUI): theToken = BareTokenizer(theProject) # Verify defaults - assert theToken._fmtTitle == "%title%" - assert theToken._fmtChapter == "%title%" - assert theToken._fmtUnNum == "%title%" - assert theToken._fmtScene == "%title%" - assert theToken._fmtSection == "%title%" + assert theToken._fmtTitle == nwHeadFmt.TITLE + assert theToken._fmtChapter == nwHeadFmt.TITLE + assert theToken._fmtUnNum == nwHeadFmt.TITLE + assert theToken._fmtScene == nwHeadFmt.TITLE + assert theToken._fmtSection == nwHeadFmt.TITLE assert theToken._textFont == "Serif" assert theToken._textSize == 11 assert theToken._textFixed is False @@ -68,11 +68,11 @@ def testCoreToken_Setters(mockGUI): assert theToken._doKeywords is False # Set new values - theToken.setTitleFormat("T: %title%") - theToken.setChapterFormat("C: %title%") - theToken.setUnNumberedFormat("U: %title%") - theToken.setSceneFormat("S: %title%", True) - theToken.setSectionFormat("X: %title%", True) + theToken.setTitleFormat(f"T: {nwHeadFmt.TITLE}") + theToken.setChapterFormat(f"C: {nwHeadFmt.TITLE}") + theToken.setUnNumberedFormat(f"U: {nwHeadFmt.TITLE}") + theToken.setSceneFormat(f"S: {nwHeadFmt.TITLE}", True) + theToken.setSectionFormat(f"X: {nwHeadFmt.TITLE}", True) theToken.setFont("Monospace", 10, True) theToken.setLineHeight(2.0) theToken.setBlockIndent(6.0) @@ -91,11 +91,11 @@ def testCoreToken_Setters(mockGUI): theToken.setKeywords(True) # Check new values - assert theToken._fmtTitle == "T: %title%" - assert theToken._fmtChapter == "C: %title%" - assert theToken._fmtUnNum == "U: %title%" - assert theToken._fmtScene == "S: %title%" - assert theToken._fmtSection == "X: %title%" + assert theToken._fmtTitle == f"T: {nwHeadFmt.TITLE}" + assert theToken._fmtChapter == f"C: {nwHeadFmt.TITLE}" + assert theToken._fmtUnNum == f"U: {nwHeadFmt.TITLE}" + assert theToken._fmtScene == f"S: {nwHeadFmt.TITLE}" + assert theToken._fmtSection == f"X: {nwHeadFmt.TITLE}" assert theToken._textFont == "Monospace" assert theToken._textSize == 10 assert theToken._textFixed is True @@ -918,7 +918,7 @@ def testCoreToken_ProcessHeaders(mockGUI): # H1: Title, First Page assert theToken._isFirst is True theToken._theText = "# Part One\n" - theToken.setTitleFormat(f"T: {nwHeadingFormats.TITLE}") + theToken.setTitleFormat(f"T: {nwHeadFmt.TITLE}") theToken.tokenizeText() theToken.doHeaders() assert theToken._theTokens == [ @@ -929,7 +929,7 @@ def testCoreToken_ProcessHeaders(mockGUI): # H1: Title, Not First Page assert theToken._isFirst is False theToken._theText = "# Part One\n" - theToken.setTitleFormat(f"T: {nwHeadingFormats.TITLE}") + theToken.setTitleFormat(f"T: {nwHeadFmt.TITLE}") theToken.tokenizeText() theToken.doHeaders() assert theToken._theTokens == [ @@ -942,7 +942,7 @@ def testCoreToken_ProcessHeaders(mockGUI): # H2: Chapter theToken._theText = "## Chapter One\n" - theToken.setChapterFormat(f"C: {nwHeadingFormats.TITLE}") + theToken.setChapterFormat(f"C: {nwHeadFmt.TITLE}") theToken.tokenizeText() theToken.doHeaders() assert theToken._theTokens == [ @@ -952,7 +952,7 @@ def testCoreToken_ProcessHeaders(mockGUI): # H2: Unnumbered Chapter theToken._theText = "##! Prologue\n" - theToken.setUnNumberedFormat(f"U: {nwHeadingFormats.TITLE}") + theToken.setUnNumberedFormat(f"U: {nwHeadFmt.TITLE}") theToken.tokenizeText() theToken.doHeaders() assert theToken._theTokens == [ @@ -962,7 +962,7 @@ def testCoreToken_ProcessHeaders(mockGUI): # H2: Chapter Word Number theToken._theText = "## Chapter\n" - theToken.setChapterFormat(f"Chapter {nwHeadingFormats.CH_WORD}") + theToken.setChapterFormat(f"Chapter {nwHeadFmt.CH_WORD}") theToken._hFormatter._chCount = 0 theToken.tokenizeText() theToken.doHeaders() @@ -973,7 +973,7 @@ def testCoreToken_ProcessHeaders(mockGUI): # H2: Chapter Roman Number Upper Case theToken._theText = "## Chapter\n" - theToken.setChapterFormat(f"Chapter {nwHeadingFormats.CH_ROMU}") + theToken.setChapterFormat(f"Chapter {nwHeadFmt.CH_ROMU}") theToken.tokenizeText() theToken.doHeaders() assert theToken._theTokens == [ @@ -983,7 +983,7 @@ def testCoreToken_ProcessHeaders(mockGUI): # H2: Chapter Roman Number Lower Case theToken._theText = "## Chapter\n" - theToken.setChapterFormat(f"Chapter {nwHeadingFormats.CH_ROML}") + theToken.setChapterFormat(f"Chapter {nwHeadFmt.CH_ROML}") theToken.tokenizeText() theToken.doHeaders() assert theToken._theTokens == [ @@ -996,7 +996,7 @@ def testCoreToken_ProcessHeaders(mockGUI): # H3: Scene w/Title theToken._theText = "### Scene One\n" - theToken.setSceneFormat(f"S: {nwHeadingFormats.TITLE}", False) + theToken.setSceneFormat(f"S: {nwHeadFmt.TITLE}", False) theToken.tokenizeText() theToken.doHeaders() assert theToken._theTokens == [ @@ -1060,7 +1060,7 @@ def testCoreToken_ProcessHeaders(mockGUI): # H3: Scene w/Absolute Number theToken._theText = "### A Scene\n" - theToken.setSceneFormat(f"Scene {nwHeadingFormats.SC_ABS}", False) + theToken.setSceneFormat(f"Scene {nwHeadFmt.SC_ABS}", False) theToken._hFormatter._scAbsCount = 0 theToken._hFormatter._scChCount = 0 theToken.tokenizeText() @@ -1072,7 +1072,7 @@ def testCoreToken_ProcessHeaders(mockGUI): # H3: Scene w/Chapter Number theToken._theText = "### A Scene\n" - theToken.setSceneFormat(f"Scene {nwHeadingFormats.CH_NUM}.{nwHeadingFormats.SC_NUM}", False) + theToken.setSceneFormat(f"Scene {nwHeadFmt.CH_NUM}.{nwHeadFmt.SC_NUM}", False) theToken._hFormatter._scAbsCount = 0 theToken._hFormatter._scChCount = 1 theToken.tokenizeText() @@ -1107,7 +1107,7 @@ def testCoreToken_ProcessHeaders(mockGUI): # H4: Section w/Format theToken._theText = "#### A Section\n" - theToken.setSectionFormat(f"X: {nwHeadingFormats.TITLE}", False) + theToken.setSectionFormat(f"X: {nwHeadFmt.TITLE}", False) theToken.tokenizeText() theToken.doHeaders() assert theToken._theTokens == [