From 20709145c801d8453d568ee773ef7553b73e405e Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Thu, 17 Jul 2025 15:31:03 +0200 Subject: [PATCH 1/4] Update template title when creating file from template --- novelwriter/core/project.py | 10 +++++++++- novelwriter/gui/projtree.py | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/novelwriter/core/project.py b/novelwriter/core/project.py index e86b744b..3e3152ee 100644 --- a/novelwriter/core/project.py +++ b/novelwriter/core/project.py @@ -224,7 +224,7 @@ class NWProject: return True - def copyFileContent(self, tHandle: str, sHandle: str) -> bool: + def copyFileContent(self, tHandle: str, sHandle: str, newTitle: str | None = None) -> bool: """Copy content to a new document after it is created. This will not run if the file exists and is not empty. """ @@ -239,6 +239,14 @@ class NWProject: logger.debug("Populating '%s' with text from '%s'", tHandle, sHandle) text = self._storage.getDocumentText(sHandle) + if ( + newTitle and (lines := text.split("\n")) and lines + and lines[0].startswith(("# ", "## ", "### ", "#### ", "#! ", "##! ", "###! ")) + ): + prefix, _, _ = lines[0].partition(" ") + lines[0] = f"{prefix} {newTitle}" + text = "\n".join(lines) + self._storage.getDocument(tHandle).writeDocument(text) sItem.setLayout(tItem.itemLayout) self._index.reIndexHandle(tHandle) diff --git a/novelwriter/gui/projtree.py b/novelwriter/gui/projtree.py index 4565e315..b35445ae 100644 --- a/novelwriter/gui/projtree.py +++ b/novelwriter/gui/projtree.py @@ -667,7 +667,7 @@ class GuiProjectTree(QTreeView): if itemType == nwItemType.FILE: if tHandle := SHARED.project.newFile(newLabel, sHandle, sPos): if copyDoc: - SHARED.project.copyFileContent(tHandle, copyDoc) + SHARED.project.copyFileContent(tHandle, copyDoc, newLabel) elif hLevel > 0: SHARED.project.writeNewFile(tHandle, hLevel, not nNote) SHARED.project.index.reIndexHandle(tHandle) From c816b24437898e41ec087635f3ea657b49e18c82 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Thu, 17 Jul 2025 15:31:13 +0200 Subject: [PATCH 2/4] Update tests --- sample/content/5aec885635c85.nwd | 4 ++-- .../reference/coreProject_NewFileFolder_nwProject.nwx | 10 +++++++--- tests/test_core/test_core_project.py | 7 +++++++ tests/test_gui/test_gui_projtree.py | 2 +- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/sample/content/5aec885635c85.nwd b/sample/content/5aec885635c85.nwd index 376e4f9a..e024df62 100644 --- a/sample/content/5aec885635c85.nwd +++ b/sample/content/5aec885635c85.nwd @@ -1,7 +1,7 @@ %%~name: Scene -%%~path: 09f22ae8369f7/5aec885635c85 +%%~path: f4ed1ae756a1f/5aec885635c85 %%~kind: TEMPLATE/DOCUMENT -%%~hash: 05c9750b00020f8cf9a7bc7362bf19681022cc03 +%%~hash: 537fb0132a80101060b40b42ad8875ebbcb40a5b %%~date: 2024-02-02 10:58:47/2024-02-02 10:59:32 ### Scene diff --git a/tests/reference/coreProject_NewFileFolder_nwProject.nwx b/tests/reference/coreProject_NewFileFolder_nwProject.nwx index d3eec6c6..af93504f 100644 --- a/tests/reference/coreProject_NewFileFolder_nwProject.nwx +++ b/tests/reference/coreProject_NewFileFolder_nwProject.nwx @@ -1,5 +1,5 @@ - + New Project Jane Doe @@ -22,13 +22,13 @@ Finished - New + New Minor Major Main - + Novel @@ -73,6 +73,10 @@ John + + + Mike + Locations diff --git a/tests/test_core/test_core_project.py b/tests/test_core/test_core_project.py index 7547419f..b72c8132 100644 --- a/tests/test_core/test_core_project.py +++ b/tests/test_core/test_core_project.py @@ -100,6 +100,7 @@ def testCoreProject_NewFileFolder(monkeypatch, fncPath, tstPaths, mockGUI, mockR bHandle = "0000000000011" cHandle = "0000000000012" dHandle = "0000000000013" + eHandle = "0000000000014" xHandle = "1234567890abc" # Invalid call @@ -111,11 +112,13 @@ def testCoreProject_NewFileFolder(monkeypatch, fncPath, tstPaths, mockGUI, mockR assert project.newFile("Hello", aHandle) == bHandle assert project.newFile("Jane", C.hCharRoot) == cHandle assert project.newFile("John", C.hCharRoot) == dHandle + assert project.newFile("Mike", C.hCharRoot) == eHandle assert aHandle in project.tree assert bHandle in project.tree assert cHandle in project.tree assert dHandle in project.tree + assert eHandle in project.tree # Write to file, failed assert project.writeNewFile("blabla", 1, True) is False # Not a handle @@ -151,6 +154,10 @@ def testCoreProject_NewFileFolder(monkeypatch, fncPath, tstPaths, mockGUI, mockR assert dText == cText + # Copy file content, and update title + assert project.copyFileContent(eHandle, cHandle, "Lars") is True + assert project.storage.getDocument(eHandle).readDocument() == "# Lars\n\nHi Jane\n\n" + # Save, close and check assert project.projChanged is True assert project.saveProject() is True diff --git a/tests/test_gui/test_gui_projtree.py b/tests/test_gui/test_gui_projtree.py index ae8e9af0..02216700 100644 --- a/tests/test_gui/test_gui_projtree.py +++ b/tests/test_gui/test_gui_projtree.py @@ -1454,7 +1454,7 @@ def testGuiProjTree_Templates(qtbot, monkeypatch, nwGUI, projPath, mockRnd): nwNewCharacter = project.tree[hNewCharacter] assert nwNewCharacter is not None assert nwNewCharacter.itemName == "Note" - assert project.storage.getDocument(hNewCharacter).readDocument() == "# Jane\n\n@tag: Jane\n\n" + assert project.storage.getDocument(hNewCharacter).readDocument() == "# Note\n\n@tag: Jane\n\n" # Clearing the menu and rebuilding it should work projBar.mTemplates.clearMenu() From ebdd0dbdc0d9bb31a01b61cd274d5f9eb860f96f Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Thu, 17 Jul 2025 15:37:42 +0200 Subject: [PATCH 3/4] Update docs --- docs/source/usage/organising_project.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/source/usage/organising_project.rst b/docs/source/usage/organising_project.rst index 4c8f1cf4..4f4e3c9b 100644 --- a/docs/source/usage/organising_project.rst +++ b/docs/source/usage/organising_project.rst @@ -166,6 +166,9 @@ a character note template, you can add a **Templates** root folder to your proje added to this root folder will show up in the **Add Item** menu in the project tree toolbar. When selected, a new document is created with its content copied from the chosen template. +If the first line of the template file is a title line, the title text will be replaced with the +text of the document label when it is first created. + .. versionadded:: 2.3 From ee370ec8bf7cf503990ffa4483af93949c2d1573 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Thu, 17 Jul 2025 15:44:06 +0200 Subject: [PATCH 4/4] Fix wrong meta data in sample files --- sample/content/5ee8aebcdebc9.nwd | 4 ++-- sample/nwProject.nwx | 30 +++++++++++++++--------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/sample/content/5ee8aebcdebc9.nwd b/sample/content/5ee8aebcdebc9.nwd index d31c4d96..442156da 100644 --- a/sample/content/5ee8aebcdebc9.nwd +++ b/sample/content/5ee8aebcdebc9.nwd @@ -1,7 +1,7 @@ %%~name: Character Note -%%~path: 09f22ae8369f7/5ee8aebcdebc9 +%%~path: f4ed1ae756a1f/5ee8aebcdebc9 %%~kind: TEMPLATE/NOTE -%%~hash: 6230bb6f796e708a72f4c29dc79d63dc90a47ba5 +%%~hash: 0d33e49259d13a6be92b493d2122dcacc677eb0b %%~date: 2024-02-02 10:54:52/2024-02-02 10:56:05 # Character Name diff --git a/sample/nwProject.nwx b/sample/nwProject.nwx index 54a1078e..b01942bb 100644 --- a/sample/nwProject.nwx +++ b/sample/nwProject.nwx @@ -1,6 +1,6 @@ - - + + Sample Project Jane Smith @@ -42,19 +42,19 @@ Novel - + Title Page - + Page - + Part One - + Chapter One @@ -62,19 +62,19 @@ Making a Scene - + Another Scene - + Interlude - + A Note on Structure - + Chapter Two @@ -86,11 +86,11 @@ Sequel - + Title Page - + Chapter One @@ -102,11 +102,11 @@ Main Characters - + John Smith - + Jane Smith @@ -118,7 +118,7 @@ Earth - + Space