napkin/template/blog.lua

99 lines
2.6 KiB
Lua
Raw Normal View History

---@diagnostic disable:undefined-global
2025-02-27 13:10:42 -03:00
local wrapper = require "template.wrapper"
local error = require "template.error"
local phosphor = require "template.phosphor"
local foppy = require "./foppy"
return function(data)
-- if the blog name is empty, then redirect to the homepage
-- todo: do this on the actual router (main.lua)
local blog_name = data.request.params.blog
if #blog_name == 0 then
data.response.code = 301
data.response.headers["Location"] = "/"
return ""
end
-- set a buncha things up
local handle = "@" .. blog_name
local blog_path = "database/blogs/" .. blog_name .. ".json"
-- attempt to read the blog
local blog = foppy.open(blog_path)
-- could find no darn blog!!!
if not blog then
return error(404, "blog", handle) -- so, fuck it, error screen.
end
-- our little post renderer
local function render_post(post)
return a {
class = "button post",
href = "/" .. handle .. "/" .. post.url,
h3(post.title),
p {
class = "timestamp",
os.date("%Y-%m-%d %H:%M:%S", post.creation)
}
2025-02-27 13:10:42 -03:00
}
end
local posts = {}
local pinned_posts = {}
2025-02-27 13:10:42 -03:00
for _, post in ipairs(blog.index) do
if post.pin then
table.insert(pinned_posts, post)
end
table.insert(posts, post)
end
2025-02-27 13:10:42 -03:00
-- return our whole flippin html document
return wrapper(data) {
2025-02-27 13:10:42 -03:00
-- render header!
section {
h1(blog.name),
p { class = "description", blog.description }
},
2025-02-27 13:10:42 -03:00
-- render the pinned post list
section(function(o)
if #pinned_posts == 0 then
o.style = "display: none;"
return
end
o.class = "post-list pinned-posts"
table.insert(o, h2 { phosphor("push-pin"), " Pinned:" })
for _, post in ipairs(pinned_posts) do
table.insert(o, render_post(post))
end
end),
-- render the post list
section(function(o)
if #posts == 0 then
table.insert(o,
p {
class = "centered",
"Nothing here yet!"
}
)
return
end
o.class = "post-list"
table.insert(o, h2 { phosphor("note"), " Posts:" })
-- for each post
for _, post in ipairs(blog.index) do
table.insert(o, render_post(post)) -- render that boy
end
end)
}
end