Skip to content

Commit

Permalink
register importers in app, allow both text and binary import formats, f…
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswilm committed Nov 19, 2024
1 parent cc622a0 commit 4ece30f
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 79 deletions.
35 changes: 35 additions & 0 deletions fiduswriter/pandoc/static/js/modules/pandoc/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {getJson} from "../common"

import {formats} from "./constants"
import {PandocConversionImporter} from "./importer"

import {registerImporter} from "../importer/register"

export class AppPandoc {
constructor(app) {
this.app = app

this.pandocAvailable = null
}

init() {
this.checkPandoc().then(() => {
if (this.pandocAvailable) {
registerImporter(
formats.map(format => [format[0], format[1]]),
PandocConversionImporter
)
}
})
}

checkPandoc() {
if (this.pandocAvailable !== null) {
return Promise.resolve(this.pandocAvailable)
}

return getJson("/api/pandoc/available/").then(({available}) => {
this.pandocAvailable = available
})
}
}
28 changes: 16 additions & 12 deletions fiduswriter/pandoc/static/js/modules/pandoc/constants.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
export const formats = [
"docx",
"odt",
"doc",
"latex",
"markdown",
"mediawiki",
"org",
"rst",
"textile",
"html",
"json",
"zip"
// file formats that can be converted by pandoc
// see https://pandoc.org/MANUAL.html#general-options
// for a list of supported formats.
// The first element is the format as presented to the user,
// the second element is the file extensions.
// the third element is the format as used by pandoc,
// the fourth element is whether it is a binary format.
["DOCX", ["docx"], "docx", true],
["LaTeX", ["tex"], "latex", false],
["Markdown", ["md"], "markdown", false],
["JATS XML", ["xml"], "jats", false],
["Emacs Org Mode", ["org"], "org", false],
["reStructuredText", ["rst"], "rst", false],
["Textile", ["textile"], "textile", false],
["HTML", ["html", "htm"], "html", false],
["EPUB", ["epub"], "epub", true]
]
55 changes: 0 additions & 55 deletions fiduswriter/pandoc/static/js/modules/pandoc/document_overview.js

This file was deleted.

15 changes: 15 additions & 0 deletions fiduswriter/pandoc/static/js/modules/pandoc/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,18 @@ export const fileToBase64 = file =>
reader.onload = () => resolve(reader.result.split("base64,")[1])
reader.readAsDataURL(file)
})


export const fileToString = (file, binary = false) => {

if (binary) {
return fileToBase64(file)
} else {
return new Promise((resolve, reject) => {
const reader = new window.FileReader()
reader.onerror = reject
reader.onload = () => resolve(reader.result)
reader.readAsText(file)
})
}
}
30 changes: 19 additions & 11 deletions fiduswriter/pandoc/static/js/modules/pandoc/importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import {jsonPost} from "../common"
import {PandocImporter} from "../importer/pandoc"

import {formats} from "./constants"
import {fileToBase64} from "./helpers"
import {fileToString} from "./helpers"

export class PandocConversionImporter extends PandocImporter {
init() {
return this.getTemplate().then(() => {
if (this.file.type === "application/json") {
return this.importJSON()
} else if (this.file.type === "application/zip") {
return this.importZip()
} else if (formats.includes(this.file.name.split(".").pop())) {
if (
formats
.map(format => format[1])
.flat()
.includes(this.file.name.split(".").pop())
) {
return this.convertAndImport()
} else {
this.output.statusText = gettext("Unknown file type")
Expand All @@ -21,14 +22,17 @@ export class PandocConversionImporter extends PandocImporter {
}

convertAndImport() {
const from = this.file.name.split(".").pop()
return fileToBase64(this.file)
.then(base64String => {
const fromExtension = this.file.name.split(".").pop()
const format = formats.find(format => format[1].includes(fromExtension))
const from = format[2]
const binary = format[3]
return fileToString(this.file, binary)
.then(text => {
return jsonPost("/api/pandoc/export/", {
from,
to: "json",
standalone: true,
text: base64String
text
})
})
.then(response => response.json())
Expand All @@ -37,7 +41,11 @@ export class PandocConversionImporter extends PandocImporter {
this.output.statusText = json.error
return this.output
}
return this.handlePandocJson(json.output)
return this.handlePandocJson(
json.output,
this.additionalFiles?.images,
this.additionalFiles?.bibliography
)
})
}
}
1 change: 1 addition & 0 deletions fiduswriter/pandoc/static/js/plugins/app/pandoc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {AppPandoc} from "../../modules/pandoc/app"

This file was deleted.

0 comments on commit 4ece30f

Please sign in to comment.