From 3fc17c543984402867298508a26a76e5804285e8 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Fri, 12 Jun 2020 17:08:18 +0200 Subject: [PATCH] Save more meta data to the top of files --- nw/core/document.py | 34 +++++++++++++++++++++++++++++++--- nw/core/project.py | 6 +++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/nw/core/document.py b/nw/core/document.py index 52f1517b..b9d3de83 100644 --- a/nw/core/document.py +++ b/nw/core/document.py @@ -30,8 +30,10 @@ import nw from os import path, mkdir, rename, unlink +from nw.core.item import NWItem from nw.constants import nwAlert from nw.common import isHandle +from nw.constants import nwItemLayout, nwItemClass logger = logging.getLogger(__name__) @@ -139,8 +141,18 @@ class NWDoc(): docPath = path.join(self.theProject.projContent, docFile) docTemp = path.join(self.theProject.projContent, docFile+"~") - itemPath = self.theProject.projTree.getItemPath(self.docHandle) - docMeta = "%%~ "+":".join(itemPath)+":"+self.theItem.itemName+"\n" + if isinstance(self.theItem, NWItem): + itemPath = self.theProject.projTree.getItemPath(self.docHandle) + docMeta = ( + "%%~ {handlepath:s}:{itemclass:s}:{itemlayout:s}:{itemname:s}\n" + ).format( + handlepath = ":".join(itemPath), + itemclass = self.theItem.itemClass.name, + itemlayout = self.theItem.itemLayout.name, + itemname = self.theItem.itemName, + ) + else: + docMeta = "" try: with open(docTemp, mode="w", encoding="utf8") as outFile: @@ -213,6 +225,22 @@ class NWDoc(): else: break - return theMeta, thePath + theClass = nwItemClass.NO_CLASS + theLayout = nwItemLayout.NO_LAYOUT + theData = theMeta.split(":") + + if (n := len(theData)) > 1: + if theData[0] in nwItemClass.__members__: + theClass = nwItemClass[theData[0]] + if n > 2: + if theData[1] in nwItemLayout.__members__: + theLayout = nwItemLayout[theData[1]] + theName = theData[2] + else: + theName = theData[1] + else: + theName = theData[0] + + return theName, thePath, theClass, theLayout # END Class NWDoc diff --git a/nw/core/project.py b/nw/core/project.py index 0784fc7a..53286656 100644 --- a/nw/core/project.py +++ b/nw/core/project.py @@ -1075,7 +1075,7 @@ class NWProject(): # Look for meta data oName = "" if aDoc.openDocument(oHandle, showStatus=False, isOrphan=True): - oName, oPath = aDoc.getMeta() + oName, oPath, oClass, oLayout = aDoc.getMeta() if oName == "": nOrph += 1 @@ -1084,8 +1084,8 @@ class NWProject(): orphItem = NWItem(self) orphItem.setName(oName) orphItem.setType(nwItemType.FILE) - orphItem.setClass(nwItemClass.NO_CLASS) - orphItem.setLayout(nwItemLayout.NO_LAYOUT) + orphItem.setClass(oClass) + orphItem.setLayout(oLayout) self.projTree.append(oHandle, None, orphItem) return