summaryrefslogtreecommitdiffstats
path: root/_scripts/new.lua
blob: 308bf2c9dcedbf60a1ec79029c71f965568fd3ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/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::