Use the split function from 'sitegen.common' library

This commit is contained in:
Catalin Constantin Mititiuc 2025-05-21 09:38:58 -07:00
parent d01ab609c1
commit e12088f10d

View File

@ -14,20 +14,19 @@ import p from require "moon" -- debug
-- 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 because it conflicts with pandoc highlighting -- Configure the command our custom renderer uses to convert markdown to html
rend = "renderers.markdown"
require(rend).cmd = "pandoc --mathjax >"
-- Replace the default markdown renderer with our custom renderer
Site.default_renderers = for v in *Site.default_renderers
if v\find "markdown" then rend else v
-- Remove "pygments" plugin because it conflicts with pandoc syntax 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
-- Remove default markdown renderer -- from https://github.com/leafo/sitegen/blob/v0.2/spec/sitegen_spec.moon#L9-L18
Site.default_renderers = for v in *Site.default_renderers
if v\find "markdown" then continue else v
-- Add pandoc markdown renderer
rend = "renderers.markdown"
require(rend).cmd = "pandoc --mathjax >"
table.insert Site.default_renderers, rend
-- https://github.com/leafo/sitegen/blob/v0.2/spec/sitegen_spec.moon#L9-L18
get_files = (path, prefix=path) -> get_files = (path, prefix=path) ->
files = Path.read_exec "find", path, "-type", "f" files = Path.read_exec "find", path, "-type", "f"
files = [f for f in files\gmatch "[^\n]+"] files = [f for f in files\gmatch "[^\n]+"]
@ -39,18 +38,15 @@ get_files = (path, prefix=path) ->
table.sort files table.sort files
files files
-- strip file extension from filename
rootname = (str) -> rootname = (str) ->
result = string.gsub str, "%..+", "" result = string.gsub str, "%..+", ""
result result
-- TODO use split function from common
split = (str, delimiter="/") ->
[capture for capture in str\gmatch "[^#{delimiter}]+"]
last = (list) -> list[#list] last = (list) -> list[#list]
join = (first, last, delimiter="/") -> table.concat { first, last }, delimiter join = (first, last, delimiter="/") -> table.concat { first, last }, delimiter
reverse = (list={}) -> [item for item in *list[#list, 1, -1]] reverse = (list={}) -> [item for item in *list[#list, 1, -1]]
target = (path, prefix) -> join prefix, rootname(last(split path)) target = (path, prefix) -> join prefix, rootname(last(common.split path, "/"))
extract_id = (source) -> string.match Path.filename(source), "%a[%w%-]+" extract_id = (source) -> string.match Path.filename(source), "%a[%w%-]+"
extract_date = (source) -> string.match Path.filename(source), "%d+%-%d%d%-%d%d" extract_date = (source) -> string.match Path.filename(source), "%d+%-%d%d%-%d%d"
@ -73,27 +69,23 @@ template = [[
</section> </section>
]] ]]
-- limit the number with args {limit = 5} -- use in templates like this
list_posts = (page, args={}) -> -- $each{list_posts({limit = 5}), "post"}[[$post]]
posts = page.site\query_pages { is_a: "post" } -- or like this
-- $each{list_posts(), "post"}[[$post]]
list_posts = (args={}) =>
posts = @site\query_pages { is_a: "post" }
table.sort posts, (a, b) -> a.source > b.source table.sort posts, (a, b) -> a.source > b.source
a = table.concat [common.fill_ignoring_pre template, { return for { meta: meta, source: source } in *posts[1, args.limit or #posts]
common.fill_ignoring_pre template,
target: meta.target target: meta.target
post_title: meta.title or titleize_slug meta.id post_title: meta.title or titleize_slug meta.id
publish_date: publish_date source publish_date: publish_date source
blurb: meta.blurb blurb: meta.blurb
} for { meta: meta, source: source } in *posts[1, args.limit or #posts]]
p for { meta: meta, source: source } in *posts[1, args.limit or #posts]
common.fill_ignoring_pre template, {
target: "target"
post_title: "post_title"
publish_date: "publish_date"
blurb: "blurb"
}
a
-- call like this from a template: $(get_posts())[[<p>$target</p>]]
-- or like this: $(get_posts({limit = 5}))[[<p>$target</p>]]
get_posts = (args={}) => get_posts = (args={}) =>
posts = @site\query_pages { is_a: "post" } posts = @site\query_pages { is_a: "post" }
table.sort posts, (a, b) -> a.source > b.source table.sort posts, (a, b) -> a.source > b.source
@ -115,30 +107,12 @@ documents = (path=".") ->
files = reverse get_files path files = reverse get_files path
{join(path, file), target(file, "/posts") for file in *files} {join(path, file), target(file, "/posts") for file in *files}
-- get_some = () -> {{say: "this"}, {say: "that"}}
get_some = (args={}) =>
posts = @site\query_pages { is_a: "post" }
table.sort posts, (a, b) -> a.source > b.source
[{
target: post.meta.target
post_title: post.meta.title or titleize_slug post.meta.id
publish_date: publish_date post.source
blurb: post.meta.blurb
} for post in *posts[1, args.limit or #posts]]
get_something = (args={}, a) =>
p a
{limit: "ok"}
sitegen.create => sitegen.create =>
@site_title = "WebDevCat.me" @site_title = "WebDevCat.me"
@app_name = "stasis" @app_name = "stasis"
@version = "0.2.12" @version = "0.2.12"
@list_posts = list_posts @list_posts = list_posts
@get_posts = get_posts @get_posts = get_posts
@get_some = get_some
@get_something = get_something
add "index.html", title: "Catalin Mititiuc", blah: {{say: "wan"},{say: "tuu"}} add "index.html", title: "Catalin Mititiuc", blah: {{say: "wan"},{say: "tuu"}}
add "blog.html", title: "Posts", target: "posts/index", template: "blog" add "blog.html", title: "Posts", target: "posts/index", template: "blog"