Skip to content

Commit 20da333

Browse files
authored
Block adding spaces before colon in certain meta data cases (#1099)
2 parents 90b50fe + 7e82461 commit 20da333

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

novelwriter/gui/doceditor.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,19 +1982,35 @@ def _docAutoReplace(self, theBlock):
19821982

19831983
tCheck = tInsert
19841984
if tCheck in self.mainConf.fmtPadBefore:
1985-
nDelete = max(nDelete, 1)
1986-
tInsert = self._typPadChar + tInsert
1985+
if self.allowSpaceBeforeColon(theText, tCheck):
1986+
nDelete = max(nDelete, 1)
1987+
tInsert = self._typPadChar + tInsert
19871988

19881989
if tCheck in self.mainConf.fmtPadAfter:
1989-
nDelete = max(nDelete, 1)
1990-
tInsert = tInsert + self._typPadChar
1990+
if self.allowSpaceBeforeColon(theText, tCheck):
1991+
nDelete = max(nDelete, 1)
1992+
tInsert = tInsert + self._typPadChar
19911993

19921994
if nDelete > 0:
19931995
theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, nDelete)
19941996
theCursor.insertText(tInsert)
19951997

19961998
return
19971999

2000+
@staticmethod
2001+
def _allowSpaceBeforeColon(text, char):
2002+
"""Special checker function only used by the insert space
2003+
feature for French, Spanish, etc, so it doesn't insert a
2004+
sapce before colons in meta data lines.
2005+
"""
2006+
if char == ":" and len(text) > 1:
2007+
if text[0] == "@":
2008+
return False
2009+
if text[0] == "%":
2010+
if text[1:].lstrip()[:9].lower() == "synopsis:":
2011+
return False
2012+
return True
2013+
19982014
def _updateHeaders(self, checkPos=False, checkLevel=False):
19992015
"""Update the headers record and return True if anything
20002016
changed, if a check flag was provided.

tests/test_gui/test_gui_doceditor.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ def testGuiEditor_Tags(qtbot, monkeypatch, nwGUI, nwMinimal, ipsumText):
11891189

11901190

11911191
@pytest.mark.gui
1192-
def testGuiEditor_WordCounters(qtbot, monkeypatch, caplog, nwGUI, nwMinimal, ipsumText):
1192+
def testGuiEditor_WordCounters(qtbot, monkeypatch, nwGUI, nwMinimal, ipsumText):
11931193
"""Test saving text from the editor.
11941194
"""
11951195
# Block message box
@@ -1488,3 +1488,30 @@ def testGuiEditor_Search(qtbot, monkeypatch, nwGUI, nwLipsum):
14881488
# qtbot.stopForInteraction()
14891489

14901490
# END Test testGuiEditor_Search
1491+
1492+
1493+
@pytest.mark.gui
1494+
def testGuiEditor_StaticMethods():
1495+
"""Test the document editor's static methods.
1496+
"""
1497+
# Check the method that decides if it is allowed to insert a space
1498+
# before a colon using the French, Spanish, etc language feature
1499+
assert GuiDocEditor._allowSpaceBeforeColon("", "") is True
1500+
assert GuiDocEditor._allowSpaceBeforeColon("", ":") is True
1501+
assert GuiDocEditor._allowSpaceBeforeColon("some text", ":") is True
1502+
1503+
assert GuiDocEditor._allowSpaceBeforeColon("@:", ":") is False
1504+
assert GuiDocEditor._allowSpaceBeforeColon("@>", ">") is True
1505+
1506+
assert GuiDocEditor._allowSpaceBeforeColon("%", ":") is True
1507+
assert GuiDocEditor._allowSpaceBeforeColon("%:", ":") is True
1508+
assert GuiDocEditor._allowSpaceBeforeColon("%synopsis:", ":") is False
1509+
assert GuiDocEditor._allowSpaceBeforeColon("%Synopsis:", ":") is False
1510+
assert GuiDocEditor._allowSpaceBeforeColon("% synopsis:", ":") is False
1511+
assert GuiDocEditor._allowSpaceBeforeColon("% Synopsis:", ":") is False
1512+
assert GuiDocEditor._allowSpaceBeforeColon("% synopsis:", ":") is False
1513+
assert GuiDocEditor._allowSpaceBeforeColon("% Synopsis:", ":") is False
1514+
assert GuiDocEditor._allowSpaceBeforeColon("%synopsis :", ":") is True
1515+
assert GuiDocEditor._allowSpaceBeforeColon("%Synopsis :", ":") is True
1516+
1517+
# END Test testGuiEditor_StaticMethods

0 commit comments

Comments
 (0)