summaryrefslogtreecommitdiffstats
path: root/_scripts
diff options
context:
space:
mode:
authorJustin Gassner <justin.gassner@mailbox.org>2023-09-12 07:36:33 +0200
committerJustin Gassner <justin.gassner@mailbox.org>2024-01-13 20:41:27 +0100
commit777f9d3fd8caf56e6bc6999a4b05379307d0733f (patch)
treedc42d2ae9b4a8e7ee467f59e25c9e122e63f2e04 /_scripts
downloadsite-777f9d3fd8caf56e6bc6999a4b05379307d0733f.tar.zst
Initial commit
Diffstat (limited to '_scripts')
-rwxr-xr-x_scripts/new.lua112
1 files changed, 112 insertions, 0 deletions
diff --git a/_scripts/new.lua b/_scripts/new.lua
new file mode 100755
index 0000000..e57100d
--- /dev/null
+++ b/_scripts/new.lua
@@ -0,0 +1,112 @@
+#!/usr/bin/lua
+
+local lfs = require("lfs")
+
+local subcommand = arg[1]
+if not subcommand then
+ print("No arguments given.")
+ goto exit
+end
+
+if not (subcommand == "page" or subcommand == "dir") then
+ print("Unknown subcommand '" .. subcommand .. "'.")
+ goto exit
+end
+
+local title = table.concat(arg, " ", 2)
+if not title or title == "" then
+ print("No title given.")
+ goto exit
+end
+
+local function slugify(str)
+ local strlow = string.lower(str)
+ local slug = string.gsub(strlow, "%W", "-")
+ return slug
+end
+
+local titleslug = slugify(title)
+print("Title: " .. title)
+print(" Slug: " .. titleslug)
+
+local parent, grand_parent
+local file = io.open("index.md", "r")
+if file then
+ print("Found index.md.")
+ io.input(file)
+ for line in io.lines() do
+ local match = string.match(line, "title: (.*)")
+ if match then
+ parent = match
+ print("--> Parent: " .. parent)
+ end
+ match = string.match(line, "parent: (.*)")
+ if match then
+ grand_parent = match
+ print("--> Grand parent: " .. grand_parent)
+ end
+ end
+ io.close(file)
+else
+ print("No index.md found.")
+end
+
+if arg[1] == "page" then
+ file = io.open(titleslug .. ".md", "w")
+ if file then
+ io.output(file)
+ print("Writing file: " .. titleslug .. ".md")
+ io.write("---\ntitle: " .. title .. "\n")
+ if parent then
+ io.write("parent: " .. parent .. "\n")
+ end
+ if grand_parent then
+ io.write("grand_parent: " .. grand_parent .. "\n")
+ end
+ io.write([[
+nav_order: 1
+# cspell:words
+---
+
+# {{ page.title }}
+
+{: .theorem-title }
+> {{ page.title }}
+> {: #{{ page.title | slugify }} }
+>
+> ...
+
+{% proof %}
+{% endproof %}
+]])
+ io.close(file)
+ end
+elseif arg[1] == "dir" then
+ if grand_parent then
+ print("Cannot have deeper directories.")
+ goto exit
+ end
+ print("Making directory: " .. titleslug .. "/")
+ lfs.mkdir(titleslug)
+ file = io.open(titleslug .. "/index.md", "w")
+ if file then
+ io.output(file)
+ print("Writing file: " .. titleslug .. "/index.md")
+ io.write("---\ntitle: " .. title .. "\n")
+ if parent then
+ io.write("parent: " .. parent .. "\n")
+ end
+ io.write([[
+nav_order: 1
+has_children: true
+has_toc: false
+# cspell:words
+---
+
+# {{ page.title }}
+]])
+ io.close(file)
+ end
+end
+
+::exit::