-
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 books exporter for jats (bits) format, fiduswriter/fiduswriter#1275
- Loading branch information
1 parent
d006bc4
commit afba2a3
Showing
4 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
fiduswriter/book/static/js/modules/books/exporter/bits/index.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,111 @@ | ||
import download from "downloadjs" | ||
import pretty from "pretty" | ||
|
||
import {createSlug} from "../../../exporter/tools/file" | ||
import {JATSExporterConverter} from "../../../exporter/jats/convert" | ||
import {ZipFileCreator} from "../../../exporter/tools/zip" | ||
import {darManifest} from "../../../exporter/jats/templates" | ||
import {getMissingChapterData} from "../tools" | ||
import {addAlert} from "../../../common" | ||
|
||
import {bitsTemplate} from "./templates" | ||
|
||
|
||
export class BITSExporter { | ||
constructor(schema, csl, book, user, docList, updated) { | ||
this.schema = schema | ||
this.csl = csl | ||
this.book = book | ||
this.user = user | ||
this.docList = docList | ||
this.updated = updated | ||
this.type = "book" | ||
this.textFiles = [] | ||
this.httpFiles = [] | ||
|
||
} | ||
|
||
init() { | ||
if (this.book.chapters.length === 0) { | ||
addAlert( | ||
"error", | ||
gettext("Book cannot be exported due to lack of chapters.") | ||
) | ||
return false | ||
} | ||
return getMissingChapterData(this.book, this.docList, this.schema).then( | ||
() => this.export() | ||
) | ||
} | ||
|
||
export() { | ||
this.book.chapters.sort((a, b) => (a.number > b.number ? 1 : -1)) | ||
|
||
const imageDict = {} | ||
Promise.all(this.book.chapters.map(chapter => { | ||
const doc = this.docList.find(doc => doc.id === chapter.text) | ||
const converter = new JATSExporterConverter( | ||
this.type, | ||
doc, | ||
{db: doc.images}, | ||
{db: doc.bibliography} | ||
) | ||
return converter.init().then(({front, body, back, imageIds}) => { | ||
imageIds.forEach( | ||
imageId => (imageDict[imageId] = doc.images[imageId]) | ||
) | ||
return {front, body, back} | ||
}) | ||
})).then( | ||
chapters => this.createFiles(chapters, imageDict) | ||
) | ||
} | ||
|
||
createFiles(chapters, imageDict) { | ||
const images = Object.values(imageDict).map(image => ({ | ||
filename: image.image.split("/").pop().split("?")[0], | ||
url: image.image.split("?")[0], | ||
title: image.title | ||
})) | ||
|
||
this.zipFileName = `${createSlug(this.book.title)}.bits.zip` | ||
|
||
this.textFiles = [ | ||
{ | ||
filename: "manuscript.xml", | ||
contents: pretty(bitsTemplate(chapters), {ocd: true}) | ||
}, | ||
{ | ||
filename: "manifest.xml", | ||
contents: pretty(darManifest({ | ||
title: this.book.title, | ||
type: this.type, | ||
images | ||
}), {ocd: true}) | ||
} | ||
] | ||
|
||
images.forEach(image => { | ||
this.httpFiles.push({filename: image.filename, url: image.url}) | ||
}) | ||
|
||
return this.createZip() | ||
} | ||
|
||
createZip() { | ||
const zipper = new ZipFileCreator( | ||
this.textFiles, | ||
this.httpFiles, | ||
undefined, | ||
undefined, | ||
this.updated | ||
) | ||
return zipper.init().then( | ||
blob => this.download(blob) | ||
) | ||
} | ||
|
||
download(blob) { | ||
return download(blob, this.zipFileName, "application/zip") | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
fiduswriter/book/static/js/modules/books/exporter/bits/templates.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,10 @@ | ||
const bookPartTemplate = ({front, body, back}) => | ||
`<book-part book-part-type="chapter" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ali="http://www.niso.org/schemas/ali/1.0/">${front}${body}${back}</book-part>` | ||
|
||
|
||
export const bitsTemplate = (chapters) => | ||
`<?xml version="1.0" encoding="UTF-8"?> | ||
<!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"> | ||
<book dtd-version="2.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ali="http://www.niso.org/schemas/ali/1.0/"> | ||
${chapters.map((chapter) => bookPartTemplate(chapter)).join("\n")} | ||
</book>` |
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