Simplify text labels in XML (#1038)

This commit is contained in:
Veronica Berglyd Olsen
2022-04-16 17:03:44 +02:00
committed by GitHub
4 changed files with 74 additions and 66 deletions
+52 -52
View File
@@ -29,7 +29,7 @@ from lxml import etree
from novelwriter.enum import nwItemType, nwItemClass, nwItemLayout from novelwriter.enum import nwItemType, nwItemClass, nwItemLayout
from novelwriter.common import ( from novelwriter.common import (
checkInt, isHandle, isItemClass, isItemLayout, isItemType checkInt, isHandle, isItemClass, isItemLayout, isItemType, simplified
) )
from novelwriter.constants import nwLabels, nwLists, trConst from novelwriter.constants import nwLabels, nwLists, trConst
@@ -301,142 +301,142 @@ class NWItem():
# Set Item Values # Set Item Values
## ##
def setName(self, theName): def setName(self, name):
"""Set the item name. """Set the item name.
""" """
if isinstance(theName, str): if isinstance(name, str):
self._name = theName.strip() self._name = simplified(name)
else: else:
self._name = "" self._name = ""
return return
def setHandle(self, theHandle): def setHandle(self, tHandle):
"""Set the item handle, and ensure it is valid. """Set the item handle, and ensure it is valid.
""" """
if isHandle(theHandle): if isHandle(tHandle):
self._handle = theHandle self._handle = tHandle
else: else:
self._handle = None self._handle = None
return return
def setParent(self, theParent): def setParent(self, pHandle):
"""Set the parent handle, and ensure it is valid. """Set the parent handle, and ensure it is valid.
""" """
if theParent is None: if pHandle is None:
self._parent = None self._parent = None
elif isHandle(theParent): elif isHandle(pHandle):
self._parent = theParent self._parent = pHandle
else: else:
self._parent = None self._parent = None
return return
def setOrder(self, theOrder): def setOrder(self, order):
"""Set the item order, and ensure that it is valid. This value """Set the item order, and ensure that it is valid. This value
is purely a meta value, and not actually used by novelWriter at is purely a meta value, and not actually used by novelWriter at
the moment. the moment.
""" """
self._order = checkInt(theOrder, 0) self._order = checkInt(order, 0)
return return
def setType(self, theType): def setType(self, itemType):
"""Set the item type from either a proper nwItemType, or set it """Set the item type from either a proper nwItemType, or set it
from a string representing an nwItemType. from a string representing an nwItemType.
""" """
if isinstance(theType, nwItemType): if isinstance(itemType, nwItemType):
self._type = theType self._type = itemType
elif isItemType(theType): elif isItemType(itemType):
self._type = nwItemType[theType] self._type = nwItemType[itemType]
else: else:
logger.error("Unrecognised item type '%s'", theType) logger.error("Unrecognised item type '%s'", itemType)
self._type = nwItemType.NO_TYPE self._type = nwItemType.NO_TYPE
return return
def setClass(self, theClass): def setClass(self, itemClass):
"""Set the item class from either a proper nwItemClass, or set """Set the item class from either a proper nwItemClass, or set
it from a string representing an nwItemClass. it from a string representing an nwItemClass.
""" """
if isinstance(theClass, nwItemClass): if isinstance(itemClass, nwItemClass):
self._class = theClass self._class = itemClass
elif isItemClass(theClass): elif isItemClass(itemClass):
self._class = nwItemClass[theClass] self._class = nwItemClass[itemClass]
else: else:
logger.error("Unrecognised item class '%s'", theClass) logger.error("Unrecognised item class '%s'", itemClass)
self._class = nwItemClass.NO_CLASS self._class = nwItemClass.NO_CLASS
return return
def setLayout(self, theLayout): def setLayout(self, itemLayout):
"""Set the item layout from either a proper nwItemLayout, or set """Set the item layout from either a proper nwItemLayout, or set
it from a string representing an nwItemLayout. it from a string representing an nwItemLayout.
""" """
if isinstance(theLayout, nwItemLayout): if isinstance(itemLayout, nwItemLayout):
self._layout = theLayout self._layout = itemLayout
elif isItemLayout(theLayout): elif isItemLayout(itemLayout):
self._layout = nwItemLayout[theLayout] self._layout = nwItemLayout[itemLayout]
elif theLayout in nwLists.DEP_LAYOUT: elif itemLayout in nwLists.DEP_LAYOUT:
self._layout = nwItemLayout.DOCUMENT self._layout = nwItemLayout.DOCUMENT
else: else:
logger.error("Unrecognised item layout '%s'", theLayout) logger.error("Unrecognised item layout '%s'", itemLayout)
self._layout = nwItemLayout.NO_LAYOUT self._layout = nwItemLayout.NO_LAYOUT
return return
def setStatus(self, theStatus): def setStatus(self, itemStatus):
"""Set the item status by looking it up in the valid status """Set the item status by looking it up in the valid status
items of the current project. items of the current project.
""" """
self._status = self.theProject.statusItems.check(theStatus) self._status = self.theProject.statusItems.check(itemStatus)
return return
def setImport(self, theImport): def setImport(self, itemImport):
"""Set the item importance by looking it up in the valid import """Set the item importance by looking it up in the valid import
items of the current project. items of the current project.
""" """
self._import = self.theProject.importItems.check(theImport) self._import = self.theProject.importItems.check(itemImport)
return return
def setExpanded(self, expState): def setExpanded(self, state):
"""Set the expanded status of an item in the project tree. """Set the expanded status of an item in the project tree.
""" """
if isinstance(expState, str): if isinstance(state, str):
self._expanded = (expState == str(True)) self._expanded = (state == str(True))
else: else:
self._expanded = (expState is True) self._expanded = (state is True)
return return
def setExported(self, expState): def setExported(self, state):
"""Set the export flag. """Set the export flag.
""" """
if isinstance(expState, str): if isinstance(state, str):
self._exported = (expState == str(True)) self._exported = (state == str(True))
else: else:
self._exported = (expState is True) self._exported = (state is True)
return return
## ##
# Set Document Meta Data # Set Document Meta Data
## ##
def setCharCount(self, theCount): def setCharCount(self, count):
"""Set the character count, and ensure that it is an integer. """Set the character count, and ensure that it is an integer.
""" """
self._charCount = max(0, checkInt(theCount, 0)) self._charCount = max(0, checkInt(count, 0))
return return
def setWordCount(self, theCount): def setWordCount(self, count):
"""Set the word count, and ensure that it is an integer. """Set the word count, and ensure that it is an integer.
""" """
self._wordCount = max(0, checkInt(theCount, 0)) self._wordCount = max(0, checkInt(count, 0))
return return
def setParaCount(self, theCount): def setParaCount(self, count):
"""Set the paragraph count, and ensure that it is an integer. """Set the paragraph count, and ensure that it is an integer.
""" """
self._paraCount = max(0, checkInt(theCount, 0)) self._paraCount = max(0, checkInt(count, 0))
return return
def setCursorPos(self, thePosition): def setCursorPos(self, position):
"""Set the cursor position, and ensure that it is an integer. """Set the cursor position, and ensure that it is an integer.
""" """
self._cursorPos = max(0, checkInt(thePosition, 0)) self._cursorPos = max(0, checkInt(position, 0))
return return
def saveInitialCount(self): def saveInitialCount(self):
+18 -12
View File
@@ -44,7 +44,7 @@ from novelwriter.enum import nwItemType, nwItemClass, nwItemLayout, nwAlert
from novelwriter.error import logException from novelwriter.error import logException
from novelwriter.common import ( from novelwriter.common import (
checkString, checkBool, checkInt, isHandle, formatTimeStamp, checkString, checkBool, checkInt, isHandle, formatTimeStamp,
makeFileNameSafe, hexToInt makeFileNameSafe, hexToInt, simplified
) )
from novelwriter.constants import nwLists, trConst, nwFiles, nwLabels from novelwriter.constants import nwLists, trConst, nwFiles, nwLabels
@@ -534,14 +534,16 @@ class NWProject():
if xItem.text is None: if xItem.text is None:
continue continue
if xItem.tag == "name": if xItem.tag == "name":
logger.verbose("Working Title: '%s'", xItem.text) self.projName = checkString(simplified(xItem.text), "")
self.projName = xItem.text logger.verbose("Working Title: '%s'", self.projName)
elif xItem.tag == "title": elif xItem.tag == "title":
logger.verbose("Title is '%s'", xItem.text) self.bookTitle = checkString(simplified(xItem.text), "")
self.bookTitle = xItem.text logger.verbose("Title is '%s'", self.bookTitle)
elif xItem.tag == "author": elif xItem.tag == "author":
logger.verbose("Author: '%s'", xItem.text) author = checkString(simplified(xItem.text), "")
self.bookAuthors.append(xItem.text) if author:
self.bookAuthors.append(author)
logger.verbose("Author: '%s'", author)
elif xItem.tag == "saveCount": elif xItem.tag == "saveCount":
self.saveCount = checkInt(xItem.text, 0) self.saveCount = checkInt(xItem.text, 0)
elif xItem.tag == "autoCount": elif xItem.tag == "autoCount":
@@ -956,14 +958,14 @@ class NWProject():
"""Set the project name (working title), This is the the title """Set the project name (working title), This is the the title
used for backup files etc. used for backup files etc.
""" """
self.projName = projName.strip() self.projName = simplified(projName)
self.setProjectChanged(True) self.setProjectChanged(True)
return True return True
def setBookTitle(self, bookTitle): def setBookTitle(self, bookTitle):
"""Set the book title, that is, the title to include in exports. """Set the book title, that is, the title to include in exports.
""" """
self.bookTitle = bookTitle.strip() self.bookTitle = simplified(bookTitle)
self.setProjectChanged(True) self.setProjectChanged(True)
return True return True
@@ -975,7 +977,7 @@ class NWProject():
self.bookAuthors = [] self.bookAuthors = []
for bookAuthor in bookAuthors.splitlines(): for bookAuthor in bookAuthors.splitlines():
bookAuthor = bookAuthor.strip() bookAuthor = simplified(bookAuthor)
if bookAuthor == "": if bookAuthor == "":
continue continue
self.bookAuthors.append(bookAuthor) self.bookAuthors.append(bookAuthor)
@@ -1114,7 +1116,9 @@ class NWProject():
def setAutoReplace(self, autoReplace): def setAutoReplace(self, autoReplace):
"""Update the auto-replace dictionary. """Update the auto-replace dictionary.
""" """
self.autoReplace = autoReplace self.autoReplace = {}
for key, entry in autoReplace.items():
self.autoReplace[key] = simplified(entry)
self.setProjectChanged(True) self.setProjectChanged(True)
return True return True
@@ -1123,7 +1127,9 @@ class NWProject():
""" """
for valKey, valEntry in titleFormat.items(): for valKey, valEntry in titleFormat.items():
if valKey in self.titleFormat: if valKey in self.titleFormat:
self.titleFormat[valKey] = checkString(valEntry, self.titleFormat[valKey]) self.titleFormat[valKey] = checkString(
simplified(valEntry), self.titleFormat[valKey]
)
return True return True
def setProjectChanged(self, bValue): def setProjectChanged(self, bValue):
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?> <?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="1.7-alpha0" hexVersion="0x010700a0" fileVersion="1.4" timeStamp="2022-04-16 13:57:03"> <novelWriterXML appVersion="1.7-alpha0" hexVersion="0x010700a0" fileVersion="1.4" timeStamp="2022-04-16 16:54:15">
<project> <project>
<name>Project Name</name> <name>Project Name</name>
<title>Project Title</title> <title>Project Title</title>
@@ -23,7 +23,7 @@
<autoReplace> <autoReplace>
<entry key="A">B</entry> <entry key="A">B</entry>
<entry key="C">D</entry> <entry key="C">D</entry>
<entry key="This">With This Stuff </entry> <entry key="This">With This Stuff</entry>
</autoReplace> </autoReplace>
<titleFormat> <titleFormat>
<title>%title%</title> <title>%title%</title>
+2
View File
@@ -44,6 +44,8 @@ def testCoreItem_Setters(mockGUI, constData):
assert theItem.itemName == "A Name" assert theItem.itemName == "A Name"
theItem.setName("\t A Name ") theItem.setName("\t A Name ")
assert theItem.itemName == "A Name" assert theItem.itemName == "A Name"
theItem.setName("\t A\t\u2009\u202f\u2002\u2003\u2028\u2029Name ")
assert theItem.itemName == "A Name"
theItem.setName(123) theItem.setName(123)
assert theItem.itemName == "" assert theItem.itemName == ""