-
Notifications
You must be signed in to change notification settings - Fork 1
/
cblargh.lua
executable file
·287 lines (246 loc) · 6.71 KB
/
cblargh.lua
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
-- CBlargh Server
-- Libraries
local template = require("template")
local markdown = markdown and markdown.github or require("3rdparty/markdown")
-- Load Settings
settings = assert(loadfile("settings.lua"))()
-- IO Helper
local function readfile(path)
if fs.readfile then
return fs.readfile(path)
else
local f, err = io.open(path)
if f then
local content = f:read("*a")
f:close()
return content
else
return nil, err
end
end
end
local function list(path)
if fs.list and not settings.dontusephysfs then
return fs.list(path)
else
return io.list(path)
end
end
local function exists(path)
if fs.exists and not settings.dontusephysfs then
return fs.exists(path)
else
return os.exists(path)
end
end
local function modtime(path)
if fs.modtime and not settings.dontusephyfs then
return fs.modtime(path)
else
return io.modtime(path)
end
end
-- Read template(s) into memory
local main_template = assert(readfile("templates/"..settings.template_pack.."/main.html"))
local about_template = readfile("templates/"..settings.template_pack.."/about.html")
local blog_template = assert(readfile("templates/"..settings.template_pack.."/post.html"))
local fail_template = readfile("templates/"..settings.template_pack.."/notfound.html")
local rss_template = readfile("templates/rss.xml")
-- Put the stuff in the kv store for eventual live reload or something.
kvstore.set("title", settings.title)
kvstore.set("aboutme", settings.aboutme)
kvstore.set("url", settings.url)
kvstore.set("template_main", main_template)
kvstore.set("template_about", about_template)
kvstore.set("template_post", blog_template)
kvstore.set("template_notfound", fail_template)
kvstore.set("template_rss", rss_template)
-- Blog posts here!
local posts = {}
local posts_source = {}
local posts_preview = {}
local posts_title = {}
local modtimes = {}
local titles, err = list(settings.posts_path)
if err then
print(err)
os.exit(1)
end
for k, v in pairs(titles) do
local file = settings.posts_path .. v
print("post/"..v, "->", file)
local src = readfile(file)
if string.sub(src, 1, 2) == "# " then
posts_title[v] = string.match(src, "^#* ([^\n]*)\n")
src = src:match("^# [^\n]*\n(.*)$")
else
posts_title[v] = v
end
modtimes[v] = modtime(file)
posts_source[v] = src
posts[v] = markdown(src)
local preview_src = src
local preview = ""
local line_count = 0
for m in string.gmatch(src, "[^\n]*\n*") do
if m:sub(-2,-1) == "\n\n" then
line_count = line_count + 1
if line_count >= 5 then
break
end
end
if m:match("^(%#* ).*$") then
break
elseif m:sub(1, 3) == "```" then
break
end
preview = preview .. m
end
preview = preview .. "\n"
posts_preview[v] = markdown(preview) -- TODO: Check for cut-off markdown stuff
end
print() -- empty line
kvstore.set("posts", posts)
kvstore.set("posts_source", posts_source)
kvstore.set("posts_preview", posts_preview)
kvstore.set("posts_title", posts_title)
kvstore.set("modtimes", modtimes)
-- Load static files into memory.
local static_exists = exists("templates/"..settings.template_pack.."/static")
local static, err
if static_exists then
static, err = list("templates/"..settings.template_pack.."/static")
if err then
print(err)
os.exit(1)
end
end
-- The routes
srv.GET("/", mw.new(function() -- Front page
local template = require("template")
local modtimes = kvstore.get("modtimes")
local posts = kvstore.get("posts")
local res, err = template.render(kvstore.get("template_main"), {
title=kvstore.get("title"),
aboutme=kvstore.get("aboutme"),
posts=kvstore.get("posts"),
posts_source=kvstore.get("posts_source"),
posts_preview=kvstore.get("posts_preview"),
posts_title=kvstore.get("posts_title"),
url=kvstore.get("url"),
modtimes=modtimes,
modtimes_r=table.flip(modtimes),
os=os,
table=table,
string=string
})
if err then
print("Template error:", err)
end
content(res)
end, nil, nil, true))
if about_template then
srv.GET("/about", mw.new(function()
local template = require("template")
local modtimes = kvstore.get("modtimes")
local res, err = template.render(kvstore.get("template_about"), {
title=kvstore.get("title"),
aboutme=kvstore.get("aboutme"),
posts=kvstore.get("posts"),
posts_source=kvstore.get("posts_source"),
posts_preview=kvstore.get("posts_preview"),
posts_title=kvstore.get("posts_title"),
url=kvstore.get("url"),
modtimes=modtimes,
modtimes_r=table.flip(modtimes),
os=os,
table=table,
string=string
})
if err then
print("Template error:", err)
end
content(res)
end, nil, nil, true))
end
srv.GET("/post/:postid", mw.new(function()
local template = require("template")
local posts = kvstore.get("posts")
local posts_source = kvstore.get("posts_source")
local posts_preview = kvstore.get("posts_preview")
local posts_title = kvstore.get("posts_title")
local modtimes=kvstore.get("modtimes")
local postid = params("postid")
local src
local respcode = 200
if posts[postid] then -- Post exists.
src = kvstore.get("template_post")
else -- Render fail template
src = kvstore.get("template_notfound")
respcode = 404
end
local res, err = template.render(src, {
postid=postid,
post=posts[postid],
post_title=posts_title[postid],
preview=posts_preview[postid],
posts=posts,
posts_source=posts_source,
posts_preview=posts_preview,
posts_title=posts_title,
title=kvstore.get("title"),
aboutme=kvstore.get("aboutme"),
url=kvstore.get("url"),
modtimes=modtimes,
os=os,
string=string
})
if err then
print("Template error:", err)
end
content(res, respcode)
end, nil, nil, true))
-- Generate RSS
srv.GET("/rss.xml", mw.new(function()
local template = require("template")
local modtimes=kvstore.get("modtimes")
local src = kvstore.get("template_rss")
local res, err = template.render(src, {
posts=kvstore.get("posts"),
posts_source=kvstore.get("posts_source"),
posts_preview=kvstore.get("posts_preview"),
posts_title=kvstore.get("posts_title"),
title=kvstore.get("title"),
aboutme=kvstore.get("aboutme"),
url=kvstore.get("url"),
modtimes=modtimes,
modtimes_r=table.flip(modtimes),
os=os,
table=table,
string=string
})
if err then
print("Template error:", err)
end
content(res, 200, "application/rss+xml; charset=UTF-8")
end, nil, nil, true))
if static_exists then
for _, name in pairs(static) do
local handler = mw.echo(readfile("templates/"..settings.template_pack.."/static/"..name))
srv.GET("/theme_static/"..name, handler)
end
end
if os.exists("content") then
srv.GET("/content/*path", mw.static("/content"))
end
if srv.DefaultRoute then
local src, err = template.render(kvstore.get("template_notfound"), {
title=kvstore.get("title"),
aboutme=aboutme
})
if err then
print(err)
os.exit(1)
end
srv.DefaultRoute(mw.echo(src, 404))
end