Render posts with a function so we can change the count

This commit is contained in:
Catalin Constantin Mititiuc 2025-05-15 15:35:51 -07:00
parent 6cb73bcf8f
commit 9f12248303
5 changed files with 22 additions and 42 deletions

View File

@ -61,7 +61,6 @@
## todo ## todo
* titleize slugs * titleize slugs
* limit number of posts on home page
* treesitter highlighting for moonscript * treesitter highlighting for moonscript
* penlight library * penlight library

View File

@ -1,4 +1,4 @@
<h2>Posts</h2> <h2>Posts</h2>
<!-- render posts --> <!-- render posts -->
$render{"posts"} $list_posts

View File

@ -10,7 +10,7 @@ $render{"templates/wares"}
<h2>Posts</h2> <h2>Posts</h2>
<!-- render posts --> <!-- render posts -->
$render{"posts"} $(list_posts(5))
<h4 style="text-align: center;"> <h4 style="text-align: center;">
<a href="$root/posts">View more posts</a> <a href="$root/posts">View more posts</a>

View File

@ -1,35 +0,0 @@
date = require "date"
path = require "sitegen.path"
-- import slugify from "sitegen.common"
common = require "sitegen.common"
rootname = (str) ->
result = string.gsub str, "%..+", ""
result
extract_id = (source) -> string.match path.filename(source), "%a[%w%-]+"
extract_date = (source) -> string.match path.filename(source), "%d+%-%d%d%-%d%d"
format_date = (str) -> date(str)\fmt "%b %d, %Y"
publish_date = (path) -> format_date extract_date path
pages = site\query_pages { is_a: "post" }
table.sort pages, (a, b) -> a.source > b.source
html ->
[tag["section"] {
tag["h3"] {
tag["a"] {
href: p.meta.target .. ".html",
p.meta.title or p.meta.id
}
}
tag["time"] { publish_date p.source }
{ "—", if p.meta.blurb then text p.meta.blurb }
tag["a"] {
class: "read-post-link",
href: p.meta.target .. ".html",
"Read post →"
}
} for _, p in ipairs pages]

View File

@ -2,6 +2,7 @@ sitegen = require "sitegen"
tools = require "sitegen.tools" tools = require "sitegen.tools"
site = require "sitegen.site" site = require "sitegen.site"
path = require "sitegen.path" path = require "sitegen.path"
html = require "sitegen.html"
-- import slugify from "sitegen.common" -- import slugify from "sitegen.common"
lfs = require "lfs" lfs = require "lfs"
@ -10,7 +11,7 @@ date = require "date"
-- Change output dir to what Openresty prefers -- Change output dir to what Openresty prefers
site.config.out_dir = "html/" site.config.out_dir = "html/"
-- Deactivate "pygments" plugin -- Deactivate "pygments" plugin because it conflicts with pandoc highlighting
site.default_plugins = for v in *site.default_plugins site.default_plugins = for v in *site.default_plugins
if v\find "pygments" then continue else v if v\find "pygments" then continue else v
@ -43,16 +44,31 @@ sitegen.create =>
@app_name = "stasis" @app_name = "stasis"
@version = "0.2.12" @version = "0.2.12"
add "index.html" @list_posts = (page, limit) ->
posts = page.site\query_pages { is_a: "post" }
table.sort posts, (a, b) -> a.source > b.source
html.build ->
[section {
h3 { a { href: meta.target .. ".html", meta.title or meta.id }}
time { publish_date source }
{ "—", if meta.blurb then text meta.blurb }
a { class: "read-post-link", href: meta.target .. ".html",
"Read post →"
}
} for { meta: meta, source: source } in *posts[1, limit or #posts]]
add "blog.html", target: "posts/index", template: "blog" add "blog.html", target: "posts/index", template: "blog"
add path, target: out, template: "post", is_a: "post", post: { add path, target: out, template: "post", is_a: "post", post: {
publish_date: publish_date(path) publish_date: publish_date(path)
}, id: extract_id(path) for path, out in pairs posts "docs" }, id: extract_id(path) for path, out in pairs posts "docs"
-- replace yaml headers with moonscript headers add "index.html"
-- replace post markdown yaml headers with moonscript headers
filter "docs", (body) => filter "docs", (body) =>
body\gsub "^%s*%-%-%-.-%.%.%.", (yaml_header) -> body\gsub "^%-%-%-.-%.%.%.", (yaml_header) ->
moonscript_header = yaml_header\gsub "%-%-%-", "{" moonscript_header = yaml_header\gsub "%-%-%-", "{"
result = moonscript_header\gsub "%.%.%.", "}" result = moonscript_header\gsub "%.%.%.", "}"
result result