html.nix/templaters/simple/default.nix

184 lines
4.4 KiB
Nix
Raw Normal View History

2023-04-06 05:43:25 +03:00
{
config,
lib,
...
}: let
l = lib // builtins;
t = l.types;
inherit (config.html-nix.lib) html css;
func = ctx: let
stylesheets = l.map html.mkStylesheet ["${ctx.baseurl}/site.css"];
stylesheet = import ./stylesheet.nix {inherit css l;};
renderPost = post:
with html;
article [
(h1 {inherit (post) id;} post.displayName)
2023-04-06 06:21:12 +03:00
(
l.optionalString
(post.date != null)
(h4 {class = "nohashtag";} ("date: " + post.date))
)
2023-04-06 05:43:25 +03:00
post.content
];
mkPage = {
content,
titleStr ? ctx.config.title,
2023-04-06 07:52:11 +03:00
description ? null,
2023-04-06 05:43:25 +03:00
}:
with html; ''
<!DOCTYPE html>
2023-04-06 07:14:38 +03:00
${html.html {lang = ctx.config.siteLang or "en";} [
2023-04-06 05:43:25 +03:00
(head (stylesheets
++ [
(title titleStr)
2023-04-06 07:52:11 +03:00
(l.optionalString (description != null) (meta {
name = "description";
content = ctx.config.description;
}))
2023-04-06 05:43:25 +03:00
(meta {
name = "viewport";
content = "width=device-width, initial-scale=1";
})
2023-04-06 07:14:38 +03:00
(l.optionalString (ctx.config ? iconPath) (link {
rel = "icon";
2023-04-06 07:15:47 +03:00
href = "${ctx.baseurl}/${ctx.config.iconPath}";
2023-04-06 07:14:38 +03:00
}))
2023-04-06 05:43:25 +03:00
]))
(body ''
${script "0"}
${div (l.flatten [
navBar
(hr {})
content
])}
'')
]}
'';
navBar = with html;
nav (
[
(a {
href = "${ctx.baseurl}/";
class = "novisited";
} "home")
]
2023-04-06 07:04:05 +03:00
++ l.optionals (l.length ctx.posts > 0) [
" "
(a {
href = "${ctx.baseurl}/posts";
class = "novisited";
} "posts")
]
2023-04-06 05:43:25 +03:00
++ (
l.map
(
page:
" "
+ (
a {
href = "${ctx.baseurl}/${page.id}/";
class = "novisited";
}
page.displayName
)
)
ctx.pages
)
);
2023-04-06 06:21:12 +03:00
mkPostsLinks = posts:
with html;
l.singleton
(ul (
l.map
(
post:
li (
a {href = "${ctx.baseurl}/${post.id}";}
(
if post.date != null
then "${post.date} - ${post.displayName}"
else post.displayName
)
)
)
posts
));
postsLinksWithDate = mkPostsLinks (l.filter (p: p.date != null) ctx.posts);
postsLinksWithoutDate = mkPostsLinks (l.filter (p: p.date == null) ctx.posts);
2023-04-06 05:43:25 +03:00
2023-04-06 06:21:12 +03:00
postsSectionContent =
[(html.h1 "posts")]
++ postsLinksWithDate
++ [(html.h2 "miscellaneous")]
++ postsLinksWithoutDate;
2023-04-06 05:43:25 +03:00
postsRendered = l.listToAttrs (
l.map
(post:
l.nameValuePair post.id {
content = renderPost post;
name = post.displayName;
2023-04-06 07:52:11 +03:00
description = post.description;
2023-04-06 05:43:25 +03:00
})
ctx.posts
);
pagesRendered = l.listToAttrs (
l.map
(page:
l.nameValuePair page.id {
content = page.content;
name = page.displayName;
2023-04-06 07:52:11 +03:00
description = page.description;
2023-04-06 05:43:25 +03:00
})
ctx.pages
);
indexPage = mkPage {
content = ctx.indexContent or postsSectionContent;
};
in {
2023-04-06 06:29:22 +03:00
inherit ctx stylesheets mkPage stylesheet postsSectionContent;
2023-04-06 05:43:25 +03:00
site =
ctx.site
// {
"index.html" = indexPage;
"posts"."index.html" = mkPage {
content = postsSectionContent;
titleStr = "posts - ${ctx.config.title}";
};
"404.html" = mkPage {
2023-04-06 06:29:22 +03:00
content = html.h1 {class = "nohashtag";} "404 - page not found";
2023-04-06 05:43:25 +03:00
titleStr = "page not found - ${ctx.config.title}";
};
"site.css" = stylesheet;
}
// (
l.mapAttrs
(
name: value: {
"index.html" = mkPage {
2023-04-06 07:52:11 +03:00
inherit (value) content description;
2023-04-06 05:43:25 +03:00
titleStr = "${value.name} - ${ctx.config.title}";
};
}
)
(pagesRendered // postsRendered)
2023-04-06 07:18:55 +03:00
);
2023-04-06 05:43:25 +03:00
};
in {
options = {
html-nix.lib.templaters.simple = l.mkOption {
type = t.uniq (t.functionTo t.attrs);
};
};
config = {
html-nix.lib.templaters.simple = func;
};
}