Make background fiddling work

master
Dario Ernst 4 years ago
parent 129e0bad07
commit 4712618343

@ -1,9 +1,8 @@
#!/bin/bash #!/bin/bash
rm -rf public/ rm -rf public/ pdf/
hugo
rm -rf pdf
mkdir pdf mkdir pdf
hugo
for dir in public/print/* for dir in public/print/*
do do
chromium --temp-profile --headless --disable-gpu --run-all-compositor-stages-before-draw --allow-file-access-from-files --allow-running-insecure-content --print-to-pdf-no-header --print-to-pdf="pdf/$(basename $dir).pdf" "${dir}/print.html" chromium --temp-profile --headless --disable-gpu --run-all-compositor-stages-before-draw --allow-file-access-from-files --allow-running-insecure-content --print-to-pdf-no-header --print-to-pdf="pdf/$(basename $dir).pdf" "${dir}/print.html"

@ -139,7 +139,7 @@
</div> </div>
</div> </div>
</div> </div>
<div id="spacer" /> <div id="spacer" style="position: absolute; top: 0px; right: 0mm; width: 156mm;"/>
</body> </body>
{{ partial "printscript.html" }} {{ partial "printscript.html" }}
</html> </html>

@ -1,90 +1,105 @@
// Shrink up the spacing and change the size of the headers // Shrink up the spacing and change the size of the headers
u('#ingredients').attr("style", "margin-bottom: .5em;") u("#ingredients").attr("style", "margin-bottom: .5em;");
u('#ingredients').attr("class", "is-size-5") u("#ingredients").attr("class", "is-size-5");
u('#directions').attr("style", "margin: 0px;") u("#directions").attr("style", "margin: 0px;");
u('#directions').attr("class", "is-size-5") u("#directions").attr("class", "is-size-5");
// If we have ingredients subheadings, change the h4 headings // If we have ingredients subheadings, change the h4 headings
u('h4').addClass("is-size-6") u("h4").addClass("is-size-6");
u('h4').attr("style", "margin: 0px;") u("h4").attr("style", "margin: 0px;");
// Change the title size // Change the title size
u('#mainTitle').addClass("is-size-4") u("#mainTitle").addClass("is-size-4");
// Change the lists margins // Change the lists margins
var directions = u('ol'); var directions = u("ol");
directions.attr("style", "margin-top: 0px; margin-bottom: 0px;") directions.attr("style", "margin-top: 0px; margin-bottom: 0px;");
var ingredients = u('ul'); var ingredients = u("ul");
ingredients.attr("style", "margin-top: 0px; margin-bottom: 0px;") ingredients.attr("style", "margin-top: 0px; margin-bottom: 0px;");
var listItems = u('li'); var listItems = u("li");
listItems.attr("style", "margin-top: 0px;") listItems.attr("style", "margin-top: 0px;");
// Remove the last <hr> if we have a footnote // Remove the last <hr> if we have a footnote
var hrs = u('hr') var hrs = u("hr");
if (hrs.length > 2) { if (hrs.length > 2) {
hrs.last().remove() hrs.last().remove();
} }
// Remove the footnote links // Remove the footnote links
u(document.getElementsByClassName('footnote-backref')).remove() u(document.getElementsByClassName("footnote-backref")).remove();
// space to full pages and add kirby background on every page
window.addEventListener('load', function () {
const pageHeightInPx = 869
const docHeight = document.children[0].offsetHeight
const pages = Math.ceil(docHeight/pageHeightInPx)
if((docHeight/pageHeightInPx)%1 != 0) {
// need spacing
const spacer = document.evaluate('//*[@id="spacer"]', document).iterateNext()
// spacer.style.border = "5px solid red"
spacer.style.height = `${(pages*pageHeightInPx) - docHeight}px`
}
const imageString = 'url("../../recipeskirby.svg")'
imageArray = [imageString]
let positionString = "background-position: 10mm 55mm"
for(let i = 1 ; i < pages ; i+=1) {
positionString += `, 10mm ${230*i+55}mm`
imageArray.push(imageString)
}
const styleString = `body { background-image: ${imageArray.join(', ')}; ${positionString}; background-blend-mode: luminosity; background-repeat: no-repeat; }`
window.document.styleSheets[2].insertRule(styleString, 0)
})
// Break ingredients into column-able blocks // Break ingredients into column-able blocks
window.addEventListener('load', function () { window.addEventListener("load", function () {
const zutatenNode = document.evaluate('//*[@id="zutaten"]', document).iterateNext() const zutatenNode = document
.evaluate('//*[@id="zutaten"]', document)
.iterateNext();
let nodeBlocks = [[]] let nodeBlocks = [[]];
let nextNode = zutatenNode.nextElementSibling let nextNode = zutatenNode.nextElementSibling;
while (nextNode && nextNode.tagName != "H2") { while (nextNode && nextNode.tagName != "H2") {
if (nextNode.tagName == "H4") {
if(nextNode.tagName == 'H4') { nodeBlocks.push([]);
nodeBlocks.push([])
} }
nodeBlocks[nodeBlocks.length - 1].push(nextNode) nodeBlocks[nodeBlocks.length - 1].push(nextNode);
nextNode = nextNode.nextElementSibling nextNode = nextNode.nextElementSibling;
} }
let allIngredientsDiv = document.createElement('div') let allIngredientsDiv = document.createElement("div");
allIngredientsDiv.id = "allZutaten" allIngredientsDiv.id = "allZutaten";
allIngredientsDiv.classList.add('allZutaten') allIngredientsDiv.classList.add("allZutaten");
let nodeBlockIndex = 0 let nodeBlockIndex = 0;
nodeBlocks.forEach(function (nodeArray) { nodeBlocks.forEach(function (nodeArray) {
let outerDiv = document.createElement('div') let outerDiv = document.createElement("div");
outerDiv.id = `wrap${nodeBlockIndex}` outerDiv.id = `wrap${nodeBlockIndex}`;
outerDiv.classList.add("zutatenWrapper") outerDiv.classList.add("zutatenWrapper");
allIngredientsDiv.appendChild(outerDiv) allIngredientsDiv.appendChild(outerDiv);
for (elem of nodeArray) { for (elem of nodeArray) {
outerDiv.appendChild(elem) outerDiv.appendChild(elem);
}
nodeBlockIndex += 1;
});
zutatenNode.parentNode.insertBefore(
allIngredientsDiv,
zutatenNode.nextSibling
);
// space to full pages and add kirby background on every page
const pageHeightInPx = 860;
const docHeight = document.children[0].offsetHeight;
const pages = Math.ceil(docHeight / pageHeightInPx);
if ((docHeight / pageHeightInPx) % 1 != 0) {
// need spacing
const spacer = document
.evaluate('//*[@id="spacer"]', document)
.iterateNext();
// spacer.style.border = "1px solid red";
spacer.style.height = `${pages * pageHeightInPx}px`;
console.log("docHeight", docHeight);
console.log("pages", pages);
console.log("pages * pageHeightInPx", pages * pageHeightInPx);
console.log(
"(docHeight/pageHeightInPx)%1",
(docHeight / pageHeightInPx) % 1
);
}
// fill all pages with kirby background
const imageString = 'url("../../recipeskirby.svg")';
imageArray = [imageString];
let positionString = "background-position: 10mm 55mm";
for (let i = 1; i < pages; i += 1) {
positionString += `, 10mm ${230 * i + 55}mm`;
imageArray.push(imageString);
} }
nodeBlockIndex += 1 const styleString = `#spacer { background-image: ${imageArray.join(
}) ", "
zutatenNode.parentNode.insertBefore(allIngredientsDiv, zutatenNode.nextSibling) )}; ${positionString}; background-blend-mode: normal; background-repeat: no-repeat; }`;
}) window.document.styleSheets[2].insertRule(styleString, 0);
});

Loading…
Cancel
Save