From 777f9d3fd8caf56e6bc6999a4b05379307d0733f Mon Sep 17 00:00:00 2001 From: Justin Gassner Date: Tue, 12 Sep 2023 07:36:33 +0200 Subject: Initial commit --- _scripts/new.lua | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100755 _scripts/new.lua (limited to '_scripts') 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:: -- cgit v1.2.3-54-g00ecf