about summary refs log tree commit diff
path: root/src/preproc.lua
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2026-09-24 18:00:17 +0200
committerRobert Günzler <r@gnzler.io>2026-10-02 10:39:48 +0200
commit5f748385efffecf2e30ed5c70e2394cfac39a4b5 (patch)
tree77b5715b2870b0ce90d1f99771c20a953bef19eb /src/preproc.lua
parentd76cefb4fe1155ee8303328cdcff036e03fd9be9 (diff)
downloadsite-5f748385efffecf2e30ed5c70e2394cfac39a4b5.tar.gz
embed preproc.lua script
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to 'src/preproc.lua')
-rw-r--r--src/preproc.lua60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/preproc.lua b/src/preproc.lua
new file mode 100644
index 0000000..76f0678
--- /dev/null
+++ b/src/preproc.lua
@@ -0,0 +1,60 @@
+---@diagnostic disable: undefined-global
+-- see https://pandoc.org/lua-filters.html
+return {
+	Link = function(link)
+		-- set target="_blank" for outgoing links (open them in new tab)
+		if string.match(link.target, "gzr.im") == nil then
+			table.insert(link.attributes, { "target", "_blank" })
+		end
+		return link
+	end,
+	Pandoc = function(doc)
+		local front = {}
+
+		-- add a comment
+		table.insert(
+			front,
+			pandoc.RawBlock(
+				"html5",
+				"<!-- Generated file, you may edit and discard "
+					.. pandoc.path.filename(PANDOC_STATE.input_files[1])
+					.. " :) -->"
+			)
+		)
+
+		local toc = pandoc.structure.table_of_contents(doc)
+		if doc.meta.toc and toc then
+			table.insert(front, pandoc.RawBlock("html5", '<nav class="table-of-contents capital">'))
+			table.insert(front, pandoc.RawBlock("html5", "<ol>"))
+			-- insert jump to top
+			table.insert(front, pandoc.RawBlock("html5", "<li>"))
+			table.insert(front, pandoc.Link("back to top", "#top"))
+			table.insert(front, pandoc.RawBlock("html5", "</li>"))
+			-- create custom toc
+			toc:walk({
+				Link = function(e)
+					table.insert(front, pandoc.RawBlock("html5", "<li>"))
+					table.insert(front, e)
+					table.insert(front, pandoc.RawBlock("html5", "</li>"))
+				end,
+			})
+			table.insert(front, pandoc.RawBlock("html5", "</ol>"))
+			table.insert(front, pandoc.RawBlock("html5", "</nav>"))
+		end
+
+		-- insert date blocks
+		if doc.meta.date then
+			table.insert(front, pandoc.RawBlock("html5", '<span class="date">'))
+			table.insert(front, doc.meta.date)
+			table.insert(front, pandoc.RawBlock("html5", "</span>"))
+		end
+
+		-- prepend front stuffs
+		for i = #front, 1, -1 do
+			table.insert(doc.blocks, 1, front[i])
+		end
+
+		-- return finished ast
+		return doc
+	end,
+}