miti.sh/site.moon
2025-05-16 10:53:31 -07:00

100 lines
3.0 KiB
Plaintext

sitegen = require "sitegen"
Site = require "sitegen.site"
Path = require "sitegen.path"
html = require "sitegen.html"
import tag from html
-- import slugify from "sitegen.common"
lfs = require "lfs"
date = require "date"
-- Change output dir to what Openresty prefers
Site.config.out_dir = "html/"
-- Deactivate "pygments" plugin because it conflicts with pandoc highlighting
Site.default_plugins = for v in *Site.default_plugins
if v\find "pygments" then continue else v
-- Remove default markdown renderer
Site.default_renderers = for v in *Site.default_renderers
if v\find "markdown" then continue else v
-- Add pandoc markdown renderer
table.insert Site.default_renderers, "renderers.markdown"
rootname = (str) ->
result = string.gsub str, "%..+", ""
result
split = (str, delimiter="/") ->
[capture for capture in string.gmatch str, "[^#{delimiter}]+"]
last = (list) -> list[#list]
join = (first, last, delimiter="/") -> table.concat { first, last }, delimiter
target = (path, prefix) -> join prefix, rootname(last(split path))
import escape_patt from require "sitegen.common"
get_files = (path, prefix=path) ->
files = Path.read_exec "find", path, "-type", "f"
files = [f for f in files\gmatch "[^\n]+"]
if prefix
files = for file in *files
file\gsub "^#{escape_patt prefix}/?", ""
table.sort files
files
-- require("moon").p get_files("docs")
posts = (path=".") ->
files = [file for file in lfs.dir path when file != "." and file != ".."]
{join(path, file), target(file, "/posts") for _, file in ipairs files}
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
list_posts = (page, limit) ->
posts = page.site\query_pages { is_a: "post" }
table.sort posts, (a, b) -> a.source > b.source
html.build ->
[tag.section {
tag.h3 { tag.a { href: meta.target .. ".html", meta.title or meta.id }}
tag.time { publish_date source }
{ "—", if meta.blurb then html.builders.text! meta.blurb }
tag.a { class: "read-post-link", href: meta.target .. ".html",
"Read post →"
}
} for { meta: meta, source: source } in *posts[1, limit or #posts]]
sitegen.create =>
@site_title = "WebDevCat.me · Catalin Mititiuc"
@app_name = "stasis"
@version = "0.2.12"
@list_posts = list_posts
add "index.html"
add "blog.html", target: "posts/index", template: "blog"
for path, target in pairs posts "docs"
add path,
target: target
template: "post"
is_a: "post"
publish_date: publish_date(path)
id: extract_id(path)
-- replace post markdown yaml headers with moonscript headers
filter "docs", (body) =>
body\gsub "^%-%-%-.-%.%.%.", (yaml_header) ->
header = yaml_header\gsub "%-%-%-", "{"
moonscript_header = header\gsub "%.%.%.", "}"
moonscript_header
copy "app.css"