@fiduswriter/books-document 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -0
- package/README.md +24 -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 +98 -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 +27 -0
- package/dist/exporter/docx/index.d.ts.map +1 -0
- package/dist/exporter/docx/index.js +129 -0
- package/dist/exporter/docx/index.js.map +1 -0
- package/dist/exporter/docx/render.d.ts +14 -0
- package/dist/exporter/docx/render.d.ts.map +1 -0
- package/dist/exporter/docx/render.js +116 -0
- package/dist/exporter/docx/render.js.map +1 -0
- package/dist/exporter/epub/index.d.ts +65 -0
- package/dist/exporter/epub/index.d.ts.map +1 -0
- package/dist/exporter/epub/index.js +268 -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 +223 -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 +67 -0
- package/dist/exporter/html/index.d.ts.map +1 -0
- package/dist/exporter/html/index.js +300 -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 +226 -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 +32 -0
- package/dist/exporter/latex/index.d.ts.map +1 -0
- package/dist/exporter/latex/index.js +102 -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 +20 -0
- package/dist/exporter/native/index.d.ts.map +1 -0
- package/dist/exporter/native/index.js +109 -0
- package/dist/exporter/native/index.js.map +1 -0
- package/dist/exporter/odt/index.d.ts +27 -0
- package/dist/exporter/odt/index.d.ts.map +1 -0
- package/dist/exporter/odt/index.js +123 -0
- package/dist/exporter/odt/index.js.map +1 -0
- package/dist/exporter/odt/render.d.ts +15 -0
- package/dist/exporter/odt/render.d.ts.map +1 -0
- package/dist/exporter/odt/render.js +119 -0
- package/dist/exporter/odt/render.js.map +1 -0
- package/dist/exporter/print/index.d.ts +24 -0
- package/dist/exporter/print/index.d.ts.map +1 -0
- package/dist/exporter/print/index.js +60 -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 +29 -0
- package/dist/exporter/tools.d.ts.map +1 -0
- package/dist/exporter/tools.js +151 -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/importer/native/index.d.ts +34 -0
- package/dist/importer/native/index.d.ts.map +1 -0
- package/dist/importer/native/index.js +152 -0
- package/dist/importer/native/index.js.map +1 -0
- package/dist/importer/native/reader.d.ts +26 -0
- package/dist/importer/native/reader.d.ts.map +1 -0
- package/dist/importer/native/reader.js +105 -0
- package/dist/importer/native/reader.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/schema/index.d.ts +4 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +4 -0
- package/dist/schema/index.js.map +1 -0
- package/dist/types.d.ts +151 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/package.json +101 -0
- package/src/exporter/bits/index.ts +163 -0
- package/src/exporter/bits/templates.ts +107 -0
- package/src/exporter/docx/index.ts +239 -0
- package/src/exporter/docx/render.ts +142 -0
- package/src/exporter/epub/index.ts +426 -0
- package/src/exporter/epub/templates.ts +401 -0
- package/src/exporter/epub/tools.ts +68 -0
- package/src/exporter/html/index.ts +443 -0
- package/src/exporter/html/templates.ts +363 -0
- package/src/exporter/html/tools.ts +26 -0
- package/src/exporter/latex/index.ts +161 -0
- package/src/exporter/latex/templates.ts +35 -0
- package/src/exporter/native/index.ts +187 -0
- package/src/exporter/odt/index.ts +211 -0
- package/src/exporter/odt/render.ts +146 -0
- package/src/exporter/print/index.ts +92 -0
- package/src/exporter/print/templates.ts +8 -0
- package/src/exporter/tools.ts +233 -0
- package/src/global.d.ts +2 -0
- package/src/i18n.ts +936 -0
- package/src/importer/native/index.ts +233 -0
- package/src/importer/native/reader.ts +165 -0
- package/src/index.ts +19 -0
- package/src/modules.d.ts +11 -0
- package/src/schema/index.ts +4 -0
- package/src/types.ts +188 -0
package/package.json
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fiduswriter/books-document",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Fidus Writer book importers and exporters",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "ssh://git@codeberg.org/fiduswriter/fiduswriter-books-document.git"
|
|
8
|
+
},
|
|
9
|
+
"license": "AGPL-3.0",
|
|
10
|
+
"author": "Johannes Wilm",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./schema": {
|
|
20
|
+
"types": "./dist/schema/index.d.ts",
|
|
21
|
+
"default": "./dist/schema/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./importer/native": {
|
|
24
|
+
"types": "./dist/importer/native/index.d.ts",
|
|
25
|
+
"default": "./dist/importer/native/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./exporter/native": {
|
|
28
|
+
"types": "./dist/exporter/native/index.d.ts",
|
|
29
|
+
"default": "./dist/exporter/native/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./exporter/html": {
|
|
32
|
+
"types": "./dist/exporter/html/index.d.ts",
|
|
33
|
+
"default": "./dist/exporter/html/index.js"
|
|
34
|
+
},
|
|
35
|
+
"./exporter/epub": {
|
|
36
|
+
"types": "./dist/exporter/epub/index.d.ts",
|
|
37
|
+
"default": "./dist/exporter/epub/index.js"
|
|
38
|
+
},
|
|
39
|
+
"./exporter/latex": {
|
|
40
|
+
"types": "./dist/exporter/latex/index.d.ts",
|
|
41
|
+
"default": "./dist/exporter/latex/index.js"
|
|
42
|
+
},
|
|
43
|
+
"./exporter/docx": {
|
|
44
|
+
"types": "./dist/exporter/docx/index.d.ts",
|
|
45
|
+
"default": "./dist/exporter/docx/index.js"
|
|
46
|
+
},
|
|
47
|
+
"./exporter/odt": {
|
|
48
|
+
"types": "./dist/exporter/odt/index.d.ts",
|
|
49
|
+
"default": "./dist/exporter/odt/index.js"
|
|
50
|
+
},
|
|
51
|
+
"./exporter/print": {
|
|
52
|
+
"types": "./dist/exporter/print/index.d.ts",
|
|
53
|
+
"default": "./dist/exporter/print/index.js"
|
|
54
|
+
},
|
|
55
|
+
"./exporter/bits": {
|
|
56
|
+
"types": "./dist/exporter/bits/index.d.ts",
|
|
57
|
+
"default": "./dist/exporter/bits/index.js"
|
|
58
|
+
},
|
|
59
|
+
"./exporter/tools": {
|
|
60
|
+
"types": "./dist/exporter/tools.d.ts",
|
|
61
|
+
"default": "./dist/exporter/tools.js"
|
|
62
|
+
},
|
|
63
|
+
"./i18n": {
|
|
64
|
+
"types": "./dist/i18n.d.ts",
|
|
65
|
+
"default": "./dist/i18n.js"
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
"directories": {
|
|
69
|
+
"test": "test"
|
|
70
|
+
},
|
|
71
|
+
"files": [
|
|
72
|
+
"dist/",
|
|
73
|
+
"src/",
|
|
74
|
+
"README.md",
|
|
75
|
+
"LICENSE"
|
|
76
|
+
],
|
|
77
|
+
"scripts": {
|
|
78
|
+
"build": "tsc",
|
|
79
|
+
"prepare": "npm run build",
|
|
80
|
+
"prepublishOnly": "npm run build",
|
|
81
|
+
"typecheck": "tsc --noEmit",
|
|
82
|
+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --config jest.config.js"
|
|
83
|
+
},
|
|
84
|
+
"dependencies": {
|
|
85
|
+
"@fiduswriter/document": "^0.2.0",
|
|
86
|
+
"@vivliostyle/print": "^2.42.0",
|
|
87
|
+
"biblatex-csl-converter": "^3.0.0",
|
|
88
|
+
"downloadjs": "^1.4.7",
|
|
89
|
+
"fast-deep-equal": "^3.1.3",
|
|
90
|
+
"fwtoolkit": "^0.1.3",
|
|
91
|
+
"jszip": "^3.10.1",
|
|
92
|
+
"pretty": "^2.0.0"
|
|
93
|
+
},
|
|
94
|
+
"devDependencies": {
|
|
95
|
+
"@types/node": "^26.0.1",
|
|
96
|
+
"jest": "^30.4.2",
|
|
97
|
+
"ts-jest": "^29.1.2",
|
|
98
|
+
"ts-jest-resolver": "^2.0.1",
|
|
99
|
+
"typescript": "^6.0.3"
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BITS (Book Interchange Tag Suite) book exporter.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type {Schema} from "prosemirror-model"
|
|
6
|
+
import pretty from "pretty"
|
|
7
|
+
import {JATSExporterConverter} from "@fiduswriter/document/exporter/jats/convert"
|
|
8
|
+
import {darManifest} from "@fiduswriter/document/exporter/jats/templates"
|
|
9
|
+
import {createSlug} from "@fiduswriter/document/exporter/tools/file"
|
|
10
|
+
import {ZipFileCreator} from "@fiduswriter/document/exporter/tools/zip"
|
|
11
|
+
import {addAlert} from "fwtoolkit"
|
|
12
|
+
|
|
13
|
+
import type {Book, CSL, DocumentListEntry, User} from "../../types.js"
|
|
14
|
+
import {getMissingChapterData} from "../tools.js"
|
|
15
|
+
import {bitsTemplate} from "./templates.js"
|
|
16
|
+
|
|
17
|
+
interface TextFile {
|
|
18
|
+
filename: string
|
|
19
|
+
contents: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface HttpFile {
|
|
23
|
+
filename: string
|
|
24
|
+
url: string
|
|
25
|
+
title?: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class BITSBookExporter {
|
|
29
|
+
schema: Schema
|
|
30
|
+
csl: CSL
|
|
31
|
+
book: Book
|
|
32
|
+
user: User
|
|
33
|
+
docList: DocumentListEntry[]
|
|
34
|
+
updated: any
|
|
35
|
+
type: string
|
|
36
|
+
textFiles: TextFile[]
|
|
37
|
+
httpFiles: HttpFile[]
|
|
38
|
+
zipFileName: string
|
|
39
|
+
|
|
40
|
+
constructor(
|
|
41
|
+
schema: Schema,
|
|
42
|
+
csl: CSL,
|
|
43
|
+
book: Book,
|
|
44
|
+
user: User,
|
|
45
|
+
docList: DocumentListEntry[],
|
|
46
|
+
updated: any
|
|
47
|
+
) {
|
|
48
|
+
this.schema = schema
|
|
49
|
+
this.csl = csl
|
|
50
|
+
this.book = book
|
|
51
|
+
this.user = user
|
|
52
|
+
this.docList = docList
|
|
53
|
+
this.updated = updated
|
|
54
|
+
this.type = "book"
|
|
55
|
+
this.textFiles = []
|
|
56
|
+
this.httpFiles = []
|
|
57
|
+
this.zipFileName = ""
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
init(): Promise<Blob> | false {
|
|
61
|
+
if (this.book.chapters.length === 0) {
|
|
62
|
+
addAlert(
|
|
63
|
+
"error",
|
|
64
|
+
gettext("Book cannot be exported due to lack of chapters.")
|
|
65
|
+
)
|
|
66
|
+
return false
|
|
67
|
+
}
|
|
68
|
+
return getMissingChapterData(this.book, this.docList, this.schema).then(
|
|
69
|
+
() => this.export()
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export(): Promise<Blob> {
|
|
74
|
+
this.book.chapters.sort((a, b) => (a.number > b.number ? 1 : -1))
|
|
75
|
+
|
|
76
|
+
const imageDict: Record<string, {image: string; title?: string}> = {}
|
|
77
|
+
return Promise.all(
|
|
78
|
+
this.book.chapters.map(chapter => {
|
|
79
|
+
const doc = this.docList.find(d => d.id === chapter.text)
|
|
80
|
+
if (!doc) {
|
|
81
|
+
return Promise.resolve({front: "", body: "", back: ""})
|
|
82
|
+
}
|
|
83
|
+
const converter = new JATSExporterConverter(
|
|
84
|
+
this.type,
|
|
85
|
+
doc,
|
|
86
|
+
this.csl,
|
|
87
|
+
{db: doc.images || {}},
|
|
88
|
+
{db: doc.bibliography || {}}
|
|
89
|
+
)
|
|
90
|
+
return converter.init().then(({front, body, back, imageIds}) => {
|
|
91
|
+
imageIds.forEach(
|
|
92
|
+
(imageId: string | number) =>
|
|
93
|
+
(imageDict[imageId] = doc.images?.[imageId] as {
|
|
94
|
+
image: string
|
|
95
|
+
title?: string
|
|
96
|
+
})
|
|
97
|
+
)
|
|
98
|
+
return {front, body, back}
|
|
99
|
+
})
|
|
100
|
+
})
|
|
101
|
+
).then(chapters => this.createFiles(chapters, imageDict))
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
createFiles(
|
|
105
|
+
chapters: Array<{front: string; body: string; back: string}>,
|
|
106
|
+
imageDict: Record<string, {image: string; title?: string}>
|
|
107
|
+
): Promise<Blob> {
|
|
108
|
+
const images = Object.values(imageDict).map(image => ({
|
|
109
|
+
filename: image.image.split("/").pop()?.split("?")[0] || "",
|
|
110
|
+
url: image.image.split("?")[0],
|
|
111
|
+
title: image.title || ""
|
|
112
|
+
}))
|
|
113
|
+
|
|
114
|
+
this.zipFileName = `${createSlug(this.book.title)}.bits.zip`
|
|
115
|
+
|
|
116
|
+
this.textFiles = [
|
|
117
|
+
{
|
|
118
|
+
filename: "manuscript.xml",
|
|
119
|
+
contents: pretty(bitsTemplate(this.book, chapters), {ocd: true})
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
filename: "manifest.xml",
|
|
123
|
+
contents: pretty(
|
|
124
|
+
darManifest({
|
|
125
|
+
title: this.book.title,
|
|
126
|
+
type: this.type,
|
|
127
|
+
images
|
|
128
|
+
}),
|
|
129
|
+
{ocd: true}
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
]
|
|
133
|
+
|
|
134
|
+
images.forEach(image => {
|
|
135
|
+
this.httpFiles.push({
|
|
136
|
+
filename: image.filename,
|
|
137
|
+
url: image.url,
|
|
138
|
+
title: image.title
|
|
139
|
+
})
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
return this.createZip()
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
createZip(): Promise<Blob> {
|
|
146
|
+
const zipper = new ZipFileCreator(
|
|
147
|
+
this.textFiles,
|
|
148
|
+
this.httpFiles,
|
|
149
|
+
undefined,
|
|
150
|
+
undefined,
|
|
151
|
+
this.updated
|
|
152
|
+
)
|
|
153
|
+
return zipper.init().then(blob => this.download(blob))
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
download(blob: Blob): Blob {
|
|
157
|
+
return blob
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
get defaultFilename(): string {
|
|
161
|
+
return this.zipFileName
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import {escapeText} from "fwtoolkit"
|
|
2
|
+
|
|
3
|
+
import type {Book, BitsTemplateParams} from "../../types.js"
|
|
4
|
+
|
|
5
|
+
const bookPartTemplate = ({front, body, back}: BitsTemplateParams) =>
|
|
6
|
+
`<book-part book-part-type="chapter" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ali="http://www.niso.org/schemas/ali/1.0/" xmlns:mml="http://www.w3.org/1998/Math/MathML">${front}${body}${back}</book-part>`
|
|
7
|
+
|
|
8
|
+
export const bitsTemplate = (
|
|
9
|
+
book: Book,
|
|
10
|
+
chapters: Array<{front: string; body: string; back: string}>
|
|
11
|
+
) => {
|
|
12
|
+
const updated = new Date((book.updated || 0) * 1000)
|
|
13
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
14
|
+
<!DOCTYPE book PUBLIC "-//NLM//DTD BITS Book Interchange DTD v2.1 20220202//EN" "https://jats.nlm.nih.gov/extensions/bits/2.1/BITS-book2-1.dtd">
|
|
15
|
+
<book dtd-version="2.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ali="http://www.niso.org/schemas/ali/1.0/">
|
|
16
|
+
<processing-meta tagset-family="bits" table-model="xhtml" math-representation="tex mathml"></processing-meta>
|
|
17
|
+
<book-meta>
|
|
18
|
+
<book-title-group>
|
|
19
|
+
<book-title>${escapeText(book.title)}</book-title>
|
|
20
|
+
${book.metadata.subtitle?.length ? `<subtitle>${escapeText(book.metadata.subtitle)}</subtitle>` : ""}
|
|
21
|
+
</book-title-group>
|
|
22
|
+
${
|
|
23
|
+
book.metadata.version?.length
|
|
24
|
+
? `<content-version>${escapeText(book.metadata.version)}</content-version>`
|
|
25
|
+
: ""
|
|
26
|
+
}
|
|
27
|
+
<content-language>${book.settings.language.split("-")[0]}</content-language>
|
|
28
|
+
${
|
|
29
|
+
book.metadata.description?.length
|
|
30
|
+
? `<abstract>
|
|
31
|
+
<p>${escapeText(book.metadata.description)}</p>
|
|
32
|
+
</abstract>`
|
|
33
|
+
: ""
|
|
34
|
+
}
|
|
35
|
+
${
|
|
36
|
+
book.metadata.author?.length
|
|
37
|
+
? `<contrib-group>
|
|
38
|
+
<contrib contrib-type="author">
|
|
39
|
+
<name>
|
|
40
|
+
<surname>${escapeText(book.metadata.author.split(" ").slice(1).join(" "))}</surname>
|
|
41
|
+
<given-names>${escapeText(book.metadata.author.split(" ").pop() || "")}</given-names>
|
|
42
|
+
</name>
|
|
43
|
+
</contrib>
|
|
44
|
+
</contrib-group>`
|
|
45
|
+
: ""
|
|
46
|
+
}
|
|
47
|
+
<pub-date>
|
|
48
|
+
<year>${updated.getFullYear()}</year>
|
|
49
|
+
<month>${updated.getMonth() + 1}</month>
|
|
50
|
+
<day>${updated.getDate()}</day>
|
|
51
|
+
</pub-date>
|
|
52
|
+
${
|
|
53
|
+
book.metadata.publication_date?.length
|
|
54
|
+
? `<pub-date date-type="pub" publication-format="electronic">
|
|
55
|
+
<string-date>${escapeText(book.metadata.publication_date)}</string-date>
|
|
56
|
+
</pub-date>`
|
|
57
|
+
: ""
|
|
58
|
+
}
|
|
59
|
+
${
|
|
60
|
+
book.metadata.isbn?.length
|
|
61
|
+
? `<isbn>${escapeText(book.metadata.isbn)}</isbn>`
|
|
62
|
+
: ""
|
|
63
|
+
}
|
|
64
|
+
${
|
|
65
|
+
book.metadata.publisher?.length
|
|
66
|
+
? `<publisher>
|
|
67
|
+
<publisher-name>${escapeText(book.metadata.publisher)}</publisher-name>
|
|
68
|
+
</publisher>`
|
|
69
|
+
: ""
|
|
70
|
+
}
|
|
71
|
+
${
|
|
72
|
+
book.metadata.series_title?.length
|
|
73
|
+
? `<series>
|
|
74
|
+
<series-title>${escapeText(book.metadata.series_title)}</series-title>
|
|
75
|
+
${
|
|
76
|
+
book.metadata.series_position?.length
|
|
77
|
+
? `<series-text>${escapeText(book.metadata.series_position)}</series-text>`
|
|
78
|
+
: ""
|
|
79
|
+
}
|
|
80
|
+
</series>`
|
|
81
|
+
: ""
|
|
82
|
+
}
|
|
83
|
+
${
|
|
84
|
+
book.metadata.copyright?.length
|
|
85
|
+
? `<permissions>
|
|
86
|
+
<copyright-holder>
|
|
87
|
+
<name>${escapeText(book.metadata.copyright)}</name>
|
|
88
|
+
</copyright-holder>
|
|
89
|
+
</permissions>`
|
|
90
|
+
: ""
|
|
91
|
+
}
|
|
92
|
+
${
|
|
93
|
+
book.metadata.keywords?.length
|
|
94
|
+
? `<kwd-group>${book.metadata.keywords
|
|
95
|
+
.split(",")
|
|
96
|
+
.map(
|
|
97
|
+
keyword => `<kwd>${escapeText(keyword.trim())}</kwd>`
|
|
98
|
+
)
|
|
99
|
+
.join("")}</kwd-group>`
|
|
100
|
+
: ""
|
|
101
|
+
}
|
|
102
|
+
</book-meta>
|
|
103
|
+
<book-body>
|
|
104
|
+
${chapters.map(chapter => bookPartTemplate(chapter)).join("\n")}
|
|
105
|
+
</book-body>
|
|
106
|
+
</book>`
|
|
107
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DOCX book exporter.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type {Schema} from "prosemirror-model"
|
|
6
|
+
import type {CSL, User} from "@fiduswriter/document"
|
|
7
|
+
import {
|
|
8
|
+
fixTables,
|
|
9
|
+
removeHidden
|
|
10
|
+
} from "@fiduswriter/document/exporter/tools/doc_content"
|
|
11
|
+
import {createSlug} from "@fiduswriter/document/exporter/tools/file"
|
|
12
|
+
import {XmlZip} from "@fiduswriter/document/exporter/tools/xml_zip"
|
|
13
|
+
import {addAlert} from "fwtoolkit"
|
|
14
|
+
|
|
15
|
+
import {DOCXExporterCitations} from "@fiduswriter/document/exporter/docx/citations"
|
|
16
|
+
import {DOCXExporterComments} from "@fiduswriter/document/exporter/docx/comments"
|
|
17
|
+
import {DOCXExporterFootnotes} from "@fiduswriter/document/exporter/docx/footnotes"
|
|
18
|
+
import {DOCXExporterImages} from "@fiduswriter/document/exporter/docx/images"
|
|
19
|
+
import {DOCXExporterLists} from "@fiduswriter/document/exporter/docx/lists"
|
|
20
|
+
import {DOCXExporterMath} from "@fiduswriter/document/exporter/docx/math"
|
|
21
|
+
import {DOCXExporterMetadata} from "@fiduswriter/document/exporter/docx/metadata"
|
|
22
|
+
import {DOCXExporterRels} from "@fiduswriter/document/exporter/docx/rels"
|
|
23
|
+
import {DOCXExporterRichtext} from "@fiduswriter/document/exporter/docx/richtext"
|
|
24
|
+
import {DOCXExporterTables} from "@fiduswriter/document/exporter/docx/tables"
|
|
25
|
+
import {moveFootnoteComments} from "@fiduswriter/document/exporter/docx/tools"
|
|
26
|
+
|
|
27
|
+
import type {Book, DocumentListEntry} from "../../types.js"
|
|
28
|
+
import {getMissingChapterData} from "../tools.js"
|
|
29
|
+
import {DOCXBookExporterRender} from "./render.js"
|
|
30
|
+
|
|
31
|
+
export class DOCXBookExporter {
|
|
32
|
+
schema: Schema
|
|
33
|
+
csl: CSL
|
|
34
|
+
book: Book
|
|
35
|
+
user: User
|
|
36
|
+
docList: DocumentListEntry[]
|
|
37
|
+
templateUrl: string
|
|
38
|
+
updated: any
|
|
39
|
+
textFiles: Array<Record<string, unknown>>
|
|
40
|
+
httpFiles: Array<Record<string, unknown>>
|
|
41
|
+
mimeType: string
|
|
42
|
+
|
|
43
|
+
constructor(
|
|
44
|
+
schema: Schema,
|
|
45
|
+
csl: CSL,
|
|
46
|
+
book: Book,
|
|
47
|
+
user: User,
|
|
48
|
+
docList: DocumentListEntry[],
|
|
49
|
+
updated: any
|
|
50
|
+
) {
|
|
51
|
+
this.schema = schema
|
|
52
|
+
this.csl = csl
|
|
53
|
+
this.book = book
|
|
54
|
+
this.user = user
|
|
55
|
+
this.docList = docList
|
|
56
|
+
this.templateUrl = book.docx_template || ""
|
|
57
|
+
this.updated = updated
|
|
58
|
+
this.textFiles = []
|
|
59
|
+
this.httpFiles = []
|
|
60
|
+
|
|
61
|
+
this.mimeType =
|
|
62
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
init(): Promise<Blob> | false {
|
|
66
|
+
if (this.book.chapters.length === 0) {
|
|
67
|
+
addAlert(
|
|
68
|
+
"error",
|
|
69
|
+
gettext("Book cannot be exported due to lack of chapters.")
|
|
70
|
+
)
|
|
71
|
+
return false
|
|
72
|
+
}
|
|
73
|
+
return getMissingChapterData(
|
|
74
|
+
this.book,
|
|
75
|
+
this.docList,
|
|
76
|
+
this.schema,
|
|
77
|
+
{rawContent: true}
|
|
78
|
+
).then(() => this.export())
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export(): Promise<Blob> {
|
|
82
|
+
this.book.chapters.sort((a, b) => (a.number > b.number ? 1 : -1))
|
|
83
|
+
const xml = new XmlZip(this.templateUrl, this.mimeType)
|
|
84
|
+
|
|
85
|
+
const tables = new DOCXExporterTables(xml as any)
|
|
86
|
+
const math = new DOCXExporterMath(xml as any)
|
|
87
|
+
const render = new DOCXBookExporterRender(xml as any)
|
|
88
|
+
const rels = new DOCXExporterRels(xml as any, "document")
|
|
89
|
+
const metadata = new DOCXExporterMetadata(
|
|
90
|
+
xml as any,
|
|
91
|
+
this.getBaseMetadata() as any
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
return xml
|
|
95
|
+
.init()
|
|
96
|
+
.then(() => metadata.init())
|
|
97
|
+
.then(() => tables.init())
|
|
98
|
+
.then(() => math.init())
|
|
99
|
+
.then(() => render.init())
|
|
100
|
+
.then(() => rels.init())
|
|
101
|
+
.then(() => this.exportChapters(xml as any, render, rels, math, tables))
|
|
102
|
+
.then(() => xml.prepareBlob())
|
|
103
|
+
.then(blob => this.download(blob))
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
exportChapters(
|
|
107
|
+
xml: any,
|
|
108
|
+
render: DOCXBookExporterRender,
|
|
109
|
+
rels: any,
|
|
110
|
+
math: any,
|
|
111
|
+
tables: any
|
|
112
|
+
): Promise<void> {
|
|
113
|
+
return this.book.chapters
|
|
114
|
+
.map((chapter, chapterIndex) => {
|
|
115
|
+
return (): Promise<void> => {
|
|
116
|
+
const doc = this.docList.find(d => d.id === chapter.text)
|
|
117
|
+
if (!doc) {
|
|
118
|
+
return Promise.resolve()
|
|
119
|
+
}
|
|
120
|
+
const docContent = moveFootnoteComments(
|
|
121
|
+
fixTables(
|
|
122
|
+
removeHidden(doc.rawContent as any) as any
|
|
123
|
+
) as any
|
|
124
|
+
) as any
|
|
125
|
+
|
|
126
|
+
const images = new DOCXExporterImages(
|
|
127
|
+
docContent as any,
|
|
128
|
+
{db: doc.images || {}},
|
|
129
|
+
xml,
|
|
130
|
+
rels
|
|
131
|
+
)
|
|
132
|
+
const lists = new DOCXExporterLists(docContent, xml, rels)
|
|
133
|
+
const citations = new DOCXExporterCitations(
|
|
134
|
+
docContent,
|
|
135
|
+
doc.settings,
|
|
136
|
+
{db: doc.bibliography || {}},
|
|
137
|
+
this.csl,
|
|
138
|
+
xml
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
const footnotes = new DOCXExporterFootnotes(
|
|
142
|
+
doc as any,
|
|
143
|
+
docContent,
|
|
144
|
+
doc.settings,
|
|
145
|
+
{db: doc.images || {}},
|
|
146
|
+
{db: doc.bibliography || {}},
|
|
147
|
+
xml,
|
|
148
|
+
citations,
|
|
149
|
+
this.csl,
|
|
150
|
+
lists,
|
|
151
|
+
math,
|
|
152
|
+
tables,
|
|
153
|
+
rels
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
const richtext = new DOCXExporterRichtext(
|
|
157
|
+
doc as any,
|
|
158
|
+
doc.settings,
|
|
159
|
+
lists,
|
|
160
|
+
footnotes,
|
|
161
|
+
math,
|
|
162
|
+
tables,
|
|
163
|
+
rels,
|
|
164
|
+
citations,
|
|
165
|
+
images
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
const comments = new DOCXExporterComments(
|
|
169
|
+
docContent,
|
|
170
|
+
doc.comments || {},
|
|
171
|
+
xml,
|
|
172
|
+
rels,
|
|
173
|
+
richtext
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
return citations
|
|
177
|
+
.init()
|
|
178
|
+
.then(() => images.init())
|
|
179
|
+
.then(() => comments.init())
|
|
180
|
+
.then(() => lists.init())
|
|
181
|
+
.then(() => footnotes.init())
|
|
182
|
+
.then(() => {
|
|
183
|
+
const pmBib = footnotes.pmBib || citations.pmBib
|
|
184
|
+
render.render(
|
|
185
|
+
docContent as any,
|
|
186
|
+
pmBib,
|
|
187
|
+
doc.settings,
|
|
188
|
+
richtext,
|
|
189
|
+
citations,
|
|
190
|
+
chapterIndex
|
|
191
|
+
)
|
|
192
|
+
return Promise.resolve()
|
|
193
|
+
})
|
|
194
|
+
}
|
|
195
|
+
})
|
|
196
|
+
.reduce(
|
|
197
|
+
(promiseChain, currentChapter) =>
|
|
198
|
+
promiseChain.then(() => currentChapter()),
|
|
199
|
+
Promise.resolve()
|
|
200
|
+
)
|
|
201
|
+
.then(() =>
|
|
202
|
+
render.renderAmbles(
|
|
203
|
+
Object.assign(
|
|
204
|
+
{
|
|
205
|
+
title: this.book.title,
|
|
206
|
+
language: this.book.settings.language
|
|
207
|
+
},
|
|
208
|
+
this.book.metadata
|
|
209
|
+
)
|
|
210
|
+
)
|
|
211
|
+
)
|
|
212
|
+
.then(() => render.assemble())
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
getBaseMetadata(): Record<string, unknown> {
|
|
216
|
+
const authors = this.book.metadata.author?.length
|
|
217
|
+
? [{institution: this.book.metadata.author}]
|
|
218
|
+
: []
|
|
219
|
+
const keywords = this.book.metadata.keywords?.length
|
|
220
|
+
? this.book.metadata.keywords
|
|
221
|
+
.split(",")
|
|
222
|
+
.map(keyword => keyword.trim())
|
|
223
|
+
: []
|
|
224
|
+
return {
|
|
225
|
+
authors,
|
|
226
|
+
keywords,
|
|
227
|
+
title: this.book.title,
|
|
228
|
+
language: this.book.settings.language
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
download(blob: Blob): Blob {
|
|
233
|
+
return blob
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
get defaultFilename(): string {
|
|
237
|
+
return `${createSlug(this.book.title)}.docx`
|
|
238
|
+
}
|
|
239
|
+
}
|