From 92924b3288e6c9abf404e197d86b3ad34edf458b Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sun, 5 Jun 2022 17:41:29 +0200 Subject: [PATCH] Make the new item function better at guessing header level of new files --- novelwriter/core/index.py | 5 +++++ novelwriter/gui/projtree.py | 11 ++++++++--- tests/test_core/test_core_index.py | 3 +++ tests/test_gui/test_gui_projtree.py | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/novelwriter/core/index.py b/novelwriter/core/index.py index ee9ea089..f86de8c2 100644 --- a/novelwriter/core/index.py +++ b/novelwriter/core/index.py @@ -502,6 +502,11 @@ class NWIndex: """ return self._itemIndex.mainItemHeader(tHandle) + def getHandleHeaderIntLevel(self, tHandle): + """Get the integer header level of the first header of a handle. + """ + return H_LEVEL.get(self._itemIndex.mainItemHeader(tHandle), 0) + def getTableOfContents(self, maxDepth, skipExcl=True): """Generate a table of contents up to a maximum depth. """ diff --git a/novelwriter/gui/projtree.py b/novelwriter/gui/projtree.py index b4f99421..57981904 100644 --- a/novelwriter/gui/projtree.py +++ b/novelwriter/gui/projtree.py @@ -38,6 +38,7 @@ from PyQt5.QtWidgets import ( from novelwriter.core import NWDoc from novelwriter.enum import nwItemType, nwItemClass, nwItemLayout, nwAlert +from novelwriter.common import minmax from novelwriter.dialogs.itemeditor import GuiItemEditor logger = logging.getLogger(__name__) @@ -188,9 +189,11 @@ class GuiProjectTree(QTreeWidget): ), nwAlert.ERROR) return False - # If the selected item is a file, the new item will be a sibling + # If the selected item is a file, the new item will be a + # sibling if the file has no children, otherwise a child pItem = self.theProject.tree[sHandle] - if pItem.itemType == nwItemType.FILE: + qItem = self._getTreeItem(sHandle) + if pItem.itemType == nwItemType.FILE and qItem.childCount() == 0: nHandle = sHandle sHandle = pItem.itemParent if sHandle is None: @@ -233,7 +236,9 @@ class GuiProjectTree(QTreeWidget): newDoc = NWDoc(self.theProject, tHandle) if not newDoc.readDocument(): if nwItem.itemLayout == nwItemLayout.DOCUMENT: - newText = f"### {nwItem.itemName}\n\n" + iLvl = self.theProject.index.getHandleHeaderIntLevel(sHandle) + hLvl = "#"*minmax(iLvl + 1, 2, 4) + newText = f"{hLvl} {nwItem.itemName}\n\n" else: newText = f"# {nwItem.itemName}\n\n" diff --git a/tests/test_core/test_core_index.py b/tests/test_core/test_core_index.py index 8f126e56..3d40a9df 100644 --- a/tests/test_core/test_core_index.py +++ b/tests/test_core/test_core_index.py @@ -232,6 +232,9 @@ def testCoreIndex_CheckThese(mockGUI, fncDir, mockRnd): assert theIndex.getHandleHeaderLevel(cHandle) == "H1" assert theIndex.getHandleHeaderLevel(nHandle) == "H1" + assert theIndex.getHandleHeaderIntLevel(cHandle) == 1 + assert theIndex.getHandleHeaderIntLevel(nHandle) == 1 + assert theIndex.getHandleHeaderIntLevel("stuff") == 0 # Zero Items assert theIndex.checkThese([], cItem) == [] diff --git a/tests/test_gui/test_gui_projtree.py b/tests/test_gui/test_gui_projtree.py index 96e04da4..258ad150 100644 --- a/tests/test_gui/test_gui_projtree.py +++ b/tests/test_gui/test_gui_projtree.py @@ -96,7 +96,7 @@ def testGuiProjTree_NewItems(qtbot, caplog, monkeypatch, nwGUI, fncDir, mockRnd) assert nwGUI.theProject.tree["0000000000013"].itemRoot == "0000000000008" assert nwGUI.theProject.tree["0000000000013"].itemClass == nwItemClass.NOVEL assert nwGUI.openDocument("0000000000013") - assert nwGUI.docEditor.getText() == "### New Document\n\n" + assert nwGUI.docEditor.getText() == "## New Document\n\n" # Add a new file to the characters folder nwTree.setSelectedHandle("000000000000a")