napkin/template/blog.lua
2025-03-06 19:34:12 -03:00

111 lines
2.9 KiB
Lua

---@diagnostic disable:undefined-global
local wrapper = require "template.wrapper"
local error_handler = require "template.error_handler"
local phosphor = require "template.phosphor"
local blogs = require "./blogs"
return function(data)
local handle = data.request.params.blog
-- set a buncha things up
local blog = blogs.from_handle(handle)
-- could find no darn blog!!!
if not blog then
return error_handler(data, "blog_not_found", "/")
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)
}
}
end
local posts = {}
local pinned_posts = {}
for _, post in ipairs(blog.index) do
if post.pin then
table.insert(pinned_posts, post)
end
table.insert(posts, post)
end
-- return our whole flippin html document
return wrapper(data) {
-- render header!
section {
class = "blog-header",
div(function(o)
o.class = "banner"
local banner = blog.banner
if banner.source then
o.style = "--banner: url('/media/" .. banner.source .. "')"
if banner.alt then
o.role = "img"
o.alt = banner.alt
end
end
end),
h1 {
class = "title",
blog.title,
a {
class = "handle",
href = "/@" .. handle,
"(@" .. handle .. ")"
}
},
p { class = "description", blog.description }
},
-- 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)
o.class = "post-list"
table.insert(o, h2 { phosphor("note"), " Posts:" })
if #posts == 0 then
table.insert(o,
p {
class = "centered",
"Nothing here yet!"
}
)
return
end
-- for each post
for _, post in ipairs(blog.index) do
table.insert(o, render_post(post)) -- render that boy
end
end)
}
end