miti.sh/site.moon
2025-05-23 13:28:39 -07:00

310 lines
9.5 KiB
Plaintext

sitegen = require "sitegen"
Site = require "sitegen.site"
Path = require "sitegen.path"
html = require "sitegen.html"
common = require "sitegen.common"
cosmo = require "sitegen.cosmo"
date = require "date"
html_renderer = require "sitegen.renderers.html"
import tag from html
import escape_patt from require "sitegen.common"
import p from require "moon" -- debug
-- Change output dir to what Openresty prefers
Site.config.out_dir = "html/"
-- 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
if v\find "pygments" then continue else v
-- from https://github.com/leafo/sitegen/blob/v0.2/spec/sitegen_spec.moon#L9-L18
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
-- strip file extension from filename
rootname = (str) ->
result = string.gsub str, "%..+", ""
result
reflect = (obj) -> [key for key, value in pairs obj]
last = (list) -> list[#list]
reverse = (list={}) -> [item for item in *list[#list, 1, -1]]
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
strip_date = (filename) ->
filename\gsub "#{escape_patt(extract_date(filename) .. "-")}/?", ""
target = (path, prefix=".") ->
filename = rootname last common.split path, "/"
Path.join prefix, strip_date filename
h = (word) -> word\sub(1, 1)\upper!
t = (word) -> word\sub(2, -1)\lower!
titleize = (word) -> h(word), t(word)
titleize_slug = (slug="") ->
words = [{ titleize word } for word in *common.split slug, "-"]
table.concat [head .. tail for { head, tail } in *words], " "
template = [[
<section>
<h3><a href="$url">$title</a></h3>
<time>$publish_date</time> — $blurb
<a class="read-post-link" href="$url">Read post →</a>
</section>
]]
--- creates a list of post links for rendering in templates.
-- @usage
-- set up in create function:
--
-- sitegen.create =>
-- @list_posts = list_posts @, [template]
--
-- use in templates:
--
-- `list_posts @` expects $(list_posts({limit = 2}))[[<p>$title</p>]]
-- `list_posts @, template` expects $each{list_posts({limit = 2}), "post"}[[$post]]
--
-- @tparam Page @ the calling page.
-- @tparam[opt] string template a string that can contain variables and
-- helpers.
--
-- @treturn func the function that will be called from the template.
list_posts = (template) => (args={}) =>
pages = @site\query_pages { is_a: "post" }
table.sort pages, (a, b) -> a.source > b.source
limit_or_page_count = math.min args.limit or #pages, #pages
return for page in *pages[1, limit_or_page_count]
{ :title, :id, publish_date: pub_date, :blurb } = page.meta
title = title or titleize_slug id
url = rootname page\url_for!
vals = { title: title, publish_date: pub_date, blurb: blurb, url: url }
if template then common.fill_ignoring_pre template, vals else vals
-- replace '$if' helper function with cosmo's 'cif'
html_renderer.cosmo_helpers.if = (args, has_block) => cosmo.cif args, has_block
-- add a '$titleize' helper function
html_renderer.cosmo_helpers.titleize = (slug) => titleize_slug slug
files_from = (path=".") ->
[Path.join path, file for file in *reverse get_files path]
test = (args={}, has_block) =>
if has_block
cosmo.yield(name: "yupyup")
nil
else
common.fill_ignoring_pre [[passed in template $blove]], {blove: "haha"}
-- args._template = 2
-- cosmo.yield(args)
-- html = @site\get_renderer "sitegen.renderers.html"
-- page\merge_meta vals
-- page.tpl_scope = page.meta
-- page.template_stack = common.Stack!
-- html\render page, assert args[1] or template, "missing html string"
listing_info = (page) ->
{ :title, :id, publish_date: pub_date, :blurb } = page.meta
title = title or titleize_slug id
url = rootname page\url_for!
{ title: title, publish_date: pub_date, blurb: blurb, url: url }
list3 = (args={}, has_block) =>
pages = @site\query_pages { is_a: "post" }
table.sort pages, (a, b) -> a.source > b.source
limit_or_page_count = math.min args.limit or #pages, #pages
result = for page in *pages[1, limit_or_page_count]
vals = listing_info page
if has_block then cosmo.yield vals else common.fill_ignoring_pre template, vals
return result unless has_block
-- use in templates
-- $list2{[[<p>$title</p>]], limit = 2}
-- $each{$list2({limit = 3}), "item"}[[$item]]
list2 = (args={}) =>
pages = @site\query_pages { is_a: "post" }
table.sort pages, (a, b) -> a.source > b.source
limit_or_page_count = math.min args.limit or #pages, #pages
result = for page in *pages[1, limit_or_page_count]
{ :title, :id, publish_date: pub_date, :blurb } = page.meta
title = title or titleize_slug id
url = rootname page\url_for!
vals = { title: title, publish_date: pub_date, blurb: blurb, url: url }
return if args[1] then table.concat result else result
one = (template) =>
(arg, _has_block) =>
common.fill_ignoring_pre template, arg
two = (arg, has_block) =>
common.throw_error "expecting a block" if not has_block
cosmo.yield arg
nil -- VERY IMPORTANT!
three = (arg, has_block) =>
common.throw_error "expecting a block" if not has_block
{ list } = arg
cosmo.yield item for item in *list
nil -- VERY IMPORTANT!
four1 = (template) =>
(arg, _has_block) =>
{ list } = arg
return [common.fill_ignoring_pre template, item for item in *list]
-- four = (arg) =>
-- p arg
-- { list } = arg
-- p list
-- return [common.fill_ignoring_pre template, item for item in *list]
get_posts = (limit) =>
pages = @site\query_pages { is_a: "post" }
table.sort pages, (a, b) -> a.source > b.source
limit_or_page_count = math.min limit or #pages, #pages
[post for post in *pages[1, limit_or_page_count]]
sitegen.create =>
@site_title = "WebDevCat.me"
@app_name = "stasis"
@version = "0.2.12"
@list_posts = list_posts @, template
@test = test
@list2 = list2
@list3 = list3
@some_name = "robertino farterino"
@some_list = {
{say: "this"}
{say: "that"}
{say: "the other"}
}
@one = one @, [[inline template $some_var]]
@two = two
@three = three
@four = (arg, _) =>
unpack for post in *get_posts @, arg.limit
common.fill_ignoring_pre template, listing_info post
@map = (...) => cosmo.map ...
@do_cards = {{rank: "Ace", suit: "Spades"}, {rank: "Queen", suit: "Diamonds"}}
@do_other = (arg, _) =>
{{rank: "Ace", suit: "Spades"}, {rank: "Queen", suit: "Diamonds"}}
@do_unpack = (arg, _) =>
unpack {"three", "five", "seven"}
-- <section>
-- <h3>
-- <a href="/posts/build-a-neovim-qt-appimage-from-source">
-- Build A Neovim Qt AppImage from Source
-- </a>
-- </h3>
--
-- <time datetime="2024-01-01">Jan 1, 2024</time>
-- — Building an AppImage package from source allows us to run the latest version of Neovim-QT on our machine running the Debian Linux distribution.
-- <a href="/posts/build-a-neovim-qt-appimage-from-source" class="read-post-link">
-- Read post →
-- </a>
-- </section>
@just_render_the_damn_list = (arg={}, _) =>
posts = [listing_info post for post in *get_posts @, arg.limit]
html.build ->
import section, h3, time, a from tag
return for { :title, :url, :blurb, publish_date: { iso, pretty }} in *posts
section {
h3 { a { title, href: url }}
time { pretty, datetime: iso }
"—", blurb, a { "Read post →", class: "read-post-link", href: url }
}
-- [tag.section { "hoh" } for post in *posts]
-- for key,value in pairs(o) do
-- print("found member " .. key);
-- end
-- p @@__name
-- p @.source
-- p reflect @
-- p reflect @renderer
-- unpack for post in *get_posts @, arg.limit
-- common.fill_ignoring_pre template, listing_info post
-- return for post in *get_posts @, arg.limit
-- p reflect post
-- p reflect post
-- p post\get_tpl_scope!
-- p post.meta
-- common.fill_ignoring_pre template, listing_info post
-- p reflect post
-- p html_renderer
-- p html_renderer.__base
-- p html_renderer.cosmo_helpers
-- html_renderer.render
-- p post.renderer.helpers post
-- p html_renderer.__base
-- p reflect @@__base
-- html.build -> tag.h2 { "justrender call: $some_name (doesn't work)" }
add_all = (files) ->
for path in *files
add path,
target: target(path, "/posts")
template: "post"
is_a: "post"
publish_date: { extract_date(path), publish_date path }
id: extract_id path
add "index.html", title: "Catalin Mititiuc"
add "blog.html", title: "Posts", target: "posts/index", template: "blog"
add_all files_from "docs"
-- add_all files_from "docs", to: "/posts", as: "post"
-- 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"