@fiduswriter/books 0.0.0-stage → 0.2.1
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.
- package/LICENSE +661 -0
- package/README.md +87 -2
- package/css/book.css +219 -0
- package/dist/exporter/bits/index.d.ts +42 -0
- package/dist/exporter/bits/index.d.ts.map +1 -0
- package/dist/exporter/bits/index.js +113 -0
- package/dist/exporter/bits/index.js.map +1 -0
- package/dist/exporter/bits/templates.d.ts +7 -0
- package/dist/exporter/bits/templates.d.ts.map +1 -0
- package/dist/exporter/bits/templates.js +78 -0
- package/dist/exporter/bits/templates.js.map +1 -0
- package/dist/exporter/docx/index.d.ts +33 -0
- package/dist/exporter/docx/index.d.ts.map +1 -0
- package/dist/exporter/docx/index.js +139 -0
- package/dist/exporter/docx/index.js.map +1 -0
- package/dist/exporter/docx/render.d.ts +19 -0
- package/dist/exporter/docx/render.d.ts.map +1 -0
- package/dist/exporter/docx/render.js +123 -0
- package/dist/exporter/docx/render.js.map +1 -0
- package/dist/exporter/epub/index.d.ts +70 -0
- package/dist/exporter/epub/index.d.ts.map +1 -0
- package/dist/exporter/epub/index.js +310 -0
- package/dist/exporter/epub/index.js.map +1 -0
- package/dist/exporter/epub/templates.d.ts +65 -0
- package/dist/exporter/epub/templates.d.ts.map +1 -0
- package/dist/exporter/epub/templates.js +224 -0
- package/dist/exporter/epub/templates.js.map +1 -0
- package/dist/exporter/epub/tools.d.ts +5 -0
- package/dist/exporter/epub/tools.d.ts.map +1 -0
- package/dist/exporter/epub/tools.js +59 -0
- package/dist/exporter/epub/tools.js.map +1 -0
- package/dist/exporter/html/index.d.ts +70 -0
- package/dist/exporter/html/index.d.ts.map +1 -0
- package/dist/exporter/html/index.js +320 -0
- package/dist/exporter/html/index.js.map +1 -0
- package/dist/exporter/html/templates.d.ts +40 -0
- package/dist/exporter/html/templates.d.ts.map +1 -0
- package/dist/exporter/html/templates.js +225 -0
- package/dist/exporter/html/templates.js.map +1 -0
- package/dist/exporter/html/tools.d.ts +2 -0
- package/dist/exporter/html/tools.d.ts.map +1 -0
- package/dist/exporter/html/tools.js +23 -0
- package/dist/exporter/html/tools.js.map +1 -0
- package/dist/exporter/latex/index.d.ts +36 -0
- package/dist/exporter/latex/index.d.ts.map +1 -0
- package/dist/exporter/latex/index.js +116 -0
- package/dist/exporter/latex/index.js.map +1 -0
- package/dist/exporter/latex/templates.d.ts +4 -0
- package/dist/exporter/latex/templates.d.ts.map +1 -0
- package/dist/exporter/latex/templates.js +26 -0
- package/dist/exporter/latex/templates.js.map +1 -0
- package/dist/exporter/native/index.d.ts +22 -0
- package/dist/exporter/native/index.d.ts.map +1 -0
- package/dist/exporter/native/index.js +141 -0
- package/dist/exporter/native/index.js.map +1 -0
- package/dist/exporter/odt/index.d.ts +33 -0
- package/dist/exporter/odt/index.d.ts.map +1 -0
- package/dist/exporter/odt/index.js +133 -0
- package/dist/exporter/odt/index.js.map +1 -0
- package/dist/exporter/odt/render.d.ts +21 -0
- package/dist/exporter/odt/render.d.ts.map +1 -0
- package/dist/exporter/odt/render.js +126 -0
- package/dist/exporter/odt/render.js.map +1 -0
- package/dist/exporter/pretty.d.ts +17 -0
- package/dist/exporter/pretty.d.ts.map +1 -0
- package/dist/exporter/pretty.js +55 -0
- package/dist/exporter/pretty.js.map +1 -0
- package/dist/exporter/print/index.d.ts +25 -0
- package/dist/exporter/print/index.d.ts.map +1 -0
- package/dist/exporter/print/index.js +46 -0
- package/dist/exporter/print/index.js.map +1 -0
- package/dist/exporter/print/templates.d.ts +2 -0
- package/dist/exporter/print/templates.d.ts.map +1 -0
- package/dist/exporter/print/templates.js +5 -0
- package/dist/exporter/print/templates.js.map +1 -0
- package/dist/exporter/tools.d.ts +31 -0
- package/dist/exporter/tools.d.ts.map +1 -0
- package/dist/exporter/tools.js +177 -0
- package/dist/exporter/tools.js.map +1 -0
- package/dist/i18n.d.ts +7 -0
- package/dist/i18n.d.ts.map +1 -0
- package/dist/i18n.js +918 -0
- package/dist/i18n.js.map +1 -0
- package/dist/ids.d.ts +19 -0
- package/dist/ids.d.ts.map +1 -0
- package/dist/ids.js +27 -0
- package/dist/ids.js.map +1 -0
- package/dist/importer/native/index.d.ts +45 -0
- package/dist/importer/native/index.d.ts.map +1 -0
- package/dist/importer/native/index.js +166 -0
- package/dist/importer/native/index.js.map +1 -0
- package/dist/importer/native/reader.d.ts +32 -0
- package/dist/importer/native/reader.d.ts.map +1 -0
- package/dist/importer/native/reader.js +170 -0
- package/dist/importer/native/reader.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/schema/index.d.ts +7 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +8 -0
- package/dist/schema/index.js.map +1 -0
- package/dist/types.d.ts +171 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/dist/ui/book_dialog/dialog.d.ts +161 -0
- package/dist/ui/book_dialog/dialog.d.ts.map +1 -0
- package/dist/ui/book_dialog/dialog.js +750 -0
- package/dist/ui/book_dialog/dialog.js.map +1 -0
- package/dist/ui/book_dialog/export.d.ts +95 -0
- package/dist/ui/book_dialog/export.d.ts.map +1 -0
- package/dist/ui/book_dialog/export.js +217 -0
- package/dist/ui/book_dialog/export.js.map +1 -0
- package/dist/ui/book_dialog/index.d.ts +38 -0
- package/dist/ui/book_dialog/index.d.ts.map +1 -0
- package/dist/ui/book_dialog/index.js +36 -0
- package/dist/ui/book_dialog/index.js.map +1 -0
- package/dist/ui/book_dialog/sanity_check.d.ts +22 -0
- package/dist/ui/book_dialog/sanity_check.d.ts.map +1 -0
- package/dist/ui/book_dialog/sanity_check.js +109 -0
- package/dist/ui/book_dialog/sanity_check.js.map +1 -0
- package/dist/ui/book_dialog/templates.d.ts +76 -0
- package/dist/ui/book_dialog/templates.d.ts.map +1 -0
- package/dist/ui/book_dialog/templates.js +542 -0
- package/dist/ui/book_dialog/templates.js.map +1 -0
- package/dist/ui/book_dialog/types.d.ts +258 -0
- package/dist/ui/book_dialog/types.d.ts.map +1 -0
- package/dist/ui/book_dialog/types.js +15 -0
- package/dist/ui/book_dialog/types.js.map +1 -0
- package/locale/ar/messages.json +49 -0
- package/locale/bg/messages.json +49 -0
- package/locale/cs/messages.json +49 -0
- package/locale/da/messages.json +49 -0
- package/locale/de/messages.json +49 -0
- package/locale/en/messages.json +49 -0
- package/locale/en_US/messages.json +49 -0
- package/locale/es/messages.json +49 -0
- package/locale/fr/messages.json +49 -0
- package/locale/it/messages.json +49 -0
- package/locale/ja/messages.json +49 -0
- package/locale/ko/messages.json +49 -0
- package/locale/nb/messages.json +49 -0
- package/locale/nl/messages.json +49 -0
- package/locale/pl/messages.json +49 -0
- package/locale/pt_BR/messages.json +49 -0
- package/locale/pt_PT/messages.json +49 -0
- package/locale/ru/messages.json +49 -0
- package/locale/sv/messages.json +49 -0
- package/locale/tr/messages.json +49 -0
- package/locale/zh_Hans/messages.json +49 -0
- package/logo.svg +10 -0
- package/package.json +150 -5
- package/src/exporter/bits/index.ts +184 -0
- package/src/exporter/bits/templates.ts +107 -0
- package/src/exporter/docx/index.ts +258 -0
- package/src/exporter/docx/render.ts +177 -0
- package/src/exporter/epub/index.ts +496 -0
- package/src/exporter/epub/templates.ts +403 -0
- package/src/exporter/epub/tools.ts +68 -0
- package/src/exporter/html/index.ts +479 -0
- package/src/exporter/html/templates.ts +362 -0
- package/src/exporter/html/tools.ts +26 -0
- package/src/exporter/latex/index.ts +190 -0
- package/src/exporter/latex/templates.ts +35 -0
- package/src/exporter/native/index.ts +238 -0
- package/src/exporter/odt/index.ts +230 -0
- package/src/exporter/odt/render.ts +181 -0
- package/src/exporter/pretty.ts +75 -0
- package/src/exporter/print/index.ts +76 -0
- package/src/exporter/print/templates.ts +8 -0
- package/src/exporter/tools.ts +270 -0
- package/src/global.d.ts +2 -0
- package/src/i18n.ts +936 -0
- package/src/ids.ts +27 -0
- package/src/importer/native/index.ts +269 -0
- package/src/importer/native/reader.ts +247 -0
- package/src/index.ts +20 -0
- package/src/modules.d.ts +7 -0
- package/src/schema/index.ts +8 -0
- package/src/types.ts +208 -0
- package/src/ui/book_dialog/dialog.ts +940 -0
- package/src/ui/book_dialog/export.ts +449 -0
- package/src/ui/book_dialog/index.ts +84 -0
- package/src/ui/book_dialog/sanity_check.ts +208 -0
- package/src/ui/book_dialog/templates.ts +736 -0
- package/src/ui/book_dialog/types.ts +283 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DOCX book exporter.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type {Schema} from "prosemirror-model"
|
|
6
|
+
import type {CSL, ExportMetadata, FidusNode, User} from "@fiduswriter/document"
|
|
7
|
+
import type {ProgressCallback} from "@fiduswriter/document/exporter/tools/progress"
|
|
8
|
+
import {
|
|
9
|
+
fixTables,
|
|
10
|
+
removeHidden
|
|
11
|
+
} from "@fiduswriter/document/exporter/tools/doc_content"
|
|
12
|
+
import {createSlug} from "@fiduswriter/document/exporter/tools/file"
|
|
13
|
+
import {XmlZip} from "@fiduswriter/document/exporter/tools/xml_zip"
|
|
14
|
+
import {gettext} from "fwtoolkit"
|
|
15
|
+
|
|
16
|
+
import {DOCXExporterCitations} from "@fiduswriter/document/exporter/docx/citations"
|
|
17
|
+
import {DOCXExporterComments} from "@fiduswriter/document/exporter/docx/comments"
|
|
18
|
+
import {DOCXExporterFootnotes} from "@fiduswriter/document/exporter/docx/footnotes"
|
|
19
|
+
import {DOCXExporterImages} from "@fiduswriter/document/exporter/docx/images"
|
|
20
|
+
import {DOCXExporterLists} from "@fiduswriter/document/exporter/docx/lists"
|
|
21
|
+
import {DOCXExporterMath} from "@fiduswriter/document/exporter/docx/math"
|
|
22
|
+
import {DOCXExporterMetadata} from "@fiduswriter/document/exporter/docx/metadata"
|
|
23
|
+
import {DOCXExporterRels} from "@fiduswriter/document/exporter/docx/rels"
|
|
24
|
+
import {DOCXExporterRichtext} from "@fiduswriter/document/exporter/docx/richtext"
|
|
25
|
+
import {DOCXExporterTables} from "@fiduswriter/document/exporter/docx/tables"
|
|
26
|
+
import {moveFootnoteComments} from "@fiduswriter/document/exporter/docx/tools"
|
|
27
|
+
|
|
28
|
+
import type {Book, DocumentListEntry} from "../../types.js"
|
|
29
|
+
import {getMissingChapterData} from "../tools.js"
|
|
30
|
+
import {DOCXBookExporterRender} from "./render.js"
|
|
31
|
+
|
|
32
|
+
export class DOCXBookExporter {
|
|
33
|
+
schema: Schema
|
|
34
|
+
csl: CSL
|
|
35
|
+
book: Book
|
|
36
|
+
user: User
|
|
37
|
+
docList: DocumentListEntry[]
|
|
38
|
+
templateUrl: string
|
|
39
|
+
updated: number
|
|
40
|
+
textFiles: Array<Record<string, unknown>>
|
|
41
|
+
httpFiles: Array<Record<string, unknown>>
|
|
42
|
+
mimeType: string
|
|
43
|
+
progressCallback?: ProgressCallback
|
|
44
|
+
|
|
45
|
+
constructor(
|
|
46
|
+
schema: Schema,
|
|
47
|
+
csl: CSL,
|
|
48
|
+
book: Book,
|
|
49
|
+
user: User,
|
|
50
|
+
docList: DocumentListEntry[],
|
|
51
|
+
updated: number
|
|
52
|
+
) {
|
|
53
|
+
this.schema = schema
|
|
54
|
+
this.csl = csl
|
|
55
|
+
this.book = book
|
|
56
|
+
this.user = user
|
|
57
|
+
this.docList = docList
|
|
58
|
+
this.templateUrl = book.docx_template || ""
|
|
59
|
+
this.updated = updated
|
|
60
|
+
this.textFiles = []
|
|
61
|
+
this.httpFiles = []
|
|
62
|
+
|
|
63
|
+
this.mimeType =
|
|
64
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
init(progressCallback?: ProgressCallback): Promise<Blob> | false {
|
|
68
|
+
this.progressCallback = progressCallback
|
|
69
|
+
this.progressCallback?.(
|
|
70
|
+
gettext("DOCX book export has been initiated."),
|
|
71
|
+
0
|
|
72
|
+
)
|
|
73
|
+
if (this.book.chapters.length === 0) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
gettext("Book cannot be exported due to lack of chapters.")
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
return getMissingChapterData(
|
|
79
|
+
this.book,
|
|
80
|
+
this.docList,
|
|
81
|
+
this.schema,
|
|
82
|
+
{rawContent: true, progressCallback: this.progressCallback}
|
|
83
|
+
).then(() => this.export())
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export(): Promise<Blob> {
|
|
87
|
+
this.progressCallback?.(gettext("Preparing DOCX book..."), 10)
|
|
88
|
+
this.book.chapters.sort((a, b) => (a.number > b.number ? 1 : -1))
|
|
89
|
+
const xml = new XmlZip(this.templateUrl, this.mimeType)
|
|
90
|
+
|
|
91
|
+
const tables = new DOCXExporterTables(xml)
|
|
92
|
+
const math = new DOCXExporterMath(xml)
|
|
93
|
+
const render = new DOCXBookExporterRender(xml)
|
|
94
|
+
const rels = new DOCXExporterRels(xml, "document")
|
|
95
|
+
const metadata = new DOCXExporterMetadata(
|
|
96
|
+
xml,
|
|
97
|
+
this.getBaseMetadata(),
|
|
98
|
+
this.csl
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
return xml
|
|
102
|
+
.init()
|
|
103
|
+
.then(() => metadata.init())
|
|
104
|
+
.then(() => tables.init())
|
|
105
|
+
.then(() => math.init())
|
|
106
|
+
.then(() => render.init())
|
|
107
|
+
.then(() => rels.init())
|
|
108
|
+
.then(() => this.exportChapters(xml, render, rels, math, tables))
|
|
109
|
+
.then(() => {
|
|
110
|
+
this.progressCallback?.(gettext("Finalizing DOCX book..."), 90)
|
|
111
|
+
return xml.prepareBlob()
|
|
112
|
+
})
|
|
113
|
+
.then(blob => {
|
|
114
|
+
this.progressCallback?.(
|
|
115
|
+
gettext("DOCX book export complete."),
|
|
116
|
+
100
|
|
117
|
+
)
|
|
118
|
+
return this.download(blob)
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
exportChapters(
|
|
123
|
+
xml: XmlZip,
|
|
124
|
+
render: DOCXBookExporterRender,
|
|
125
|
+
rels: DOCXExporterRels,
|
|
126
|
+
math: DOCXExporterMath,
|
|
127
|
+
tables: DOCXExporterTables
|
|
128
|
+
): Promise<void> {
|
|
129
|
+
return this.book.chapters
|
|
130
|
+
.map((chapter, chapterIndex) => {
|
|
131
|
+
return (): Promise<void> => {
|
|
132
|
+
const doc = this.docList.find(d => d.id === chapter.text)
|
|
133
|
+
if (!doc) {
|
|
134
|
+
return Promise.resolve()
|
|
135
|
+
}
|
|
136
|
+
const docContent = moveFootnoteComments(
|
|
137
|
+
fixTables(
|
|
138
|
+
removeHidden(
|
|
139
|
+
doc.rawContent as unknown as FidusNode
|
|
140
|
+
) as FidusNode
|
|
141
|
+
)
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
const images = new DOCXExporterImages(
|
|
145
|
+
docContent,
|
|
146
|
+
{db: doc.images || {}},
|
|
147
|
+
xml,
|
|
148
|
+
rels
|
|
149
|
+
)
|
|
150
|
+
const lists = new DOCXExporterLists(docContent, xml, rels)
|
|
151
|
+
const citations = new DOCXExporterCitations(
|
|
152
|
+
docContent,
|
|
153
|
+
doc.settings,
|
|
154
|
+
{db: doc.bibliography || {}},
|
|
155
|
+
this.csl,
|
|
156
|
+
xml
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
const footnotes = new DOCXExporterFootnotes(
|
|
160
|
+
doc,
|
|
161
|
+
docContent,
|
|
162
|
+
doc.settings,
|
|
163
|
+
{db: doc.images || {}},
|
|
164
|
+
{db: doc.bibliography || {}},
|
|
165
|
+
xml,
|
|
166
|
+
citations,
|
|
167
|
+
this.csl,
|
|
168
|
+
lists,
|
|
169
|
+
math,
|
|
170
|
+
tables,
|
|
171
|
+
rels
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
const richtext = new DOCXExporterRichtext(
|
|
175
|
+
doc,
|
|
176
|
+
doc.settings,
|
|
177
|
+
lists,
|
|
178
|
+
footnotes,
|
|
179
|
+
math,
|
|
180
|
+
tables,
|
|
181
|
+
rels,
|
|
182
|
+
citations,
|
|
183
|
+
images
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
const comments = new DOCXExporterComments(
|
|
187
|
+
docContent,
|
|
188
|
+
doc.comments || {},
|
|
189
|
+
xml,
|
|
190
|
+
rels,
|
|
191
|
+
richtext
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
return citations
|
|
195
|
+
.init()
|
|
196
|
+
.then(() => images.init())
|
|
197
|
+
.then(() => comments.init())
|
|
198
|
+
.then(() => lists.init())
|
|
199
|
+
.then(() => footnotes.init())
|
|
200
|
+
.then(() => {
|
|
201
|
+
const pmBib = footnotes.pmBib || citations.pmBib
|
|
202
|
+
render.render(
|
|
203
|
+
docContent,
|
|
204
|
+
pmBib,
|
|
205
|
+
doc.settings,
|
|
206
|
+
richtext,
|
|
207
|
+
citations,
|
|
208
|
+
chapterIndex
|
|
209
|
+
)
|
|
210
|
+
return Promise.resolve()
|
|
211
|
+
})
|
|
212
|
+
}
|
|
213
|
+
})
|
|
214
|
+
.reduce(
|
|
215
|
+
(promiseChain, currentChapter) =>
|
|
216
|
+
promiseChain.then(() => currentChapter()),
|
|
217
|
+
Promise.resolve()
|
|
218
|
+
)
|
|
219
|
+
.then(() =>
|
|
220
|
+
render.renderAmbles(
|
|
221
|
+
Object.assign(
|
|
222
|
+
{
|
|
223
|
+
title: this.book.title,
|
|
224
|
+
language: this.book.settings.language
|
|
225
|
+
},
|
|
226
|
+
this.book.metadata
|
|
227
|
+
)
|
|
228
|
+
)
|
|
229
|
+
)
|
|
230
|
+
.then(() => render.assemble())
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
getBaseMetadata(): ExportMetadata {
|
|
234
|
+
const authors = this.book.metadata.author?.length
|
|
235
|
+
? [{institution: this.book.metadata.author}]
|
|
236
|
+
: []
|
|
237
|
+
const keywords = this.book.metadata.keywords?.length
|
|
238
|
+
? this.book.metadata.keywords
|
|
239
|
+
.split(",")
|
|
240
|
+
.map(keyword => keyword.trim())
|
|
241
|
+
: []
|
|
242
|
+
return {
|
|
243
|
+
authors,
|
|
244
|
+
contributors: [],
|
|
245
|
+
keywords,
|
|
246
|
+
title: this.book.title,
|
|
247
|
+
language: this.book.settings.language
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
download(blob: Blob): Blob {
|
|
252
|
+
return blob
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
get defaultFilename(): string {
|
|
256
|
+
return `${createSlug(this.book.title)}.docx`
|
|
257
|
+
}
|
|
258
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import {XMLElement} from "@fiduswriter/document/exporter/tools/xml"
|
|
2
|
+
import type {XmlZip} from "@fiduswriter/document/exporter/tools/xml_zip"
|
|
3
|
+
import {DOCXExporterCitations} from "@fiduswriter/document/exporter/docx/citations"
|
|
4
|
+
import {DOCXExporterRender} from "@fiduswriter/document/exporter/docx/render"
|
|
5
|
+
import {DOCXExporterRichtext} from "@fiduswriter/document/exporter/docx/richtext"
|
|
6
|
+
import type {DocSettings, FidusNode} from "@fiduswriter/document"
|
|
7
|
+
|
|
8
|
+
interface TagData {
|
|
9
|
+
title: string
|
|
10
|
+
content?: string | unknown[]
|
|
11
|
+
block: XMLElement
|
|
12
|
+
dimensions?: {width: number; height: number}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class DOCXBookExporterRender extends DOCXExporterRender {
|
|
16
|
+
preamble: XMLElement | null
|
|
17
|
+
bodyTemplate: XMLElement | null
|
|
18
|
+
postamble: XMLElement | null
|
|
19
|
+
fileXML: XMLElement | null
|
|
20
|
+
bodyParts: XMLElement[]
|
|
21
|
+
|
|
22
|
+
constructor(xml: XmlZip) {
|
|
23
|
+
super(xml)
|
|
24
|
+
|
|
25
|
+
this.preamble = null
|
|
26
|
+
this.bodyTemplate = null
|
|
27
|
+
this.postamble = null
|
|
28
|
+
this.fileXML = null
|
|
29
|
+
this.bodyParts = []
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
init(): Promise<void> {
|
|
33
|
+
return super.init().then(() => {
|
|
34
|
+
this.fileXML = this.text as XMLElement
|
|
35
|
+
const text = this.fileXML.query("w:body") as XMLElement
|
|
36
|
+
this.preamble = text.cloneNode(false)
|
|
37
|
+
this.bodyTemplate = text.cloneNode(false)
|
|
38
|
+
this.postamble = text.cloneNode(false)
|
|
39
|
+
let currentSection: XMLElement | null = this.bodyTemplate
|
|
40
|
+
const textChildren = Array.from(text.children).filter(
|
|
41
|
+
(node): node is XMLElement => node instanceof XMLElement
|
|
42
|
+
)
|
|
43
|
+
textChildren.forEach(node => {
|
|
44
|
+
const bookmarkStart = node.query("w:bookmarkStart")
|
|
45
|
+
if (bookmarkStart) {
|
|
46
|
+
const bookmarkName = String(
|
|
47
|
+
bookmarkStart.getAttribute("w:name")
|
|
48
|
+
).toLowerCase()
|
|
49
|
+
if (bookmarkName === "preamble") {
|
|
50
|
+
currentSection = this.preamble
|
|
51
|
+
} else if (bookmarkName === "body") {
|
|
52
|
+
currentSection = this.bodyTemplate
|
|
53
|
+
} else if (bookmarkName === "postamble") {
|
|
54
|
+
currentSection = this.postamble
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
currentSection!.appendChild(node)
|
|
58
|
+
})
|
|
59
|
+
return Promise.resolve()
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
render(
|
|
64
|
+
docContent: FidusNode,
|
|
65
|
+
pmBib: unknown,
|
|
66
|
+
settings: DocSettings,
|
|
67
|
+
richtext: DOCXExporterRichtext,
|
|
68
|
+
citations: DOCXExporterCitations,
|
|
69
|
+
chapterIndex = 0
|
|
70
|
+
): void {
|
|
71
|
+
this.text = this.bodyTemplate!.cloneNode(true)
|
|
72
|
+
const textEl = this.text as XMLElement
|
|
73
|
+
const bodyBookmark = textEl.query("w:bookmarkStart", {
|
|
74
|
+
"w:name": "body"
|
|
75
|
+
})
|
|
76
|
+
if (bodyBookmark) {
|
|
77
|
+
bodyBookmark.setAttribute("w:name", `chapter ${chapterIndex + 1}`)
|
|
78
|
+
}
|
|
79
|
+
super.render(
|
|
80
|
+
docContent,
|
|
81
|
+
pmBib as false | FidusNode,
|
|
82
|
+
settings,
|
|
83
|
+
richtext,
|
|
84
|
+
citations
|
|
85
|
+
)
|
|
86
|
+
this.bodyParts.push(this.text)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
renderAmbles({
|
|
90
|
+
title,
|
|
91
|
+
subtitle,
|
|
92
|
+
version,
|
|
93
|
+
publisher,
|
|
94
|
+
copyright,
|
|
95
|
+
author,
|
|
96
|
+
keywords,
|
|
97
|
+
language,
|
|
98
|
+
description,
|
|
99
|
+
isbn,
|
|
100
|
+
publication_date,
|
|
101
|
+
series_title,
|
|
102
|
+
series_position
|
|
103
|
+
}: Record<string, unknown>): void {
|
|
104
|
+
const tags = [
|
|
105
|
+
{title: "book.title", content: title},
|
|
106
|
+
{title: "book.subtitle", content: subtitle},
|
|
107
|
+
{title: "book.version", content: version},
|
|
108
|
+
{title: "book.publisher", content: publisher},
|
|
109
|
+
{title: "book.copyright", content: copyright},
|
|
110
|
+
{title: "book.author", content: author},
|
|
111
|
+
{title: "book.keywords", content: keywords},
|
|
112
|
+
{title: "book.language", content: language},
|
|
113
|
+
{title: "book.description", content: description},
|
|
114
|
+
{title: "book.isbn", content: isbn},
|
|
115
|
+
{title: "book.publication_date", content: publication_date},
|
|
116
|
+
{title: "book.series_title", content: series_title},
|
|
117
|
+
{title: "book.series_position", content: series_position}
|
|
118
|
+
]
|
|
119
|
+
const usedTags: Array<{
|
|
120
|
+
title: string
|
|
121
|
+
content?: string | unknown[]
|
|
122
|
+
block: XMLElement
|
|
123
|
+
}> = []
|
|
124
|
+
const ambles = [this.preamble, this.postamble].filter(
|
|
125
|
+
(amble): amble is XMLElement => amble !== null
|
|
126
|
+
)
|
|
127
|
+
ambles.forEach(amble => {
|
|
128
|
+
const blocks = amble.queryAll(["w:p", "w:sectPr"])
|
|
129
|
+
blocks.forEach(block => {
|
|
130
|
+
const text = block.textContent
|
|
131
|
+
tags.forEach(tag => {
|
|
132
|
+
const tagString = tag.title
|
|
133
|
+
if (text.includes(`{${tagString}}`)) {
|
|
134
|
+
usedTags.push(Object.assign({block}, tag) as TagData)
|
|
135
|
+
}
|
|
136
|
+
})
|
|
137
|
+
})
|
|
138
|
+
})
|
|
139
|
+
usedTags.forEach(tag => this.inlineRender(tag as unknown as TagData))
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
assemble(): void {
|
|
143
|
+
const text = this.fileXML!.query("w:body") as XMLElement
|
|
144
|
+
Array.from(this.preamble!.children)
|
|
145
|
+
.filter((node): node is XMLElement => node instanceof XMLElement)
|
|
146
|
+
.forEach(node => text.appendChild(node))
|
|
147
|
+
this.bodyParts.forEach((bodyPart, index) => {
|
|
148
|
+
const children = bodyPart.children
|
|
149
|
+
.slice()
|
|
150
|
+
.filter((node): node is XMLElement => node instanceof XMLElement)
|
|
151
|
+
children.forEach(node => {
|
|
152
|
+
text.appendChild(node)
|
|
153
|
+
})
|
|
154
|
+
if (index < this.bodyParts.length - 1) {
|
|
155
|
+
text.appendXML(
|
|
156
|
+
`<w:p>
|
|
157
|
+
<w:pPr>
|
|
158
|
+
<w:pStyle w:val="Normal"/>
|
|
159
|
+
<w:bidi w:val="0"/>
|
|
160
|
+
<w:jc w:val="start"/>
|
|
161
|
+
<w:rPr/>
|
|
162
|
+
</w:pPr>
|
|
163
|
+
<w:r>
|
|
164
|
+
<w:rPr/>
|
|
165
|
+
</w:r>
|
|
166
|
+
<w:r>
|
|
167
|
+
<w:br w:type="page"/>
|
|
168
|
+
</w:r>
|
|
169
|
+
</w:p>`
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
})
|
|
173
|
+
Array.from(this.postamble!.children)
|
|
174
|
+
.filter((node): node is XMLElement => node instanceof XMLElement)
|
|
175
|
+
.forEach(node => text.appendChild(node))
|
|
176
|
+
}
|
|
177
|
+
}
|