Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Odt export #20

Merged
merged 5 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions fiduswriter/book/migrations/0015_book_odt_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.14 on 2024-08-28 06:08

import book.models
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("book", "0014_add_language"),
]

operations = [
migrations.AddField(
model_name="book",
name="odt_template",
field=models.FileField(
blank=True,
null=True,
upload_to=book.models.export_template_filename,
),
),
]
8 changes: 8 additions & 0 deletions fiduswriter/book/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
from usermedia.models import Image


def export_template_filename(instance, filename):
instance.title = filename.split(".")[0]
return "/".join(["book-export-template-files", filename])


class Book(models.Model):
title = models.CharField(max_length=128)
path = models.TextField(default="", blank=True)
Expand All @@ -20,6 +25,9 @@ class Book(models.Model):
default=None,
on_delete=models.deletion.CASCADE,
)
odt_template = models.FileField(
upload_to=export_template_filename, blank=True, null=True
)
chapters = models.ManyToManyField(
Document, through="Chapter", blank=True, default=None
)
Expand Down
85 changes: 64 additions & 21 deletions fiduswriter/book/static/js/modules/books/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
bookChapterListTemplate,
bookChapterDialogTemplate,
bookEpubDataCoverTemplate,
bookSanityCheckTemplate
bookSanityCheckTemplate,
bookODTDataTemplate,
bookODTDataRowTemplate
} from "./templates"
import {exportMenuModel} from "./menu"
import {bookSanityCheck} from "./sanity_check"
Expand Down Expand Up @@ -63,6 +65,11 @@ export class BookActions {
description: gettext("Epub related settings"),
template: bookEpubDataTemplate
},
{
title: gettext("ODT"),
description: gettext("ODT related settings"),
template: bookODTDataTemplate
},
{
title: gettext("Print/PDF"),
description: gettext("Print related settings"),
Expand Down Expand Up @@ -522,29 +529,60 @@ export class BookActions {
? imageDB.db[image.id]
: bookImageDB.db[image.id]
}
document.getElementById(
"figure-preview-row"
).innerHTML = bookEpubDataCoverTemplate({
imageDB: {
db: Object.assign(
{},
imageDB.db,
bookImageDB.db
)
},
book
const coverPreviewRow = document.getElementById(
"cover-preview-row"
)
if (coverPreviewRow) {
coverPreviewRow.innerHTML = bookEpubDataCoverTemplate({
book,
imageDB: {db: Object.assign({}, imageDB.db, bookImageDB.db)}
})
}
})
break
}
case findTarget(event, "#select-odt-template", el): {
const fileSelector = document.createElement("input")
fileSelector.type = "file"
fileSelector.accept = ".odt"
fileSelector.addEventListener("change", event => {
const file = event.target.files[0]
return postJson(
"/api/book/odt_template/save/",
{id: book.id, file}
).then(({status, json}) => {
if (status !== 200) {
return
}
book.odt_template = json.odt_template
const odtTemplateRow = document.getElementById("odt-template-row")
if (odtTemplateRow) {
odtTemplateRow.innerHTML = bookODTDataRowTemplate({book})
}
})
})
fileSelector.click()
break
}
case findTarget(event, "#remove-cover-image-button", el):
case findTarget(event, "#remove-cover-image-button", el): {
delete book.cover_image
document.getElementById("figure-preview-row").innerHTML =
bookEpubDataCoverTemplate({
book,
imageDB: {db: {}} // We just deleted the cover image, so we don't need a full DB
})
const coverPreviewRow = document.getElementById("cover-preview-row")
if (coverPreviewRow) {
coverPreviewRow.innerHTML = bookEpubDataCoverTemplate({
book,
imageDB: {db: {}} // We just deleted the cover image, so we don't need a full DB
})
}
break
}
case findTarget(event, "#remove-odt-template-button", el): {
delete book.odt_template
const odtTemplateRow = document.getElementById("odt-template-row")
if (odtTemplateRow) {
odtTemplateRow.innerHTML = bookODTDataRowTemplate({book})
}
break
}
case findTarget(event, "#perform-sanity-check-button", el): {
this.saveBook(book, oldBookId)
.then(() =>
Expand All @@ -555,10 +593,15 @@ export class BookActions {
)
)
.then(
sanityCheckOutputHTML =>
(document.getElementById(
sanityCheckOutputHTML => {
const sanityCheckOutput = document.getElementById(
"sanity-check-output"
).innerHTML = sanityCheckOutputHTML)
)
if (sanityCheckOutput) {
sanityCheckOutput.innerHTML = sanityCheckOutputHTML
}
}

)
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {addAlert} from "../../../common"
import {bitsTemplate} from "./templates"


export class BITSExporter {
export class BITSBookExporter {
constructor(schema, csl, book, user, docList, updated) {
this.schema = schema
this.csl = csl
Expand Down
155 changes: 155 additions & 0 deletions fiduswriter/book/static/js/modules/books/exporter/odt/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import download from "downloadjs"

import {removeHidden, fixTables} from "../../../exporter/tools/doc_content"
import {createSlug} from "../../../exporter/tools/file"
import {XmlZip} from "../../../exporter/tools/xml_zip"
import {getMissingChapterData} from "../tools"
import {addAlert} from "../../../common"


import {ODTExporterCitations} from "../../../exporter/odt/citations"
import {ODTExporterImages} from "../../../exporter/odt/images"

import {ODTExporterRichtext} from "../../../exporter/odt/richtext"
import {ODTExporterFootnotes} from "../../../exporter/odt/footnotes"
import {ODTExporterMetadata} from "../../../exporter/odt/metadata"
import {ODTExporterStyles} from "../../../exporter/odt/styles"
import {ODTExporterMath} from "../../../exporter/odt/math"
import {ODTExporterTracks} from "../../../exporter/odt/track"

import {ODTBookExporterRender} from "./render"


export class ODTBookExporter {
constructor(schema, csl, book, user, docList, updated) {
this.schema = schema
this.csl = csl
this.book = book
this.user = user
this.docList = docList
this.templateUrl = book.odt_template
this.updated = updated
this.textFiles = []
this.httpFiles = []

this.mimeType = "application/vnd.oasis.opendocument.text"
}

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, true).then(
() => this.export()
)
}

export() {
this.book.chapters.sort((a, b) => (a.number > b.number ? 1 : -1))
const xml = new XmlZip(
this.templateUrl,
this.mimeType
)
const styles = new ODTExporterStyles(xml)
const math = new ODTExporterMath(xml)
const tracks = new ODTExporterTracks(xml)
const render = new ODTBookExporterRender(xml, styles)
const metadata = new ODTExporterMetadata(xml, styles, this.getBaseMetadata())

return xml.init().then(
() => styles.init()
).then(
() => tracks.init()
).then(
() => render.init()
).then(
() => metadata.init()
).then(
() => math.init()
).then(
() => this.exportChapters(xml, render, styles, math, tracks)
).then(
() => xml.prepareBlob()
).then(
blob => this.download(blob)
)
}

exportChapters(xml, render, styles, math, tracks) {

return this.book.chapters.map(chapter => {
return () => {
const doc = this.docList.find(doc => doc.id === chapter.text)
const docContent = fixTables(removeHidden(doc.rawContent))
const citations = new ODTExporterCitations(
docContent,
doc.settings,
styles,
{db: doc.bibliography},
this.csl
)
const footnotes = new ODTExporterFootnotes(
docContent,
doc.settings,
xml,
citations,
styles,
{db: doc.bibliography},
{db: doc.images},
this.csl
)

const images = new ODTExporterImages(
docContent,
xml,
{db: doc.images}
)
const richtext = new ODTExporterRichtext(
doc.comments,
doc.settings,
styles,
tracks,
footnotes,
citations,
math,
images
)
return citations.init().then(
() => images.init()
).then(
() => footnotes.init()
).then(
() => {
const pmBib = footnotes.pmBib || citations.pmBib
render.render(docContent, pmBib, doc.settings, richtext, citations)
return Promise.resolve()
}
)
}
}).reduce((promiseChain, currentChapter) => {
return promiseChain.then(() => currentChapter())
}, Promise.resolve()).then(
() => {
return render.assemble()
}
)
}


getBaseMetadata() {
return {
authors: [],
keywords: [],
title: this.book.title,
language: this.book.settings.language
}
}

download(blob) {
return download(blob, createSlug(this.book.title) + ".odt", this.mimeType)
}
}
Loading