Fix word counter for align and indent tags
This commit is contained in:
+27
-19
@@ -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
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<novelWriterXML appVersion="1.4a0" hexVersion="0x010400a0" fileVersion="1.2" timeStamp="2021-06-10 14:15:49">
|
||||
<novelWriterXML appVersion="1.4a0" hexVersion="0x010400a0" fileVersion="1.2" timeStamp="2021-06-10 23:37:32">
|
||||
<project>
|
||||
<name>Sample Project</name>
|
||||
<title>Sample Project</title>
|
||||
<author>Jane Smith</author>
|
||||
<author>Jay Doh</author>
|
||||
<saveCount>1095</saveCount>
|
||||
<saveCount>1096</saveCount>
|
||||
<autoCount>188</autoCount>
|
||||
<editTime>52304</editTime>
|
||||
<editTime>52328</editTime>
|
||||
</project>
|
||||
<settings>
|
||||
<doBackup>False</doBackup>
|
||||
@@ -17,8 +17,8 @@
|
||||
<autoOutline>True</autoOutline>
|
||||
<lastEdited>636b6aa9b697b</lastEdited>
|
||||
<lastViewed>636b6aa9b697b</lastViewed>
|
||||
<lastWordCount>1224</lastWordCount>
|
||||
<novelWordCount>848</novelWordCount>
|
||||
<lastWordCount>1217</lastWordCount>
|
||||
<novelWordCount>841</novelWordCount>
|
||||
<notesWordCount>376</notesWordCount>
|
||||
<autoReplace>
|
||||
<entry key="A">B</entry>
|
||||
@@ -63,8 +63,8 @@
|
||||
<status>Started</status>
|
||||
<exported>True</exported>
|
||||
<layout>TITLE</layout>
|
||||
<charCount>244</charCount>
|
||||
<wordCount>43</wordCount>
|
||||
<charCount>241</charCount>
|
||||
<wordCount>42</wordCount>
|
||||
<paraCount>3</paraCount>
|
||||
<cursorPos>252</cursorPos>
|
||||
</item>
|
||||
@@ -118,8 +118,8 @@
|
||||
<status>1st Draft</status>
|
||||
<exported>True</exported>
|
||||
<layout>SCENE</layout>
|
||||
<charCount>2450</charCount>
|
||||
<wordCount>439</wordCount>
|
||||
<charCount>2434</charCount>
|
||||
<wordCount>433</wordCount>
|
||||
<paraCount>14</paraCount>
|
||||
<cursorPos>61</cursorPos>
|
||||
</item>
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user