Skip to content

Commit

Permalink
Fix bug in splitting on alt scene headings (#2234)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkbo authored Feb 9, 2025
2 parents 2401fc2 + ec8a6b9 commit c6991b3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ i18n/*.qph

# Documentation
/docs/build/
/docs/source/locales/**/*.mo
/novelwriter/assets/help/
/novelwriter/assets/manual.pdf
/novelwriter/assets/manual*.pdf
*.qch
*.qhc
.~lock.*
Expand Down
94 changes: 49 additions & 45 deletions novelwriter/dialogs/docsplit.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ def __init__(self, parent: QWidget, sHandle: str) -> None:
vSp = CONFIG.pxInt(8)
bSp = CONFIG.pxInt(12)

pOptions = SHARED.project.options
spLevel = pOptions.getInt("GuiDocSplit", "spLevel", 3)
intoFolder = pOptions.getBool("GuiDocSplit", "intoFolder", True)
docHierarchy = pOptions.getBool("GuiDocSplit", "docHierarchy", True)
options = SHARED.project.options
spLevel = options.getInt("GuiDocSplit", "spLevel", 3)
intoFolder = options.getBool("GuiDocSplit", "intoFolder", True)
docHierarchy = options.getBool("GuiDocSplit", "docHierarchy", True)

# Heading Selection
self.listBox = QListWidget(self)
Expand Down Expand Up @@ -171,10 +171,10 @@ def data(self) -> tuple[dict, list[str]]:
self._data["moveToTrash"] = moveToTrash

logger.debug("Saving State: GuiDocSplit")
pOptions = SHARED.project.options
pOptions.setValue("GuiDocSplit", "spLevel", spLevel)
pOptions.setValue("GuiDocSplit", "intoFolder", intoFolder)
pOptions.setValue("GuiDocSplit", "docHierarchy", docHierarchy)
options = SHARED.project.options
options.setValue("GuiDocSplit", "spLevel", spLevel)
options.setValue("GuiDocSplit", "intoFolder", intoFolder)
options.setValue("GuiDocSplit", "docHierarchy", docHierarchy)

return self._data, self._text

Expand Down Expand Up @@ -218,42 +218,46 @@ def _loadContent(self, sHandle: str) -> None:
if not self._text:
self._text = SHARED.project.storage.getDocumentText(sHandle).splitlines()

for lineNo, aLine in enumerate(self._text):

onLine = -1
hLevel = 0
hLabel = aLine.strip()
if aLine.startswith("# ") and spLevel >= 1:
onLine = lineNo
hLevel = 1
hLabel = aLine[2:].strip()
elif aLine.startswith("## ") and spLevel >= 2:
onLine = lineNo
hLevel = 2
hLabel = aLine[3:].strip()
elif aLine.startswith("### ") and spLevel >= 3:
onLine = lineNo
hLevel = 3
hLabel = aLine[4:].strip()
elif aLine.startswith("#### ") and spLevel >= 4:
onLine = lineNo
hLevel = 4
hLabel = aLine[5:].strip()
elif aLine.startswith("#! ") and spLevel >= 1:
onLine = lineNo
hLevel = 1
hLabel = aLine[3:].strip()
elif aLine.startswith("##! ") and spLevel >= 2:
onLine = lineNo
hLevel = 2
hLabel = aLine[4:].strip()

if onLine >= 0 and hLevel > 0:
newItem = QListWidgetItem()
newItem.setText(aLine.strip())
newItem.setData(self.LINE_ROLE, onLine)
newItem.setData(self.LEVEL_ROLE, hLevel)
newItem.setData(self.LABEL_ROLE, hLabel)
self.listBox.addItem(newItem)
for i, line in enumerate(self._text):

pos = -1
level = 0
label = line.strip()
if line.startswith("# ") and spLevel >= 1:
pos = i
level = 1
label = line[2:].strip()
elif line.startswith("## ") and spLevel >= 2:
pos = i
level = 2
label = line[3:].strip()
elif line.startswith("### ") and spLevel >= 3:
pos = i
level = 3
label = line[4:].strip()
elif line.startswith("#### ") and spLevel >= 4:
pos = i
level = 4
label = line[5:].strip()
elif line.startswith("#! ") and spLevel >= 1:
pos = i
level = 1
label = line[3:].strip()
elif line.startswith("##! ") and spLevel >= 2:
pos = i
level = 2
label = line[4:].strip()
elif line.startswith("###! ") and spLevel >= 3:
pos = i
level = 3
label = line[5:].strip()

if pos >= 0 and level > 0:
trItem = QListWidgetItem()
trItem.setText(line.strip())
trItem.setData(self.LINE_ROLE, pos)
trItem.setData(self.LEVEL_ROLE, level)
trItem.setData(self.LABEL_ROLE, label)
self.listBox.addItem(trItem)

return
4 changes: 2 additions & 2 deletions tests/test_dialogs/test_dlg_docsplit.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ def testDlgSplit_Main(qtbot, monkeypatch, nwGUI, projPath, mockRnd):
"##! Prologue\n\nText\n\n"
"## Chapter One\n\nText\n\n"
"### Scene One\n\nText\n\n"
"### Scene Two\n\nText\n\n"
"###! Scene Two\n\nText\n\n"
"## Chapter Two\n\nText\n\n"
"### Scene Three\n\nText\n\n"
"### Scene Four\n\nText\n\n"
"###! Scene Four\n\nText\n\n"
"#! New Title\n\nText\n\n"
"## New Chapter\n\nText\n\n"
"### New Scene\n\nText\n\n"
Expand Down
4 changes: 2 additions & 2 deletions tests/test_gui/test_gui_projtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,10 +858,10 @@ def testGuiProjTree_SplitDocument(qtbot, monkeypatch, nwGUI, projPath, mockRnd,
"##! Prologue\n\nText\n\n"
"## Chapter One\n\nText\n\n"
"### Scene One\n\nText\n\n"
"### Scene Two\n\nText\n\n"
"###! Scene Two\n\nText\n\n"
"## Chapter Two\n\nText\n\n"
"### Scene Three\n\nText\n\n"
"### Scene Four\n\nText\n\n"
"###! Scene Four\n\nText\n\n"
"#! New Title\n\nText\n\n"
"## New Chapter\n\nText\n\n"
"### New Scene\n\nText\n\n"
Expand Down

0 comments on commit c6991b3

Please sign in to comment.