Skip to content

Commit

Permalink
convert HTML + print exporters from DOMExporter to string concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswilm committed Dec 10, 2024
1 parent e1d196a commit a4be94a
Show file tree
Hide file tree
Showing 12 changed files with 622 additions and 743 deletions.
10 changes: 5 additions & 5 deletions fiduswriter/book/fixtures/initial_book_data.json

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions fiduswriter/book/migrations/0017_book_styles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 5.1.2 on 2024-12-10 11:49

from django.db import migrations


def article_to_doc_selector(apps, schema_editor):
BookStyle = apps.get_model("book", "BookStyle")
for style in BookStyle.objects.all():
style.contents = style.contents.replace(".article-", ".doc-")
style.save()


def doc_to_article_selector(apps, schema_editor):
BookStyle = apps.get_model("book", "BookStyle")
for style in BookStyle.objects.all():
style.contents = style.contents.replace(".doc-", ".article-")
style.save()


class Migration(migrations.Migration):
dependencies = [
("book", "0016_book_docx_template"),
]

operations = [
migrations.RunPython(
article_to_doc_selector,
doc_to_article_selector,
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from "./templates"
import {
addCategoryLabels,
modifyImages,
orderLinks,
setLinks,
styleEpubFootnotes
Expand All @@ -34,7 +35,6 @@ import {removeHidden} from "../../../exporter/tools/doc_content"
import {createSlug} from "../../../exporter/tools/file"
import {node2Obj, obj2Node} from "../../../exporter/tools/json"
import {ZipFileCreator} from "../../../exporter/tools/zip"
import {modifyImages} from "../html/tools"

export class EpubBookExporter extends DOMExporter {
constructor(schema, csl, bookStyles, book, user, docList, updated) {
Expand Down
31 changes: 31 additions & 0 deletions fiduswriter/book/static/js/modules/books/exporter/epub/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,34 @@ export function addCategoryLabels(htmlEl, language, footnote = false) {
})
return htmlEl
}

export const modifyImages = htmlEl => {
const imageLinks = htmlEl.querySelectorAll("img"),
images = []

imageLinks.forEach((el, index) => {
const src = el.getAttribute("src").split("?")[0]
let filename = `images/${src.split("/").pop()}`
// JPGs are output as PNG elements as well.
if (filename === "images/") {
// name was not retrievable so we give the image a unique numerical
// name like 1.png, 2.jpg, 3.svg, etc. .
filename = `images/${index}`
}

const newImg = document.createElement("img")
// We set the src of the image as "data-src" for now so that the browser
// won't try to load the file immediately
newImg.setAttribute("data-src", filename)
el.parentNode.replaceChild(newImg, el)

if (!images.find(image => image.filename === filename)) {
images.push({
filename,
url: src
})
}
})

return images
}
Loading

0 comments on commit a4be94a

Please sign in to comment.