Update tests and word counter
This commit is contained in:
@@ -23,6 +23,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
from PyQt5.QtCore import QCoreApplication, QT_TRANSLATE_NOOP
|
||||
|
||||
from novelwriter.enum import nwBuildFmt, nwItemClass, nwItemLayout, nwOutline
|
||||
@@ -59,9 +61,12 @@ class nwRegEx:
|
||||
FMT_EI = r"(?<![\w\\])(_)(?![\s_])(.+?)(?<![\s\\])(\1)(?!\w)"
|
||||
FMT_EB = r"(?<![\w\\])([\*]{2})(?![\s\*])(.+?)(?<![\s\\])(\1)(?!\w)"
|
||||
FMT_ST = r"(?<![\w\\])([~]{2})(?![\s~])(.+?)(?<![\s\\])(\1)(?!\w)"
|
||||
FMT_SC = r"(?<!\\)(\[[\/\!]?(?i)(?:i|b|s|u|sup|sub)\])"
|
||||
FMT_SC = r"(?i)(?<!\\)(\[[\/\!]?(?:i|b|s|u|sup|sub)\])"
|
||||
FMT_SV = r"(?<!\\)(\[(?i)(?:fn|footnote):)(.+?)(?<!\\)(\])"
|
||||
|
||||
# Pre-Compiled RegEx
|
||||
RX_SC = re.compile(FMT_SC)
|
||||
|
||||
# END Class nwRegEx
|
||||
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ from pathlib import Path
|
||||
from novelwriter.enum import nwItemClass, nwItemType, nwItemLayout
|
||||
from novelwriter.error import logException
|
||||
from novelwriter.common import checkInt, isHandle, isItemClass, isTitleTag, jsonEncode
|
||||
from novelwriter.constants import nwFiles, nwKeyWords, nwUnicode, nwHeaders
|
||||
from novelwriter.constants import nwFiles, nwKeyWords, nwRegEx, nwUnicode, nwHeaders
|
||||
|
||||
if TYPE_CHECKING: # pragma: no cover
|
||||
from novelwriter.core.item import NWItem
|
||||
@@ -1229,6 +1229,10 @@ def countWords(text: str) -> tuple[int, int, int]:
|
||||
if nwUnicode.U_EMDASH in text:
|
||||
text = text.replace(nwUnicode.U_EMDASH, " ")
|
||||
|
||||
# Strip shortcodes
|
||||
if "[" in text:
|
||||
text = nwRegEx.RX_SC.sub("", text)
|
||||
|
||||
for line in text.splitlines():
|
||||
|
||||
countPara = True
|
||||
@@ -1241,9 +1245,10 @@ def countWords(text: str) -> tuple[int, int, int]:
|
||||
continue
|
||||
|
||||
if line[0] == "[":
|
||||
if line.startswith(("[NEWPAGE]", "[NEW PAGE]", "[VSPACE]")):
|
||||
check = line.lower()
|
||||
if check.startswith(("[newpage]", "[new page]", "[vspace]")):
|
||||
continue
|
||||
elif line.startswith("[VSPACE:") and line.endswith("]"):
|
||||
elif check.startswith("[vspace:") and line.endswith("]"):
|
||||
continue
|
||||
|
||||
elif line[0] == "#":
|
||||
|
||||
@@ -55,6 +55,7 @@ from novelwriter.enum import nwDocAction, nwDocInsert, nwDocMode, nwItemClass
|
||||
from novelwriter.common import minmax, transferCase
|
||||
from novelwriter.constants import nwKeyWords, nwUnicode
|
||||
from novelwriter.core.index import countWords
|
||||
from novelwriter.core.document import NWDocument
|
||||
from novelwriter.gui.dochighlight import GuiDocHighlighter
|
||||
from novelwriter.gui.editordocument import GuiTextDocument
|
||||
from novelwriter.extensions.wheeleventfilter import WheelEventFilter
|
||||
@@ -408,12 +409,6 @@ class GuiDocEditor(QPlainTextEdit):
|
||||
self._qDocument.syntaxHighlighter.rehighlightByType(GuiDocHighlighter.BLOCK_META)
|
||||
return
|
||||
|
||||
def redrawText(self) -> None:
|
||||
"""Redraw the text by marking the document content as dirty."""
|
||||
self._qDocument.markContentsDirty(0, self._qDocument.characterCount())
|
||||
self.updateDocMargins()
|
||||
return
|
||||
|
||||
def replaceText(self, text: str) -> None:
|
||||
"""Replace the text of the current document with the provided
|
||||
text. This also clears undo history.
|
||||
@@ -735,19 +730,17 @@ class GuiDocEditor(QPlainTextEdit):
|
||||
"""Tell the user where on the file system the file in the editor
|
||||
is saved.
|
||||
"""
|
||||
if self._nwDocument is None:
|
||||
logger.error("No document open")
|
||||
return
|
||||
SHARED.info(
|
||||
"<br>".join([
|
||||
self.tr("Document Details"),
|
||||
"–"*40,
|
||||
self.tr("Created: {0}").format(self._nwDocument.createdDate),
|
||||
self.tr("Updated: {0}").format(self._nwDocument.updatedDate),
|
||||
]),
|
||||
details=self.tr("File Location: {0}").format(self._nwDocument.fileLocation),
|
||||
log=False
|
||||
)
|
||||
if isinstance(self._nwDocument, NWDocument):
|
||||
SHARED.info(
|
||||
"<br>".join([
|
||||
self.tr("Document Details"),
|
||||
"–"*40,
|
||||
self.tr("Created: {0}").format(self._nwDocument.createdDate),
|
||||
self.tr("Updated: {0}").format(self._nwDocument.updatedDate),
|
||||
]),
|
||||
details=self.tr("File Location: {0}").format(self._nwDocument.fileLocation),
|
||||
log=False
|
||||
)
|
||||
return
|
||||
|
||||
def insertText(self, insert: str | nwDocInsert) -> bool:
|
||||
@@ -775,15 +768,15 @@ class GuiDocEditor(QPlainTextEdit):
|
||||
newBlock = True
|
||||
goAfter = True
|
||||
elif insert == nwDocInsert.NEW_PAGE:
|
||||
text = "[NEW PAGE]"
|
||||
text = "[newpage]"
|
||||
newBlock = True
|
||||
goAfter = False
|
||||
elif insert == nwDocInsert.VSPACE_S:
|
||||
text = "[VSPACE]"
|
||||
text = "[vspace]"
|
||||
newBlock = True
|
||||
goAfter = False
|
||||
elif insert == nwDocInsert.VSPACE_M:
|
||||
text = "[VSPACE:2]"
|
||||
text = "[vspace:2]"
|
||||
newBlock = True
|
||||
goAfter = False
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user