152 lines
4.6 KiB
Plaintext
152 lines
4.6 KiB
Plaintext
sitegen = require "sitegen"
|
|
Site = require "sitegen.site"
|
|
Path = require "sitegen.path"
|
|
html = require "sitegen.html"
|
|
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"
|
|
|
|
-- 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
|
|
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) ->
|
|
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
|
|
|
|
rootname = (str) ->
|
|
result = string.gsub str, "%..+", ""
|
|
result
|
|
|
|
-- TODO use split function from common
|
|
split = (str, delimiter="/") ->
|
|
[capture for capture in str\gmatch "[^#{delimiter}]+"]
|
|
|
|
last = (list) -> list[#list]
|
|
join = (first, last, delimiter="/") -> table.concat { first, last }, delimiter
|
|
reverse = (list={}) -> [item for item in *list[#list, 1, -1]]
|
|
target = (path, prefix) -> join prefix, rootname(last(split path))
|
|
|
|
posts = (path=".") ->
|
|
files = reverse get_files path
|
|
{join(path, file), target(file, "/posts") for file in *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
|
|
|
|
posts2 = (path=".") ->
|
|
files = reverse get_files path
|
|
|
|
[{
|
|
path: join(path, file)
|
|
target: Path.join Path.basepath(target(file, "/posts")), extract_id(join(path, file))
|
|
post_id: extract_id join(path, file)
|
|
publish_date: publish_date join(path, file)
|
|
} for file in *files]
|
|
|
|
common = require("sitegen.common")
|
|
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="$target">$post_title</a></h3>
|
|
<time>$publish_date</time> — $blurb
|
|
<a class="read-post-link" href="$target">Read post →</a>
|
|
</section>
|
|
]]
|
|
|
|
list_posts = (page, args={}) ->
|
|
posts = page.site\query_pages { is_a: "post" }
|
|
table.sort posts, (a, b) -> a.source > b.source
|
|
|
|
table.concat [common.fill_ignoring_pre template, {
|
|
target: meta.target
|
|
post_title: meta.title or titleize_slug meta.id
|
|
publish_date: publish_date source
|
|
blurb: meta.blurb
|
|
} for { meta: meta, source: source } in *posts[1, args.limit or #posts]]
|
|
|
|
list_posts2 = (page, args={}) ->
|
|
pages = page.site\query_pages { is_a: "post" }
|
|
table.sort pages, (a, b) -> a.source > b.source
|
|
|
|
posts = [{
|
|
target: meta.target
|
|
post_title: meta.title or titleize_slug(meta.id)
|
|
publish_date: publish_date source
|
|
blurb: meta.blurb
|
|
} for { meta: meta, source: source } in *pages[1, args.limit or #pages]]
|
|
|
|
for post in *posts do cosmo.yield post
|
|
|
|
html_renderer.cosmo_helpers.if = (args, has_block) => cosmo.cif args, has_block
|
|
html_renderer.cosmo_helpers.titleize = (slug) => html.build ->
|
|
titleize_slug(slug) if slug
|
|
|
|
sitegen.create =>
|
|
@site_title = "WebDevCat.me"
|
|
@app_name = "stasis"
|
|
@version = "0.2.12"
|
|
@list_posts = list_posts2
|
|
@lp = list_posts
|
|
|
|
add "index.html", title: "Catalin Mititiuc"
|
|
add "blog.html", title: "Posts", target: "posts/index", template: "blog"
|
|
|
|
-- add "about.html", some: {
|
|
-- { one: "alpha", two: "beta" },
|
|
-- { one: "A", two: "B" }
|
|
-- }, cif: cosmo.cif, math: math, x: 2
|
|
|
|
for path, target in pairs posts "docs"
|
|
id = extract_id path
|
|
|
|
add path,
|
|
target: Path.join Path.basepath(target), id
|
|
template: "post"
|
|
is_a: "post"
|
|
publish_date: publish_date path
|
|
id: id
|
|
-- titleize: (slug) -> html.build -> titleize_slug(slug)
|
|
|
|
-- 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"
|