add project document and sync with live.kladjes.nl
Some checks failed
generate pdf files / build (push) Failing after 2m4s

This commit is contained in:
2025-06-22 12:06:51 +02:00
parent d9498b8135
commit fc6a086ca2
8 changed files with 734 additions and 90 deletions

View File

@@ -0,0 +1,127 @@
--- include-files.lua filter to include Markdown files
---
--- Copyright: © 20192021 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) and string.sub(path.filename(link.target), 1, 1) ~= '#' 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 }
}

View File

@@ -7,21 +7,47 @@ TEMP_MD_FILE="$BUILD_DIR/$(basename "$MD_FILE")"
TEMP_TEX_FILE="$BUILD_DIR/$(basename "$MD_FILE" | sed -e 's|md$|latex|')"
mkdir -p "$(dirname "$TEMP_MD_FILE")"
title="$(grep '^# ' "$MD_FILE" | sed 's|^# ||')"
cp "$MD_FILE" "$TEMP_MD_FILE"
cat "$MD_FILE" | sed \
for line in $(grep '^!\[.*\](.*\.md)$' "$TEMP_MD_FILE" | sed 's/ /%20;/g')
do
src=$(echo "$line" | sed -e 's/^.*(//' -e 's/).*$//' -e 's/%20;/ /g')
echo "include found: markdown/$src"
cp "markdown/$src" "$BUILD_DIR/$src"
sed -i "$BUILD_DIR/$src" \
-e 's|\[toc\]||' \
-e 's|^\[parent\].*$||' \
-e 's|^# |\\newpage\n# |' \
-e 's|^## |\\newpage\n# |' \
-e 's|\[\([^]]*\)\](#\([^)]*\))|[\1](#\L\2)|'
sed -i "$TEMP_MD_FILE" \
-e "s/^\!\[.*\]($src)\$/\`\`\`\\{.include\\}\n${src}\n\`\`\`/"
done
title="$(grep '^# ' "$MD_FILE" | head -l 1 | sed 's|^# ||')"
sed -i "$TEMP_MD_FILE" \
-e 's|\[toc\]|\\tableofcontents|' \
-e 's|^\[parent\].*$||' \
-e 's|^# .*$||' \
-e 's|^#||' \
-e 's|^# |\\newpage\n# |' \
-e 's|\[\([^]]*\)\](#\([^)]*\))|[\1](#\L\2)|' \
>"$TEMP_MD_FILE"
-e 's|\[\([^]]*\)\](#\([^)]*\))|[\1](#\L\2)|'
cd "$BUILD_DIR"
pandoc --to=latex --template "${BASE_DIR}/converters/template.latex" -o "$TEX_FILE" "$(basename "$TEMP_MD_FILE")"
pandoc --lua-filter=../../converters/include-files.lua --to=latex --from=markdown+abbreviations --template "${BASE_DIR}/converters/template.latex" -o "$TEX_FILE" --data-dir="../../markdown" "$(basename "$TEMP_MD_FILE")"
cd "$BASE_DIR"
# for line in $(grep '^!\[.*\](.*\.md)$' "$TEMP_MD_FILE" | sed 's/ /%20;/g')
# do
# src=$(echo "$line" | sed -e 's/^.*(//' -e 's/).*$//' -e 's/%20;/ /g')
# sed -i "$TEMP_MD_FILE" \
# -e "s/^!\[.*\]($src)\$/\\include{$src}/"
# done
sed --in-place \
-e "s|?title?|$title|" \
"$TEX_FILE"