diff --git a/nw/core/index.py b/nw/core/index.py index c48c5329..da11e401 100644 --- a/nw/core/index.py +++ b/nw/core/index.py @@ -908,6 +908,9 @@ def countWords(theText): paraCount = 0 prevEmpty = True + if not isinstance(theText, str): + return charCount, wordCount, paraCount + # We need to treat dashes as word separators for counting words. # The check+replace apprach is much faster that direct replace for # large texts, and a bit slower for small texts, but in the latter @@ -920,33 +923,38 @@ def countWords(theText): for aLine in theText.splitlines(): countPara = True - theLen = len(aLine) - if theLen == 0: + if not aLine: prevEmpty = True continue if aLine[0] == "@" or aLine[0] == "%": continue - if aLine[0:5] == "#### ": - wordCount -= 1 - charCount -= 5 - countPara = False - elif aLine[0:4] == "### ": - wordCount -= 1 - charCount -= 4 - countPara = False - elif aLine[0:3] == "## ": - wordCount -= 1 - charCount -= 3 - countPara = False - elif aLine[0:2] == "# ": - wordCount -= 1 - charCount -= 2 - countPara = False + if aLine[0] == "#": + if aLine[:5] == "#### ": + aLine = aLine[5:] + countPara = False + elif aLine[:4] == "### ": + aLine = aLine[4:] + countPara = False + elif aLine[:3] == "## ": + aLine = aLine[3:] + countPara = False + elif aLine[:2] == "# ": + aLine = aLine[2:] + countPara = False + elif aLine[0] == ">" or aLine[-1] == "<": + if aLine[:2] == ">>": + aLine = aLine[2:].lstrip(" ") + elif aLine[:1] == ">": + aLine = aLine[1:].lstrip(" ") + if aLine[-2:] == "<<": + aLine = aLine[:-2].rstrip(" ") + elif aLine[-1:] == "<": + aLine = aLine[:-1].rstrip(" ") wordCount += len(aLine.split()) - charCount += theLen + charCount += len(aLine) if countPara and prevEmpty: paraCount += 1 diff --git a/sample/nwProject.nwx b/sample/nwProject.nwx index be00422c..c1960d23 100644 --- a/sample/nwProject.nwx +++ b/sample/nwProject.nwx @@ -1,13 +1,13 @@ - + Sample Project Sample Project Jane Smith Jay Doh - 1095 + 1096 188 - 52304 + 52328 False @@ -17,8 +17,8 @@ True 636b6aa9b697b 636b6aa9b697b - 1224 - 848 + 1217 + 841 376 B @@ -63,8 +63,8 @@ Started True TITLE - 244 - 43 + 241 + 42 3 252 @@ -118,8 +118,8 @@ 1st Draft True SCENE - 2450 - 439 + 2434 + 433 14 61 diff --git a/tests/test_core/test_core_index.py b/tests/test_core/test_core_index.py index 87ce2b02..0fdde671 100644 --- a/tests/test_core/test_core_index.py +++ b/tests/test_core/test_core_index.py @@ -1177,29 +1177,59 @@ def testCoreIndex_CheckTextCounts(dummyGUI): def testCoreIndex_CountWords(): """Test the word counter and the exclusion filers. """ - testText = ( + # Non-Text + assert countWords(None) == (0, 0, 0) + assert countWords(1234) == (0, 0, 0) + + # General Text + cC, wC, pC = countWords(( "# Heading One\n" "## Heading Two\n" "### Heading Three\n" - "#### Heading Four\n" - "\n" - "@tag: value\n" - "\n" - "% A comment that should n ot be counted.\n" - "\n" - "The first paragraph.\n" - "\n" - "The second paragraph.\n" - "\n" - "\n" - "The third paragraph.\n" - "\n" + "#### Heading Four\n\n" + "@tag: value\n\n" + "% A comment that should not be counted.\n\n" + "The first paragraph.\n\n" + "The second paragraph.\n\n\n" + "The third paragraph.\n\n" "Dashes\u2013and even longer\u2014dashes." - ) - cC, wC, pC = countWords(testText) - + )) assert cC == 138 assert wC == 22 assert pC == 4 + # Text Alignment + cC, wC, pC = countWords(( + "# Title\n\n" + "Left aligned<<\n\n" + "Left aligned <<\n\n" + "Right indent<\n\n" + "Right indent <\n\n" + )) + assert cC == 53 + assert wC == 9 + assert pC == 4 + + cC, wC, pC = countWords(( + "# Title\n\n" + ">>Right aligned\n\n" + ">> Right aligned\n\n" + ">Left indent\n\n" + "> Left indent\n\n" + )) + assert cC == 53 + assert wC == 9 + assert pC == 4 + + cC, wC, pC = countWords(( + "# Title\n\n" + ">>Centre aligned<<\n\n" + ">> Centre aligned <<\n\n" + ">Double indent<\n\n" + "> Double indent <\n\n" + )) + assert cC == 59 + assert wC == 9 + assert pC == 4 + # END Test testCoreIndex_CountWords