@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.
Files changed (188) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +87 -2
  3. package/css/book.css +219 -0
  4. package/dist/exporter/bits/index.d.ts +42 -0
  5. package/dist/exporter/bits/index.d.ts.map +1 -0
  6. package/dist/exporter/bits/index.js +113 -0
  7. package/dist/exporter/bits/index.js.map +1 -0
  8. package/dist/exporter/bits/templates.d.ts +7 -0
  9. package/dist/exporter/bits/templates.d.ts.map +1 -0
  10. package/dist/exporter/bits/templates.js +78 -0
  11. package/dist/exporter/bits/templates.js.map +1 -0
  12. package/dist/exporter/docx/index.d.ts +33 -0
  13. package/dist/exporter/docx/index.d.ts.map +1 -0
  14. package/dist/exporter/docx/index.js +139 -0
  15. package/dist/exporter/docx/index.js.map +1 -0
  16. package/dist/exporter/docx/render.d.ts +19 -0
  17. package/dist/exporter/docx/render.d.ts.map +1 -0
  18. package/dist/exporter/docx/render.js +123 -0
  19. package/dist/exporter/docx/render.js.map +1 -0
  20. package/dist/exporter/epub/index.d.ts +70 -0
  21. package/dist/exporter/epub/index.d.ts.map +1 -0
  22. package/dist/exporter/epub/index.js +310 -0
  23. package/dist/exporter/epub/index.js.map +1 -0
  24. package/dist/exporter/epub/templates.d.ts +65 -0
  25. package/dist/exporter/epub/templates.d.ts.map +1 -0
  26. package/dist/exporter/epub/templates.js +224 -0
  27. package/dist/exporter/epub/templates.js.map +1 -0
  28. package/dist/exporter/epub/tools.d.ts +5 -0
  29. package/dist/exporter/epub/tools.d.ts.map +1 -0
  30. package/dist/exporter/epub/tools.js +59 -0
  31. package/dist/exporter/epub/tools.js.map +1 -0
  32. package/dist/exporter/html/index.d.ts +70 -0
  33. package/dist/exporter/html/index.d.ts.map +1 -0
  34. package/dist/exporter/html/index.js +320 -0
  35. package/dist/exporter/html/index.js.map +1 -0
  36. package/dist/exporter/html/templates.d.ts +40 -0
  37. package/dist/exporter/html/templates.d.ts.map +1 -0
  38. package/dist/exporter/html/templates.js +225 -0
  39. package/dist/exporter/html/templates.js.map +1 -0
  40. package/dist/exporter/html/tools.d.ts +2 -0
  41. package/dist/exporter/html/tools.d.ts.map +1 -0
  42. package/dist/exporter/html/tools.js +23 -0
  43. package/dist/exporter/html/tools.js.map +1 -0
  44. package/dist/exporter/latex/index.d.ts +36 -0
  45. package/dist/exporter/latex/index.d.ts.map +1 -0
  46. package/dist/exporter/latex/index.js +116 -0
  47. package/dist/exporter/latex/index.js.map +1 -0
  48. package/dist/exporter/latex/templates.d.ts +4 -0
  49. package/dist/exporter/latex/templates.d.ts.map +1 -0
  50. package/dist/exporter/latex/templates.js +26 -0
  51. package/dist/exporter/latex/templates.js.map +1 -0
  52. package/dist/exporter/native/index.d.ts +22 -0
  53. package/dist/exporter/native/index.d.ts.map +1 -0
  54. package/dist/exporter/native/index.js +141 -0
  55. package/dist/exporter/native/index.js.map +1 -0
  56. package/dist/exporter/odt/index.d.ts +33 -0
  57. package/dist/exporter/odt/index.d.ts.map +1 -0
  58. package/dist/exporter/odt/index.js +133 -0
  59. package/dist/exporter/odt/index.js.map +1 -0
  60. package/dist/exporter/odt/render.d.ts +21 -0
  61. package/dist/exporter/odt/render.d.ts.map +1 -0
  62. package/dist/exporter/odt/render.js +126 -0
  63. package/dist/exporter/odt/render.js.map +1 -0
  64. package/dist/exporter/pretty.d.ts +17 -0
  65. package/dist/exporter/pretty.d.ts.map +1 -0
  66. package/dist/exporter/pretty.js +55 -0
  67. package/dist/exporter/pretty.js.map +1 -0
  68. package/dist/exporter/print/index.d.ts +25 -0
  69. package/dist/exporter/print/index.d.ts.map +1 -0
  70. package/dist/exporter/print/index.js +46 -0
  71. package/dist/exporter/print/index.js.map +1 -0
  72. package/dist/exporter/print/templates.d.ts +2 -0
  73. package/dist/exporter/print/templates.d.ts.map +1 -0
  74. package/dist/exporter/print/templates.js +5 -0
  75. package/dist/exporter/print/templates.js.map +1 -0
  76. package/dist/exporter/tools.d.ts +31 -0
  77. package/dist/exporter/tools.d.ts.map +1 -0
  78. package/dist/exporter/tools.js +177 -0
  79. package/dist/exporter/tools.js.map +1 -0
  80. package/dist/i18n.d.ts +7 -0
  81. package/dist/i18n.d.ts.map +1 -0
  82. package/dist/i18n.js +918 -0
  83. package/dist/i18n.js.map +1 -0
  84. package/dist/ids.d.ts +19 -0
  85. package/dist/ids.d.ts.map +1 -0
  86. package/dist/ids.js +27 -0
  87. package/dist/ids.js.map +1 -0
  88. package/dist/importer/native/index.d.ts +45 -0
  89. package/dist/importer/native/index.d.ts.map +1 -0
  90. package/dist/importer/native/index.js +166 -0
  91. package/dist/importer/native/index.js.map +1 -0
  92. package/dist/importer/native/reader.d.ts +32 -0
  93. package/dist/importer/native/reader.d.ts.map +1 -0
  94. package/dist/importer/native/reader.js +170 -0
  95. package/dist/importer/native/reader.js.map +1 -0
  96. package/dist/index.d.ts +4 -0
  97. package/dist/index.d.ts.map +1 -0
  98. package/dist/index.js +3 -0
  99. package/dist/index.js.map +1 -0
  100. package/dist/schema/index.d.ts +7 -0
  101. package/dist/schema/index.d.ts.map +1 -0
  102. package/dist/schema/index.js +8 -0
  103. package/dist/schema/index.js.map +1 -0
  104. package/dist/types.d.ts +171 -0
  105. package/dist/types.d.ts.map +1 -0
  106. package/dist/types.js +5 -0
  107. package/dist/types.js.map +1 -0
  108. package/dist/ui/book_dialog/dialog.d.ts +161 -0
  109. package/dist/ui/book_dialog/dialog.d.ts.map +1 -0
  110. package/dist/ui/book_dialog/dialog.js +750 -0
  111. package/dist/ui/book_dialog/dialog.js.map +1 -0
  112. package/dist/ui/book_dialog/export.d.ts +95 -0
  113. package/dist/ui/book_dialog/export.d.ts.map +1 -0
  114. package/dist/ui/book_dialog/export.js +217 -0
  115. package/dist/ui/book_dialog/export.js.map +1 -0
  116. package/dist/ui/book_dialog/index.d.ts +38 -0
  117. package/dist/ui/book_dialog/index.d.ts.map +1 -0
  118. package/dist/ui/book_dialog/index.js +36 -0
  119. package/dist/ui/book_dialog/index.js.map +1 -0
  120. package/dist/ui/book_dialog/sanity_check.d.ts +22 -0
  121. package/dist/ui/book_dialog/sanity_check.d.ts.map +1 -0
  122. package/dist/ui/book_dialog/sanity_check.js +109 -0
  123. package/dist/ui/book_dialog/sanity_check.js.map +1 -0
  124. package/dist/ui/book_dialog/templates.d.ts +76 -0
  125. package/dist/ui/book_dialog/templates.d.ts.map +1 -0
  126. package/dist/ui/book_dialog/templates.js +542 -0
  127. package/dist/ui/book_dialog/templates.js.map +1 -0
  128. package/dist/ui/book_dialog/types.d.ts +258 -0
  129. package/dist/ui/book_dialog/types.d.ts.map +1 -0
  130. package/dist/ui/book_dialog/types.js +15 -0
  131. package/dist/ui/book_dialog/types.js.map +1 -0
  132. package/locale/ar/messages.json +49 -0
  133. package/locale/bg/messages.json +49 -0
  134. package/locale/cs/messages.json +49 -0
  135. package/locale/da/messages.json +49 -0
  136. package/locale/de/messages.json +49 -0
  137. package/locale/en/messages.json +49 -0
  138. package/locale/en_US/messages.json +49 -0
  139. package/locale/es/messages.json +49 -0
  140. package/locale/fr/messages.json +49 -0
  141. package/locale/it/messages.json +49 -0
  142. package/locale/ja/messages.json +49 -0
  143. package/locale/ko/messages.json +49 -0
  144. package/locale/nb/messages.json +49 -0
  145. package/locale/nl/messages.json +49 -0
  146. package/locale/pl/messages.json +49 -0
  147. package/locale/pt_BR/messages.json +49 -0
  148. package/locale/pt_PT/messages.json +49 -0
  149. package/locale/ru/messages.json +49 -0
  150. package/locale/sv/messages.json +49 -0
  151. package/locale/tr/messages.json +49 -0
  152. package/locale/zh_Hans/messages.json +49 -0
  153. package/logo.svg +10 -0
  154. package/package.json +150 -5
  155. package/src/exporter/bits/index.ts +184 -0
  156. package/src/exporter/bits/templates.ts +107 -0
  157. package/src/exporter/docx/index.ts +258 -0
  158. package/src/exporter/docx/render.ts +177 -0
  159. package/src/exporter/epub/index.ts +496 -0
  160. package/src/exporter/epub/templates.ts +403 -0
  161. package/src/exporter/epub/tools.ts +68 -0
  162. package/src/exporter/html/index.ts +479 -0
  163. package/src/exporter/html/templates.ts +362 -0
  164. package/src/exporter/html/tools.ts +26 -0
  165. package/src/exporter/latex/index.ts +190 -0
  166. package/src/exporter/latex/templates.ts +35 -0
  167. package/src/exporter/native/index.ts +238 -0
  168. package/src/exporter/odt/index.ts +230 -0
  169. package/src/exporter/odt/render.ts +181 -0
  170. package/src/exporter/pretty.ts +75 -0
  171. package/src/exporter/print/index.ts +76 -0
  172. package/src/exporter/print/templates.ts +8 -0
  173. package/src/exporter/tools.ts +270 -0
  174. package/src/global.d.ts +2 -0
  175. package/src/i18n.ts +936 -0
  176. package/src/ids.ts +27 -0
  177. package/src/importer/native/index.ts +269 -0
  178. package/src/importer/native/reader.ts +247 -0
  179. package/src/index.ts +20 -0
  180. package/src/modules.d.ts +7 -0
  181. package/src/schema/index.ts +8 -0
  182. package/src/types.ts +208 -0
  183. package/src/ui/book_dialog/dialog.ts +940 -0
  184. package/src/ui/book_dialog/export.ts +449 -0
  185. package/src/ui/book_dialog/index.ts +84 -0
  186. package/src/ui/book_dialog/sanity_check.ts +208 -0
  187. package/src/ui/book_dialog/templates.ts +736 -0
  188. 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
+ }