novelWriter 2.5.3__py3-none-any.whl → 2.6b2__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.5.3.dist-info → novelWriter-2.6b2.dist-info}/METADATA +1 -1
- {novelWriter-2.5.3.dist-info → novelWriter-2.6b2.dist-info}/RECORD +80 -60
- novelwriter/__init__.py +49 -10
- novelwriter/assets/i18n/project_en_GB.json +1 -0
- novelwriter/assets/icons/typicons_dark/icons.conf +8 -0
- novelwriter/assets/icons/typicons_dark/mixed_copy.svg +4 -0
- novelwriter/assets/icons/typicons_dark/mixed_margin-bottom.svg +6 -0
- novelwriter/assets/icons/typicons_dark/mixed_margin-left.svg +6 -0
- novelwriter/assets/icons/typicons_dark/mixed_margin-right.svg +6 -0
- novelwriter/assets/icons/typicons_dark/mixed_margin-top.svg +6 -0
- novelwriter/assets/icons/typicons_dark/mixed_size-height.svg +6 -0
- novelwriter/assets/icons/typicons_dark/mixed_size-width.svg +6 -0
- novelwriter/assets/icons/typicons_dark/nw_toolbar.svg +5 -0
- novelwriter/assets/icons/typicons_light/icons.conf +8 -0
- novelwriter/assets/icons/typicons_light/mixed_copy.svg +4 -0
- novelwriter/assets/icons/typicons_light/mixed_margin-bottom.svg +6 -0
- novelwriter/assets/icons/typicons_light/mixed_margin-left.svg +6 -0
- novelwriter/assets/icons/typicons_light/mixed_margin-right.svg +6 -0
- novelwriter/assets/icons/typicons_light/mixed_margin-top.svg +6 -0
- novelwriter/assets/icons/typicons_light/mixed_size-height.svg +6 -0
- novelwriter/assets/icons/typicons_light/mixed_size-width.svg +6 -0
- novelwriter/assets/icons/typicons_light/nw_toolbar.svg +5 -0
- novelwriter/assets/manual.pdf +0 -0
- novelwriter/assets/sample.zip +0 -0
- novelwriter/common.py +100 -2
- novelwriter/config.py +25 -15
- novelwriter/constants.py +168 -60
- novelwriter/core/buildsettings.py +66 -39
- novelwriter/core/coretools.py +145 -147
- novelwriter/core/docbuild.py +132 -170
- novelwriter/core/index.py +38 -37
- novelwriter/core/item.py +41 -8
- novelwriter/core/itemmodel.py +518 -0
- novelwriter/core/options.py +4 -1
- novelwriter/core/project.py +67 -89
- novelwriter/core/spellcheck.py +9 -14
- novelwriter/core/status.py +7 -5
- novelwriter/core/tree.py +268 -287
- novelwriter/dialogs/docmerge.py +7 -17
- novelwriter/dialogs/preferences.py +46 -33
- novelwriter/dialogs/projectsettings.py +5 -5
- novelwriter/enum.py +36 -23
- novelwriter/extensions/configlayout.py +27 -12
- novelwriter/extensions/modified.py +13 -1
- novelwriter/extensions/pagedsidebar.py +5 -5
- novelwriter/formats/shared.py +155 -0
- novelwriter/formats/todocx.py +1191 -0
- novelwriter/formats/tohtml.py +451 -0
- novelwriter/{core → formats}/tokenizer.py +487 -491
- novelwriter/formats/tomarkdown.py +217 -0
- novelwriter/{core → formats}/toodt.py +311 -432
- novelwriter/formats/toqdoc.py +484 -0
- novelwriter/formats/toraw.py +91 -0
- novelwriter/gui/doceditor.py +342 -284
- novelwriter/gui/dochighlight.py +96 -84
- novelwriter/gui/docviewer.py +88 -31
- novelwriter/gui/docviewerpanel.py +17 -25
- novelwriter/gui/editordocument.py +17 -2
- novelwriter/gui/itemdetails.py +25 -28
- novelwriter/gui/mainmenu.py +129 -63
- novelwriter/gui/noveltree.py +45 -47
- novelwriter/gui/outline.py +196 -249
- novelwriter/gui/projtree.py +594 -1241
- novelwriter/gui/search.py +9 -10
- novelwriter/gui/sidebar.py +7 -6
- novelwriter/gui/theme.py +10 -5
- novelwriter/guimain.py +100 -196
- novelwriter/shared.py +66 -27
- novelwriter/text/counting.py +2 -0
- novelwriter/text/patterns.py +168 -60
- novelwriter/tools/manusbuild.py +14 -12
- novelwriter/tools/manuscript.py +120 -78
- novelwriter/tools/manussettings.py +424 -291
- novelwriter/tools/welcome.py +4 -4
- novelwriter/tools/writingstats.py +3 -3
- novelwriter/types.py +23 -7
- novelwriter/core/tohtml.py +0 -530
- novelwriter/core/tomarkdown.py +0 -252
- novelwriter/core/toqdoc.py +0 -419
- {novelWriter-2.5.3.dist-info → novelWriter-2.6b2.dist-info}/LICENSE.md +0 -0
- {novelWriter-2.5.3.dist-info → novelWriter-2.6b2.dist-info}/WHEEL +0 -0
- {novelWriter-2.5.3.dist-info → novelWriter-2.6b2.dist-info}/entry_points.txt +0 -0
- {novelWriter-2.5.3.dist-info → novelWriter-2.6b2.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,451 @@
|
|
1
|
+
"""
|
2
|
+
novelWriter – HTML Text Converter
|
3
|
+
=================================
|
4
|
+
|
5
|
+
File History:
|
6
|
+
Created: 2019-05-07 [0.0.1] ToHtml
|
7
|
+
|
8
|
+
This file is a part of novelWriter
|
9
|
+
Copyright 2018–2024, Veronica Berglyd Olsen
|
10
|
+
|
11
|
+
This program is free software: you can redistribute it and/or modify
|
12
|
+
it under the terms of the GNU General Public License as published by
|
13
|
+
the Free Software Foundation, either version 3 of the License, or
|
14
|
+
(at your option) any later version.
|
15
|
+
|
16
|
+
This program is distributed in the hope that it will be useful, but
|
17
|
+
WITHOUT ANY WARRANTY; without even the implied warranty of
|
18
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
19
|
+
General Public License for more details.
|
20
|
+
|
21
|
+
You should have received a copy of the GNU General Public License
|
22
|
+
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
23
|
+
"""
|
24
|
+
from __future__ import annotations
|
25
|
+
|
26
|
+
import json
|
27
|
+
import logging
|
28
|
+
|
29
|
+
from pathlib import Path
|
30
|
+
from time import time
|
31
|
+
|
32
|
+
from novelwriter.common import formatTimeStamp
|
33
|
+
from novelwriter.constants import nwHtmlUnicode
|
34
|
+
from novelwriter.core.project import NWProject
|
35
|
+
from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt, stripEscape
|
36
|
+
from novelwriter.formats.tokenizer import Tokenizer
|
37
|
+
from novelwriter.types import FONT_STYLE, FONT_WEIGHTS, QtHexRgb
|
38
|
+
|
39
|
+
logger = logging.getLogger(__name__)
|
40
|
+
|
41
|
+
# Each opener tag, with the id of its corresponding closer and tag format
|
42
|
+
HTML_OPENER: dict[int, tuple[int, str]] = {
|
43
|
+
TextFmt.B_B: (TextFmt.B_E, "<strong>"),
|
44
|
+
TextFmt.I_B: (TextFmt.I_E, "<em>"),
|
45
|
+
TextFmt.D_B: (TextFmt.D_E, "<del>"),
|
46
|
+
TextFmt.U_B: (TextFmt.U_E, "<span style='text-decoration: underline;'>"),
|
47
|
+
TextFmt.M_B: (TextFmt.M_E, "<mark>"),
|
48
|
+
TextFmt.SUP_B: (TextFmt.SUP_E, "<sup>"),
|
49
|
+
TextFmt.SUB_B: (TextFmt.SUB_E, "<sub>"),
|
50
|
+
TextFmt.COL_B: (TextFmt.COL_E, "<span style='color: {0}'>"),
|
51
|
+
TextFmt.ANM_B: (TextFmt.ANM_E, "<a name='{0}'>"),
|
52
|
+
TextFmt.ARF_B: (TextFmt.ARF_E, "<a href='{0}'>"),
|
53
|
+
TextFmt.HRF_B: (TextFmt.HRF_E, "<a href='{0}'>"),
|
54
|
+
}
|
55
|
+
|
56
|
+
# Each closer tag, with the id of its corresponding opener and tag format
|
57
|
+
HTML_CLOSER: dict[int, tuple[int, str]] = {
|
58
|
+
TextFmt.B_E: (TextFmt.B_B, "</strong>"),
|
59
|
+
TextFmt.I_E: (TextFmt.I_B, "</em>"),
|
60
|
+
TextFmt.D_E: (TextFmt.D_B, "</del>"),
|
61
|
+
TextFmt.U_E: (TextFmt.U_B, "</span>"),
|
62
|
+
TextFmt.M_E: (TextFmt.M_B, "</mark>"),
|
63
|
+
TextFmt.SUP_E: (TextFmt.SUP_B, "</sup>"),
|
64
|
+
TextFmt.SUB_E: (TextFmt.SUB_B, "</sub>"),
|
65
|
+
TextFmt.COL_E: (TextFmt.COL_B, "</span>"),
|
66
|
+
TextFmt.ANM_E: (TextFmt.ANM_B, "</a>"),
|
67
|
+
TextFmt.ARF_E: (TextFmt.ARF_B, "</a>"),
|
68
|
+
TextFmt.HRF_E: (TextFmt.HRF_B, "</a>"),
|
69
|
+
}
|
70
|
+
|
71
|
+
# Empty HTML tag record
|
72
|
+
HTML_NONE = (0, "")
|
73
|
+
|
74
|
+
|
75
|
+
class ToHtml(Tokenizer):
|
76
|
+
"""Core: HTML Document Writer
|
77
|
+
|
78
|
+
Extend the Tokenizer class to writer HTML output. This class is
|
79
|
+
also used by the Document Viewer, and Manuscript Build Preview.
|
80
|
+
"""
|
81
|
+
|
82
|
+
def __init__(self, project: NWProject) -> None:
|
83
|
+
super().__init__(project)
|
84
|
+
self._trMap = {}
|
85
|
+
self._cssStyles = True
|
86
|
+
self._usedNotes: dict[str, int] = {}
|
87
|
+
self._usedFields: list[tuple[int, str]] = []
|
88
|
+
self.setReplaceUnicode(False)
|
89
|
+
return
|
90
|
+
|
91
|
+
##
|
92
|
+
# Setters
|
93
|
+
##
|
94
|
+
|
95
|
+
def setStyles(self, cssStyles: bool) -> None:
|
96
|
+
"""Enable or disable CSS styling. Some elements may still have
|
97
|
+
class tags.
|
98
|
+
"""
|
99
|
+
self._cssStyles = cssStyles
|
100
|
+
return
|
101
|
+
|
102
|
+
def setReplaceUnicode(self, doReplace: bool) -> None:
|
103
|
+
"""Set the translation map to either minimal or full unicode for
|
104
|
+
html entities replacement.
|
105
|
+
"""
|
106
|
+
# Control characters must always be replaced
|
107
|
+
# Angle brackets are replaced later as they are also used in
|
108
|
+
# formatting codes
|
109
|
+
self._trMap = str.maketrans({"&": "&"})
|
110
|
+
if doReplace:
|
111
|
+
# Extend to all relevant Unicode characters
|
112
|
+
self._trMap.update(str.maketrans(nwHtmlUnicode.U_TO_H))
|
113
|
+
return
|
114
|
+
|
115
|
+
##
|
116
|
+
# Class Methods
|
117
|
+
##
|
118
|
+
|
119
|
+
def getFullResultSize(self) -> int:
|
120
|
+
"""Return the size of the full HTML result."""
|
121
|
+
return sum(len(x) for x in self._pages)
|
122
|
+
|
123
|
+
def doPreProcessing(self) -> None:
|
124
|
+
"""Extend the auto-replace to also properly encode some unicode
|
125
|
+
characters into their respective HTML entities.
|
126
|
+
"""
|
127
|
+
super().doPreProcessing()
|
128
|
+
self._text = self._text.translate(self._trMap)
|
129
|
+
return
|
130
|
+
|
131
|
+
def doConvert(self) -> None:
|
132
|
+
"""Convert the list of text tokens into an HTML document."""
|
133
|
+
if self._isNovel:
|
134
|
+
# For story files, we bump the titles one level up
|
135
|
+
h1Cl = " class='title'"
|
136
|
+
h1 = "h1"
|
137
|
+
h2 = "h1"
|
138
|
+
h3 = "h2"
|
139
|
+
h4 = "h3"
|
140
|
+
else:
|
141
|
+
h1Cl = ""
|
142
|
+
h1 = "h1"
|
143
|
+
h2 = "h2"
|
144
|
+
h3 = "h3"
|
145
|
+
h4 = "h4"
|
146
|
+
|
147
|
+
lines = []
|
148
|
+
for tType, tMeta, tText, tFmt, tStyle in self._blocks:
|
149
|
+
|
150
|
+
# Replace < and > with HTML entities
|
151
|
+
if tFmt:
|
152
|
+
# If we have formatting, we must recompute the locations
|
153
|
+
cText = []
|
154
|
+
i = 0
|
155
|
+
for c in tText:
|
156
|
+
if c == "<":
|
157
|
+
cText.append("<")
|
158
|
+
tFmt = [(p + 3 if p > i else p, f, k) for p, f, k in tFmt]
|
159
|
+
i += 4
|
160
|
+
elif c == ">":
|
161
|
+
cText.append(">")
|
162
|
+
tFmt = [(p + 3 if p > i else p, f, k) for p, f, k in tFmt]
|
163
|
+
i += 4
|
164
|
+
else:
|
165
|
+
cText.append(c)
|
166
|
+
i += 1
|
167
|
+
tText = "".join(cText)
|
168
|
+
else:
|
169
|
+
# If we don't have formatting, we can do a plain replace
|
170
|
+
tText = tText.replace("<", "<").replace(">", ">")
|
171
|
+
|
172
|
+
# Styles
|
173
|
+
aStyle = []
|
174
|
+
if self._cssStyles:
|
175
|
+
if tStyle & BlockFmt.LEFT:
|
176
|
+
aStyle.append("text-align: left;")
|
177
|
+
elif tStyle & BlockFmt.RIGHT:
|
178
|
+
aStyle.append("text-align: right;")
|
179
|
+
elif tStyle & BlockFmt.CENTRE:
|
180
|
+
aStyle.append("text-align: center;")
|
181
|
+
elif tStyle & BlockFmt.JUSTIFY:
|
182
|
+
aStyle.append("text-align: justify;")
|
183
|
+
|
184
|
+
if tStyle & BlockFmt.PBB:
|
185
|
+
aStyle.append("page-break-before: always;")
|
186
|
+
if tStyle & BlockFmt.PBA:
|
187
|
+
aStyle.append("page-break-after: always;")
|
188
|
+
|
189
|
+
if tStyle & BlockFmt.Z_BTM:
|
190
|
+
aStyle.append("margin-bottom: 0;")
|
191
|
+
if tStyle & BlockFmt.Z_TOP:
|
192
|
+
aStyle.append("margin-top: 0;")
|
193
|
+
|
194
|
+
if tStyle & BlockFmt.IND_L:
|
195
|
+
aStyle.append(f"margin-left: {self._blockIndent:.2f}em;")
|
196
|
+
if tStyle & BlockFmt.IND_R:
|
197
|
+
aStyle.append(f"margin-right: {self._blockIndent:.2f}em;")
|
198
|
+
if tStyle & BlockFmt.IND_T:
|
199
|
+
aStyle.append(f"text-indent: {self._firstWidth:.2f}em;")
|
200
|
+
|
201
|
+
if aStyle:
|
202
|
+
stVals = " ".join(aStyle)
|
203
|
+
hStyle = f" style='{stVals}'"
|
204
|
+
else:
|
205
|
+
hStyle = ""
|
206
|
+
|
207
|
+
if self._linkHeadings and tMeta:
|
208
|
+
aNm = f"<a name='{tMeta}'></a>"
|
209
|
+
else:
|
210
|
+
aNm = ""
|
211
|
+
|
212
|
+
# Process Text Type
|
213
|
+
if tType == BlockTyp.TEXT:
|
214
|
+
lines.append(f"<p{hStyle}>{self._formatText(tText, tFmt)}</p>\n")
|
215
|
+
|
216
|
+
elif tType == BlockTyp.TITLE:
|
217
|
+
tHead = tText.replace("\n", "<br>")
|
218
|
+
lines.append(f"<h1 class='title'{hStyle}>{aNm}{tHead}</h1>\n")
|
219
|
+
|
220
|
+
elif tType == BlockTyp.HEAD1:
|
221
|
+
tHead = tText.replace("\n", "<br>")
|
222
|
+
lines.append(f"<{h1}{h1Cl}{hStyle}>{aNm}{tHead}</{h1}>\n")
|
223
|
+
|
224
|
+
elif tType == BlockTyp.HEAD2:
|
225
|
+
tHead = tText.replace("\n", "<br>")
|
226
|
+
lines.append(f"<{h2}{hStyle}>{aNm}{tHead}</{h2}>\n")
|
227
|
+
|
228
|
+
elif tType == BlockTyp.HEAD3:
|
229
|
+
tHead = tText.replace("\n", "<br>")
|
230
|
+
lines.append(f"<{h3}{hStyle}>{aNm}{tHead}</{h3}>\n")
|
231
|
+
|
232
|
+
elif tType == BlockTyp.HEAD4:
|
233
|
+
tHead = tText.replace("\n", "<br>")
|
234
|
+
lines.append(f"<{h4}{hStyle}>{aNm}{tHead}</{h4}>\n")
|
235
|
+
|
236
|
+
elif tType == BlockTyp.SEP:
|
237
|
+
lines.append(f"<p class='sep'{hStyle}>{tText}</p>\n")
|
238
|
+
|
239
|
+
elif tType == BlockTyp.SKIP:
|
240
|
+
lines.append(f"<p{hStyle}> </p>\n")
|
241
|
+
|
242
|
+
elif tType == BlockTyp.COMMENT:
|
243
|
+
lines.append(f"<p class='comment'{hStyle}>{self._formatText(tText, tFmt)}</p>\n")
|
244
|
+
|
245
|
+
elif tType == BlockTyp.KEYWORD:
|
246
|
+
tClass = f"meta meta-{tMeta}"
|
247
|
+
lines.append(f"<p class='{tClass}'{hStyle}>{self._formatText(tText, tFmt)}</p>\n")
|
248
|
+
|
249
|
+
self._pages.append("".join(lines))
|
250
|
+
|
251
|
+
return
|
252
|
+
|
253
|
+
def closeDocument(self) -> None:
|
254
|
+
"""Run close document tasks."""
|
255
|
+
# Replace fields if there are stats available
|
256
|
+
if self._usedFields and self._counts:
|
257
|
+
pages = len(self._pages)
|
258
|
+
for doc, field in self._usedFields:
|
259
|
+
if doc >= 0 and doc < pages and (value := self._counts.get(field)) is not None:
|
260
|
+
self._pages[doc] = self._pages[doc].replace(
|
261
|
+
f"{{{{{field}}}}}", self._formatInt(value)
|
262
|
+
)
|
263
|
+
|
264
|
+
# Add footnotes
|
265
|
+
if self._usedNotes:
|
266
|
+
footnotes = self._localLookup("Footnotes")
|
267
|
+
|
268
|
+
lines = []
|
269
|
+
lines.append(f"<h3>{footnotes}</h3>\n")
|
270
|
+
lines.append("<ol>\n")
|
271
|
+
for key, index in self._usedNotes.items():
|
272
|
+
if content := self._footnotes.get(key):
|
273
|
+
text = self._formatText(*content)
|
274
|
+
lines.append(f"<li id='footnote_{index}'><p>{text}</p></li>\n")
|
275
|
+
lines.append("</ol>\n")
|
276
|
+
|
277
|
+
self._pages.append("".join(lines))
|
278
|
+
|
279
|
+
return
|
280
|
+
|
281
|
+
def saveDocument(self, path: Path) -> None:
|
282
|
+
"""Save the data to an HTML file."""
|
283
|
+
if path.suffix.lower() == ".json":
|
284
|
+
ts = time()
|
285
|
+
data = {
|
286
|
+
"meta": {
|
287
|
+
"projectName": self._project.data.name,
|
288
|
+
"novelAuthor": self._project.data.author,
|
289
|
+
"buildTime": int(ts),
|
290
|
+
"buildTimeStr": formatTimeStamp(ts),
|
291
|
+
},
|
292
|
+
"text": {
|
293
|
+
"css": self.getStyleSheet(),
|
294
|
+
"html": [t.replace("\t", "	").rstrip().split("\n") for t in self._pages],
|
295
|
+
}
|
296
|
+
}
|
297
|
+
with open(path, mode="w", encoding="utf-8") as fObj:
|
298
|
+
json.dump(data, fObj, indent=2)
|
299
|
+
|
300
|
+
else:
|
301
|
+
with open(path, mode="w", encoding="utf-8") as fObj:
|
302
|
+
fObj.write((
|
303
|
+
"<!DOCTYPE html>\n"
|
304
|
+
"<html>\n"
|
305
|
+
"<head>\n"
|
306
|
+
"<meta charset='utf-8'>\n"
|
307
|
+
"<title>{title:s}</title>\n"
|
308
|
+
"<style>\n"
|
309
|
+
"{style:s}\n"
|
310
|
+
"</style>\n"
|
311
|
+
"</head>\n"
|
312
|
+
"<body>\n"
|
313
|
+
"<article>\n"
|
314
|
+
"{body:s}\n"
|
315
|
+
"</article>\n"
|
316
|
+
"</body>\n"
|
317
|
+
"</html>\n"
|
318
|
+
).format(
|
319
|
+
title=self._project.data.name,
|
320
|
+
style="\n".join(self.getStyleSheet()),
|
321
|
+
body=("".join(self._pages)).replace("\t", "	").rstrip(),
|
322
|
+
))
|
323
|
+
|
324
|
+
logger.info("Wrote file: %s", path)
|
325
|
+
|
326
|
+
return
|
327
|
+
|
328
|
+
def replaceTabs(self, nSpaces: int = 8, spaceChar: str = " ") -> None:
|
329
|
+
"""Replace tabs with spaces in the html."""
|
330
|
+
pages = []
|
331
|
+
tabSpace = spaceChar*nSpaces
|
332
|
+
for aLine in self._pages:
|
333
|
+
pages.append(aLine.replace("\t", tabSpace))
|
334
|
+
self._pages = pages
|
335
|
+
return
|
336
|
+
|
337
|
+
def getStyleSheet(self) -> list[str]:
|
338
|
+
"""Generate a stylesheet for the current settings."""
|
339
|
+
if not self._cssStyles:
|
340
|
+
return []
|
341
|
+
|
342
|
+
tColor = self._theme.text.name(QtHexRgb)
|
343
|
+
hColor = self._theme.head.name(QtHexRgb) if self._colorHeads else tColor
|
344
|
+
lColor = self._theme.head.name(QtHexRgb)
|
345
|
+
mColor = self._theme.highlight.name(QtHexRgb)
|
346
|
+
|
347
|
+
mtH0 = self._marginTitle[0]
|
348
|
+
mbH0 = self._marginTitle[1]
|
349
|
+
mtH1 = self._marginHead1[0]
|
350
|
+
mbH1 = self._marginHead1[1]
|
351
|
+
mtH2 = self._marginHead2[0]
|
352
|
+
mbH2 = self._marginHead2[1]
|
353
|
+
mtH3 = self._marginHead3[0]
|
354
|
+
mbH3 = self._marginHead3[1]
|
355
|
+
mtH4 = self._marginHead4[0]
|
356
|
+
mbH4 = self._marginHead4[1]
|
357
|
+
mtTT = self._marginText[0]
|
358
|
+
mbTT = self._marginText[1]
|
359
|
+
mtSP = self._marginSep[0]
|
360
|
+
mbSP = self._marginSep[1]
|
361
|
+
|
362
|
+
font = self._textFont
|
363
|
+
fFam = font.family()
|
364
|
+
fSz = font.pointSize()
|
365
|
+
fW = FONT_WEIGHTS.get(font.weight(), 400)
|
366
|
+
fS = FONT_STYLE.get(font.style(), "normal")
|
367
|
+
|
368
|
+
lHeight = round(100 * self._lineHeight)
|
369
|
+
|
370
|
+
styles = []
|
371
|
+
styles.append(
|
372
|
+
f"body {{color: {tColor}; font-family: '{fFam}'; font-size: {fSz}pt; "
|
373
|
+
f"font-weight: {fW}; font-style: {fS};}}"
|
374
|
+
)
|
375
|
+
styles.append(
|
376
|
+
f"p {{text-align: {self._defaultAlign}; line-height: {lHeight}%; "
|
377
|
+
f"margin-top: {mtTT:.2f}em; margin-bottom: {mbTT:.2f}em;}}"
|
378
|
+
)
|
379
|
+
styles.append(f"a {{color: {lColor};}}")
|
380
|
+
styles.append(f"mark {{background: {mColor};}}")
|
381
|
+
styles.append(f"h1, h2, h3, h4 {{color: {hColor}; page-break-after: avoid;}}")
|
382
|
+
styles.append(f"h1 {{margin-top: {mtH1:.2f}em; margin-bottom: {mbH1:.2f}em;}}")
|
383
|
+
styles.append(f"h2 {{margin-top: {mtH2:.2f}em; margin-bottom: {mbH2:.2f}em;}}")
|
384
|
+
styles.append(f"h3 {{margin-top: {mtH3:.2f}em; margin-bottom: {mbH3:.2f}em;}}")
|
385
|
+
styles.append(f"h4 {{margin-top: {mtH4:.2f}em; margin-bottom: {mbH4:.2f}em;}}")
|
386
|
+
styles.append(
|
387
|
+
f".title {{font-size: 2.5em; margin-top: {mtH0:.2f}em; margin-bottom: {mbH0:.2f}em;}}"
|
388
|
+
)
|
389
|
+
styles.append(
|
390
|
+
f".sep {{text-align: center; margin-top: {mtSP:.2f}em; margin-bottom: {mbSP:.2f}em;}}"
|
391
|
+
)
|
392
|
+
|
393
|
+
return styles
|
394
|
+
|
395
|
+
##
|
396
|
+
# Internal Functions
|
397
|
+
##
|
398
|
+
|
399
|
+
def _formatText(self, text: str, tFmt: T_Formats) -> str:
|
400
|
+
"""Apply formatting tags to text."""
|
401
|
+
temp = text
|
402
|
+
|
403
|
+
# Build a list of all html tags that need to be inserted in the text.
|
404
|
+
# This is done in the forward direction, and a tag is only opened if it
|
405
|
+
# isn't already open, and only closed if it has previously been opened.
|
406
|
+
tags: list[tuple[int, str]] = []
|
407
|
+
state = dict.fromkeys(HTML_OPENER, False)
|
408
|
+
for pos, fmt, data in tFmt:
|
409
|
+
if m := HTML_OPENER.get(fmt):
|
410
|
+
if not state.get(fmt, True):
|
411
|
+
if fmt == TextFmt.COL_B and (color := self._classes.get(data)):
|
412
|
+
tags.append((pos, m[1].format(color.name(QtHexRgb))))
|
413
|
+
elif fmt in (TextFmt.ANM_B, TextFmt.ARF_B, TextFmt.HRF_B):
|
414
|
+
tags.append((pos, m[1].format(data or "#")))
|
415
|
+
else:
|
416
|
+
tags.append((pos, m[1]))
|
417
|
+
state[fmt] = True
|
418
|
+
elif m := HTML_CLOSER.get(fmt):
|
419
|
+
if state.get(m[0], False):
|
420
|
+
tags.append((pos, m[1]))
|
421
|
+
state[m[0]] = False
|
422
|
+
elif fmt == TextFmt.FNOTE:
|
423
|
+
if data in self._footnotes:
|
424
|
+
index = len(self._usedNotes) + 1
|
425
|
+
self._usedNotes[data] = index
|
426
|
+
tags.append((pos, f"<sup><a href='#footnote_{index}'>{index}</a></sup>"))
|
427
|
+
else:
|
428
|
+
tags.append((pos, "<sup>ERR</sup>"))
|
429
|
+
elif fmt == TextFmt.FIELD:
|
430
|
+
if field := data.partition(":")[2]:
|
431
|
+
self._usedFields.append((len(self._pages), field))
|
432
|
+
tags.append((pos, f"{{{{{field}}}}}"))
|
433
|
+
|
434
|
+
# Check all format types and close any tag that is still open. This
|
435
|
+
# ensures that unclosed tags don't spill over to the next paragraph.
|
436
|
+
end = len(text)
|
437
|
+
for opener, active in state.items():
|
438
|
+
if active:
|
439
|
+
closer = HTML_OPENER.get(opener, HTML_NONE)[0]
|
440
|
+
tags.append((end, HTML_CLOSER.get(closer, HTML_NONE)[1]))
|
441
|
+
|
442
|
+
# Insert all tags at their correct position, starting from the back.
|
443
|
+
# The reverse order ensures that the positions are not shifted while we
|
444
|
+
# insert tags.
|
445
|
+
for pos, tag in reversed(tags):
|
446
|
+
temp = f"{temp[:pos]}{tag}{temp[pos:]}"
|
447
|
+
|
448
|
+
# Replace all line breaks with proper HTML break tags
|
449
|
+
temp = temp.replace("\n", "<br>")
|
450
|
+
|
451
|
+
return stripEscape(temp)
|