-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add pandoc based conversion for imports, fiduswriter/fiduswriter#1277
- Loading branch information
1 parent
827fe1b
commit cc622a0
Showing
9 changed files
with
165 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export const formats = [ | ||
"docx", | ||
"odt", | ||
"doc", | ||
"latex", | ||
"markdown", | ||
"mediawiki", | ||
"org", | ||
"rst", | ||
"textile", | ||
"html", | ||
"json", | ||
"zip" | ||
] |
55 changes: 55 additions & 0 deletions
55
fiduswriter/pandoc/static/js/modules/pandoc/document_overview.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import {getJson} from "../common" | ||
|
||
import {formats} from "./constants" | ||
import {PandocConversionImporter} from "./importer" | ||
|
||
export class DocumentOverviewPandoc { | ||
constructor(documentOverview) { | ||
this.documentOverview = documentOverview | ||
|
||
this.pandocAvailable = null | ||
} | ||
|
||
init() { | ||
this.checkPandoc().then(() => { | ||
if (this.pandocAvailable) { | ||
this.modifyMenu() | ||
this.modifyExternalImporter() | ||
} | ||
}) | ||
} | ||
|
||
checkPandoc() { | ||
if (this.pandocAvailable !== null) { | ||
return Promise.resolve(this.pandocAvailable) | ||
} | ||
|
||
return getJson("/api/pandoc/available/").then(({available}) => { | ||
this.pandocAvailable = available | ||
}) | ||
} | ||
|
||
modifyMenu() { | ||
const menu = this.documentOverview.menu | ||
let menuItem = menu.model.content.find( | ||
menuItem => menuItem.id === "import_external" | ||
) | ||
if (!menuItem) { | ||
menuItem = { | ||
id: "import_external" | ||
} | ||
menu.model.contents.push(menuItem) | ||
} | ||
menuItem.title = gettext("Import document") | ||
menuItem.description = gettext("Import a document in another format") | ||
menuItem.icon = "file" | ||
menu.update() | ||
} | ||
|
||
modifyExternalImporter() { | ||
const actions = this.documentOverview.mod.actions | ||
actions.externalFormats = actions.externalFormats.concat(formats) | ||
actions.externalFormatsTitle = gettext("Import Pandoc JSON/ZIP file") | ||
actions.externalFileImporter = PandocConversionImporter | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const fileToBase64 = file => | ||
new Promise((resolve, reject) => { | ||
const reader = new window.FileReader() | ||
reader.onerror = reject | ||
reader.onload = () => resolve(reader.result.split("base64,")[1]) | ||
reader.readAsDataURL(file) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import {jsonPost} from "../common" | ||
import {PandocImporter} from "../importer/pandoc" | ||
|
||
import {formats} from "./constants" | ||
import {fileToBase64} 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())) { | ||
return this.convertAndImport() | ||
} else { | ||
this.output.statusText = gettext("Unknown file type") | ||
return Promise.resolve(this.output) | ||
} | ||
}) | ||
} | ||
|
||
convertAndImport() { | ||
const from = this.file.name.split(".").pop() | ||
return fileToBase64(this.file) | ||
.then(base64String => { | ||
return jsonPost("/api/pandoc/export/", { | ||
from, | ||
to: "json", | ||
standalone: true, | ||
text: base64String | ||
}) | ||
}) | ||
.then(response => response.json()) | ||
.then(json => { | ||
if (json.error) { | ||
this.output.statusText = json.error | ||
return this.output | ||
} | ||
return this.handlePandocJson(json.output) | ||
}) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
fiduswriter/pandoc/static/js/plugins/documents_overview/pandoc.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {DocumentOverviewPandoc} from "../../modules/pandoc/document_overview" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters