napkin/template/blog.lua

94 lines
2.4 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_handler = require "template.error_handler"
2025-02-27 13:10:42 -03:00
local phosphor = require "template.phosphor"
local foppy = require "./foppy"
return function(data)
local blog_name = data.request.params.blog
-- set a buncha things up
local blog = foppy.open(
"database/blogs/" .. blog_name .. ".json"
)
2025-02-27 13:10:42 -03:00
-- could find no darn blog!!!
if not blog then
return error_handler(data, "blog_not_found", "/")
2025-02-27 13:10:42 -03:00
end
-- our little post renderer
local function render_post(post)
return a {
class = "button post",
href = "/@" .. blog_name .. "/" .. post.url,
2025-02-27 13:10:42 -03:00
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 = {}
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 {
blog.banner and div {
class = "banner",
style = "--banner: url('/media/" .. blog.banner .. "')"
},
2025-02-27 13:10:42 -03:00
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