#!/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 --- # {{ page.title }} {% theorem %} {% endtheorem %} {% 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 --- # {{ page.title }} ]]) io.close(file) end end ::exit::