From bf0a4f30a13a6eadf222278f21b6e885c897ea45 Mon Sep 17 00:00:00 2001 From: Janne Mareike Koschinski <janne@kuschku.de> Date: Thu, 29 Jul 2021 17:55:49 +0200 Subject: [PATCH] Automatically generate print versions from markdown --- .gitignore | 1 + Kare Raisu.md => Kare_Raisu.md | 0 Makefile | 10 +++++ Miso Ramen.md => Miso_Ramen.md | 0 MuzenBerliner.md => Muzen_Berliner.md | 0 meta/header.html | 36 ++++++++++++++++++ meta/wrap_sections.mjs | 53 +++++++++++++++++++++++++++ 7 files changed, 100 insertions(+) create mode 100644 .gitignore rename Kare Raisu.md => Kare_Raisu.md (100%) create mode 100644 Makefile rename Miso Ramen.md => Miso_Ramen.md (100%) rename MuzenBerliner.md => Muzen_Berliner.md (100%) create mode 100644 meta/header.html create mode 100755 meta/wrap_sections.mjs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..43a28b4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/print diff --git a/Kare Raisu.md b/Kare_Raisu.md similarity index 100% rename from Kare Raisu.md rename to Kare_Raisu.md diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b4d15ba --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) +SOURCES := $(wildcard *.md) +OBJECTS := $(addprefix print/,$(SOURCES:%.md=%.pdf)) + +.PHONY: all +all: $(OBJECTS) + +print/%.pdf: %.md meta/header.html meta/wrap_sections.mjs + mkdir -p "$(@D)" + pandoc "$<" -t html -H meta/header.html --filter meta/wrap_sections.mjs -o - | wkhtmltopdf -B 10mm -L 20mm -R 10mm -T 10mm --javascript-delay 0 - "$@" diff --git a/Miso Ramen.md b/Miso_Ramen.md similarity index 100% rename from Miso Ramen.md rename to Miso_Ramen.md diff --git a/MuzenBerliner.md b/Muzen_Berliner.md similarity index 100% rename from MuzenBerliner.md rename to Muzen_Berliner.md diff --git a/meta/header.html b/meta/header.html new file mode 100644 index 0000000..211f152 --- /dev/null +++ b/meta/header.html @@ -0,0 +1,36 @@ +<meta charset="utf-8"> +<style> +h1,h2,h3,h4,h5,h6 { + font-family: "Franziska Pro"; + font-weight: 400; +} + +th { + display: none; +} + +:root { + font-family: "Roboto Condensed"; + font-weight: 200; + line-height: 1.5; + font-size: 14pt; +} + +p, li { + margin-bottom: 0.75rem; +} + +td { + font-weight: 200; + font-size: 1rem; +} + +body > div > div { + page-break-inside: avoid; +} + +div#zutaten table td:first-child { + padding-right: 1rem; + min-width: 6rem; +} +</style> diff --git a/meta/wrap_sections.mjs b/meta/wrap_sections.mjs new file mode 100755 index 0000000..ec76a53 --- /dev/null +++ b/meta/wrap_sections.mjs @@ -0,0 +1,53 @@ +#!/usr/bin/env node +import readline from "readline"; + +function groupChildren(blocks, parentLevel) { + const result = []; + while (blocks.length) { + const block = blocks.shift(); + if (block.t == "Header") { + const level = block.c[0]; + const id = block.c[1][0]; + + if (parentLevel >= level) { + blocks.unshift(block); + return result; + } else { + const children = groupChildren(blocks, level); + result.push({ + "t": "RawBlock", + "c": [ + "html", + "<div id=\"" + id + "\">" + ] + }); + result.push(block); + result.push(...children); + result.push({ + "t": "RawBlock", + "c": [ + "html", + "</div>" + ] + }); + } + } else { + result.push(block); + } + } + return result; +} + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, + terminal: false +}); +rl.on('line', function (line) { + const data = JSON.parse(line); + const blocks = groupChildren(data.blocks, 0); + console.log(JSON.stringify({ + ...data, + blocks: blocks + })); +}); -- GitLab