Fix index error in word counter (#1817)

This commit is contained in:
Veronica Berglyd Olsen
2024-04-13 00:47:16 +02:00
committed by GitHub
2 changed files with 11 additions and 5 deletions
+1
View File
@@ -57,6 +57,7 @@ def preProcessText(text: str, keepHeaders: bool = True) -> list[str]:
continue continue
if line[0] == ">": if line[0] == ">":
line = line.lstrip(">").lstrip(" ") line = line.lstrip(">").lstrip(" ")
if line: # Above block can return empty line (Issue #1816)
if line[-1] == "<": if line[-1] == "<":
line = line.rstrip("<").rstrip(" ") line = line.rstrip("<").rstrip(" ")
if "[" in line: if "[" in line:
+10 -5
View File
@@ -29,7 +29,7 @@ from novelwriter.text.counting import bodyTextCounter, preProcessText, standardC
def testTextCounting_preProcessText(): def testTextCounting_preProcessText():
"""Test the text preprocessor for counters.""" """Test the text preprocessor for counters."""
# Not Text # Not Text
assert preProcessText(None) == [] assert preProcessText(None) == [] # type: ignore
# No Text # No Text
assert preProcessText("") == [] assert preProcessText("") == []
@@ -81,6 +81,10 @@ def testTextCounting_standardCounter():
assert standardCounter(None) == (0, 0, 0) # type: ignore assert standardCounter(None) == (0, 0, 0) # type: ignore
assert standardCounter(1234) == (0, 0, 0) # type: ignore assert standardCounter(1234) == (0, 0, 0) # type: ignore
# Test Corner Cases, Bug #1816
assert standardCounter("> ") == (0, 0, 0)
assert standardCounter(" <") == (0, 0, 0)
# General Text # General Text
cC, wC, pC = standardCounter(( cC, wC, pC = standardCounter((
"#! Title\n\n" "#! Title\n\n"
@@ -88,7 +92,8 @@ def testTextCounting_standardCounter():
"# Heading One\n" "# Heading One\n"
"## Heading Two\n" "## Heading Two\n"
"### Heading Three\n" "### Heading Three\n"
"#### Heading Four\n\n" "###! Heading Four\n"
"#### Heading Five\n\n"
"@tag: value\n\n" "@tag: value\n\n"
"% A comment that should not be counted.\n\n" "% A comment that should not be counted.\n\n"
"The first paragraph.\n\n" "The first paragraph.\n\n"
@@ -96,8 +101,8 @@ def testTextCounting_standardCounter():
"The third paragraph.\n\n" "The third paragraph.\n\n"
"Dashes\u2013and even longer\u2014dashes." "Dashes\u2013and even longer\u2014dashes."
)) ))
assert cC == 151 assert cC == 163
assert wC == 24 assert wC == 26
assert pC == 4 assert pC == 4
# Text Alignment # Text Alignment
@@ -182,7 +187,7 @@ def testTextCounting_standardCounter():
def testTextCounting_bodyTextCounter(): def testTextCounting_bodyTextCounter():
"""Test the body text counter.""" """Test the body text counter."""
# Not Text # Not Text
assert bodyTextCounter(None) == (0, 0, 0) assert bodyTextCounter(None) == (0, 0, 0) # type: ignore
# General Text # General Text
wC, cC, sC = bodyTextCounter(( wC, cC, sC = bodyTextCounter((