@fiduswriter/books 0.0.0-stage → 0.2.0
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,238 @@
|
|
|
1
|
+
import type {ImageDBEntry} from "@fiduswriter/document"
|
|
2
|
+
/**
|
|
3
|
+
* Native `.fidusbook` exporter.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type {Schema} from "prosemirror-model"
|
|
7
|
+
import {ShrinkFidus} from "@fiduswriter/document/exporter/native/shrink"
|
|
8
|
+
import {
|
|
9
|
+
createSlug,
|
|
10
|
+
getImageDBEntryFilename
|
|
11
|
+
} from "@fiduswriter/document/exporter/tools/file"
|
|
12
|
+
import {ZipFileCreator} from "fwtoolkit/file/zip"
|
|
13
|
+
import {gettext} from "fwtoolkit"
|
|
14
|
+
|
|
15
|
+
import {FIDUSBOOK_VERSION} from "../../schema/index.js"
|
|
16
|
+
import type {Book, DocumentListEntry, User} from "../../types.js"
|
|
17
|
+
import {getMissingChapterData} from "../tools.js"
|
|
18
|
+
import type {ProgressCallback} from "@fiduswriter/document/exporter/native/shrink"
|
|
19
|
+
import type {FidusNode} from "@fiduswriter/document"
|
|
20
|
+
|
|
21
|
+
interface FileInclude {
|
|
22
|
+
url: string
|
|
23
|
+
filename: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface TextFile {
|
|
27
|
+
filename: string
|
|
28
|
+
contents: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export {FIDUSBOOK_VERSION}
|
|
32
|
+
|
|
33
|
+
export class NativeBookExporter {
|
|
34
|
+
schema: Schema
|
|
35
|
+
book: Book
|
|
36
|
+
user: User
|
|
37
|
+
documentList: DocumentListEntry[]
|
|
38
|
+
updated: number
|
|
39
|
+
|
|
40
|
+
progressCallback?: ProgressCallback
|
|
41
|
+
|
|
42
|
+
constructor(
|
|
43
|
+
schema: Schema,
|
|
44
|
+
book: Book,
|
|
45
|
+
user: User,
|
|
46
|
+
documentList: DocumentListEntry[],
|
|
47
|
+
updated: number,
|
|
48
|
+
progressCallback?: ProgressCallback
|
|
49
|
+
) {
|
|
50
|
+
this.schema = schema
|
|
51
|
+
this.book = book
|
|
52
|
+
this.user = user
|
|
53
|
+
this.documentList = documentList
|
|
54
|
+
this.updated = updated
|
|
55
|
+
this.progressCallback = progressCallback
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
init(progressCallback?: ProgressCallback): Promise<Blob> {
|
|
59
|
+
if (progressCallback) {
|
|
60
|
+
this.progressCallback = progressCallback
|
|
61
|
+
}
|
|
62
|
+
// A chapter-less book is a valid container: book.json with an empty
|
|
63
|
+
// chapter list and no chapter data. File-based hosts start books
|
|
64
|
+
// empty and add chapters as standalone files afterwards.
|
|
65
|
+
|
|
66
|
+
this.progressCallback?.(
|
|
67
|
+
`${this.book.title}: ${gettext("Fidusbook export has been initiated.")}`,
|
|
68
|
+
0
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
return getMissingChapterData(this.book, this.documentList, this.schema)
|
|
72
|
+
.then(() => this.exportContents())
|
|
73
|
+
.then(blob => {
|
|
74
|
+
this.progressCallback?.(
|
|
75
|
+
`${this.book.title}: ${gettext("Fidusbook export complete.")}`,
|
|
76
|
+
100
|
|
77
|
+
)
|
|
78
|
+
return blob
|
|
79
|
+
})
|
|
80
|
+
.catch(error => {
|
|
81
|
+
this.progressCallback?.(
|
|
82
|
+
`${this.book.title}: ${gettext("Fidusbook export failed.")}`,
|
|
83
|
+
100
|
|
84
|
+
)
|
|
85
|
+
throw error
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
exportContents(): Promise<Blob> {
|
|
90
|
+
const textFiles: TextFile[] = []
|
|
91
|
+
const httpFiles: FileInclude[] = []
|
|
92
|
+
|
|
93
|
+
const sortedChapters = [...this.book.chapters].sort(
|
|
94
|
+
(a, b) => a.number - b.number
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
const bookData: Record<string, unknown> = {
|
|
98
|
+
title: this.book.title,
|
|
99
|
+
path: this.book.path || "/",
|
|
100
|
+
metadata: this.book.metadata || {},
|
|
101
|
+
settings: this.book.settings || {},
|
|
102
|
+
// `text` is the chapter's document id and must be persisted: the
|
|
103
|
+
// reader rebuilds the document list from the chapter entries and
|
|
104
|
+
// `getMissingChapterData` matches them with
|
|
105
|
+
// `documentList.find(doc => doc.id === chapter.text)`. Omitting it
|
|
106
|
+
// makes every re-read of a written book fail to resolve its
|
|
107
|
+
// chapters, so the book cannot be exported again.
|
|
108
|
+
chapters: sortedChapters.map((chapter, index) => ({
|
|
109
|
+
text: chapter.text,
|
|
110
|
+
number: chapter.number,
|
|
111
|
+
part: chapter.part || "",
|
|
112
|
+
chapter_index: index,
|
|
113
|
+
// File-based hosts reference chapters as standalone `.fidus`
|
|
114
|
+
// files; the path must be persisted so the host can re-resolve
|
|
115
|
+
// the live file on the next load.
|
|
116
|
+
...(chapter.path ? {path: chapter.path} : {})
|
|
117
|
+
}))
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (this.book.cover_image_data) {
|
|
121
|
+
const coverImage = this.book.cover_image_data
|
|
122
|
+
const imageUrl = (coverImage.image || "").split("?")[0]
|
|
123
|
+
const filename = getImageDBEntryFilename(
|
|
124
|
+
coverImage as ImageDBEntry,
|
|
125
|
+
"cover"
|
|
126
|
+
)
|
|
127
|
+
bookData.cover_image = {
|
|
128
|
+
title: coverImage.title || "",
|
|
129
|
+
checksum: coverImage.checksum || "",
|
|
130
|
+
file_type: coverImage.file_type,
|
|
131
|
+
image: `cover/${filename}`
|
|
132
|
+
}
|
|
133
|
+
httpFiles.push({url: imageUrl, filename: `cover/${filename}`})
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const processChapter = (index: number): Promise<void> => {
|
|
137
|
+
if (index >= sortedChapters.length) {
|
|
138
|
+
return Promise.resolve()
|
|
139
|
+
}
|
|
140
|
+
const chapter = sortedChapters[index]
|
|
141
|
+
const doc = this.documentList.find(d => d.id === chapter.text)
|
|
142
|
+
if (!doc || doc.missing) {
|
|
143
|
+
// A chapter without a document entry cannot be rendered; one
|
|
144
|
+
// marked `missing` has no readable source (the file is gone
|
|
145
|
+
// and no snapshot is embedded). Both are left out rather than
|
|
146
|
+
// exported as empty chapters.
|
|
147
|
+
return processChapter(index + 1)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
this.progressCallback?.(
|
|
151
|
+
gettext("Exporting chapter %s of %s..."),
|
|
152
|
+
Math.round((index / sortedChapters.length) * 90)
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
const shrinker = new ShrinkFidus(
|
|
156
|
+
doc as unknown as {content: FidusNode; [key: string]: unknown},
|
|
157
|
+
{db: doc.images || {}},
|
|
158
|
+
{db: doc.bibliography || {}},
|
|
159
|
+
this.progressCallback
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
return shrinker
|
|
163
|
+
.init()
|
|
164
|
+
.then(
|
|
165
|
+
({
|
|
166
|
+
doc: shrunkDoc,
|
|
167
|
+
shrunkImageDB,
|
|
168
|
+
shrunkBibDB,
|
|
169
|
+
httpIncludes
|
|
170
|
+
}: {
|
|
171
|
+
doc: Record<string, unknown>
|
|
172
|
+
shrunkImageDB: Record<string, unknown>
|
|
173
|
+
shrunkBibDB: Record<string, unknown>
|
|
174
|
+
httpIncludes: FileInclude[]
|
|
175
|
+
}) => {
|
|
176
|
+
httpIncludes.forEach(include => {
|
|
177
|
+
include.filename = `chapters/${index}/${include.filename}`
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
// `ShrinkFidus` drops the document `id`, but the reader
|
|
181
|
+
// rebuilds each document-list entry with
|
|
182
|
+
// `id: docJson.id || 0` and `getMissingChapterData`
|
|
183
|
+
// matches chapters by `doc.id === chapter.text`. A
|
|
184
|
+
// chapter written without its id therefore reads back
|
|
185
|
+
// as 0 and cannot be resolved, so re-exporting a book
|
|
186
|
+
// would fail. Re-attach it before serializing.
|
|
187
|
+
const chapterDoc: Record<string, unknown> = {
|
|
188
|
+
id: doc.id,
|
|
189
|
+
...shrunkDoc
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
textFiles.push(
|
|
193
|
+
{
|
|
194
|
+
filename: `chapters/${index}/document.json`,
|
|
195
|
+
contents: JSON.stringify(chapterDoc)
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
filename: `chapters/${index}/images.json`,
|
|
199
|
+
contents: JSON.stringify(shrunkImageDB)
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
filename: `chapters/${index}/bibliography.json`,
|
|
203
|
+
contents: JSON.stringify(shrunkBibDB)
|
|
204
|
+
}
|
|
205
|
+
)
|
|
206
|
+
httpFiles.push(...httpIncludes)
|
|
207
|
+
}
|
|
208
|
+
)
|
|
209
|
+
.then(() => processChapter(index + 1))
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return processChapter(0).then(() => {
|
|
213
|
+
textFiles.push(
|
|
214
|
+
{filename: "book.json", contents: JSON.stringify(bookData)},
|
|
215
|
+
{filename: "filetype-version", contents: FIDUSBOOK_VERSION}
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
const zipper = new ZipFileCreator(
|
|
219
|
+
textFiles,
|
|
220
|
+
httpFiles,
|
|
221
|
+
[],
|
|
222
|
+
"application/vnd.fiduswriter.book+zip",
|
|
223
|
+
new Date(this.updated * 1000)
|
|
224
|
+
)
|
|
225
|
+
return zipper.init().then(blob => this.download(blob))
|
|
226
|
+
})
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
download(blob: Blob): Blob {
|
|
230
|
+
// Base implementation returns the Blob. Browser/CLI subclasses can
|
|
231
|
+
// override this method to trigger a real download.
|
|
232
|
+
return blob
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
get defaultFilename(): string {
|
|
236
|
+
return `${createSlug(this.book.title)}.fidusbook`
|
|
237
|
+
}
|
|
238
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ODT 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 {ODTExporterCitations} from "@fiduswriter/document/exporter/odt/citations"
|
|
17
|
+
import {ODTExporterImages} from "@fiduswriter/document/exporter/odt/images"
|
|
18
|
+
import {ODTExporterFootnotes} from "@fiduswriter/document/exporter/odt/footnotes"
|
|
19
|
+
import {ODTExporterMath} from "@fiduswriter/document/exporter/odt/math"
|
|
20
|
+
import {ODTExporterMetadata} from "@fiduswriter/document/exporter/odt/metadata"
|
|
21
|
+
import {ODTExporterRichtext} from "@fiduswriter/document/exporter/odt/richtext"
|
|
22
|
+
import {ODTExporterStyles} from "@fiduswriter/document/exporter/odt/styles"
|
|
23
|
+
import {ODTExporterTracks} from "@fiduswriter/document/exporter/odt/track"
|
|
24
|
+
|
|
25
|
+
import type {Book, DocumentListEntry} from "../../types.js"
|
|
26
|
+
import {getMissingChapterData} from "../tools.js"
|
|
27
|
+
import {ODTBookExporterRender} from "./render.js"
|
|
28
|
+
|
|
29
|
+
export class ODTBookExporter {
|
|
30
|
+
schema: Schema
|
|
31
|
+
csl: CSL
|
|
32
|
+
book: Book
|
|
33
|
+
user: User
|
|
34
|
+
docList: DocumentListEntry[]
|
|
35
|
+
templateUrl: string
|
|
36
|
+
updated: number
|
|
37
|
+
textFiles: Array<Record<string, unknown>>
|
|
38
|
+
httpFiles: Array<Record<string, unknown>>
|
|
39
|
+
mimeType: string
|
|
40
|
+
progressCallback?: ProgressCallback
|
|
41
|
+
|
|
42
|
+
constructor(
|
|
43
|
+
schema: Schema,
|
|
44
|
+
csl: CSL,
|
|
45
|
+
book: Book,
|
|
46
|
+
user: User,
|
|
47
|
+
docList: DocumentListEntry[],
|
|
48
|
+
updated: number
|
|
49
|
+
) {
|
|
50
|
+
this.schema = schema
|
|
51
|
+
this.csl = csl
|
|
52
|
+
this.book = book
|
|
53
|
+
this.user = user
|
|
54
|
+
this.docList = docList
|
|
55
|
+
this.templateUrl = book.odt_template || ""
|
|
56
|
+
this.updated = updated
|
|
57
|
+
this.textFiles = []
|
|
58
|
+
this.httpFiles = []
|
|
59
|
+
|
|
60
|
+
this.mimeType = "application/vnd.oasis.opendocument.text"
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
init(progressCallback?: ProgressCallback): Promise<Blob> | false {
|
|
64
|
+
this.progressCallback = progressCallback
|
|
65
|
+
this.progressCallback?.(
|
|
66
|
+
gettext("ODT book export has been initiated."),
|
|
67
|
+
0
|
|
68
|
+
)
|
|
69
|
+
if (this.book.chapters.length === 0) {
|
|
70
|
+
throw new Error(
|
|
71
|
+
gettext("Book cannot be exported due to lack of chapters.")
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
return getMissingChapterData(
|
|
75
|
+
this.book,
|
|
76
|
+
this.docList,
|
|
77
|
+
this.schema,
|
|
78
|
+
{rawContent: true, progressCallback: this.progressCallback}
|
|
79
|
+
).then(() => this.export())
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export(): Promise<Blob> {
|
|
83
|
+
this.progressCallback?.(gettext("Preparing ODT book..."), 10)
|
|
84
|
+
this.book.chapters.sort((a, b) => (a.number > b.number ? 1 : -1))
|
|
85
|
+
const xml = new XmlZip(this.templateUrl, this.mimeType)
|
|
86
|
+
const styles = new ODTExporterStyles(xml)
|
|
87
|
+
const math = new ODTExporterMath(xml)
|
|
88
|
+
const tracks = new ODTExporterTracks(xml)
|
|
89
|
+
const render = new ODTBookExporterRender(xml, styles)
|
|
90
|
+
const metadata = new ODTExporterMetadata(
|
|
91
|
+
xml,
|
|
92
|
+
styles,
|
|
93
|
+
this.getBaseMetadata(),
|
|
94
|
+
this.csl
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
return xml
|
|
98
|
+
.init()
|
|
99
|
+
.then(() => styles.init())
|
|
100
|
+
.then(() => tracks.init())
|
|
101
|
+
.then(() => render.init())
|
|
102
|
+
.then(() => metadata.init())
|
|
103
|
+
.then(() => math.init())
|
|
104
|
+
.then(() => this.exportChapters(xml, render, styles, math, tracks))
|
|
105
|
+
.then(() => {
|
|
106
|
+
this.progressCallback?.(gettext("Finalizing ODT book..."), 90)
|
|
107
|
+
return xml.prepareBlob()
|
|
108
|
+
})
|
|
109
|
+
.then(blob => {
|
|
110
|
+
this.progressCallback?.(
|
|
111
|
+
gettext("ODT book export complete."),
|
|
112
|
+
100
|
|
113
|
+
)
|
|
114
|
+
return this.download(blob)
|
|
115
|
+
})
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
exportChapters(
|
|
119
|
+
xml: XmlZip,
|
|
120
|
+
render: ODTBookExporterRender,
|
|
121
|
+
styles: ODTExporterStyles,
|
|
122
|
+
math: ODTExporterMath,
|
|
123
|
+
tracks: ODTExporterTracks
|
|
124
|
+
): Promise<void> {
|
|
125
|
+
return this.book.chapters
|
|
126
|
+
.map((chapter, chapterIndex) => {
|
|
127
|
+
return (): Promise<void> => {
|
|
128
|
+
const doc = this.docList.find(d => d.id === chapter.text)
|
|
129
|
+
if (!doc) {
|
|
130
|
+
return Promise.resolve()
|
|
131
|
+
}
|
|
132
|
+
const docContent = fixTables(
|
|
133
|
+
removeHidden(
|
|
134
|
+
doc.rawContent as unknown as FidusNode
|
|
135
|
+
) as FidusNode
|
|
136
|
+
)
|
|
137
|
+
const citations = new ODTExporterCitations(
|
|
138
|
+
docContent,
|
|
139
|
+
doc.settings,
|
|
140
|
+
styles,
|
|
141
|
+
{db: doc.bibliography || {}},
|
|
142
|
+
this.csl
|
|
143
|
+
)
|
|
144
|
+
const footnotes = new ODTExporterFootnotes(
|
|
145
|
+
docContent,
|
|
146
|
+
doc.settings,
|
|
147
|
+
xml,
|
|
148
|
+
citations,
|
|
149
|
+
styles,
|
|
150
|
+
{db: doc.bibliography || {}},
|
|
151
|
+
{db: doc.images || {}},
|
|
152
|
+
this.csl
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
const images = new ODTExporterImages(docContent, xml, {
|
|
156
|
+
db: doc.images || {}
|
|
157
|
+
})
|
|
158
|
+
const richtext = new ODTExporterRichtext(
|
|
159
|
+
doc.comments || {},
|
|
160
|
+
doc.settings,
|
|
161
|
+
styles,
|
|
162
|
+
tracks,
|
|
163
|
+
footnotes,
|
|
164
|
+
citations,
|
|
165
|
+
math,
|
|
166
|
+
images
|
|
167
|
+
)
|
|
168
|
+
return citations
|
|
169
|
+
.init()
|
|
170
|
+
.then(() => images.init())
|
|
171
|
+
.then(() => footnotes.init())
|
|
172
|
+
.then(() => {
|
|
173
|
+
const pmBib = footnotes.pmBib || citations.pmBib
|
|
174
|
+
render.render(
|
|
175
|
+
docContent,
|
|
176
|
+
pmBib,
|
|
177
|
+
doc.settings,
|
|
178
|
+
richtext,
|
|
179
|
+
citations,
|
|
180
|
+
chapterIndex
|
|
181
|
+
)
|
|
182
|
+
return Promise.resolve()
|
|
183
|
+
})
|
|
184
|
+
}
|
|
185
|
+
})
|
|
186
|
+
.reduce(
|
|
187
|
+
(promiseChain, currentChapter) =>
|
|
188
|
+
promiseChain.then(() => currentChapter()),
|
|
189
|
+
Promise.resolve()
|
|
190
|
+
)
|
|
191
|
+
.then(() =>
|
|
192
|
+
render.renderAmbles(
|
|
193
|
+
Object.assign(
|
|
194
|
+
{
|
|
195
|
+
title: this.book.title,
|
|
196
|
+
language: this.book.settings.language
|
|
197
|
+
},
|
|
198
|
+
this.book.metadata
|
|
199
|
+
)
|
|
200
|
+
)
|
|
201
|
+
)
|
|
202
|
+
.then(() => render.assemble())
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
getBaseMetadata(): ExportMetadata {
|
|
206
|
+
const authors = this.book.metadata.author?.length
|
|
207
|
+
? [{institution: this.book.metadata.author}]
|
|
208
|
+
: []
|
|
209
|
+
const keywords = this.book.metadata.keywords?.length
|
|
210
|
+
? this.book.metadata.keywords
|
|
211
|
+
.split(",")
|
|
212
|
+
.map(keyword => keyword.trim())
|
|
213
|
+
: []
|
|
214
|
+
return {
|
|
215
|
+
authors,
|
|
216
|
+
contributors: [],
|
|
217
|
+
keywords,
|
|
218
|
+
title: this.book.title,
|
|
219
|
+
language: this.book.settings.language
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
download(blob: Blob): Blob {
|
|
224
|
+
return blob
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
get defaultFilename(): string {
|
|
228
|
+
return `${createSlug(this.book.title)}.odt`
|
|
229
|
+
}
|
|
230
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import type {DocSettings, FidusNode} from "@fiduswriter/document"
|
|
2
|
+
import {XMLElement} from "@fiduswriter/document/exporter/tools/xml"
|
|
3
|
+
import type {XmlZip} from "@fiduswriter/document/exporter/tools/xml_zip"
|
|
4
|
+
import {ODTExporterCitations} from "@fiduswriter/document/exporter/odt/citations"
|
|
5
|
+
import {ODTExporterRender} from "@fiduswriter/document/exporter/odt/render"
|
|
6
|
+
import {ODTExporterRichtext} from "@fiduswriter/document/exporter/odt/richtext"
|
|
7
|
+
import type {ODTExporterStyles} from "@fiduswriter/document/exporter/odt/styles"
|
|
8
|
+
|
|
9
|
+
interface Tag {
|
|
10
|
+
title: string
|
|
11
|
+
content?: unknown[]
|
|
12
|
+
block: XMLElement
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class ODTBookExporterRender extends ODTExporterRender {
|
|
16
|
+
styles: ODTExporterStyles
|
|
17
|
+
preamble: XMLElement | null
|
|
18
|
+
bodyTemplate: XMLElement | null
|
|
19
|
+
postamble: XMLElement | null
|
|
20
|
+
bodyParts: XMLElement[]
|
|
21
|
+
fileXml: XMLElement | null
|
|
22
|
+
|
|
23
|
+
constructor(xml: XmlZip, styles: ODTExporterStyles) {
|
|
24
|
+
super(xml)
|
|
25
|
+
|
|
26
|
+
this.styles = styles
|
|
27
|
+
|
|
28
|
+
this.preamble = null
|
|
29
|
+
this.bodyTemplate = null
|
|
30
|
+
this.postamble = null
|
|
31
|
+
|
|
32
|
+
this.bodyParts = []
|
|
33
|
+
this.fileXml = null
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
init(): Promise<void> {
|
|
37
|
+
return this.xml.getXml(this.filePath).then(xml => {
|
|
38
|
+
this.fileXml = xml
|
|
39
|
+
const text = xml.query("office:text") as XMLElement
|
|
40
|
+
this.preamble = text.cloneNode(false)
|
|
41
|
+
this.bodyTemplate = text.cloneNode(false)
|
|
42
|
+
this.postamble = text.cloneNode(false)
|
|
43
|
+
let currentSection: XMLElement | null = null
|
|
44
|
+
const textChildren = Array.from(text.children).filter(
|
|
45
|
+
(node): node is XMLElement => node instanceof XMLElement
|
|
46
|
+
)
|
|
47
|
+
textChildren.forEach(node => {
|
|
48
|
+
if (node.tagName && ["text:p", "text:h"].includes(node.tagName)) {
|
|
49
|
+
const bookmark = node.query("text:bookmark")
|
|
50
|
+
if (bookmark) {
|
|
51
|
+
const sectionName = String(
|
|
52
|
+
bookmark.getAttribute("text:name")
|
|
53
|
+
).toLowerCase()
|
|
54
|
+
if (sectionName === "preamble") {
|
|
55
|
+
currentSection = this.preamble
|
|
56
|
+
} else if (sectionName === "postamble") {
|
|
57
|
+
currentSection = this.postamble
|
|
58
|
+
} else if (sectionName === "body") {
|
|
59
|
+
currentSection = this.bodyTemplate
|
|
60
|
+
} else {
|
|
61
|
+
currentSection = currentSection || this.bodyTemplate
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
currentSection = currentSection || this.bodyTemplate
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (currentSection) {
|
|
68
|
+
currentSection.appendChild(node)
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
return Promise.resolve()
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
render(
|
|
76
|
+
docContent: FidusNode,
|
|
77
|
+
pmBib: unknown,
|
|
78
|
+
settings: DocSettings,
|
|
79
|
+
richtext: ODTExporterRichtext,
|
|
80
|
+
citations: ODTExporterCitations,
|
|
81
|
+
chapterIndex = 0
|
|
82
|
+
): void {
|
|
83
|
+
this.text = this.bodyTemplate!.cloneNode(true)
|
|
84
|
+
const textEl = this.text as XMLElement
|
|
85
|
+
const bodyBookmark = textEl.query("text:bookmark", {
|
|
86
|
+
"text:name": "body"
|
|
87
|
+
})
|
|
88
|
+
if (bodyBookmark) {
|
|
89
|
+
bodyBookmark.setAttribute(
|
|
90
|
+
"text:name",
|
|
91
|
+
`chapter ${chapterIndex + 1}`
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
super.render(
|
|
95
|
+
docContent,
|
|
96
|
+
pmBib as false | FidusNode,
|
|
97
|
+
settings,
|
|
98
|
+
richtext,
|
|
99
|
+
citations
|
|
100
|
+
)
|
|
101
|
+
this.bodyParts.push(this.text)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
renderAmbles({
|
|
105
|
+
title,
|
|
106
|
+
subtitle,
|
|
107
|
+
version,
|
|
108
|
+
publisher,
|
|
109
|
+
copyright,
|
|
110
|
+
author,
|
|
111
|
+
keywords,
|
|
112
|
+
language,
|
|
113
|
+
description,
|
|
114
|
+
isbn,
|
|
115
|
+
publication_date,
|
|
116
|
+
series_title,
|
|
117
|
+
series_position
|
|
118
|
+
}: Record<string, unknown>): void {
|
|
119
|
+
const tags = [
|
|
120
|
+
{title: "book.title", content: title},
|
|
121
|
+
{title: "book.subtitle", content: subtitle},
|
|
122
|
+
{title: "book.version", content: version},
|
|
123
|
+
{title: "book.publisher", content: publisher},
|
|
124
|
+
{title: "book.copyright", content: copyright},
|
|
125
|
+
{title: "book.author", content: author},
|
|
126
|
+
{title: "book.keywords", content: keywords},
|
|
127
|
+
{title: "book.language", content: language},
|
|
128
|
+
{title: "book.description", content: description},
|
|
129
|
+
{title: "book.isbn", content: isbn},
|
|
130
|
+
{title: "book.publication_date", content: publication_date},
|
|
131
|
+
{title: "book.series_title", content: series_title},
|
|
132
|
+
{title: "book.series_position", content: series_position}
|
|
133
|
+
]
|
|
134
|
+
const usedTags: Array<{
|
|
135
|
+
title: string
|
|
136
|
+
content?: unknown[]
|
|
137
|
+
block: XMLElement
|
|
138
|
+
}> = []
|
|
139
|
+
const ambles = [this.preamble, this.postamble].filter(
|
|
140
|
+
(amble): amble is XMLElement => amble !== null
|
|
141
|
+
)
|
|
142
|
+
ambles.forEach(amble => {
|
|
143
|
+
const blocks = amble.queryAll(["text:p", "text:h"])
|
|
144
|
+
blocks.forEach(block => {
|
|
145
|
+
if (block.parentElement?.tagName === "text:deletion") {
|
|
146
|
+
return
|
|
147
|
+
}
|
|
148
|
+
const text = block.textContent
|
|
149
|
+
tags.forEach(tag => {
|
|
150
|
+
const tagString = tag.title
|
|
151
|
+
if (text.includes(`{${tagString}}`)) {
|
|
152
|
+
usedTags.push(Object.assign({block}, tag) as Tag)
|
|
153
|
+
}
|
|
154
|
+
})
|
|
155
|
+
})
|
|
156
|
+
})
|
|
157
|
+
usedTags.forEach(tag => this.inlineRender(tag as unknown as Tag))
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
assemble(): void {
|
|
161
|
+
this.styles.addPageBreakStyle()
|
|
162
|
+
const text = this.fileXml!.query("office:text") as XMLElement
|
|
163
|
+
Array.from(this.preamble!.children)
|
|
164
|
+
.filter((node): node is XMLElement => node instanceof XMLElement)
|
|
165
|
+
.forEach(node => text.appendChild(node))
|
|
166
|
+
this.bodyParts.forEach((bodyPart, index) => {
|
|
167
|
+
const children = bodyPart.children
|
|
168
|
+
.slice()
|
|
169
|
+
.filter((node): node is XMLElement => node instanceof XMLElement)
|
|
170
|
+
children.forEach(node => {
|
|
171
|
+
text.appendChild(node)
|
|
172
|
+
})
|
|
173
|
+
if (index < this.bodyParts.length - 1) {
|
|
174
|
+
text.appendXML('<text:p text:style-name="PageBreak"/>')
|
|
175
|
+
}
|
|
176
|
+
})
|
|
177
|
+
Array.from(this.postamble!.children)
|
|
178
|
+
.filter((node): node is XMLElement => node instanceof XMLElement)
|
|
179
|
+
.forEach(node => text.appendChild(node))
|
|
180
|
+
}
|
|
181
|
+
}
|