Fediversity/website/presentation/templates.nix

88 lines
1.9 KiB
Nix
Raw Normal View History

2025-02-19 18:34:19 +01:00
{
config,
lib,
pkgs,
...
}:
2025-02-19 18:38:05 +01:00
2024-11-13 15:24:41 +01:00
{
config.templates.html = {
2025-02-19 18:34:19 +01:00
dom =
document:
let
eval = lib.evalModules {
class = "DOM";
2025-02-19 18:34:19 +01:00
modules = [
document
(import ./dom.nix)
];
};
in
{
__toString = _: toString eval.config;
value = eval.config;
};
2025-02-19 18:34:19 +01:00
markdown =
{ name, body }:
2024-11-13 15:24:41 +01:00
let
2025-02-19 18:34:19 +01:00
commonmark =
pkgs.runCommand "${name}.html"
{
buildInputs = [ pkgs.cmark ];
}
''
cmark ${builtins.toFile "${name}.md" body} > $out
'';
2024-11-13 15:24:41 +01:00
in
builtins.readFile commonmark;
2025-02-19 18:34:19 +01:00
nav =
{ menu, page }:
2024-11-13 15:24:41 +01:00
let
2025-02-19 18:34:19 +01:00
render-item =
item:
if item ? menu then
''
<li><details><summary>${item.menu.label}</summary>
${lib.indent " " (item.menu.outputs.html page)}
</li>
''
else if item ? page then
''<li><a href="${page.link item.page}">${item.page.title}</a></li>''
else
''<li><a href="${item.link.url}">${item.link.label}</a></li>'';
2024-11-13 15:24:41 +01:00
in
''
<nav>
<ul>
${with lib; indent " " (join "\n" (map render-item menu.items))}
</ul>
</nav>
'';
};
2025-02-19 18:34:19 +01:00
config.templates.files =
fs:
with lib;
foldl'
# TODO: create static redirects from `tail <collection>.locations`
2025-02-19 18:34:19 +01:00
(
acc: elem:
acc
//
(mapAttrs' (
type: value: {
name = head elem.locations + optionalString (type != "") ".${type}";
value =
if isStorePath value then
value
else
builtins.toFile (elem.name + optionalString (type != "") ".${type}") (toString value);
}
))
elem.outputs
)
{ }
fs;
2024-11-13 15:24:41 +01:00
}