html.nix/templaters/simple/default.nix

174 lines
4.0 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,
}:
with html; ''
<!DOCTYPE html>
${html.html [
(head (stylesheets
++ [
(title titleStr)
(meta {
name = "viewport";
content = "width=device-width, initial-scale=1";
})
]))
(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;
})
ctx.posts
);
pagesRendered = l.listToAttrs (
l.map
(page:
l.nameValuePair page.id {
content = page.content;
name = page.displayName;
})
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 {
content = value.content;
titleStr = "${value.name} - ${ctx.config.title}";
};
}
)
(pagesRendered // postsRendered)
)
// l.optionalAttrs (ctx ? resources) {inherit (ctx) resources;};
};
in {
options = {
html-nix.lib.templaters.simple = l.mkOption {
type = t.uniq (t.functionTo t.attrs);
};
};
config = {
html-nix.lib.templaters.simple = func;
};
}