commit c5a5272ffa217e194b3499256b0f3731cb6af439 Author: vitrinekast Date: Tue Nov 12 14:11:02 2024 +0100 combining markdown diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9168178 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +events +mo \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..cbb424f --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# Klank docs + +## installation +Run the install.sh to install [Mustache](https://github.com/tests-always-included/mo) + + +Also, pandoc is needed. Additionally researching this thing https://github.com/pandoc-ext/include-files/tree/main + + +More info to come ;) + +## Scripts & folders + +- `newsletters` write your new newsletter here +- `templates` contains a markdown template for events, that are being grabbed via +- `get_events.sh` run as `sh get_events.sh` to grab all events from the klankschool calendar and store them as markdwon files +- `install.sh` the installation script for Mustache +- `make_newsletter.sh` run as `sh make_newsletter` to translate all created newsletters in the `newsletters` directory into HTML. Created newsletters are stored in the `dist` directory diff --git a/dist/newsletters/december.md.html b/dist/newsletters/december.md.html new file mode 100644 index 0000000..404c568 --- /dev/null +++ b/dist/newsletters/december.md.html @@ -0,0 +1,6 @@ +

December

+

What a great month it was

+

Event

+

Let’s (un)repair things +together!

+

This event starts at 1731603600

diff --git a/get_events.sh b/get_events.sh new file mode 100644 index 0000000..dc8ea16 --- /dev/null +++ b/get_events.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +DIRECTORY=events + +if [ ! -d "$DIRECTORY" ]; then + mkdir $DIRECTORY +fi + + +echo $(curl -s "https://calendar.klank.school/api/events") | jq -c '.[]' | while read -r item; do + slug=$(echo "$item" | jq -r '.slug') + + echo $item + + export TITLE=$(echo "$item" | jq -r '.title') + export START_DATETIME=$(echo "$item" | jq -r '.start_datetime') + export END_DATETIME=$(echo "$item" | jq -r '.end_datetime') + + filename="events/${slug}.md" + + if test -f "$filename"; then + rm -f -- $filename + fi + + mo templates/event.md >> $filename + echo "Created a new event markdown file for " $filename + +done + + diff --git a/include-files.lua b/include-files.lua new file mode 100644 index 0000000..69df689 --- /dev/null +++ b/include-files.lua @@ -0,0 +1,127 @@ +--- include-files.lua – filter to include Markdown files +--- +--- Copyright: © 2019–2021 Albert Krewinkel +--- License: MIT – see LICENSE file for details + +-- Module pandoc.path is required and was added in version 2.12 +PANDOC_VERSION:must_be_at_least '2.12' + +local List = require 'pandoc.List' +local path = require 'pandoc.path' +local system = require 'pandoc.system' + +--- Get include auto mode +local include_auto = false +function get_vars (meta) + if meta['include-auto'] then + include_auto = true + end +end + +--- Keep last heading level found +local last_heading_level = 0 +function update_last_level(header) + last_heading_level = header.level +end + +--- Update contents of included file +local function update_contents(blocks, shift_by, include_path) + local update_contents_filter = { + -- Shift headings in block list by given number + Header = function (header) + if shift_by then + header.level = header.level + shift_by + end + return header + end, + -- If link paths are relative then prepend include file path + Link = function (link) + if path.is_relative(link.target) then + link.target = path.normalize(path.join({include_path, link.target})) + end + return link + end, + -- If image paths are relative then prepend include file path + Image = function (image) + if path.is_relative(image.src) then + image.src = path.normalize(path.join({include_path, image.src})) + end + return image + end, + -- Update path for include-code-files.lua filter style CodeBlocks + CodeBlock = function (cb) + if cb.attributes.include and path.is_relative(cb.attributes.include) then + cb.attributes.include = + path.normalize(path.join({include_path, cb.attributes.include})) + end + return cb + end + } + + return pandoc.walk_block(pandoc.Div(blocks), update_contents_filter).content +end + +--- Filter function for code blocks +local transclude +function transclude (cb) + -- ignore code blocks which are not of class "include". + if not cb.classes:includes 'include' then + return + end + + -- Markdown is used if this is nil. + local format = cb.attributes['format'] + + -- Attributes shift headings + local shift_heading_level_by = 0 + local shift_input = cb.attributes['shift-heading-level-by'] + if shift_input then + shift_heading_level_by = tonumber(shift_input) + else + if include_auto then + -- Auto shift headings + shift_heading_level_by = last_heading_level + end + end + + --- keep track of level before recusion + local buffer_last_heading_level = last_heading_level + + local blocks = List:new() + for line in cb.text:gmatch('[^\n]+') do + if line:sub(1,2) ~= '//' then + local fh = io.open(line) + if not fh then + io.stderr:write("Cannot open file " .. line .. " | Skipping includes\n") + else + -- read file as the given format with global reader options + local contents = pandoc.read( + fh:read '*a', + format, + PANDOC_READER_OPTIONS + ).blocks + last_heading_level = 0 + -- recursive transclusion + contents = system.with_working_directory( + path.directory(line), + function () + return pandoc.walk_block( + pandoc.Div(contents), + { Header = update_last_level, CodeBlock = transclude } + ) + end).content + --- reset to level before recursion + last_heading_level = buffer_last_heading_level + blocks:extend(update_contents(contents, shift_heading_level_by, + path.directory(line))) + fh:close() + end + end + end + return blocks +end + +return { + { Meta = get_vars }, + { Header = update_last_level, CodeBlock = transclude } +} diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..9c10a57 --- /dev/null +++ b/install.sh @@ -0,0 +1,11 @@ +# Download +curl -sSL https://raw.githubusercontent.com/tests-always-included/mo/master/mo -o mo + +# Make executable +chmod +x mo + +# Move to the right folder +sudo mv mo /usr/local/bin/ + +# Test +echo "works" | mo \ No newline at end of file diff --git a/make_newsletter.sh b/make_newsletter.sh new file mode 100644 index 0000000..c6218c5 --- /dev/null +++ b/make_newsletter.sh @@ -0,0 +1,10 @@ + +mkdir dist +mkdir dist/newsletters + +cd newsletters + +for file in *.md; do + echo $file + pandoc --lua-filter=../include-files.lua $file --output ../dist/newsletters/$file.html +done \ No newline at end of file diff --git a/newsletters/december.md b/newsletters/december.md new file mode 100644 index 0000000..bf30cf2 --- /dev/null +++ b/newsletters/december.md @@ -0,0 +1,9 @@ +# December + +What a great month it was + +## Event + +``` {.include} +../events/lets-unrepair-things-together-2.md +``` \ No newline at end of file diff --git a/templates/event.md b/templates/event.md new file mode 100644 index 0000000..caac345 --- /dev/null +++ b/templates/event.md @@ -0,0 +1,2 @@ +# {{TITLE}} +This event starts at {{START_DATETIME}} \ No newline at end of file