From 5a4b7b726471e41a1f4925df3bc57055310564c8 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Mon, 17 Oct 2022 20:08:19 +0200 Subject: [PATCH 1/6] Add a main heading variable to the NWItem class --- novelwriter/core/item.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/novelwriter/core/item.py b/novelwriter/core/item.py index 9901f39e..185d84fa 100644 --- a/novelwriter/core/item.py +++ b/novelwriter/core/item.py @@ -31,7 +31,7 @@ from novelwriter.enum import nwItemType, nwItemClass, nwItemLayout from novelwriter.common import ( checkInt, isHandle, isItemClass, isItemLayout, isItemType, simplified ) -from novelwriter.constants import nwLabels, trConst +from novelwriter.constants import nwHeaders, nwLabels, trConst logger = logging.getLogger(__name__) @@ -56,11 +56,12 @@ class NWItem: self._exported = True # Document Meta Data - self._charCount = 0 # Current character count - self._wordCount = 0 # Current word count - self._paraCount = 0 # Current paragraph count - self._cursorPos = 0 # Last cursor position - self._initCount = 0 # Initial word count + self._heading = "H0" # The main heading + self._charCount = 0 # Current character count + self._wordCount = 0 # Current word count + self._paraCount = 0 # Current paragraph count + self._cursorPos = 0 # Last cursor position + self._initCount = 0 # Initial word count return @@ -122,6 +123,10 @@ class NWItem: def isExported(self): return self._exported + @property + def mainHeading(self): + return self._heading + @property def charCount(self): return self._charCount @@ -162,6 +167,7 @@ class NWItem: metaAttrib = {} metaAttrib["expanded"] = str(self._expanded) if self._type == nwItemType.FILE: + metaAttrib["mainHeading"] = str(self._heading) metaAttrib["charCount"] = str(self._charCount) metaAttrib["wordCount"] = str(self._wordCount) metaAttrib["paraCount"] = str(self._paraCount) @@ -202,6 +208,7 @@ class NWItem: for xValue in xItem: if xValue.tag == "meta": self.setExpanded(xValue.attrib.get("expanded", False)) + self.setMainHeading(xValue.attrib.get("mainHeading", "H0")) self.setCharCount(xValue.attrib.get("charCount", 0)) self.setWordCount(xValue.attrib.get("wordCount", 0)) self.setParaCount(xValue.attrib.get("paraCount", 0)) @@ -511,6 +518,13 @@ class NWItem: # Set Document Meta Data ## + def setMainHeading(self, value): + """Set the main heading level. + """ + if value in nwHeaders.H_LEVEL: + self._heading = value + return + def setCharCount(self, count): """Set the character count, and ensure that it is an integer. """ From 8fe25d4b43e4237e20e0b5514ba1ca236bba6172 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Mon, 17 Oct 2022 20:15:45 +0200 Subject: [PATCH 2/6] Update classes and functions to use the item as source of main heading instead of index --- novelwriter/core/doctools.py | 2 +- novelwriter/core/item.py | 10 +++++----- novelwriter/dialogs/docmerge.py | 3 +-- novelwriter/gui/doceditor.py | 7 +++---- novelwriter/gui/itemdetails.py | 5 ++--- novelwriter/gui/outline.py | 3 +-- novelwriter/gui/projtree.py | 4 ++-- 7 files changed, 15 insertions(+), 19 deletions(-) diff --git a/novelwriter/core/doctools.py b/novelwriter/core/doctools.py index e26f6681..54cb9d25 100644 --- a/novelwriter/core/doctools.py +++ b/novelwriter/core/doctools.py @@ -91,7 +91,7 @@ class DocMerger: docText = (inDoc.readDocument() or "").rstrip("\n") if addComment: - docInfo = srcItem.describeMe("H0") + docInfo = srcItem.describeMe() docSt, _ = srcItem.getImportStatus(incIcon=False) cmtLine = f"% {cmtPrefix} {docInfo}: {srcItem.itemName} [{docSt}]\n\n" docText = cmtLine + docText diff --git a/novelwriter/core/item.py b/novelwriter/core/item.py index 185d84fa..22f48cdd 100644 --- a/novelwriter/core/item.py +++ b/novelwriter/core/item.py @@ -276,7 +276,7 @@ class NWItem: # Lookup Methods ## - def describeMe(self, hLevel=None): + def describeMe(self): """Return a string description of the item. """ descKey = "none" @@ -286,13 +286,13 @@ class NWItem: descKey = "folder" elif self._type == nwItemType.FILE: if self._layout == nwItemLayout.DOCUMENT: - if hLevel == "H1": + if self._heading == "H1": descKey = "doc_h1" - elif hLevel == "H2": + elif self._heading == "H2": descKey = "doc_h2" - elif hLevel == "H3": + elif self._heading == "H3": descKey = "doc_h3" - elif hLevel == "H4": + elif self._heading == "H4": descKey = "doc_h4" else: descKey = "document" diff --git a/novelwriter/dialogs/docmerge.py b/novelwriter/dialogs/docmerge.py index fd9fcebe..9bc822d4 100644 --- a/novelwriter/dialogs/docmerge.py +++ b/novelwriter/dialogs/docmerge.py @@ -155,9 +155,8 @@ class GuiDocMerge(QDialog): if nwItem is None or not nwItem.isFileType(): continue - hLevel = self.theProject.index.getHandleHeaderLevel(tHandle) itemIcon = self.mainTheme.getItemIcon( - nwItem.itemType, nwItem.itemClass, nwItem.itemLayout, hLevel + nwItem.itemType, nwItem.itemClass, nwItem.itemLayout, nwItem.mainHeading ) newItem = QListWidgetItem() diff --git a/novelwriter/gui/doceditor.py b/novelwriter/gui/doceditor.py index cc96b9e9..6ae8bec7 100644 --- a/novelwriter/gui/doceditor.py +++ b/novelwriter/gui/doceditor.py @@ -508,9 +508,9 @@ class GuiDocEditor(QTextEdit): self.setDocumentChanged(False) - oldHeader = self.theProject.index.getHandleHeaderLevel(tHandle) + oldHeader = self._nwItem.mainHeading self.theProject.index.scanText(tHandle, docText) - newHeader = self.theProject.index.getHandleHeaderLevel(tHandle) + newHeader = self._nwItem.mainHeading # ToDo: This should be a signal if self._updateHeaders(checkLevel=True): @@ -2972,8 +2972,7 @@ class GuiDocEditFooter(QWidget): else: theStatus, theIcon = self._theItem.getImportStatus() sIcon = theIcon.pixmap(self.sPx, self.sPx) - hLevel = self.theProject.index.getHandleHeaderLevel(self._docHandle) - sText = f"{theStatus} / {self._theItem.describeMe(hLevel)}" + sText = f"{theStatus} / {self._theItem.describeMe()}" self.statusIcon.setPixmap(sIcon) self.statusText.setText(sText) diff --git a/novelwriter/gui/itemdetails.py b/novelwriter/gui/itemdetails.py index dbd2838c..56258667 100644 --- a/novelwriter/gui/itemdetails.py +++ b/novelwriter/gui/itemdetails.py @@ -273,12 +273,11 @@ class GuiItemDetails(QWidget): # Layout # ====== - hLevel = self.theProject.index.getHandleHeaderLevel(tHandle) usageIcon = self.mainTheme.getItemIcon( - nwItem.itemType, nwItem.itemClass, nwItem.itemLayout, hLevel + nwItem.itemType, nwItem.itemClass, nwItem.itemLayout, nwItem.mainHeading ) self.usageIcon.setPixmap(usageIcon.pixmap(iPx, iPx)) - self.usageData.setText(nwItem.describeMe(hLevel)) + self.usageData.setText(nwItem.describeMe()) # Counts # ====== diff --git a/novelwriter/gui/outline.py b/novelwriter/gui/outline.py index d0837360..ed18d64a 100644 --- a/novelwriter/gui/outline.py +++ b/novelwriter/gui/outline.py @@ -685,7 +685,6 @@ class GuiOutlineTree(QTreeWidget): for _, tHandle, sTitle, novIdx in novStruct: iLevel = nwHeaders.H_LEVEL.get(novIdx.level, 0) - dLevel = self.theProject.index.getHandleHeaderLevel(tHandle) if iLevel == 0: continue @@ -699,7 +698,7 @@ class GuiOutlineTree(QTreeWidget): trItem.setData(self._colIdx[nwOutline.TITLE], self.D_TITLE, sTitle) trItem.setFont(self._colIdx[nwOutline.TITLE], self._hFonts[iLevel]) trItem.setText(self._colIdx[nwOutline.LEVEL], novIdx.level) - trItem.setIcon(self._colIdx[nwOutline.LABEL], self._dIcon[dLevel]) + trItem.setIcon(self._colIdx[nwOutline.LABEL], self._dIcon[nwItem.mainHeading]) trItem.setText(self._colIdx[nwOutline.LABEL], nwItem.itemName) trItem.setText(self._colIdx[nwOutline.LINE], sTitle[1:].lstrip("0")) trItem.setText(self._colIdx[nwOutline.SYNOP], novIdx.synopsis) diff --git a/novelwriter/gui/projtree.py b/novelwriter/gui/projtree.py index 15bc7cbf..caa28602 100644 --- a/novelwriter/gui/projtree.py +++ b/novelwriter/gui/projtree.py @@ -516,7 +516,7 @@ class GuiProjectTree(QTreeWidget): # Collect some information about the selected item that pItem = self.theProject.tree[sHandle] qItem = self._getTreeItem(sHandle) - sLevel = nwHeaders.H_LEVEL.get(self.theProject.index.getHandleHeaderLevel(sHandle), 0) + sLevel = nwHeaders.H_LEVEL.get(pItem.mainHeading, 0) sIsParent = False if qItem is None else qItem.childCount() > 0 if self.theProject.tree.isTrash(sHandle): @@ -897,7 +897,7 @@ class GuiProjectTree(QTreeWidget): return itemStatus, statusIcon = nwItem.getImportStatus() - hLevel = self.theProject.index.getHandleHeaderLevel(tHandle) + hLevel = nwItem.mainHeading itemIcon = self.mainTheme.getItemIcon( nwItem.itemType, nwItem.itemClass, nwItem.itemLayout, hLevel ) From e24573f6441b5e56091458a93bff51fd2efac0b1 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Mon, 17 Oct 2022 20:22:21 +0200 Subject: [PATCH 3/6] Clean out main header data in the index and make it set the item's value instead --- novelwriter/core/index.py | 121 +++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 59 deletions(-) diff --git a/novelwriter/core/index.py b/novelwriter/core/index.py index e9491027..1332413c 100644 --- a/novelwriter/core/index.py +++ b/novelwriter/core/index.py @@ -243,20 +243,43 @@ class NWIndex: if theItem.itemParent is None: logger.info("Not indexing orphaned item '%s'", tHandle) return False - if theItem.isInactive(): - logger.debug("Not indexing inactive item '%s'", tHandle) - return False logger.debug("Indexing item with handle '%s'", tHandle) + if theItem.isInactive(): + self._scanInactive(theItem, theText) + else: + self._scanActive(tHandle, theItem, theText, itemTags) - # Scan the text content + # Update timestamps for index changes + nowTime = round(time()) + self._indexChange = nowTime + self._rootChange[theItem.itemRoot] = nowTime + + return True + + ## + # Internal Indexer Helpers + ## + + def _scanActive(self, tHandle, theItem, theText, itemTags): + """Scan an active document for meta data. + """ nTitle = 0 + findHeader = True theLines = theText.splitlines() + for nLine, aLine in enumerate(theLines, start=1): + if len(aLine.strip()) == 0: continue if aLine.startswith("#"): + if findHeader: + hDepth, _ = self._splitHeading(aLine) + if hDepth != "H0": + theItem.setMainHeading(hDepth) + findHeader = False + isTitle = self._indexTitle(tHandle, aLine, nLine) if isTitle and nLine > 0: if nTitle > 0: @@ -292,45 +315,46 @@ class NWIndex: logger.verbose("Deleting removed tag '%s'", tTag) del self._tagsIndex[tTag] - # Update timestamps for index changes - nowTime = round(time()) - self._indexChange = nowTime - self._rootChange[theItem.itemRoot] = nowTime + return - return True + def _scanInactive(self, theItem, theText): + """Scan an inactive document for meta data. + """ + for aLine in theText.splitlines(): + if aLine.startswith("#"): + hDepth, _ = self._splitHeading(aLine) + if hDepth != "H0": + theItem.setMainHeading(hDepth) + break + return - ## - # Internal Indexer Helpers - ## + def _splitHeading(self, aLine): + """Split a heading into its header level and text value. + """ + if aLine.startswith("# "): + return "H1", aLine[2:].strip() + elif aLine.startswith("## "): + return "H2", aLine[3:].strip() + elif aLine.startswith("### "): + return "H3", aLine[4:].strip() + elif aLine.startswith("#### "): + return "H4", aLine[5:].strip() + elif aLine.startswith("#! "): + return "H1", aLine[3:].strip() + elif aLine.startswith("##! "): + return "H2", aLine[4:].strip() + return "H0", "" def _indexTitle(self, tHandle, aLine, nTitle): """Save information about the title and its location in the file to the index. """ - if aLine.startswith("# "): - hDepth = "H1" - hText = aLine[2:].strip() - elif aLine.startswith("## "): - hDepth = "H2" - hText = aLine[3:].strip() - elif aLine.startswith("### "): - hDepth = "H3" - hText = aLine[4:].strip() - elif aLine.startswith("#### "): - hDepth = "H4" - hText = aLine[5:].strip() - elif aLine.startswith("#! "): - hDepth = "H1" - hText = aLine[3:].strip() - elif aLine.startswith("##! "): - hDepth = "H2" - hText = aLine[4:].strip() - else: + hDepth, hText = self._splitHeading(aLine) + if hDepth == "H0": return False sTitle = f"T{nTitle:06d}" self._itemIndex.addItemHeading(tHandle, sTitle, hDepth, hText) - return True def _indexWordCounts(self, tHandle, theText, nTitle): @@ -493,11 +517,6 @@ class NWIndex: for sTitle, hItem in self._itemIndex.iterItemHeaders(tHandle) ] - def getHandleHeaderLevel(self, tHandle): - """Get the header level of the first header of a handle. - """ - return self._itemIndex.mainItemHeader(tHandle) - def getTableOfContents(self, rootHandle, maxDepth, skipExcl=True): """Generate a table of contents up to a maximum depth. """ @@ -757,13 +776,6 @@ class ItemIndex: self._items[tHandle] = IndexItem(tHandle, tItem) return - def mainItemHeader(self, tHandle): - """Return the primary item header for an item. - """ - if tHandle in self._items: - return self._items[tHandle].level - return "H0" - def allItemTags(self, tHandle): """Get all tags set for headings of an item. """ @@ -823,7 +835,6 @@ class ItemIndex: """ if tHandle in self._items: tItem = self._items[tHandle] - tItem.updateLevel(hDepth) tItem.addHeading(IndexHeading(sTitle, hDepth, hText)) return @@ -899,7 +910,6 @@ class IndexItem: def __init__(self, tHandle, tItem): self._handle = tHandle self._item = tItem - self._level = "H0" self._headings = {} self._index = 0 @@ -919,21 +929,10 @@ class IndexItem: def item(self): return self._item - @property - def level(self): - return self._level - ## # Setters ## - def updateLevel(self, level): - """Set the level only if it has not already been set. - """ - if self._level == "H0": - self._level = level - return - def addHeading(self, tHeading): """Add a heading to the item. Also remove the placeholder entry if it exists. @@ -1013,7 +1012,7 @@ class IndexItem: if hRefs: refs[sTitle] = hRefs - data = {"level": self._level} + data = {} data["headings"] = heads if refs: data["references"] = refs @@ -1023,7 +1022,10 @@ class IndexItem: def unpackData(self, data): """Unpack an item entry from the data. """ - self._level = data.get("level", "H0") + if "level" in data: + # This value is now tracked as NWItem.mainHeading + raise ValueError("Outdated value found in index") + references = data.get("references", {}) for sTitle, hData in data.get("headings", {}).items(): if not isTitleTag(sTitle): @@ -1032,6 +1034,7 @@ class IndexItem: tHeading.unpackData(hData) tHeading.unpackReferences(references.get(sTitle, {})) self.addHeading(tHeading) + return # END Class IndexItem From 38df69398783e23e2d3e011dd33d850fa6fcb94d Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Mon, 17 Oct 2022 21:15:52 +0200 Subject: [PATCH 4/6] Update tests --- sample/nwProject.nwx | 44 ++--- tests/minimal/nwProject.nwx | 14 +- .../coreDocTools_DocMerger_0000000000010.nwd | 6 +- .../coreDocTools_DocMerger_0000000000014.nwd | 8 +- .../coreIndex_LoadSave_tagsIndex.json | 15 -- .../coreProject_NewCustomA_nwProject.nwx | 34 ++-- .../coreProject_NewCustomB_nwProject.nwx | 22 +-- .../coreProject_NewFileFolder_nwProject.nwx | 16 +- .../coreProject_NewMinimal_nwProject.nwx | 8 +- .../coreProject_NewRoot_nwProject.nwx | 12 +- .../guiEditor_Main_Final_nwProject.nwx | 16 +- .../guiEditor_Main_Initial_nwProject.nwx | 8 +- .../guiProjSettings_Dialog_nwProject.nwx | 8 +- tests/test_core/test_core_index.py | 164 +++++++++--------- tests/test_core/test_core_item.py | 39 ++++- tests/test_core/test_core_tree.py | 18 +- tests/tools.py | 6 +- 17 files changed, 225 insertions(+), 213 deletions(-) diff --git a/sample/nwProject.nwx b/sample/nwProject.nwx index c822a904..75f59236 100644 --- a/sample/nwProject.nwx +++ b/sample/nwProject.nwx @@ -1,13 +1,13 @@ - + Sample Project Sample Project Jane Smith Jay Doh - 1382 + 1383 236 - 69339 + 69344 False @@ -55,43 +55,43 @@ Novel - + Title Page - + Page - + Part One - + Chapter One - + Making a Scene - + Another Scene - + Interlude - + A Note on Structure - + Chapter Two - + We Found John! @@ -99,11 +99,11 @@ Sequel - + Title Page - + Chapter One @@ -115,11 +115,11 @@ Main Characters - + John Smith - + Jane Smith @@ -127,15 +127,15 @@ Locations - + Earth - + Space - + Mars @@ -147,7 +147,7 @@ Scenes - + Old File @@ -155,7 +155,7 @@ Trash - + Delete Me! diff --git a/tests/minimal/nwProject.nwx b/tests/minimal/nwProject.nwx index 5862d48c..99f81105 100644 --- a/tests/minimal/nwProject.nwx +++ b/tests/minimal/nwProject.nwx @@ -1,13 +1,13 @@ - + Test Minimal Minimal Jane Doe John Doh - 17 + 19 2 - 150 + 167 True @@ -16,7 +16,7 @@ None None None - None + a508bb932959c None 10 10 @@ -48,7 +48,7 @@ Novel - + Title Page @@ -56,11 +56,11 @@ New Chapter - + New Chapter - + New Scene diff --git a/tests/reference/coreDocTools_DocMerger_0000000000010.nwd b/tests/reference/coreDocTools_DocMerger_0000000000010.nwd index 6dac570d..eb13ead2 100644 --- a/tests/reference/coreDocTools_DocMerger_0000000000010.nwd +++ b/tests/reference/coreDocTools_DocMerger_0000000000010.nwd @@ -7,7 +7,7 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc maximus justo non Nullam laoreet lorem nec malesuada vehicula. Vivamus tempus sodales lectus sed viverra. Aenean lacinia sollicitudin quam, quis tempus eros suscipit id. Duis sed rutrum nisi, ut pulvinar magna. Nam et cursus tortor. Phasellus ac odio tellus. Nullam in iaculis ipsum. Vivamus ante sem, ultricies sed varius quis, tristique nec tellus. Nullam eu urna vitae lacus hendrerit gravida. Quisque pulvinar erat ex, id efficitur velit sodales vitae. Proin vestibulum, sapien eget mattis euismod, tortor quam viverra risus, at congue mauris tortor eu nunc. Mauris pellentesque elit leo, quis eleifend sem placerat a. Vivamus iaculis dui eget tellus volutpat, ac varius nisi facilisis. -% Merge Novel Document: Scene 1.1 [New] +% Merge Novel Scene: Scene 1.1 [New] ### Scene 1.1 @@ -15,7 +15,7 @@ Nullam laoreet lorem nec malesuada vehicula. Vivamus tempus sodales lectus sed v Nullam a nisl magna. Praesent commodo nec diam aliquet vestibulum. In sapien velit, sodales feugiat porta ut, rhoncus a elit. Quisque egestas nisi eu eros laoreet, quis facilisis est pretium. Nullam bibendum sed tellus nec lobortis. Duis elit massa, volutpat a lacinia a, ullamcorper in dui. Suspendisse ac laoreet dui. Curabitur elementum, tortor elementum ultricies laoreet, nunc massa vulputate augue, vitae tincidunt nunc enim eget nisl. -% Merge Novel Document: Scene 1.2 [New] +% Merge Novel Scene: Scene 1.2 [New] ### Scene 1.2 @@ -23,7 +23,7 @@ Nullam a nisl magna. Praesent commodo nec diam aliquet vestibulum. In sapien vel Pellentesque nibh urna, volutpat et feugiat porta, rutrum sed lectus. Aliquam eget risus id orci tincidunt condimentum et sit amet purus. Curabitur tincidunt odio vel ante feugiat feugiat. Proin nunc lorem, molestie a sapien et, varius elementum nunc. Donec non fermentum nisl. In et massa placerat, faucibus felis eu, congue nisi. Proin sed tortor non lorem mattis cursus. Vestibulum magna neque, bibendum vel nibh et, tincidunt rhoncus nisi. Duis pulvinar mi a quam rutrum maximus. Nunc sollicitudin, urna in cursus facilisis, augue neque imperdiet metus, ac finibus lorem ante id nulla. Sed maximus eleifend justo id feugiat. Cras eget diam vel est blandit tempor nec a leo. Mauris risus est, fringilla in aliquam a, sagittis vel enim. Nullam sodales id erat placerat lobortis. -% Merge Novel Document: Scene 1.3 [New] +% Merge Novel Scene: Scene 1.3 [New] ### Scene 1.3 diff --git a/tests/reference/coreDocTools_DocMerger_0000000000014.nwd b/tests/reference/coreDocTools_DocMerger_0000000000014.nwd index 6a0d3548..199454dc 100644 --- a/tests/reference/coreDocTools_DocMerger_0000000000014.nwd +++ b/tests/reference/coreDocTools_DocMerger_0000000000014.nwd @@ -1,7 +1,7 @@ %%~name: All of Chapter 1 %%~path: 0000000000008/0000000000014 %%~kind: NOVEL/DOCUMENT -% Merge Novel Document: Chapter 1 [New] +% Merge Novel Chapter: Chapter 1 [New] ## Chapter 1 @@ -9,7 +9,7 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc maximus justo non Nullam laoreet lorem nec malesuada vehicula. Vivamus tempus sodales lectus sed viverra. Aenean lacinia sollicitudin quam, quis tempus eros suscipit id. Duis sed rutrum nisi, ut pulvinar magna. Nam et cursus tortor. Phasellus ac odio tellus. Nullam in iaculis ipsum. Vivamus ante sem, ultricies sed varius quis, tristique nec tellus. Nullam eu urna vitae lacus hendrerit gravida. Quisque pulvinar erat ex, id efficitur velit sodales vitae. Proin vestibulum, sapien eget mattis euismod, tortor quam viverra risus, at congue mauris tortor eu nunc. Mauris pellentesque elit leo, quis eleifend sem placerat a. Vivamus iaculis dui eget tellus volutpat, ac varius nisi facilisis. -% Merge Novel Document: Scene 1.1 [New] +% Merge Novel Scene: Scene 1.1 [New] ### Scene 1.1 @@ -17,7 +17,7 @@ Nullam laoreet lorem nec malesuada vehicula. Vivamus tempus sodales lectus sed v Nullam a nisl magna. Praesent commodo nec diam aliquet vestibulum. In sapien velit, sodales feugiat porta ut, rhoncus a elit. Quisque egestas nisi eu eros laoreet, quis facilisis est pretium. Nullam bibendum sed tellus nec lobortis. Duis elit massa, volutpat a lacinia a, ullamcorper in dui. Suspendisse ac laoreet dui. Curabitur elementum, tortor elementum ultricies laoreet, nunc massa vulputate augue, vitae tincidunt nunc enim eget nisl. -% Merge Novel Document: Scene 1.2 [New] +% Merge Novel Scene: Scene 1.2 [New] ### Scene 1.2 @@ -25,7 +25,7 @@ Nullam a nisl magna. Praesent commodo nec diam aliquet vestibulum. In sapien vel Pellentesque nibh urna, volutpat et feugiat porta, rutrum sed lectus. Aliquam eget risus id orci tincidunt condimentum et sit amet purus. Curabitur tincidunt odio vel ante feugiat feugiat. Proin nunc lorem, molestie a sapien et, varius elementum nunc. Donec non fermentum nisl. In et massa placerat, faucibus felis eu, congue nisi. Proin sed tortor non lorem mattis cursus. Vestibulum magna neque, bibendum vel nibh et, tincidunt rhoncus nisi. Duis pulvinar mi a quam rutrum maximus. Nunc sollicitudin, urna in cursus facilisis, augue neque imperdiet metus, ac finibus lorem ante id nulla. Sed maximus eleifend justo id feugiat. Cras eget diam vel est blandit tempor nec a leo. Mauris risus est, fringilla in aliquam a, sagittis vel enim. Nullam sodales id erat placerat lobortis. -% Merge Novel Document: Scene 1.3 [New] +% Merge Novel Scene: Scene 1.3 [New] ### Scene 1.3 diff --git a/tests/reference/coreIndex_LoadSave_tagsIndex.json b/tests/reference/coreIndex_LoadSave_tagsIndex.json index 60c59d86..cba693dc 100644 --- a/tests/reference/coreIndex_LoadSave_tagsIndex.json +++ b/tests/reference/coreIndex_LoadSave_tagsIndex.json @@ -6,31 +6,26 @@ }, "itemIndex": { "7a992350f3eb6": { - "level": "H1", "headings": { "T000001": {"level": "H1", "title": "Lorem Ipsum", "tag": "", "cCount": 230, "wCount": 40, "pCount": 3, "synopsis": ""} } }, "8c58a65414c23": { - "level": "H0", "headings": { "T000000": {"level": "H0", "title": "", "tag": "", "cCount": 1058, "wCount": 176, "pCount": 2, "synopsis": ""} } }, "88d59a277361b": { - "level": "H2", "headings": { "T000001": {"level": "H2", "title": "Prologue", "tag": "", "cCount": 584, "wCount": 92, "pCount": 1, "synopsis": "Explanation from the lipsum.com website."} } }, "db7e733775d4d": { - "level": "H1", "headings": { "T000001": {"level": "H1", "title": "Act One", "tag": "", "cCount": 35, "wCount": 6, "pCount": 1, "synopsis": ""} } }, "fb609cd8319dc": { - "level": "H2", "headings": { "T000001": {"level": "H2", "title": "Chapter One", "tag": "", "cCount": 419, "wCount": 67, "pCount": 1, "synopsis": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque at aliquam quam."} }, @@ -39,7 +34,6 @@ } }, "88243afbe5ed8": { - "level": "H3", "headings": { "T000001": {"level": "H3", "title": "Scene One", "tag": "", "cCount": 1197, "wCount": 174, "pCount": 2, "synopsis": "Aenean ut placerat velit. Etiam laoreet ullamcorper risus, eget lobortis enim scelerisque non. Suspendisse id maximus nunc, et mollis sapien. Curabitur vel semper sapien, non pulvinar dolor. Etiam finibus nisi vel mi molestie consectetur."}, "T000013": {"level": "H4", "title": "Scene One, Section Two", "tag": "", "cCount": 1561, "wCount": 230, "pCount": 2, "synopsis": ""} @@ -49,7 +43,6 @@ } }, "f96ec11c6a3da": { - "level": "H3", "headings": { "T000001": {"level": "H3", "title": "Scene Two", "tag": "", "cCount": 2034, "wCount": 299, "pCount": 3, "synopsis": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Integer sapien nulla, dictum at lacus a, dignissim consectetur dolor. Nunc vel eleifend lacus, eu dapibus orci."}, "T000015": {"level": "H4", "title": "Scene Two, Section Two", "tag": "", "cCount": 2009, "wCount": 301, "pCount": 3, "synopsis": ""} @@ -59,13 +52,11 @@ } }, "846352075de7d": { - "level": "H2", "headings": { "T000001": {"level": "H2", "title": "Why do we use it?", "tag": "", "cCount": 631, "wCount": 109, "pCount": 3, "synopsis": ""} } }, "441420a886d82": { - "level": "H2", "headings": { "T000001": {"level": "H2", "title": "Chapter Two", "tag": "", "cCount": 477, "wCount": 70, "pCount": 1, "synopsis": "Curabitur a elit posuere, varius ex et, convallis neque. Phasellus sagittis pharetra sem vitae dapibus. Curabitur varius lorem non pulvinar congue."} }, @@ -74,7 +65,6 @@ } }, "eb103bc70c90c": { - "level": "H3", "headings": { "T000001": {"level": "H3", "title": "Scene Three", "tag": "", "cCount": 3006, "wCount": 439, "pCount": 4, "synopsis": "Aenean ut libero ut lectus porttitor rhoncus vel et massa. Nam pretium, nibh et varius vehicula, urna metus blandit eros, euismod pharetra diam diam et libero. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos."} }, @@ -83,7 +73,6 @@ } }, "f8c0562e50f1b": { - "level": "H3", "headings": { "T000001": {"level": "H3", "title": "Scene Four", "tag": "", "cCount": 3839, "wCount": 563, "pCount": 6, "synopsis": "Nam tempor blandit magna laoreet aliquet. Vestibulum auctor posuere leo, ac gravida nisi rhoncus varius. Aenean posuere dolor vitae condimentum volutpat. Donec egestas volutpat risus, quis luctus justo."} }, @@ -92,7 +81,6 @@ } }, "47666c91c7ccf": { - "level": "H3", "headings": { "T000001": {"level": "H3", "title": "Scene Five", "tag": "", "cCount": 3644, "wCount": 543, "pCount": 5, "synopsis": "Praesent eget est porta, dictum ante in, egestas risus. Mauris risus mauris, consequat aliquam mauris et, feugiat iaculis ipsum. Aliquam arcu ipsum, fermentum ut arcu sed, lobortis euismod sem. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus."} }, @@ -101,7 +89,6 @@ } }, "4c4f28287af27": { - "level": "H1", "headings": { "T000001": {"level": "H1", "title": "Nobody Owens", "tag": "Bod", "cCount": 1864, "wCount": 284, "pCount": 3, "synopsis": ""} }, @@ -110,13 +97,11 @@ } }, "2426c6f0ca922": { - "level": "H1", "headings": { "T000001": {"level": "H1", "title": "Main Plot", "tag": "Main", "cCount": 1369, "wCount": 195, "pCount": 2, "synopsis": ""} } }, "04468803b92e1": { - "level": "H1", "headings": { "T000001": {"level": "H1", "title": "Ancient Europe", "tag": "Europe", "cCount": 1770, "wCount": 259, "pCount": 3, "synopsis": ""} } diff --git a/tests/reference/coreProject_NewCustomA_nwProject.nwx b/tests/reference/coreProject_NewCustomA_nwProject.nwx index 48ae6363..c61b7f3f 100644 --- a/tests/reference/coreProject_NewCustomA_nwProject.nwx +++ b/tests/reference/coreProject_NewCustomA_nwProject.nwx @@ -1,5 +1,5 @@ - + Test Custom Test Novel @@ -48,55 +48,55 @@ Novel - + Title Page - + Chapter 1 - + Scene 1.1 - + Scene 1.2 - + Scene 1.3 - + Chapter 2 - + Scene 2.1 - + Scene 2.2 - + Scene 2.3 - + Chapter 3 - + Scene 3.1 - + Scene 3.2 - + Scene 3.3 @@ -104,7 +104,7 @@ Plot - + Main Plot @@ -112,7 +112,7 @@ Characters - + Protagonist @@ -120,7 +120,7 @@ Locations - + Main Location diff --git a/tests/reference/coreProject_NewCustomB_nwProject.nwx b/tests/reference/coreProject_NewCustomB_nwProject.nwx index 9399dd3f..9bc1bbf0 100644 --- a/tests/reference/coreProject_NewCustomB_nwProject.nwx +++ b/tests/reference/coreProject_NewCustomB_nwProject.nwx @@ -1,5 +1,5 @@ - + Test Custom Test Novel @@ -48,31 +48,31 @@ Novel - + Title Page - + Scene 1 - + Scene 2 - + Scene 3 - + Scene 4 - + Scene 5 - + Scene 6 @@ -80,7 +80,7 @@ Plot - + Main Plot @@ -88,7 +88,7 @@ Characters - + Protagonist @@ -96,7 +96,7 @@ Locations - + Main Location diff --git a/tests/reference/coreProject_NewFileFolder_nwProject.nwx b/tests/reference/coreProject_NewFileFolder_nwProject.nwx index c0ec400a..94655075 100644 --- a/tests/reference/coreProject_NewFileFolder_nwProject.nwx +++ b/tests/reference/coreProject_NewFileFolder_nwProject.nwx @@ -1,5 +1,5 @@ - + New Project New Novel @@ -17,8 +17,8 @@ None None None - 4 - 1 + 13 + 10 3 @@ -59,7 +59,7 @@ World - + Title Page @@ -67,11 +67,11 @@ New Chapter - + New Chapter - + New Scene @@ -79,11 +79,11 @@ Stuff - + Hello - + Jane diff --git a/tests/reference/coreProject_NewMinimal_nwProject.nwx b/tests/reference/coreProject_NewMinimal_nwProject.nwx index 633f9f4c..08a5d568 100644 --- a/tests/reference/coreProject_NewMinimal_nwProject.nwx +++ b/tests/reference/coreProject_NewMinimal_nwProject.nwx @@ -1,5 +1,5 @@ - + New Project @@ -46,15 +46,15 @@ Novel - + Title Page - + New Chapter - + New Scene diff --git a/tests/reference/coreProject_NewRoot_nwProject.nwx b/tests/reference/coreProject_NewRoot_nwProject.nwx index 3e372adb..24be3ad5 100644 --- a/tests/reference/coreProject_NewRoot_nwProject.nwx +++ b/tests/reference/coreProject_NewRoot_nwProject.nwx @@ -1,5 +1,5 @@ - + New Project New Novel @@ -17,8 +17,8 @@ None None None - 0 - 0 + 9 + 9 0 @@ -59,7 +59,7 @@ World - + Title Page @@ -67,11 +67,11 @@ New Chapter - + New Chapter - + New Scene diff --git a/tests/reference/guiEditor_Main_Final_nwProject.nwx b/tests/reference/guiEditor_Main_Final_nwProject.nwx index 84e7e999..1f8718b2 100644 --- a/tests/reference/guiEditor_Main_Final_nwProject.nwx +++ b/tests/reference/guiEditor_Main_Final_nwProject.nwx @@ -1,12 +1,12 @@ - + New Project New Novel Jane Doe 4 2 - 5 + 4 True @@ -47,7 +47,7 @@ Novel - + Title Page @@ -55,11 +55,11 @@ New Chapter - + New Chapter - + New Scene @@ -67,7 +67,7 @@ Plot - + New Note @@ -75,7 +75,7 @@ Characters - + New Note @@ -83,7 +83,7 @@ World - + New Note diff --git a/tests/reference/guiEditor_Main_Initial_nwProject.nwx b/tests/reference/guiEditor_Main_Initial_nwProject.nwx index 0563c440..2ee65716 100644 --- a/tests/reference/guiEditor_Main_Initial_nwProject.nwx +++ b/tests/reference/guiEditor_Main_Initial_nwProject.nwx @@ -1,5 +1,5 @@ - + New Project New Novel @@ -47,7 +47,7 @@ Novel - + Title Page @@ -55,11 +55,11 @@ New Chapter - + New Chapter - + New Scene diff --git a/tests/reference/guiProjSettings_Dialog_nwProject.nwx b/tests/reference/guiProjSettings_Dialog_nwProject.nwx index 1db9d48c..cff9285d 100644 --- a/tests/reference/guiProjSettings_Dialog_nwProject.nwx +++ b/tests/reference/guiProjSettings_Dialog_nwProject.nwx @@ -1,5 +1,5 @@ - + Project Name Project Title @@ -52,7 +52,7 @@ Novel - + Title Page @@ -60,11 +60,11 @@ New Chapter - + New Chapter - + New Scene diff --git a/tests/test_core/test_core_index.py b/tests/test_core/test_core_index.py index e2eeaa93..0d3518c6 100644 --- a/tests/test_core/test_core_index.py +++ b/tests/test_core/test_core_index.py @@ -26,7 +26,7 @@ import pytest from shutil import copyfile from mock import causeException -from tools import buildTestProject, cmpFiles, writeFile +from tools import C, buildTestProject, cmpFiles, writeFile from novelwriter.enum import nwItemClass, nwItemLayout from novelwriter.core.index import NWIndex, countWords, TagsIndex @@ -189,15 +189,17 @@ def testCoreIndex_CheckThese(mockGUI, fncDir, mockRnd): """Test the tag checker function checkThese. """ theProject = NWProject(mockGUI) + mockRnd.reset() buildTestProject(theProject, fncDir) theIndex = theProject.index + theIndex.clearIndex() - nHandle = theProject.newFile("Hello", "0000000000010") - cHandle = theProject.newFile("Jane", "0000000000012") + nHandle = theProject.newFile("Hello", C.hNovelRoot) + cHandle = theProject.newFile("Jane", C.hCharRoot) nItem = theProject.tree[nHandle] cItem = theProject.tree[cHandle] - assert theIndex.rootChangedSince("0000000000010", 0) is False + assert theIndex.rootChangedSince(C.hNovelRoot, 0) is False assert theIndex.indexChangedSince(0) is False assert theIndex.scanText(cHandle, ( @@ -227,11 +229,11 @@ def testCoreIndex_CheckThese(mockGUI, fncDir, mockRnd): "@time": [] } - assert theIndex.rootChangedSince("0000000000010", 0) is True + assert theIndex.rootChangedSince(C.hNovelRoot, 0) is True assert theIndex.indexChangedSince(0) is True - assert theIndex.getHandleHeaderLevel(cHandle) == "H1" - assert theIndex.getHandleHeaderLevel(nHandle) == "H1" + assert cItem.mainHeading == "H1" + assert nItem.mainHeading == "H1" # Zero Items assert theIndex.checkThese([], cItem) == [] @@ -265,12 +267,13 @@ def testCoreIndex_ScanText(mockGUI, fncDir, mockRnd): """Check the index text scanner. """ theProject = NWProject(mockGUI) + mockRnd.reset() buildTestProject(theProject, fncDir) theIndex = theProject.index # Some items for fail to scan tests - dHandle = theProject.newFolder("Folder", "0000000000010") - xHandle = theProject.newFile("No Layout", "0000000000010") + dHandle = theProject.newFolder("Folder", C.hNovelRoot) + xHandle = theProject.newFile("No Layout", C.hNovelRoot) xItem = theProject.tree[xHandle] xItem.setLayout(nwItemLayout.NO_LAYOUT) @@ -290,21 +293,23 @@ def testCoreIndex_ScanText(mockGUI, fncDir, mockRnd): theProject.tree.updateItemData(xItem.itemHandle) assert xItem.itemRoot == tHandle assert xItem.itemClass == nwItemClass.TRASH - assert theIndex.scanText(xHandle, "Hello World!") is False + assert theIndex.scanText(xHandle, "## Hello World!") is True + assert xItem.mainHeading == "H2" # Create the archive root aHandle = theProject.newRoot(nwItemClass.ARCHIVE) assert theProject.tree[aHandle] is not None xItem.setParent(aHandle) theProject.tree.updateItemData(xItem.itemHandle) - assert theIndex.scanText(xHandle, "Hello World!") is False + assert theIndex.scanText(xHandle, "### Hello World!") is True + assert xItem.mainHeading == "H3" # Make some usable items - tHandle = theProject.newFile("Title", "0000000000010") - pHandle = theProject.newFile("Page", "0000000000010") - nHandle = theProject.newFile("Hello", "0000000000010") - cHandle = theProject.newFile("Jane", "0000000000012") - sHandle = theProject.newFile("Scene", "0000000000010") + tHandle = theProject.newFile("Title", C.hNovelRoot) + pHandle = theProject.newFile("Page", C.hNovelRoot) + nHandle = theProject.newFile("Hello", C.hNovelRoot) + cHandle = theProject.newFile("Jane", C.hCharRoot) + sHandle = theProject.newFile("Scene", C.hNovelRoot) # Text Indexing # ============= @@ -474,23 +479,24 @@ def testCoreIndex_ExtractData(mockGUI, fncDir, mockRnd): """Check the index data extraction functions. """ theProject = NWProject(mockGUI) + mockRnd.reset() buildTestProject(theProject, fncDir) theIndex = theProject.index - theIndex.reIndexHandle("0000000000010") - theIndex.reIndexHandle("0000000000011") - theIndex.reIndexHandle("0000000000012") - theIndex.reIndexHandle("0000000000013") - theIndex.reIndexHandle("0000000000014") - theIndex.reIndexHandle("0000000000015") - theIndex.reIndexHandle("0000000000016") - theIndex.reIndexHandle("0000000000017") + theIndex.reIndexHandle(C.hNovelRoot) + theIndex.reIndexHandle(C.hPlotRoot) + theIndex.reIndexHandle(C.hCharRoot) + theIndex.reIndexHandle(C.hWorldRoot) + theIndex.reIndexHandle(C.hTitlePage) + theIndex.reIndexHandle(C.hChapterDir) + theIndex.reIndexHandle(C.hChapterDoc) + theIndex.reIndexHandle(C.hSceneDoc) - nHandle = theProject.newFile("Hello", "0000000000010") - cHandle = theProject.newFile("Jane", "0000000000012") + nHandle = theProject.newFile("Hello", C.hNovelRoot) + cHandle = theProject.newFile("Jane", C.hCharRoot) assert theIndex.getNovelData("", "") is None - assert theIndex.getNovelData("0000000000010", "") is None + assert theIndex.getNovelData(C.hNovelRoot, "") is None assert theIndex.scanText(cHandle, ( "# Jane Smith\n" @@ -511,10 +517,10 @@ def testCoreIndex_ExtractData(mockGUI, fncDir, mockRnd): theKeys.append(aKey) assert theKeys == [ - "0000000000014:T000001", - "0000000000016:T000001", - "0000000000017:T000001", - "%s:T000001" % nHandle, + f"{C.hTitlePage}:T000001", + f"{C.hChapterDoc}:T000001", + f"{C.hSceneDoc}:T000001", + f"{nHandle}:T000001", ] # Check that excluded files can be skipped @@ -525,10 +531,10 @@ def testCoreIndex_ExtractData(mockGUI, fncDir, mockRnd): theKeys.append(aKey) assert theKeys == [ - "0000000000014:T000001", - "0000000000016:T000001", - "0000000000017:T000001", - "%s:T000001" % nHandle, + f"{C.hTitlePage}:T000001", + f"{C.hChapterDoc}:T000001", + f"{C.hSceneDoc}:T000001", + f"{nHandle}:T000001", ] theKeys = [] @@ -536,9 +542,9 @@ def testCoreIndex_ExtractData(mockGUI, fncDir, mockRnd): theKeys.append(aKey) assert theKeys == [ - "0000000000014:T000001", - "0000000000016:T000001", - "0000000000017:T000001", + f"{C.hTitlePage}:T000001", + f"{C.hChapterDoc}:T000001", + f"{C.hSceneDoc}:T000001", ] # The novel file should have the correct counts @@ -567,7 +573,7 @@ def testCoreIndex_ExtractData(mockGUI, fncDir, mockRnd): assert theIndex.getBackReferenceList(None) == {} # The Title Page file should have no references as it has no tag - assert theIndex.getBackReferenceList("0000000000014") == {} + assert theIndex.getBackReferenceList(C.hTitlePage) == {} # The character file should have a record of the reference from the novel file theRefs = theIndex.getBackReferenceList(cHandle) @@ -656,9 +662,9 @@ def testCoreIndex_ExtractData(mockGUI, fncDir, mockRnd): # Novel Stats # =========== - hHandle = theProject.newFile("Chapter", "0000000000010") - sHandle = theProject.newFile("Scene One", "0000000000010") - tHandle = theProject.newFile("Scene Two", "0000000000010") + hHandle = theProject.newFile("Chapter", C.hNovelRoot) + sHandle = theProject.newFile("Scene One", C.hNovelRoot) + tHandle = theProject.newFile("Scene Two", C.hNovelRoot) theProject.tree[hHandle].itemLayout == nwItemLayout.DOCUMENT theProject.tree[sHandle].itemLayout == nwItemLayout.DOCUMENT @@ -669,9 +675,9 @@ def testCoreIndex_ExtractData(mockGUI, fncDir, mockRnd): assert theIndex.scanText(tHandle, "### Scene Two\n\n") assert [(h, t) for h, t, _ in theIndex._itemIndex.iterNovelStructure(skipExcl=False)] == [ - ("0000000000014", "T000001"), - ("0000000000016", "T000001"), - ("0000000000017", "T000001"), + (C.hTitlePage, "T000001"), + (C.hChapterDoc, "T000001"), + (C.hSceneDoc, "T000001"), (nHandle, "T000001"), (nHandle, "T000011"), (hHandle, "T000001"), @@ -680,9 +686,9 @@ def testCoreIndex_ExtractData(mockGUI, fncDir, mockRnd): ] assert [(h, t) for h, t, _ in theIndex._itemIndex.iterNovelStructure(skipExcl=True)] == [ - ("0000000000014", "T000001"), - ("0000000000016", "T000001"), - ("0000000000017", "T000001"), + (C.hTitlePage, "T000001"), + (C.hChapterDoc, "T000001"), + (C.hSceneDoc, "T000001"), (hHandle, "T000001"), (sHandle, "T000001"), (tHandle, "T000001"), @@ -691,9 +697,9 @@ def testCoreIndex_ExtractData(mockGUI, fncDir, mockRnd): # Add a fake handle to the tree and check that it's ignored theProject.tree._treeOrder.append("0000000000000") assert [(h, t) for h, t, _ in theIndex._itemIndex.iterNovelStructure(skipExcl=False)] == [ - ("0000000000014", "T000001"), - ("0000000000016", "T000001"), - ("0000000000017", "T000001"), + (C.hTitlePage, "T000001"), + (C.hChapterDoc, "T000001"), + (C.hSceneDoc, "T000001"), (nHandle, "T000001"), (nHandle, "T000011"), (hHandle, "T000001"), @@ -709,29 +715,29 @@ def testCoreIndex_ExtractData(mockGUI, fncDir, mockRnd): assert theIndex.getNovelTitleCounts(skipExcl=True) == [0, 1, 2, 3, 0] # Table of Contents - assert theIndex.getTableOfContents("0000000000010", 0, skipExcl=True) == [] - assert theIndex.getTableOfContents("0000000000010", 1, skipExcl=True) == [ - ("0000000000014:T000001", 1, "New Novel", 15), + assert theIndex.getTableOfContents(C.hNovelRoot, 0, skipExcl=True) == [] + assert theIndex.getTableOfContents(C.hNovelRoot, 1, skipExcl=True) == [ + (f"{C.hTitlePage}:T000001", 1, "New Novel", 15), ] - assert theIndex.getTableOfContents("0000000000010", 2, skipExcl=True) == [ - ("0000000000014:T000001", 1, "New Novel", 5), - ("0000000000016:T000001", 2, "New Chapter", 4), - ("%s:T000001" % hHandle, 2, "Chapter One", 6), + assert theIndex.getTableOfContents(C.hNovelRoot, 2, skipExcl=True) == [ + (f"{C.hTitlePage}:T000001", 1, "New Novel", 5), + (f"{C.hChapterDoc}:T000001", 2, "New Chapter", 4), + (f"{hHandle}:T000001", 2, "Chapter One", 6), ] - assert theIndex.getTableOfContents("0000000000010", 3, skipExcl=True) == [ - ("0000000000014:T000001", 1, "New Novel", 5), - ("0000000000016:T000001", 2, "New Chapter", 2), - ("0000000000017:T000001", 3, "New Scene", 2), - ("%s:T000001" % hHandle, 2, "Chapter One", 2), - ("%s:T000001" % sHandle, 3, "Scene One", 2), - ("%s:T000001" % tHandle, 3, "Scene Two", 2), + assert theIndex.getTableOfContents(C.hNovelRoot, 3, skipExcl=True) == [ + (f"{C.hTitlePage}:T000001", 1, "New Novel", 5), + (f"{C.hChapterDoc}:T000001", 2, "New Chapter", 2), + (f"{C.hSceneDoc}:T000001", 3, "New Scene", 2), + (f"{hHandle}:T000001", 2, "Chapter One", 2), + (f"{sHandle}:T000001", 3, "Scene One", 2), + (f"{tHandle}:T000001", 3, "Scene Two", 2), ] - assert theIndex.getTableOfContents("0000000000010", 0, skipExcl=False) == [] - assert theIndex.getTableOfContents("0000000000010", 1, skipExcl=False) == [ - ("0000000000014:T000001", 1, "New Novel", 9), - ("%s:T000001" % nHandle, 1, "Hello World!", 12), - ("%s:T000011" % nHandle, 1, "Hello World!", 22), + assert theIndex.getTableOfContents(C.hNovelRoot, 0, skipExcl=False) == [] + assert theIndex.getTableOfContents(C.hNovelRoot, 1, skipExcl=False) == [ + (f"{C.hTitlePage}:T000001", 1, "New Novel", 9), + (f"{nHandle}:T000001", 1, "Hello World!", 12), + (f"{nHandle}:T000011", 1, "Hello World!", 22), ] # Header Word Counts @@ -741,7 +747,7 @@ def testCoreIndex_ExtractData(mockGUI, fncDir, mockRnd): assert theIndex.getHandleWordCounts(sHandle) == [("%s:T000001" % sHandle, 2)] assert theIndex.getHandleWordCounts(tHandle) == [("%s:T000001" % tHandle, 2)] assert theIndex.getHandleWordCounts(nHandle) == [ - ("%s:T000001" % nHandle, 12), ("%s:T000011" % nHandle, 16) + (f"{nHandle}:T000001", 12), (f"{nHandle}:T000011", 16) ] assert theIndex.saveIndex() is True @@ -926,11 +932,13 @@ def testCoreIndex_ItemIndex(mockGUI, fncDir, mockRnd): """Check the ItemIndex class. """ theProject = NWProject(mockGUI) + mockRnd.reset() buildTestProject(theProject, fncDir) + theProject.index.clearIndex() - nHandle = "0000000000014" - cHandle = "0000000000016" - sHandle = "0000000000017" + nHandle = C.hTitlePage + cHandle = C.hChapterDoc + sHandle = C.hSceneDoc assert theProject.index.saveIndex() is True itemIndex = theProject.index._itemIndex @@ -948,13 +956,11 @@ def testCoreIndex_ItemIndex(mockGUI, fncDir, mockRnd): itemIndex.add(cHandle, theProject.tree[cHandle]) assert cHandle in itemIndex assert itemIndex[cHandle].item == theProject.tree[cHandle] - assert itemIndex.mainItemHeader(cHandle) == "H0" assert itemIndex.allItemTags(cHandle) == [] assert list(itemIndex.iterItemHeaders(cHandle))[0][0] == "T000000" # Add a heading to the item, which should replace the T000000 heading itemIndex.addItemHeading(cHandle, "T000001", "H2", "Chapter One") - assert itemIndex.mainItemHeader(cHandle) == "H2" assert list(itemIndex.iterItemHeaders(cHandle))[0][0] == "T000001" # Set the remainig data values @@ -966,7 +972,6 @@ def testCoreIndex_ItemIndex(mockGUI, fncDir, mockRnd): itemIndex.addHeadingReferences(cHandle, "T000001", ["Jane", "John"], "@char") idxData = itemIndex.packData() - assert idxData[cHandle]["level"] == "H2" assert idxData[cHandle]["headings"]["T000001"] == { "level": "H2", "title": "Chapter One", "tag": "One", "cCount": 60, "wCount": 10, "pCount": 2, "synopsis": "In the beginning ...", @@ -1026,7 +1031,6 @@ def testCoreIndex_ItemIndex(mockGUI, fncDir, mockRnd): assert allHeads[2][1] == "T000001" # Ask for stuff that doesn't exist - assert itemIndex.mainItemHeader("blablabla") == "H0" assert itemIndex.allItemTags("blablabla") == [] # Novel Structure @@ -1048,7 +1052,7 @@ def testCoreIndex_ItemIndex(mockGUI, fncDir, mockRnd): assert nStruct[3][0] == uHandle # Novel structure with root handle set - nStruct = list(itemIndex.iterNovelStructure(rootHandle="0000000000010")) + nStruct = list(itemIndex.iterNovelStructure(rootHandle=C.hNovelRoot)) assert len(nStruct) == 3 assert nStruct[0][0] == nHandle assert nStruct[1][0] == cHandle @@ -1098,7 +1102,7 @@ def testCoreIndex_ItemIndex(mockGUI, fncDir, mockRnd): itemIndex.unpackData({"stuff": "more stuff"}) # Unknown keys should be skipped - itemIndex.unpackData({"0000000000000": {}}) + itemIndex.unpackData({C.hInvalid: {}}) assert itemIndex._items == {} # Known keys can be added, even witout data diff --git a/tests/test_core/test_core_item.py b/tests/test_core/test_core_item.py index c0b92591..bd166b47 100644 --- a/tests/test_core/test_core_item.py +++ b/tests/test_core/test_core_item.py @@ -213,13 +213,34 @@ def testCoreItem_Methods(mockGUI): theItem.setLayout("DOCUMENT") assert theItem.isFileType() is True assert theItem.isDocumentLayout() is True + + theItem.setMainHeading("HH") + assert theItem.mainHeading == "H0" assert theItem.describeMe() == "Novel Document" - assert theItem.describeMe("H0") == "Novel Document" - assert theItem.describeMe("H1") == "Novel Title Page" - assert theItem.describeMe("H2") == "Novel Chapter" - assert theItem.describeMe("H3") == "Novel Scene" - assert theItem.describeMe("H4") == "Novel Section" - assert theItem.describeMe("H5") == "Novel Document" + + theItem.setMainHeading("H0") + assert theItem.mainHeading == "H0" + assert theItem.describeMe() == "Novel Document" + + theItem.setMainHeading("H1") + assert theItem.mainHeading == "H1" + assert theItem.describeMe() == "Novel Title Page" + + theItem.setMainHeading("H2") + assert theItem.mainHeading == "H2" + assert theItem.describeMe() == "Novel Chapter" + + theItem.setMainHeading("H3") + assert theItem.mainHeading == "H3" + assert theItem.describeMe() == "Novel Scene" + + theItem.setMainHeading("H4") + assert theItem.mainHeading == "H4" + assert theItem.describeMe() == "Novel Section" + + theItem.setMainHeading("H5") + assert theItem.mainHeading == "H4" + assert theItem.describeMe() == "Novel Section" theItem.setLayout("NOTE") assert theItem.isNoteLayout() is True @@ -504,9 +525,9 @@ def testCoreItem_XMLPackUnpack(mockGUI, caplog, mockRnd): assert etree.tostring(xContent, pretty_print=False, encoding="utf-8") == ( b'' b'A Name' + b'type="FILE" class="NOVEL" layout="NOTE">A Name' b'' ) % bytes(importKeys[3], encoding="utf8") diff --git a/tests/test_core/test_core_tree.py b/tests/test_core/test_core_tree.py index cfdefe70..c3c20d34 100644 --- a/tests/test_core/test_core_tree.py +++ b/tests/test_core/test_core_tree.py @@ -420,13 +420,13 @@ def testCoreTree_XMLPackUnpack(mockGUI, mockItems): b'type="FOLDER" class="NOVEL">Act One' b'Chapter One' + b'type="FILE" class="NOVEL" layout="DOCUMENT">Chapter One' b'Scene One' + b'type="FILE" class="NOVEL" layout="DOCUMENT">Scene One' b'Outtakes' @@ -437,9 +437,9 @@ def testCoreTree_XMLPackUnpack(mockGUI, mockItems): b'class="CHARACTER">Characters' b'Jane Doe' + b'type="FILE" class="CHARACTER" layout="NOTE">Jane Doe' b'' b'' ) diff --git a/tests/tools.py b/tests/tools.py index 5c126707..4744d472 100644 --- a/tests/tools.py +++ b/tests/tools.py @@ -173,13 +173,16 @@ def buildTestProject(theObject, projPath): xHandle[8] = theProject.newFile("New Scene", xHandle[6]) aDoc = NWDoc(theProject, xHandle[5]) - aDoc.writeDocument("#! New Novel\n\n>> By Jane DOe <<\n") + aDoc.writeDocument("#! New Novel\n\n>> By Jane Doe <<\n") + theProject.index.reIndexHandle(xHandle[5]) aDoc = NWDoc(theProject, xHandle[7]) aDoc.writeDocument("## %s\n\n" % theProject.tr("New Chapter")) + theProject.index.reIndexHandle(xHandle[7]) aDoc = NWDoc(theProject, xHandle[8]) aDoc.writeDocument("### %s\n\n" % theProject.tr("New Scene")) + theProject.index.reIndexHandle(xHandle[8]) theProject.projOpened = time.time() theProject.setProjectChanged(True) @@ -188,6 +191,5 @@ def buildTestProject(theObject, projPath): if theGUI is not None: theGUI.hasProject = True theGUI.rebuildTrees() - theGUI.rebuildIndex(beQuiet=True) return From 5f4a171dffa5446068a3cbdefe4d61930405168e Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Mon, 17 Oct 2022 21:18:42 +0200 Subject: [PATCH 5/6] Update lipsum test project --- tests/lipsum/nwProject.nwx | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/lipsum/nwProject.nwx b/tests/lipsum/nwProject.nwx index 4eac7bc6..ea5519c4 100644 --- a/tests/lipsum/nwProject.nwx +++ b/tests/lipsum/nwProject.nwx @@ -1,12 +1,12 @@ - + Lorem Ipsum Lorem Ipsum lipsum.com - 26 + 28 24 - 1863 + 1874 False @@ -50,19 +50,19 @@ Novel - + Lorem Ipsum - + Front Matter - + Prologue - + Act One @@ -70,19 +70,19 @@ Chapter One - + Chapter One - + Scene One - + Scene Two - + Interlude @@ -90,19 +90,19 @@ Chapter Two - + Chapter Two - + Scene Three - + Scene Four - + Scene Five @@ -110,7 +110,7 @@ Characters - + Mr. Nobody @@ -118,7 +118,7 @@ Plot - + Main @@ -126,7 +126,7 @@ World - + Ancient Europe From 6c06845ed5cdd1b22a266e402d7e0bdecbd7a011 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Mon, 17 Oct 2022 21:31:57 +0200 Subject: [PATCH 6/6] Drop the error on level tag in index file --- novelwriter/core/index.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/novelwriter/core/index.py b/novelwriter/core/index.py index 1332413c..24e38097 100644 --- a/novelwriter/core/index.py +++ b/novelwriter/core/index.py @@ -1022,10 +1022,6 @@ class IndexItem: def unpackData(self, data): """Unpack an item entry from the data. """ - if "level" in data: - # This value is now tracked as NWItem.mainHeading - raise ValueError("Outdated value found in index") - references = data.get("references", {}) for sTitle, hData in data.get("headings", {}).items(): if not isTitleTag(sTitle):