Skip to content

Commit 4ece30f

Browse files
committed
register importers in app, allow both text and binary import formats, fiduswriter/fiduswriter#1277
1 parent cc622a0 commit 4ece30f

File tree

7 files changed

+86
-79
lines changed

7 files changed

+86
-79
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {getJson} from "../common"
2+
3+
import {formats} from "./constants"
4+
import {PandocConversionImporter} from "./importer"
5+
6+
import {registerImporter} from "../importer/register"
7+
8+
export class AppPandoc {
9+
constructor(app) {
10+
this.app = app
11+
12+
this.pandocAvailable = null
13+
}
14+
15+
init() {
16+
this.checkPandoc().then(() => {
17+
if (this.pandocAvailable) {
18+
registerImporter(
19+
formats.map(format => [format[0], format[1]]),
20+
PandocConversionImporter
21+
)
22+
}
23+
})
24+
}
25+
26+
checkPandoc() {
27+
if (this.pandocAvailable !== null) {
28+
return Promise.resolve(this.pandocAvailable)
29+
}
30+
31+
return getJson("/api/pandoc/available/").then(({available}) => {
32+
this.pandocAvailable = available
33+
})
34+
}
35+
}
Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
export const formats = [
2-
"docx",
3-
"odt",
4-
"doc",
5-
"latex",
6-
"markdown",
7-
"mediawiki",
8-
"org",
9-
"rst",
10-
"textile",
11-
"html",
12-
"json",
13-
"zip"
2+
// file formats that can be converted by pandoc
3+
// see https://pandoc.org/MANUAL.html#general-options
4+
// for a list of supported formats.
5+
// The first element is the format as presented to the user,
6+
// the second element is the file extensions.
7+
// the third element is the format as used by pandoc,
8+
// the fourth element is whether it is a binary format.
9+
["DOCX", ["docx"], "docx", true],
10+
["LaTeX", ["tex"], "latex", false],
11+
["Markdown", ["md"], "markdown", false],
12+
["JATS XML", ["xml"], "jats", false],
13+
["Emacs Org Mode", ["org"], "org", false],
14+
["reStructuredText", ["rst"], "rst", false],
15+
["Textile", ["textile"], "textile", false],
16+
["HTML", ["html", "htm"], "html", false],
17+
["EPUB", ["epub"], "epub", true]
1418
]

fiduswriter/pandoc/static/js/modules/pandoc/document_overview.js

Lines changed: 0 additions & 55 deletions
This file was deleted.

fiduswriter/pandoc/static/js/modules/pandoc/helpers.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,18 @@ export const fileToBase64 = file =>
55
reader.onload = () => resolve(reader.result.split("base64,")[1])
66
reader.readAsDataURL(file)
77
})
8+
9+
10+
export const fileToString = (file, binary = false) => {
11+
12+
if (binary) {
13+
return fileToBase64(file)
14+
} else {
15+
return new Promise((resolve, reject) => {
16+
const reader = new window.FileReader()
17+
reader.onerror = reject
18+
reader.onload = () => resolve(reader.result)
19+
reader.readAsText(file)
20+
})
21+
}
22+
}

fiduswriter/pandoc/static/js/modules/pandoc/importer.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ import {jsonPost} from "../common"
22
import {PandocImporter} from "../importer/pandoc"
33

44
import {formats} from "./constants"
5-
import {fileToBase64} from "./helpers"
5+
import {fileToString} from "./helpers"
66

77
export class PandocConversionImporter extends PandocImporter {
88
init() {
99
return this.getTemplate().then(() => {
10-
if (this.file.type === "application/json") {
11-
return this.importJSON()
12-
} else if (this.file.type === "application/zip") {
13-
return this.importZip()
14-
} else if (formats.includes(this.file.name.split(".").pop())) {
10+
if (
11+
formats
12+
.map(format => format[1])
13+
.flat()
14+
.includes(this.file.name.split(".").pop())
15+
) {
1516
return this.convertAndImport()
1617
} else {
1718
this.output.statusText = gettext("Unknown file type")
@@ -21,14 +22,17 @@ export class PandocConversionImporter extends PandocImporter {
2122
}
2223

2324
convertAndImport() {
24-
const from = this.file.name.split(".").pop()
25-
return fileToBase64(this.file)
26-
.then(base64String => {
25+
const fromExtension = this.file.name.split(".").pop()
26+
const format = formats.find(format => format[1].includes(fromExtension))
27+
const from = format[2]
28+
const binary = format[3]
29+
return fileToString(this.file, binary)
30+
.then(text => {
2731
return jsonPost("/api/pandoc/export/", {
2832
from,
2933
to: "json",
3034
standalone: true,
31-
text: base64String
35+
text
3236
})
3337
})
3438
.then(response => response.json())
@@ -37,7 +41,11 @@ export class PandocConversionImporter extends PandocImporter {
3741
this.output.statusText = json.error
3842
return this.output
3943
}
40-
return this.handlePandocJson(json.output)
44+
return this.handlePandocJson(
45+
json.output,
46+
this.additionalFiles?.images,
47+
this.additionalFiles?.bibliography
48+
)
4149
})
4250
}
4351
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {AppPandoc} from "../../modules/pandoc/app"

fiduswriter/pandoc/static/js/plugins/documents_overview/pandoc.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)