@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,479 @@
1
+ /**
2
+ * HTML book exporter.
3
+ */
4
+
5
+ import type {Schema} from "prosemirror-model"
6
+ import type {CSL, User} from "@fiduswriter/document"
7
+ import type {HTMLExportMetadata} from "@fiduswriter/document/exporter/html/convert"
8
+ import type {ProgressCallback} from "@fiduswriter/document/exporter/tools/progress"
9
+ import {HTMLExporter} from "@fiduswriter/document/exporter/html/index"
10
+ import {createSlug} from "@fiduswriter/document/exporter/tools/file"
11
+ import {ZipFileCreator} from "fwtoolkit/file/zip"
12
+ import {LANGUAGES} from "@fiduswriter/document/schema/const"
13
+ import {gettext, get, staticUrl} from "fwtoolkit"
14
+ import pretty from "../pretty.js"
15
+
16
+ import type {Book, BookStyle, BookStyles, DocumentListEntry} from "../../types.js"
17
+ import {getMissingChapterData} from "../tools.js"
18
+ import {
19
+ htmlBookChapterTemplate,
20
+ htmlBookExportTemplate,
21
+ htmlBookIndexBodyTemplate,
22
+ htmlBookIndexTemplate,
23
+ singleFileHTMLBookCSSTemplate,
24
+ singleFileHTMLBookChapterTemplate,
25
+ singleFileHTMLBookTemplate
26
+ } from "./templates.js"
27
+ import {orderLinks} from "./tools.js"
28
+
29
+ interface StyleSheet {
30
+ url?: string
31
+ filename?: string
32
+ contents?: string
33
+ }
34
+
35
+ interface TextFile {
36
+ filename: string
37
+ contents?: string
38
+ }
39
+
40
+ interface HttpFile {
41
+ url: string
42
+ filename: string
43
+ }
44
+
45
+ interface IncludeZip {
46
+ directory: string
47
+ url: string
48
+ }
49
+
50
+ interface ChapterInfo {
51
+ number: number
52
+ part: string
53
+ doc: DocumentListEntry
54
+ metaData: HTMLExportMetadata
55
+ toc: Array<Record<string, unknown>>
56
+ }
57
+
58
+ export class HTMLBookExporter {
59
+ schema: Schema
60
+ csl: CSL
61
+ bookStyles: BookStyles
62
+ book: Book
63
+ user: User
64
+ docList: DocumentListEntry[]
65
+ updated: number
66
+ multiDoc: boolean
67
+ relativeUrls: boolean
68
+ chapters: ChapterInfo[]
69
+ includeZips: IncludeZip[]
70
+ textFiles: TextFile[]
71
+ httpFiles: HttpFile[]
72
+ math: boolean
73
+ bibCSS: string
74
+ chapterTemplate: (args: Record<string, unknown>) => string
75
+ indexTemplate: (args: Record<string, unknown>) => string
76
+ singleFileHTMLBookTemplate: (args: Record<string, unknown>) => string
77
+ singleFileHTMLBookCSSTemplate: (args: Record<string, unknown>) => string
78
+ styleSheets: StyleSheet[]
79
+ progressCallback?: ProgressCallback
80
+
81
+ constructor(
82
+ schema: Schema,
83
+ csl: CSL,
84
+ bookStyles: BookStyles,
85
+ book: Book,
86
+ user: User,
87
+ docList: DocumentListEntry[],
88
+ updated: number,
89
+ multiDoc = true,
90
+ {relativeUrls = true} = {}
91
+ ) {
92
+ this.schema = schema
93
+ this.csl = csl
94
+ this.bookStyles = bookStyles
95
+ this.book = book
96
+ this.user = user
97
+ this.docList = docList
98
+ this.updated = updated
99
+ this.multiDoc = multiDoc
100
+ this.relativeUrls = relativeUrls
101
+ this.chapters = []
102
+ this.includeZips = []
103
+ this.textFiles = []
104
+ this.httpFiles = []
105
+ this.math = false
106
+ this.bibCSS = ""
107
+ this.chapterTemplate = (multiDoc
108
+ ? htmlBookExportTemplate
109
+ : singleFileHTMLBookChapterTemplate) as unknown as (args: Record<string, unknown>) => string
110
+ this.indexTemplate = (multiDoc
111
+ ? htmlBookIndexTemplate
112
+ : htmlBookIndexBodyTemplate) as unknown as (args: Record<string, unknown>) => string
113
+ this.singleFileHTMLBookTemplate = singleFileHTMLBookTemplate
114
+ this.singleFileHTMLBookCSSTemplate = singleFileHTMLBookCSSTemplate
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
+ }
123
+
124
+ async init(progressCallback?: ProgressCallback): Promise<Blob | false | void> {
125
+ this.progressCallback = progressCallback
126
+ this.progressCallback?.(
127
+ gettext("HTML book export has been initiated."),
128
+ 0
129
+ )
130
+ if (this.book.chapters.length === 0) {
131
+ throw new Error(
132
+ gettext("Book cannot be exported due to lack of chapters.")
133
+ )
134
+ }
135
+
136
+ await getMissingChapterData(this.book, this.docList, this.schema, {
137
+ progressCallback: this.progressCallback
138
+ })
139
+
140
+ this.addBookStyle()
141
+
142
+ await Promise.all(
143
+ this.styleSheets.map(async sheet => this.loadStyle(sheet))
144
+ )
145
+
146
+ return this.exportChapters()
147
+ }
148
+
149
+ async loadStyle(sheet: StyleSheet): Promise<StyleSheet> {
150
+ if (sheet.url) {
151
+ const response = await get(sheet.url)
152
+ const text = await response.text()
153
+ sheet.contents = text
154
+ sheet.filename = `css/${sheet.url.split("/").pop()?.split("?")[0]}`
155
+ delete sheet.url
156
+ }
157
+ if (sheet.filename) {
158
+ this.textFiles.push(sheet as TextFile)
159
+ }
160
+ return sheet
161
+ }
162
+
163
+ async exportChapters(): Promise<Blob | void> {
164
+ this.progressCallback?.(gettext("Exporting book chapters..."), 20)
165
+ let footnoteCounter = 0,
166
+ affiliationCounter = 0,
167
+ figureCounter = 0,
168
+ equationCounter = 0,
169
+ photoCounter = 0,
170
+ tableCounter = 0
171
+
172
+ const sortedChapters = this.book.chapters.sort(
173
+ (a, b) => a.number - b.number
174
+ )
175
+
176
+ for (const chapterInfo of sortedChapters) {
177
+ const chapterNumber = chapterInfo.number
178
+ const chapterPart = chapterInfo.part || ""
179
+ const doc = this.docList.find(d => d.id === chapterInfo.text)
180
+
181
+ if (!doc) {
182
+ continue
183
+ }
184
+
185
+ const imageDB = {db: doc.images || {}}
186
+ const bibDB = {db: doc.bibliography || {}}
187
+ const styleSheets = this.styleSheets.slice()
188
+
189
+ const options = {
190
+ xhtml: false,
191
+ epub: false,
192
+ relativeUrls: this.relativeUrls,
193
+ footnoteNumbering: "decimal",
194
+ affiliationNumbering: "alpha",
195
+ idPrefix: `c-${chapterNumber}-`,
196
+ footnoteOffset: footnoteCounter,
197
+ affiliationOffset: affiliationCounter,
198
+ figureOffset: {
199
+ figure: figureCounter,
200
+ equation: equationCounter,
201
+ photo: photoCounter,
202
+ table: tableCounter
203
+ }
204
+ }
205
+
206
+ const documentHTMLExporter = new HTMLExporter(
207
+ doc,
208
+ bibDB,
209
+ imageDB,
210
+ this.csl,
211
+ new Date(this.updated * 1000),
212
+ [],
213
+ options,
214
+ htmlBookChapterTemplate as unknown as typeof HTMLExporter.prototype.htmlExportTemplate
215
+ )
216
+
217
+ await documentHTMLExporter.process()
218
+ const {metaData, converter, textFiles, httpFiles} =
219
+ documentHTMLExporter.getProcessedFiles()
220
+
221
+ const contents = textFiles.find(
222
+ (textFile: TextFile) => textFile.filename === "document.html"
223
+ )?.contents
224
+ if (!contents) {
225
+ continue
226
+ }
227
+ // Each chapter's document exporter bundles the Libertinus fallback
228
+ // fonts into its httpFiles. Deduplicate by filename so a multi-chapter
229
+ // book only fetches and embeds each font once.
230
+ this.httpFiles = this.httpFiles.concat(
231
+ httpFiles.filter(
232
+ file =>
233
+ !this.httpFiles.some(
234
+ existing => existing.filename === file.filename
235
+ )
236
+ )
237
+ )
238
+
239
+ footnoteCounter = converter.fnCounter
240
+ affiliationCounter = converter.affCounter
241
+ figureCounter = converter.categoryCounter.figure || figureCounter
242
+ equationCounter =
243
+ converter.categoryCounter.equation || equationCounter
244
+ photoCounter = converter.categoryCounter.photo || photoCounter
245
+ tableCounter = converter.categoryCounter.table || tableCounter
246
+
247
+ if (converter.features.math) {
248
+ this.math = true
249
+ styleSheets.push({filename: "css/mathlive.css"})
250
+ }
251
+
252
+ if (converter.citations.bibCSS) {
253
+ if (!this.bibCSS) {
254
+ this.bibCSS = pretty(converter.citations.bibCSS, {
255
+ ocd: true
256
+ })
257
+ }
258
+ styleSheets.push({filename: "css/bibliography.css"})
259
+ }
260
+
261
+ const chapterHTML = this.chapterTemplate({
262
+ part: chapterInfo.part,
263
+ currentPart: chapterPart,
264
+ contents,
265
+ title: doc.title,
266
+ settings: doc.settings,
267
+ styleSheets
268
+ })
269
+
270
+ this.textFiles.push({
271
+ filename: `document-${chapterNumber}.html`,
272
+ contents: pretty(chapterHTML, {ocd: true})
273
+ })
274
+
275
+ this.chapters.push({
276
+ number: chapterNumber,
277
+ part: chapterPart,
278
+ doc,
279
+ metaData,
280
+ toc: converter.metaData.toc
281
+ })
282
+ }
283
+
284
+ this.progressCallback?.(gettext("Assembling HTML book..."), 80)
285
+ return this.exportBook()
286
+ }
287
+
288
+ async exportBook(): Promise<Blob | void> {
289
+ this.progressCallback?.(gettext("Finalizing HTML book..."), 90)
290
+ if (this.math) {
291
+ this.includeZips.push({
292
+ directory: "css",
293
+ url: staticUrl("zip/mathlive_style.zip")
294
+ })
295
+ }
296
+
297
+ if (this.bibCSS) {
298
+ this.textFiles.push({
299
+ filename: "css/bibliography.css",
300
+ contents: this.bibCSS
301
+ })
302
+ }
303
+
304
+ let contentItems: Array<Record<string, unknown>> = []
305
+ for (const chapter of this.chapters) {
306
+ const chapterNumber = chapter.number
307
+ const chapterPart = chapter.part
308
+
309
+ if (chapterPart && chapterPart !== "") {
310
+ contentItems.push({
311
+ link: this.multiDoc
312
+ ? this.getChapterLink(chapterNumber)
313
+ : `#c-${chapterNumber}-body`,
314
+ title: chapterPart,
315
+ docNum: chapterNumber,
316
+ id: 0,
317
+ level: -1,
318
+ subItems: []
319
+ })
320
+ }
321
+
322
+ const contentItemsFromChapter = chapter.toc.map(item => {
323
+ return {
324
+ link: `${this.multiDoc ? this.getChapterLink(chapter.number) : ""}#c-${chapter.number}-${item.id}`,
325
+ title: item.title,
326
+ docNum: chapterNumber,
327
+ id: item.id,
328
+ level: item.level,
329
+ subItems: []
330
+ }
331
+ })
332
+
333
+ contentItems = contentItems.concat(contentItemsFromChapter)
334
+ }
335
+
336
+ contentItems = orderLinks(contentItems)
337
+
338
+ const language =
339
+ LANGUAGES.find(
340
+ lang => lang[0] === this.book.settings.language
341
+ )?.[1] ??
342
+ this.book.settings.language ??
343
+ "en-US"
344
+
345
+ this.textFiles.push({
346
+ filename: "index.html",
347
+ contents: pretty(
348
+ this.indexTemplate({
349
+ contentItems,
350
+ book: this.book,
351
+ creator: this.user.name,
352
+ styleSheets: this.styleSheets,
353
+ language,
354
+ multiDoc: this.multiDoc
355
+ }),
356
+ {ocd: true}
357
+ )
358
+ })
359
+ if (!this.multiDoc) {
360
+ this.joinChapters()
361
+ }
362
+ return this.createZip()
363
+ }
364
+
365
+ joinChapters(): void {
366
+ const styleSheets = this.styleSheets.slice()
367
+ if (this.bibCSS) {
368
+ styleSheets.push(
369
+ this.relativeUrls
370
+ ? {filename: "css/bibliography.css"}
371
+ : {contents: this.bibCSS}
372
+ )
373
+ }
374
+ if (this.math) {
375
+ styleSheets.push(
376
+ this.relativeUrls
377
+ ? {filename: "css/mathlive.css"}
378
+ : {filename: staticUrl("css/mathlive.css")}
379
+ )
380
+ }
381
+ let html = ""
382
+ this.textFiles = this.textFiles
383
+ .sort((a, b) => {
384
+ if (a.filename === "index.html") {
385
+ return -1
386
+ }
387
+ if (b.filename === "index.html") {
388
+ return 1
389
+ }
390
+ const aNum = Number.parseInt(a.filename.match(/\d+/g)?.[0] || "0")
391
+ const bNum = Number.parseInt(b.filename.match(/\d+/g)?.[0] || "0")
392
+ if (aNum < bNum) {
393
+ return -1
394
+ }
395
+ if (aNum > bNum) {
396
+ return 1
397
+ }
398
+ return 0
399
+ })
400
+ .filter(({filename, contents}) => {
401
+ if (filename.slice(-5) !== ".html") {
402
+ return true
403
+ }
404
+ html += contents
405
+ return false
406
+ })
407
+ const css = this.singleFileHTMLBookCSSTemplate({
408
+ papersize: this.book.settings.papersize
409
+ }),
410
+ title = this.book.title,
411
+ settings = this.book.settings,
412
+ htmlDoc = this.singleFileHTMLBookTemplate({
413
+ css,
414
+ html,
415
+ title,
416
+ styleSheets,
417
+ settings
418
+ })
419
+
420
+ this.textFiles.push({
421
+ filename: "index.html",
422
+ contents: pretty(htmlDoc, {ocd: true})
423
+ })
424
+ }
425
+
426
+ addBookStyle(): boolean {
427
+ const bookStyle = this.bookStyles.find(
428
+ (style: BookStyle) => style.slug === this.book.settings.book_style
429
+ )
430
+ if (!bookStyle) {
431
+ return false
432
+ }
433
+ let contents = bookStyle.contents
434
+ bookStyle.bookstylefile_set.forEach(
435
+ ([_url, filename]: [string, string]) =>
436
+ (contents = contents.replace(
437
+ new RegExp(filename, "g"),
438
+ `media/${filename}`
439
+ ))
440
+ )
441
+
442
+ this.styleSheets.push({contents, filename: `css/${bookStyle.slug}.css`})
443
+ this.httpFiles = this.httpFiles.concat(
444
+ bookStyle.bookstylefile_set.map(([url, filename]: [string, string]) => ({
445
+ filename: `css/media/${filename}`,
446
+ url
447
+ }))
448
+ )
449
+ return true
450
+ }
451
+
452
+ async createZip(): Promise<Blob | void> {
453
+ const zipper = new ZipFileCreator(
454
+ this.textFiles.concat(this.styleSheets as TextFile[]) as Array<{
455
+ filename: string
456
+ contents: string
457
+ }>,
458
+ this.httpFiles,
459
+ this.includeZips,
460
+ "application/zip",
461
+ new Date(this.updated * 1000)
462
+ )
463
+ const blob = await zipper.init()
464
+ this.progressCallback?.(gettext("HTML book export complete."), 100)
465
+ return this.download(blob)
466
+ }
467
+
468
+ download(blob: Blob): Blob {
469
+ return blob
470
+ }
471
+
472
+ get defaultFilename(): string {
473
+ return `${createSlug(this.book.title)}.html.zip`
474
+ }
475
+
476
+ getChapterLink(chapterNumber: number): string {
477
+ return `document-${chapterNumber}.html`
478
+ }
479
+ }