@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.
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,496 @@
1
+ import type {ImageDBEntry} from "@fiduswriter/document"
2
+ /**
3
+ * EPUB book exporter.
4
+ */
5
+
6
+ import type {Schema} from "prosemirror-model"
7
+ import {HTMLExporterConvert} from "@fiduswriter/document/exporter/html/convert"
8
+ import type {HTMLExportMetadata} from "@fiduswriter/document/exporter/html/convert"
9
+ import {htmlExportTemplate} from "@fiduswriter/document/exporter/html/templates"
10
+ import type {ProgressCallback} from "@fiduswriter/document/exporter/tools/progress"
11
+ import {removeHidden} from "@fiduswriter/document/exporter/tools/doc_content"
12
+ import {
13
+ createSlug,
14
+ getImageDBEntryFilename
15
+ } from "@fiduswriter/document/exporter/tools/file"
16
+ import {ZipFileCreator} from "fwtoolkit/file/zip"
17
+ import {gettext, get, staticUrl} from "fwtoolkit"
18
+ import pretty from "../pretty.js"
19
+
20
+ import type {Book, BookCoverImage, BookStyles, CSL, DocumentListEntry, User} from "../../types.js"
21
+ import type {FidusNode} from "@fiduswriter/document"
22
+ import {getMissingChapterData} from "../tools.js"
23
+ import {
24
+ containerTemplate,
25
+ epubBookCopyrightTemplate,
26
+ epubBookCoverTemplate,
27
+ epubBookOpfTemplate,
28
+ epubBookTitlepageTemplate,
29
+ navTemplate,
30
+ ncxTemplate
31
+ } from "./templates.js"
32
+ import {
33
+ buildHierarchy,
34
+ getFontMimeType,
35
+ getImageMimeType,
36
+ getTimestamp
37
+ } from "./tools.js"
38
+
39
+ interface StyleSheet {
40
+ url?: string
41
+ filename?: string
42
+ contents?: string
43
+ }
44
+
45
+ interface ImageFile {
46
+ url: string
47
+ filename: string
48
+ coverImage?: boolean
49
+ mimeType?: string
50
+ }
51
+
52
+ interface FontFile {
53
+ url: string
54
+ filename: string
55
+ mimeType?: string
56
+ }
57
+
58
+ interface TextFile {
59
+ filename: string
60
+ contents: string
61
+ }
62
+
63
+ interface IncludeZip {
64
+ directory: string
65
+ url: string
66
+ }
67
+
68
+ interface ChapterOutput {
69
+ number: number
70
+ part: string
71
+ title: string
72
+ docNum: number
73
+ metaData: HTMLExportMetadata
74
+ }
75
+
76
+ export class EpubBookExporter {
77
+ schema: Schema
78
+ csl: CSL
79
+ bookStyles: BookStyles
80
+ book: Book
81
+ user: User
82
+ docList: DocumentListEntry[]
83
+ updated: number
84
+ textFiles: TextFile[]
85
+ images: ImageFile[]
86
+ fontFiles: FontFile[]
87
+ httpFiles: (ImageFile | FontFile)[]
88
+ styleSheets: StyleSheet[]
89
+ chapters: ChapterOutput[]
90
+ contentItems: Array<Record<string, unknown>>
91
+ includeZips: IncludeZip[]
92
+ math: boolean
93
+ progressCallback?: ProgressCallback
94
+
95
+ constructor(
96
+ schema: Schema,
97
+ csl: CSL,
98
+ bookStyles: BookStyles,
99
+ book: Book,
100
+ user: User,
101
+ docList: DocumentListEntry[],
102
+ updated: number
103
+ ) {
104
+ this.schema = schema
105
+ this.csl = csl
106
+ this.bookStyles = bookStyles
107
+ this.book = book
108
+ this.user = user
109
+ this.docList = docList
110
+ this.updated = updated
111
+ this.textFiles = []
112
+ this.images = []
113
+ this.fontFiles = []
114
+ this.httpFiles = []
115
+ // Book exports load the shared document stylesheet (with the bundled
116
+ // Libertinus fallback fonts) first and the book-specific stylesheet on
117
+ // top so book rules can override the base ones.
118
+ this.styleSheets = [
119
+ {url: staticUrl("css/document/document.css")},
120
+ {url: staticUrl("css/book.css")}
121
+ ]
122
+ this.chapters = []
123
+ this.contentItems = []
124
+ this.includeZips = []
125
+ this.math = false
126
+ }
127
+
128
+ // The open-license (SIL OFL) Libertinus Serif + JetBrains Mono fallback
129
+ // fonts bundled with @fiduswriter/document. css/document.css references
130
+ // them through relative @font-face url()s (fonts/...), so they must ship
131
+ // in the export next to the stylesheet under css/fonts/ to keep the
132
+ // fallback self-contained. Chapters are converted with
133
+ // HTMLExporterConvert directly (not the document HTMLExporter), so the
134
+ // fonts have to be added here.
135
+ static FALLBACK_FONTS = [
136
+ "LibertinusSerif-Regular.ttf",
137
+ "LibertinusSerif-Bold.ttf",
138
+ "LibertinusSerif-Italic.ttf",
139
+ "LibertinusSerif-BoldItalic.ttf",
140
+ "JetBrainsMono-Regular.ttf",
141
+ "JetBrainsMono-Bold.ttf"
142
+ ]
143
+
144
+ addFallbackFonts(): void {
145
+ EpubBookExporter.FALLBACK_FONTS.forEach(filename => {
146
+ this.fontFiles.push({
147
+ url: staticUrl(`css/document/fonts/${filename}`),
148
+ filename: `css/fonts/${filename}`
149
+ })
150
+ })
151
+ }
152
+
153
+ async init(progressCallback?: ProgressCallback): Promise<Blob | false> {
154
+ this.progressCallback = progressCallback
155
+ this.progressCallback?.(
156
+ gettext("Epub book export has been initiated."),
157
+ 0
158
+ )
159
+ if (this.book.chapters.length === 0) {
160
+ throw new Error(
161
+ gettext("Book cannot be exported due to lack of chapters.")
162
+ )
163
+ }
164
+
165
+ await getMissingChapterData(this.book, this.docList, this.schema, {
166
+ progressCallback: this.progressCallback
167
+ })
168
+
169
+ this.addBookStyle()
170
+ this.addFallbackFonts()
171
+ return this.exportContents()
172
+ }
173
+
174
+ addBookStyle(): boolean {
175
+ const bookStyle = this.bookStyles.find(
176
+ style => style.slug === this.book.settings.book_style
177
+ )
178
+ if (!bookStyle) {
179
+ return false
180
+ }
181
+
182
+ let contents = bookStyle.contents
183
+ bookStyle.bookstylefile_set.forEach(([_url, filename]) => {
184
+ contents = contents.replace(
185
+ new RegExp(filename, "g"),
186
+ `media/${filename}`
187
+ )
188
+ })
189
+
190
+ this.styleSheets.push({contents, filename: `css/${bookStyle.slug}.css`})
191
+ this.fontFiles = this.fontFiles.concat(
192
+ bookStyle.bookstylefile_set.map(([url, filename]) => ({
193
+ filename: `css/media/${filename}`,
194
+ url
195
+ }))
196
+ )
197
+ return true
198
+ }
199
+
200
+ async exportContents(): Promise<Blob> {
201
+ this.progressCallback?.(gettext("Preparing Epub book..."), 10)
202
+ await Promise.all(
203
+ this.styleSheets.map(async sheet => this.loadStyle(sheet))
204
+ )
205
+
206
+ if (this.book.cover_image) {
207
+ const coverImage = this.book.cover_image_data
208
+ if (coverImage?.image) {
209
+ this.images.push({
210
+ url: coverImage.image.split("?")[0],
211
+ filename: getImageDBEntryFilename(
212
+ coverImage as ImageDBEntry,
213
+ "cover"
214
+ ),
215
+ coverImage: true
216
+ })
217
+
218
+ this.textFiles.push({
219
+ filename: "cover.xhtml",
220
+ contents: pretty(
221
+ epubBookCoverTemplate({
222
+ book: this.book,
223
+ coverImage: coverImage as BookCoverImage & {image: string},
224
+ shortLang: (this.book.settings.language || "en-US").split("-")[0]
225
+ })
226
+ )
227
+ })
228
+ }
229
+ }
230
+
231
+ this.textFiles.push({
232
+ filename: "titlepage.xhtml",
233
+ contents: pretty(
234
+ epubBookTitlepageTemplate({
235
+ book: this.book,
236
+ shortLang: (this.book.settings.language || "en-US").split("-")[0]
237
+ })
238
+ )
239
+ })
240
+
241
+ this.chapters = (
242
+ await Promise.all(
243
+ this.book.chapters
244
+ .sort((a, b) => a.number - b.number)
245
+ .map(async chapter => {
246
+ const doc = this.docList.find(
247
+ d => d.id === chapter.text
248
+ )
249
+ if (!doc) {
250
+ return false
251
+ }
252
+
253
+ const docContent = removeHidden(doc.content) as FidusNode
254
+
255
+ const converter = new HTMLExporterConvert(
256
+ doc.title,
257
+ doc.settings,
258
+ docContent,
259
+ htmlExportTemplate,
260
+ {db: doc.images || {}},
261
+ {db: doc.bibliography || {}},
262
+ this.csl,
263
+ this.styleSheets,
264
+ {
265
+ xhtml: true,
266
+ epub: true,
267
+ footnoteNumbering: "decimal",
268
+ affiliationNumbering: "alpha",
269
+ idPrefix: `c-${chapter.number}-`
270
+ }
271
+ )
272
+ const {html, imageIds, metaData, extraStyleSheets} =
273
+ await converter.init()
274
+
275
+ if (!html) {
276
+ return false
277
+ }
278
+
279
+ imageIds.forEach((id: string | number) => {
280
+ const image = doc.images?.[id]
281
+ if (image?.image && typeof image.image === "string") {
282
+ this.images.push({
283
+ // Data URLs have no usable last path
284
+ // segment; the helper derives a stable
285
+ // name.
286
+ filename: `images/${getImageDBEntryFilename(image, id)}`,
287
+ url: image.image
288
+ })
289
+ }
290
+ })
291
+
292
+ await Promise.all(
293
+ (extraStyleSheets as StyleSheet[]).map(
294
+ async (sheet: StyleSheet) => this.loadStyle(sheet)
295
+ )
296
+ )
297
+
298
+ if (converter.features.math) {
299
+ this.math = true
300
+ }
301
+
302
+ this.textFiles.push({
303
+ filename: `document-${chapter.number}.xhtml`,
304
+ contents: pretty(html)
305
+ })
306
+
307
+ return {
308
+ number: chapter.number,
309
+ part: chapter.part || "",
310
+ title: doc.title,
311
+ docNum: chapter.number,
312
+ metaData
313
+ }
314
+ })
315
+ )
316
+ ).filter((chapter): chapter is ChapterOutput => chapter !== false)
317
+
318
+ this.progressCallback?.(gettext("Assembling Epub book..."), 80)
319
+
320
+ this.textFiles.push({
321
+ filename: "copyright.xhtml",
322
+ contents: pretty(
323
+ epubBookCopyrightTemplate({
324
+ book: this.book,
325
+ creator: this.user.name || "",
326
+ language: this.book.settings.language,
327
+ shortLang: (this.book.settings.language || "en-US").split("-")[0]
328
+ })
329
+ )
330
+ })
331
+
332
+ const contentItems = this.chapters.reduce(
333
+ (items: Array<Record<string, unknown>>, chapter) => {
334
+ if (chapter.part) {
335
+ items.push({
336
+ title: chapter.part,
337
+ docNum: chapter.number,
338
+ link: `document-${chapter.number}.xhtml`,
339
+ level: -1
340
+ })
341
+ }
342
+ items = items.concat(
343
+ (chapter.metaData.toc as Array<Record<string, unknown>>).map(item => ({
344
+ ...item,
345
+ docNum: chapter.number,
346
+ link: `document-${chapter.number}.xhtml#c-${chapter.number}-${item.id}`
347
+ }))
348
+ )
349
+ return items
350
+ },
351
+ []
352
+ )
353
+
354
+ const toc = buildHierarchy(contentItems)
355
+
356
+ this.textFiles = this.textFiles.concat([
357
+ {
358
+ filename: "META-INF/container.xml",
359
+ contents: pretty(containerTemplate())
360
+ },
361
+ {
362
+ filename: "document.opf",
363
+ contents: pretty(
364
+ epubBookOpfTemplate({
365
+ book: this.book,
366
+ language: this.book.settings.language,
367
+ idType: "fidus",
368
+ date: getTimestamp(new Date(this.updated * 1000)).slice(
369
+ 0,
370
+ 10
371
+ ),
372
+ modified: getTimestamp(new Date(this.updated * 1000)),
373
+ styleSheets: this.styleSheets as Array<{filename: string}>,
374
+ math: this.math,
375
+ images: this.images.map(image => ({
376
+ ...image,
377
+ mimeType: getImageMimeType(image.filename) || undefined
378
+ })),
379
+ fontFiles: this.fontFiles.map(font => ({
380
+ ...font,
381
+ mimeType: getFontMimeType(font.filename) || undefined
382
+ })),
383
+ chapters: this.chapters,
384
+ user: {name: this.user.name || ""}
385
+ })
386
+ )
387
+ },
388
+ {
389
+ filename: "document.ncx",
390
+ contents: pretty(
391
+ ncxTemplate({
392
+ shortLang: (this.book.settings.language || "en-US").split("-")[0],
393
+ title: this.book.title,
394
+ idType: "fidus",
395
+ id: this.book.id,
396
+ toc: toc as Array<{
397
+ link: string
398
+ title: string
399
+ children?: Array<{link: string; title: string}>
400
+ }>
401
+ })
402
+ )
403
+ },
404
+ {
405
+ filename: "document-nav.xhtml",
406
+ contents: pretty(
407
+ navTemplate({
408
+ shortLang: (this.book.settings.language || "en-US").split("-")[0],
409
+ toc: toc as Array<{
410
+ link: string
411
+ title: string
412
+ children?: Array<{link: string; title: string}>
413
+ }>,
414
+ styleSheets: this.styleSheets as Array<{filename: string}>
415
+ })
416
+ )
417
+ }
418
+ ])
419
+ if (this.math) {
420
+ this.includeZips.push({
421
+ directory: "css",
422
+ url: staticUrl("zip/mathlive_style.zip")
423
+ })
424
+ }
425
+
426
+ this.httpFiles = this.images.concat(this.fontFiles)
427
+ this.prefixFiles()
428
+ return this.createZip()
429
+ }
430
+
431
+ async loadStyle(sheet: StyleSheet): Promise<StyleSheet> {
432
+ const filename =
433
+ sheet.filename ||
434
+ `css/${sheet.url?.split("/").pop()?.split("?")[0]}`
435
+ const existing = this.textFiles.find(file => file.filename === filename)
436
+ if (existing) {
437
+ return Promise.resolve(existing as StyleSheet)
438
+ }
439
+ if (sheet.url) {
440
+ const response = await get(sheet.url)
441
+ const text = await response.text()
442
+ sheet.contents = text
443
+ sheet.filename = filename
444
+ delete sheet.url
445
+ }
446
+ if (sheet.filename) {
447
+ this.textFiles.push(sheet as TextFile)
448
+ }
449
+ return Promise.resolve(sheet)
450
+ }
451
+
452
+ prefixFiles(): void {
453
+ this.textFiles = this.textFiles.map(file => {
454
+ if (
455
+ ["META-INF/container.xml", "mimetype"].includes(file.filename)
456
+ ) {
457
+ return file
458
+ }
459
+ return Object.assign({}, file, {filename: `EPUB/${file.filename}`})
460
+ })
461
+ this.fontFiles = this.fontFiles.map(file =>
462
+ Object.assign({}, file, {filename: `EPUB/${file.filename}`})
463
+ )
464
+ // httpFiles carries the images and font files added before prefixing;
465
+ // they must also live under EPUB/ so the paths referenced from the
466
+ // OPF, xhtml and CSS @font-face rules (all relative to EPUB/) resolve.
467
+ this.httpFiles = this.httpFiles.map(file =>
468
+ Object.assign({}, file, {filename: `EPUB/${file.filename}`})
469
+ )
470
+ this.includeZips = this.includeZips.map(file =>
471
+ Object.assign({}, file, {directory: `EPUB/${file.directory}`})
472
+ )
473
+ }
474
+
475
+ async createZip(): Promise<Blob> {
476
+ this.progressCallback?.(gettext("Finalizing Epub book..."), 90)
477
+ const zipper = new ZipFileCreator(
478
+ this.textFiles,
479
+ this.httpFiles,
480
+ this.includeZips,
481
+ "application/epub+zip",
482
+ new Date(this.updated * 1000)
483
+ )
484
+ const blob = await zipper.init()
485
+ this.progressCallback?.(gettext("Epub book export complete."), 100)
486
+ return this.download(blob)
487
+ }
488
+
489
+ download(blob: Blob): Blob {
490
+ return blob
491
+ }
492
+
493
+ get defaultFilename(): string {
494
+ return `${createSlug(this.book.title)}.epub`
495
+ }
496
+ }