novelWriter 2.3.1__py3-none-any.whl → 2.4b1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {novelWriter-2.3.1.dist-info → novelWriter-2.4b1.dist-info}/METADATA +1 -1
- {novelWriter-2.3.1.dist-info → novelWriter-2.4b1.dist-info}/RECORD +81 -70
- novelwriter/__init__.py +5 -5
- novelwriter/assets/icons/typicons_dark/icons.conf +4 -0
- novelwriter/assets/icons/typicons_dark/nw_tb-mark.svg +7 -0
- novelwriter/assets/icons/typicons_dark/typ_arrow-down.svg +4 -0
- novelwriter/assets/icons/typicons_dark/typ_arrow-right.svg +4 -0
- novelwriter/assets/icons/typicons_dark/typ_refresh-flipped.svg +1 -1
- novelwriter/assets/icons/typicons_dark/typ_refresh.svg +1 -1
- novelwriter/assets/icons/typicons_dark/typ_search-grey.svg +4 -0
- novelwriter/assets/icons/typicons_dark/typ_times.svg +1 -1
- novelwriter/assets/icons/typicons_light/icons.conf +4 -0
- novelwriter/assets/icons/typicons_light/nw_tb-mark.svg +7 -0
- novelwriter/assets/icons/typicons_light/typ_arrow-down.svg +4 -0
- novelwriter/assets/icons/typicons_light/typ_arrow-right.svg +4 -0
- novelwriter/assets/icons/typicons_light/typ_refresh-flipped.svg +1 -1
- novelwriter/assets/icons/typicons_light/typ_refresh.svg +1 -1
- novelwriter/assets/icons/typicons_light/typ_search-grey.svg +4 -0
- novelwriter/assets/icons/typicons_light/typ_times.svg +1 -1
- novelwriter/assets/manual.pdf +0 -0
- novelwriter/assets/sample.zip +0 -0
- novelwriter/assets/syntax/default_dark.conf +1 -0
- novelwriter/assets/syntax/default_light.conf +1 -0
- novelwriter/assets/syntax/grey_dark.conf +1 -0
- novelwriter/assets/syntax/grey_light.conf +1 -0
- novelwriter/assets/syntax/light_owl.conf +1 -0
- novelwriter/assets/syntax/night_owl.conf +1 -0
- novelwriter/assets/syntax/solarized_dark.conf +1 -0
- novelwriter/assets/syntax/solarized_light.conf +1 -0
- novelwriter/assets/syntax/tomorrow.conf +1 -0
- novelwriter/assets/syntax/tomorrow_night.conf +1 -0
- novelwriter/assets/syntax/tomorrow_night_blue.conf +1 -0
- novelwriter/assets/syntax/tomorrow_night_bright.conf +1 -0
- novelwriter/assets/syntax/tomorrow_night_eighties.conf +1 -0
- novelwriter/assets/text/credits_en.htm +25 -23
- novelwriter/common.py +1 -1
- novelwriter/config.py +35 -12
- novelwriter/constants.py +5 -6
- novelwriter/core/buildsettings.py +60 -40
- novelwriter/core/coretools.py +98 -13
- novelwriter/core/docbuild.py +74 -7
- novelwriter/core/document.py +24 -3
- novelwriter/core/index.py +31 -112
- novelwriter/core/project.py +10 -15
- novelwriter/core/sessions.py +2 -2
- novelwriter/core/status.py +4 -4
- novelwriter/core/storage.py +8 -2
- novelwriter/core/tohtml.py +22 -25
- novelwriter/core/tokenizer.py +416 -232
- novelwriter/core/tomd.py +17 -8
- novelwriter/core/toodt.py +65 -7
- novelwriter/core/tree.py +8 -8
- novelwriter/dialogs/docsplit.py +7 -8
- novelwriter/dialogs/preferences.py +3 -6
- novelwriter/enum.py +17 -14
- novelwriter/extensions/modified.py +20 -2
- novelwriter/extensions/versioninfo.py +1 -1
- novelwriter/gui/doceditor.py +257 -279
- novelwriter/gui/dochighlight.py +29 -25
- novelwriter/gui/docviewer.py +139 -148
- novelwriter/gui/docviewerpanel.py +4 -24
- novelwriter/gui/editordocument.py +12 -1
- novelwriter/gui/itemdetails.py +6 -6
- novelwriter/gui/mainmenu.py +37 -16
- novelwriter/gui/noveltree.py +11 -19
- novelwriter/gui/outline.py +43 -20
- novelwriter/gui/projtree.py +35 -43
- novelwriter/gui/search.py +316 -0
- novelwriter/gui/sidebar.py +25 -30
- novelwriter/gui/theme.py +59 -6
- novelwriter/guimain.py +176 -173
- novelwriter/shared.py +26 -1
- novelwriter/text/__init__.py +3 -0
- novelwriter/text/counting.py +137 -0
- novelwriter/tools/manuscript.py +344 -55
- novelwriter/tools/manussettings.py +213 -71
- novelwriter/tools/welcome.py +1 -1
- {novelWriter-2.3.1.dist-info → novelWriter-2.4b1.dist-info}/LICENSE.md +0 -0
- {novelWriter-2.3.1.dist-info → novelWriter-2.4b1.dist-info}/WHEEL +0 -0
- {novelWriter-2.3.1.dist-info → novelWriter-2.4b1.dist-info}/entry_points.txt +0 -0
- {novelWriter-2.3.1.dist-info → novelWriter-2.4b1.dist-info}/top_level.txt +0 -0
novelwriter/core/docbuild.py
CHANGED
@@ -52,7 +52,10 @@ class NWBuildDocument:
|
|
52
52
|
manuscript, based on a build definition object (BuildSettings).
|
53
53
|
"""
|
54
54
|
|
55
|
-
__slots__ = (
|
55
|
+
__slots__ = (
|
56
|
+
"_project", "_build", "_queue", "_error", "_cache", "_count",
|
57
|
+
"_outline", "_preview"
|
58
|
+
)
|
56
59
|
|
57
60
|
def __init__(self, project: NWProject, build: BuildSettings) -> None:
|
58
61
|
self._project = project
|
@@ -60,6 +63,9 @@ class NWBuildDocument:
|
|
60
63
|
self._queue = []
|
61
64
|
self._error = None
|
62
65
|
self._cache = None
|
66
|
+
self._count = False
|
67
|
+
self._outline = False
|
68
|
+
self._preview = False
|
63
69
|
return
|
64
70
|
|
65
71
|
##
|
@@ -79,6 +85,29 @@ class NWBuildDocument:
|
|
79
85
|
"""
|
80
86
|
return self._cache
|
81
87
|
|
88
|
+
##
|
89
|
+
# Setters
|
90
|
+
##
|
91
|
+
|
92
|
+
def setCountEnabled(self, state: bool) -> None:
|
93
|
+
"""Turn on/off stats for builds."""
|
94
|
+
self._count = state
|
95
|
+
return
|
96
|
+
|
97
|
+
def setBuildOutline(self, state: bool) -> None:
|
98
|
+
"""Turn on/off outline for builds."""
|
99
|
+
self._outline = state
|
100
|
+
return
|
101
|
+
|
102
|
+
def setPreviewMode(self, state: bool) -> None:
|
103
|
+
"""Set the preview mode of the build. This also enables stats
|
104
|
+
count and outline mode.
|
105
|
+
"""
|
106
|
+
self._preview = state
|
107
|
+
self._outline = state
|
108
|
+
self._count = state
|
109
|
+
return
|
110
|
+
|
82
111
|
##
|
83
112
|
# Special Methods
|
84
113
|
##
|
@@ -153,6 +182,8 @@ class NWBuildDocument:
|
|
153
182
|
makeObj = ToHtml(self._project)
|
154
183
|
filtered = self._setupBuild(makeObj)
|
155
184
|
|
185
|
+
makeObj.setPreview(self._preview)
|
186
|
+
makeObj.setLinkHeadings(self._preview)
|
156
187
|
for i, tHandle in enumerate(self._queue):
|
157
188
|
self._error = None
|
158
189
|
if filtered.get(tHandle, (False, 0))[0]:
|
@@ -160,7 +191,7 @@ class NWBuildDocument:
|
|
160
191
|
else:
|
161
192
|
yield i, False
|
162
193
|
|
163
|
-
if self._build.getBool("
|
194
|
+
if not (self._build.getBool("html.preserveTabs") or self._preview):
|
164
195
|
makeObj.replaceTabs()
|
165
196
|
|
166
197
|
self._error = None
|
@@ -191,6 +222,8 @@ class NWBuildDocument:
|
|
191
222
|
if self._build.getBool("format.replaceTabs"):
|
192
223
|
makeObj.replaceTabs(nSpaces=4, spaceChar=" ")
|
193
224
|
|
225
|
+
makeObj.setPreserveBreaks(self._build.getBool("md.preserveBreaks"))
|
226
|
+
|
194
227
|
for i, tHandle in enumerate(self._queue):
|
195
228
|
self._error = None
|
196
229
|
if filtered.get(tHandle, (False, 0))[0]:
|
@@ -256,26 +289,52 @@ class NWBuildDocument:
|
|
256
289
|
fontInfo = QFontInfo(bldFont)
|
257
290
|
textFixed = fontInfo.fixedPitch()
|
258
291
|
|
259
|
-
bldObj.setTitleFormat(
|
260
|
-
|
261
|
-
|
292
|
+
bldObj.setTitleFormat(
|
293
|
+
self._build.getStr("headings.fmtTitle"),
|
294
|
+
self._build.getBool("headings.hideTitle")
|
295
|
+
)
|
296
|
+
bldObj.setChapterFormat(
|
297
|
+
self._build.getStr("headings.fmtChapter"),
|
298
|
+
self._build.getBool("headings.hideChapter")
|
299
|
+
)
|
300
|
+
bldObj.setUnNumberedFormat(
|
301
|
+
self._build.getStr("headings.fmtUnnumbered"),
|
302
|
+
self._build.getBool("headings.hideUnnumbered")
|
303
|
+
)
|
262
304
|
bldObj.setSceneFormat(
|
263
305
|
self._build.getStr("headings.fmtScene"),
|
264
306
|
self._build.getBool("headings.hideScene")
|
265
307
|
)
|
308
|
+
bldObj.setHardSceneFormat(
|
309
|
+
self._build.getStr("headings.fmtHardScene"),
|
310
|
+
self._build.getBool("headings.hideHardScene")
|
311
|
+
)
|
266
312
|
bldObj.setSectionFormat(
|
267
313
|
self._build.getStr("headings.fmtSection"),
|
268
314
|
self._build.getBool("headings.hideSection")
|
269
315
|
)
|
316
|
+
bldObj.setTitleStyle(
|
317
|
+
self._build.getBool("headings.centerTitle"),
|
318
|
+
self._build.getBool("headings.breakTitle")
|
319
|
+
)
|
320
|
+
bldObj.setChapterStyle(
|
321
|
+
self._build.getBool("headings.centerChapter"),
|
322
|
+
self._build.getBool("headings.breakChapter")
|
323
|
+
)
|
324
|
+
bldObj.setSceneStyle(
|
325
|
+
self._build.getBool("headings.centerScene"),
|
326
|
+
self._build.getBool("headings.breakScene")
|
327
|
+
)
|
270
328
|
|
271
329
|
bldObj.setFont(fontFamily, textSize, textFixed)
|
272
330
|
bldObj.setJustify(self._build.getBool("format.justifyText"))
|
273
331
|
bldObj.setLineHeight(self._build.getFloat("format.lineHeight"))
|
274
332
|
|
333
|
+
bldObj.setBodyText(self._build.getBool("text.includeBodyText"))
|
275
334
|
bldObj.setSynopsis(self._build.getBool("text.includeSynopsis"))
|
276
335
|
bldObj.setComments(self._build.getBool("text.includeComments"))
|
277
336
|
bldObj.setKeywords(self._build.getBool("text.includeKeywords"))
|
278
|
-
bldObj.
|
337
|
+
bldObj.setIgnoredKeywords(self._build.getStr("text.ignoredKeywords"))
|
279
338
|
|
280
339
|
if isinstance(bldObj, ToHtml):
|
281
340
|
bldObj.setStyles(self._build.getBool("html.addStyles"))
|
@@ -287,6 +346,7 @@ class NWBuildDocument:
|
|
287
346
|
bldObj.setHeaderFormat(
|
288
347
|
self._build.getStr("odt.pageHeader"), self._build.getInt("odt.pageCountOffset")
|
289
348
|
)
|
349
|
+
bldObj.setFirstLineIndent(self._build.getBool("odt.firstLineIndent"))
|
290
350
|
|
291
351
|
scale = nwLabels.UNIT_SCALE.get(self._build.getStr("format.pageUnit"), 1.0)
|
292
352
|
pW, pH = nwLabels.PAPER_SIZE.get(self._build.getStr("format.pageSize"), (-1.0, -1.0))
|
@@ -314,11 +374,18 @@ class NWBuildDocument:
|
|
314
374
|
bldObj.addRootHeading(tHandle)
|
315
375
|
if convert:
|
316
376
|
bldObj.doConvert()
|
377
|
+
if self._count:
|
378
|
+
bldObj.countStats()
|
379
|
+
if self._outline:
|
380
|
+
bldObj.buildOutline()
|
317
381
|
elif tItem.isFileType():
|
318
382
|
bldObj.setText(tHandle)
|
319
383
|
bldObj.doPreProcessing()
|
320
384
|
bldObj.tokenizeText()
|
321
|
-
|
385
|
+
if self._count:
|
386
|
+
bldObj.countStats()
|
387
|
+
if self._outline:
|
388
|
+
bldObj.buildOutline()
|
322
389
|
if convert:
|
323
390
|
bldObj.doConvert()
|
324
391
|
else:
|
novelwriter/core/document.py
CHANGED
@@ -31,7 +31,7 @@ from typing import TYPE_CHECKING
|
|
31
31
|
from pathlib import Path
|
32
32
|
|
33
33
|
from novelwriter.enum import nwItemLayout, nwItemClass
|
34
|
-
from novelwriter.error import formatException
|
34
|
+
from novelwriter.error import formatException, logException
|
35
35
|
from novelwriter.common import formatTimeStamp, isHandle
|
36
36
|
from novelwriter.core.item import NWItem
|
37
37
|
|
@@ -106,7 +106,28 @@ class NWDocument:
|
|
106
106
|
return self._item
|
107
107
|
|
108
108
|
##
|
109
|
-
#
|
109
|
+
# Static Methods
|
110
|
+
##
|
111
|
+
|
112
|
+
@staticmethod
|
113
|
+
def quickReadText(content: Path, tHandle: str) -> str:
|
114
|
+
"""Return the text of a document in a fast and efficient way."""
|
115
|
+
if (path := content / f"{tHandle}.nwd").is_file():
|
116
|
+
try:
|
117
|
+
with open(path, mode="r", encoding="utf-8") as inFile:
|
118
|
+
line = ""
|
119
|
+
for _ in range(10):
|
120
|
+
if not (line := inFile.readline()).startswith(r"%%~"):
|
121
|
+
break
|
122
|
+
return line + inFile.read()
|
123
|
+
except Exception:
|
124
|
+
logger.error("Cannot read document with handle '%s'", tHandle)
|
125
|
+
logException()
|
126
|
+
return ""
|
127
|
+
return ""
|
128
|
+
|
129
|
+
##
|
130
|
+
# Methods
|
110
131
|
##
|
111
132
|
|
112
133
|
def fileExists(self) -> bool:
|
@@ -155,7 +176,7 @@ class NWDocument:
|
|
155
176
|
try:
|
156
177
|
with open(docPath, mode="r", encoding="utf-8") as inFile:
|
157
178
|
# Check the first <= 10 lines for metadata
|
158
|
-
for
|
179
|
+
for _ in range(10):
|
159
180
|
line = inFile.readline()
|
160
181
|
if line.startswith(r"%%~"):
|
161
182
|
self._parseMeta(line)
|
novelwriter/core/index.py
CHANGED
@@ -3,7 +3,6 @@ novelWriter – Project Index
|
|
3
3
|
===========================
|
4
4
|
|
5
5
|
File History:
|
6
|
-
Created: 2019-04-22 [0.0.1] countWords
|
7
6
|
Created: 2019-05-27 [0.1.4] NWIndex
|
8
7
|
Created: 2022-05-28 [2.0rc1] IndexItem
|
9
8
|
Created: 2022-05-28 [2.0rc1] IndexHeading
|
@@ -34,13 +33,14 @@ import logging
|
|
34
33
|
from time import time
|
35
34
|
from typing import TYPE_CHECKING
|
36
35
|
from pathlib import Path
|
37
|
-
from collections.abc import ItemsView, Iterable
|
36
|
+
from collections.abc import ItemsView, Iterable
|
38
37
|
|
39
38
|
from novelwriter import SHARED
|
40
39
|
from novelwriter.enum import nwComment, nwItemClass, nwItemType, nwItemLayout
|
41
40
|
from novelwriter.error import logException
|
42
41
|
from novelwriter.common import checkInt, isHandle, isItemClass, isTitleTag, jsonEncode
|
43
|
-
from novelwriter.constants import nwFiles, nwKeyWords,
|
42
|
+
from novelwriter.constants import nwFiles, nwKeyWords, nwHeaders
|
43
|
+
from novelwriter.text.counting import standardCounter
|
44
44
|
|
45
45
|
if TYPE_CHECKING: # pragma: no cover
|
46
46
|
from novelwriter.core.item import NWItem
|
@@ -122,9 +122,8 @@ class NWIndex:
|
|
122
122
|
self.clearIndex()
|
123
123
|
for nwItem in self._project.tree:
|
124
124
|
if nwItem.isFileType():
|
125
|
-
|
126
|
-
|
127
|
-
self.scanText(tHandle, doc.readDocument() or "", blockSignal=True)
|
125
|
+
text = self._project.storage.getDocumentText(nwItem.itemHandle)
|
126
|
+
self.scanText(nwItem.itemHandle, text, blockSignal=True)
|
128
127
|
self._indexBroken = False
|
129
128
|
SHARED.indexSignalProxy({"event": "buildIndex"})
|
130
129
|
return
|
@@ -142,17 +141,15 @@ class NWIndex:
|
|
142
141
|
})
|
143
142
|
return
|
144
143
|
|
145
|
-
def reIndexHandle(self, tHandle: str | None) ->
|
144
|
+
def reIndexHandle(self, tHandle: str | None) -> None:
|
146
145
|
"""Put a file back into the index. This is used when files are
|
147
146
|
moved from the archive or trash folders back into the active
|
148
147
|
project.
|
149
148
|
"""
|
150
149
|
if tHandle and self._project.tree.checkType(tHandle, nwItemType.FILE):
|
151
150
|
logger.debug("Re-indexing item '%s'", tHandle)
|
152
|
-
|
153
|
-
|
154
|
-
return True
|
155
|
-
return False
|
151
|
+
self.scanText(tHandle, self._project.storage.getDocumentText(tHandle))
|
152
|
+
return
|
156
153
|
|
157
154
|
def indexChangedSince(self, checkTime: int | float) -> bool:
|
158
155
|
"""Check if the index has changed since a given time."""
|
@@ -266,7 +263,7 @@ class NWIndex:
|
|
266
263
|
self._itemIndex.add(tHandle, tItem)
|
267
264
|
|
268
265
|
# Run word counter for the whole text
|
269
|
-
cC, wC, pC =
|
266
|
+
cC, wC, pC = standardCounter(text)
|
270
267
|
tItem.setCharCount(cC)
|
271
268
|
tItem.setWordCount(wC)
|
272
269
|
tItem.setParaCount(pC)
|
@@ -307,7 +304,7 @@ class NWIndex:
|
|
307
304
|
nTitle = 0 # Line Number of the previous title
|
308
305
|
cTitle = TT_NONE # Tag of the current title
|
309
306
|
pTitle = TT_NONE # Tag of the previous title
|
310
|
-
|
307
|
+
canSetHead = True # First heading has not yet been set
|
311
308
|
|
312
309
|
lines = text.splitlines()
|
313
310
|
for n, line in enumerate(lines, start=1):
|
@@ -320,9 +317,9 @@ class NWIndex:
|
|
320
317
|
if hDepth == "H0":
|
321
318
|
continue
|
322
319
|
|
323
|
-
if
|
320
|
+
if canSetHead:
|
324
321
|
nwItem.setMainHeading(hDepth)
|
325
|
-
|
322
|
+
canSetHead = False
|
326
323
|
|
327
324
|
cTitle = self._itemIndex.addItemHeading(tHandle, n, hDepth, hText)
|
328
325
|
if cTitle != TT_NONE:
|
@@ -383,7 +380,7 @@ class NWIndex:
|
|
383
380
|
return
|
384
381
|
|
385
382
|
def _splitHeading(self, line: str) -> tuple[str, str]:
|
386
|
-
"""Split a heading into its
|
383
|
+
"""Split a heading into its heading level and text value."""
|
387
384
|
if line.startswith("# "):
|
388
385
|
return "H1", line[2:].strip()
|
389
386
|
elif line.startswith("## "):
|
@@ -396,11 +393,13 @@ class NWIndex:
|
|
396
393
|
return "H1", line[3:].strip()
|
397
394
|
elif line.startswith("##! "):
|
398
395
|
return "H2", line[4:].strip()
|
396
|
+
elif line.startswith("###! "):
|
397
|
+
return "H3", line[5:].strip()
|
399
398
|
return "H0", ""
|
400
399
|
|
401
400
|
def _indexWordCounts(self, tHandle: str, text: str, sTitle: str) -> None:
|
402
401
|
"""Count text stats and save the counts to the index."""
|
403
|
-
cC, wC, pC =
|
402
|
+
cC, wC, pC = standardCounter(text)
|
404
403
|
self._itemIndex.setHeadingCounts(tHandle, sTitle, cC, wC, pC)
|
405
404
|
return
|
406
405
|
|
@@ -515,16 +514,21 @@ class NWIndex:
|
|
515
514
|
"""Get the index data for a given item."""
|
516
515
|
return self._itemIndex[tHandle]
|
517
516
|
|
518
|
-
def
|
519
|
-
"""Get the
|
520
|
-
tItem
|
521
|
-
if isinstance(tItem, IndexItem):
|
517
|
+
def getItemHeading(self, tHandle: str, sTitle: str) -> IndexHeading | None:
|
518
|
+
"""Get the heading entry for a specific item and heading."""
|
519
|
+
if tItem := self._itemIndex[tHandle]:
|
522
520
|
return tItem[sTitle]
|
523
521
|
return None
|
524
522
|
|
523
|
+
def iterItemHeadings(self, tHandle: str) -> Iterable[tuple[str, IndexHeading]]:
|
524
|
+
"""Get all headings for a specific item."""
|
525
|
+
if tItem := self._itemIndex[tHandle]:
|
526
|
+
yield from tItem.items()
|
527
|
+
return []
|
528
|
+
|
525
529
|
def novelStructure(
|
526
530
|
self, rootHandle: str | None = None, activeOnly: bool = True
|
527
|
-
) ->
|
531
|
+
) -> Iterable[tuple[str, str, str, IndexHeading]]:
|
528
532
|
"""Iterate over all titles in the novel, in the correct order as
|
529
533
|
they appear in the tree view and in the respective document
|
530
534
|
files, but skipping all note files.
|
@@ -666,7 +670,7 @@ class NWIndex:
|
|
666
670
|
|
667
671
|
def getTagsData(
|
668
672
|
self, activeOnly: bool = True
|
669
|
-
) ->
|
673
|
+
) -> Iterable[tuple[str, str, str, IndexItem | None, IndexHeading | None]]:
|
670
674
|
"""Return all known tags."""
|
671
675
|
for tag, data in self._tagsIndex.items():
|
672
676
|
iItem = self._itemIndex[data.get("handle")]
|
@@ -825,7 +829,7 @@ class ItemIndex:
|
|
825
829
|
class around a single storage dictionary with a set of utility
|
826
830
|
functions for setting and accessing the index data. Each indexed
|
827
831
|
item is stored in an IndexItem object, which again holds an
|
828
|
-
IndexHeading object for each
|
832
|
+
IndexHeading object for each heading of the text.
|
829
833
|
"""
|
830
834
|
|
831
835
|
__slots__ = ("_project", "_items")
|
@@ -898,13 +902,11 @@ class ItemIndex:
|
|
898
902
|
|
899
903
|
if rHandle is None:
|
900
904
|
for sTitle in self._items[tHandle].headings():
|
901
|
-
hItem
|
902
|
-
if hItem:
|
905
|
+
if hItem := self._items[tHandle][sTitle]:
|
903
906
|
yield tHandle, sTitle, hItem
|
904
907
|
elif tItem.itemRoot == rHandle:
|
905
908
|
for sTitle in self._items[tHandle].headings():
|
906
|
-
hItem
|
907
|
-
if hItem:
|
909
|
+
if hItem := self._items[tHandle][sTitle]:
|
908
910
|
yield tHandle, sTitle, hItem
|
909
911
|
|
910
912
|
return
|
@@ -1200,7 +1202,7 @@ class IndexHeading:
|
|
1200
1202
|
##
|
1201
1203
|
|
1202
1204
|
def setLevel(self, level: str) -> None:
|
1203
|
-
"""Set the level of the
|
1205
|
+
"""Set the level of the heading if it's a valid value."""
|
1204
1206
|
if level in nwHeaders.H_VALID:
|
1205
1207
|
self._level = level
|
1206
1208
|
return
|
@@ -1315,86 +1317,3 @@ def processComment(text: str) -> tuple[nwComment, str, int]:
|
|
1315
1317
|
if content and (clean := classifier.strip().lower()) in CLASSIFIERS:
|
1316
1318
|
return CLASSIFIERS[clean], content.strip(), text.find(":") + 1
|
1317
1319
|
return nwComment.PLAIN, check, 0
|
1318
|
-
|
1319
|
-
|
1320
|
-
def countWords(text: str) -> tuple[int, int, int]:
|
1321
|
-
"""Count words in a piece of text, skipping special syntax and
|
1322
|
-
comments.
|
1323
|
-
"""
|
1324
|
-
charCount = 0
|
1325
|
-
wordCount = 0
|
1326
|
-
paraCount = 0
|
1327
|
-
prevEmpty = True
|
1328
|
-
|
1329
|
-
if not isinstance(text, str):
|
1330
|
-
return charCount, wordCount, paraCount
|
1331
|
-
|
1332
|
-
# We need to treat dashes as word separators for counting words.
|
1333
|
-
# The check+replace approach is much faster than direct replace for
|
1334
|
-
# large texts, and a bit slower for small texts, but in the latter
|
1335
|
-
# case it doesn't really matter.
|
1336
|
-
if nwUnicode.U_ENDASH in text:
|
1337
|
-
text = text.replace(nwUnicode.U_ENDASH, " ")
|
1338
|
-
if nwUnicode.U_EMDASH in text:
|
1339
|
-
text = text.replace(nwUnicode.U_EMDASH, " ")
|
1340
|
-
|
1341
|
-
# Strip shortcodes
|
1342
|
-
if "[" in text:
|
1343
|
-
text = nwRegEx.RX_SC.sub("", text)
|
1344
|
-
|
1345
|
-
for line in text.splitlines():
|
1346
|
-
|
1347
|
-
countPara = True
|
1348
|
-
|
1349
|
-
if not line:
|
1350
|
-
prevEmpty = True
|
1351
|
-
continue
|
1352
|
-
|
1353
|
-
if line[0] == "@" or line[0] == "%":
|
1354
|
-
continue
|
1355
|
-
|
1356
|
-
if line[0] == "[":
|
1357
|
-
check = line.lower()
|
1358
|
-
if check.startswith(("[newpage]", "[new page]", "[vspace]")):
|
1359
|
-
continue
|
1360
|
-
elif check.startswith("[vspace:") and line.endswith("]"):
|
1361
|
-
continue
|
1362
|
-
|
1363
|
-
elif line[0] == "#":
|
1364
|
-
if line[:5] == "#### ":
|
1365
|
-
line = line[5:]
|
1366
|
-
countPara = False
|
1367
|
-
elif line[:4] == "### ":
|
1368
|
-
line = line[4:]
|
1369
|
-
countPara = False
|
1370
|
-
elif line[:3] == "## ":
|
1371
|
-
line = line[3:]
|
1372
|
-
countPara = False
|
1373
|
-
elif line[:2] == "# ":
|
1374
|
-
line = line[2:]
|
1375
|
-
countPara = False
|
1376
|
-
elif line[:3] == "#! ":
|
1377
|
-
line = line[3:]
|
1378
|
-
countPara = False
|
1379
|
-
elif line[:4] == "##! ":
|
1380
|
-
line = line[4:]
|
1381
|
-
countPara = False
|
1382
|
-
|
1383
|
-
elif line[0] == ">" or line[-1] == "<":
|
1384
|
-
if line[:2] == ">>":
|
1385
|
-
line = line[2:].lstrip(" ")
|
1386
|
-
elif line[:1] == ">":
|
1387
|
-
line = line[1:].lstrip(" ")
|
1388
|
-
if line[-2:] == "<<":
|
1389
|
-
line = line[:-2].rstrip(" ")
|
1390
|
-
elif line[-1:] == "<":
|
1391
|
-
line = line[:-1].rstrip(" ")
|
1392
|
-
|
1393
|
-
wordCount += len(line.split())
|
1394
|
-
charCount += len(line)
|
1395
|
-
if countPara and prevEmpty:
|
1396
|
-
paraCount += 1
|
1397
|
-
|
1398
|
-
prevEmpty = not countPara
|
1399
|
-
|
1400
|
-
return charCount, wordCount, paraCount
|
novelwriter/core/project.py
CHANGED
@@ -31,7 +31,7 @@ from time import time
|
|
31
31
|
from typing import TYPE_CHECKING
|
32
32
|
from pathlib import Path
|
33
33
|
from functools import partial
|
34
|
-
from collections.abc import
|
34
|
+
from collections.abc import Iterable
|
35
35
|
|
36
36
|
from PyQt5.QtCore import QCoreApplication
|
37
37
|
|
@@ -175,12 +175,10 @@ class NWProject:
|
|
175
175
|
"""Write content to a new document after it is created. This
|
176
176
|
will not run if the file exists and is not empty.
|
177
177
|
"""
|
178
|
-
tItem
|
179
|
-
if not (tItem and tItem.isFileType()):
|
178
|
+
if not ((tItem := self._tree[tHandle]) and tItem.isFileType()):
|
180
179
|
return False
|
181
180
|
|
182
|
-
|
183
|
-
if (newDoc.readDocument() or "").strip():
|
181
|
+
if self._storage.getDocumentText(tHandle).strip():
|
184
182
|
return False
|
185
183
|
|
186
184
|
indent = "#"*minmax(hLevel, 1, 4)
|
@@ -191,7 +189,7 @@ class NWProject:
|
|
191
189
|
else:
|
192
190
|
tItem.setLayout(nwItemLayout.NOTE)
|
193
191
|
|
194
|
-
|
192
|
+
self._storage.getDocument(tHandle).writeDocument(text)
|
195
193
|
self._index.scanText(tHandle, text)
|
196
194
|
|
197
195
|
return True
|
@@ -200,21 +198,18 @@ class NWProject:
|
|
200
198
|
"""Copy content to a new document after it is created. This
|
201
199
|
will not run if the file exists and is not empty.
|
202
200
|
"""
|
203
|
-
tItem
|
204
|
-
if not (tItem and tItem.isFileType()):
|
201
|
+
if not ((tItem := self._tree[tHandle]) and tItem.isFileType()):
|
205
202
|
return False
|
206
203
|
|
207
|
-
sItem
|
208
|
-
if not (sItem and sItem.isFileType()):
|
204
|
+
if not ((sItem := self._tree[sHandle]) and sItem.isFileType()):
|
209
205
|
return False
|
210
206
|
|
211
|
-
|
212
|
-
if (newDoc.readDocument() or "").strip():
|
207
|
+
if self._storage.getDocumentText(tHandle).strip():
|
213
208
|
return False
|
214
209
|
|
215
210
|
logger.debug("Populating '%s' with text from '%s'", tHandle, sHandle)
|
216
|
-
text = self._storage.
|
217
|
-
|
211
|
+
text = self._storage.getDocumentText(sHandle)
|
212
|
+
self._storage.getDocument(tHandle).writeDocument(text)
|
218
213
|
sItem.setLayout(tItem.itemLayout)
|
219
214
|
self._index.scanText(tHandle, text)
|
220
215
|
|
@@ -517,7 +512,7 @@ class NWProject:
|
|
517
512
|
# Class Methods
|
518
513
|
##
|
519
514
|
|
520
|
-
def iterProjectItems(self) ->
|
515
|
+
def iterProjectItems(self) -> Iterable[NWItem]:
|
521
516
|
"""This function ensures that the item tree loaded is sent to
|
522
517
|
the GUI tree view in such a way that the tree can be built. That
|
523
518
|
is, the parent item must be sent before its child. In principle,
|
novelwriter/core/sessions.py
CHANGED
@@ -29,7 +29,7 @@ import logging
|
|
29
29
|
from time import time
|
30
30
|
from typing import TYPE_CHECKING
|
31
31
|
from pathlib import Path
|
32
|
-
from collections.abc import
|
32
|
+
from collections.abc import Iterable
|
33
33
|
|
34
34
|
from novelwriter.error import logException
|
35
35
|
from novelwriter.common import formatTimeStamp
|
@@ -110,7 +110,7 @@ class NWSessionLog:
|
|
110
110
|
|
111
111
|
return True
|
112
112
|
|
113
|
-
def iterRecords(self) ->
|
113
|
+
def iterRecords(self) -> Iterable[dict]:
|
114
114
|
"""Iterate through all records in the log."""
|
115
115
|
sessFile = self._project.storage.getMetaFile(nwFiles.SESS_FILE)
|
116
116
|
if isinstance(sessFile, Path) and sessFile.is_file():
|
novelwriter/core/status.py
CHANGED
@@ -28,10 +28,10 @@ import random
|
|
28
28
|
import logging
|
29
29
|
|
30
30
|
from typing import TYPE_CHECKING, Literal
|
31
|
-
from collections.abc import ItemsView, Iterator, KeysView, ValuesView
|
31
|
+
from collections.abc import ItemsView, Iterable, Iterator, KeysView, ValuesView
|
32
32
|
|
33
33
|
from PyQt5.QtGui import QIcon, QPainter, QPainterPath, QPixmap, QColor
|
34
|
-
from PyQt5.QtCore import QRectF
|
34
|
+
from PyQt5.QtCore import QRectF
|
35
35
|
|
36
36
|
from novelwriter import CONFIG
|
37
37
|
from novelwriter.common import minmax, simplified
|
@@ -193,7 +193,7 @@ class NWStatus:
|
|
193
193
|
self._store[key]["count"] += 1
|
194
194
|
return
|
195
195
|
|
196
|
-
def pack(self) ->
|
196
|
+
def pack(self) -> Iterable[tuple[str, dict]]:
|
197
197
|
"""Pack the status entries into a dictionary."""
|
198
198
|
for key, data in self._store.items():
|
199
199
|
yield (data["name"], {
|
@@ -248,7 +248,7 @@ class NWStatus:
|
|
248
248
|
def _createIcon(self, red: int, green: int, blue: int) -> QIcon:
|
249
249
|
"""Generate an icon for a status label."""
|
250
250
|
pixmap = QPixmap(self._iPX, self._iPX)
|
251
|
-
pixmap.fill(
|
251
|
+
pixmap.fill(QColor(0, 0, 0, 0))
|
252
252
|
|
253
253
|
painter = QPainter(pixmap)
|
254
254
|
painter.setRenderHint(QPainter.Antialiasing)
|
novelwriter/core/storage.py
CHANGED
@@ -27,18 +27,18 @@ import json
|
|
27
27
|
import logging
|
28
28
|
|
29
29
|
from enum import Enum
|
30
|
+
from pathlib import Path
|
30
31
|
from time import time
|
31
32
|
from typing import TYPE_CHECKING
|
32
|
-
from pathlib import Path
|
33
33
|
from zipfile import ZIP_DEFLATED, ZIP_STORED, ZipFile
|
34
34
|
|
35
35
|
from novelwriter import CONFIG
|
36
|
-
from novelwriter.error import logException
|
37
36
|
from novelwriter.common import isHandle, minmax
|
38
37
|
from novelwriter.constants import nwFiles
|
39
38
|
from novelwriter.core.document import NWDocument
|
40
39
|
from novelwriter.core.projectxml import ProjectXMLReader, ProjectXMLWriter
|
41
40
|
from novelwriter.core.spellcheck import UserDictionary
|
41
|
+
from novelwriter.error import logException
|
42
42
|
|
43
43
|
if TYPE_CHECKING: # pragma: no cover
|
44
44
|
from novelwriter.core.project import NWProject
|
@@ -287,6 +287,12 @@ class NWStorage:
|
|
287
287
|
return self._runtimePath / "meta" / fileName
|
288
288
|
return None
|
289
289
|
|
290
|
+
def getDocumentText(self, tHandle: str) -> str:
|
291
|
+
"""Return the text of a document in a fast and efficient way."""
|
292
|
+
if isinstance(self._runtimePath, Path):
|
293
|
+
return NWDocument.quickReadText(self._runtimePath / "content", tHandle)
|
294
|
+
return ""
|
295
|
+
|
290
296
|
def scanContent(self) -> list[str]:
|
291
297
|
"""Scan the content folder and return the handle of all files
|
292
298
|
found in it. Files that do not match the pattern are ignored.
|