unify template parameters

This commit is contained in:
Valentin Gagarin 2024-11-13 15:24:41 +01:00 committed by Valentin Gagarin
parent cbe10ec304
commit 6f90db7193
4 changed files with 15 additions and 17 deletions

View file

@ -20,35 +20,31 @@ in
options.templates =
let
# arbitrarily nested attribute set where the leaves are of type `type`
recursiveAttrs = type: with types;
# NOTE: due to how `either` works, the first match is significant,
# so if `type` happens to be an attrset, the typecheck will consider
# `type`, not `attrsOf`
recursiveAttrs = type: with types; attrsOf (either type (recursiveAttrs type));
attrsOf (either type (recursiveAttrs type));
in
mkOption {
description = ''
Collection of named functions to convert document contents to a string representation
Each template function takes the complete site `config` and the document's data structure.
Collection of named helper functions for conversion different structured representations which can be rendered to a string
'';
# TODO: this function should probably take a single attrs,
# otherwise it's quite inflexible.
# named parameters would also help with readability at the call site
type = recursiveAttrs (with types; functionTo (functionTo str));
type = recursiveAttrs (with types; functionTo (coercedTo attrs toString str));
};
config.templates.html = {
markdown = name: text:
markdown = { name, body }:
let
commonmark = pkgs.runCommand "${name}.html"
{
buildInputs = [ pkgs.cmark ];
} ''
cmark ${builtins.toFile "${name}.md" text} > $out
cmark ${builtins.toFile "${name}.md" body} > $out
'';
in
builtins.readFile commonmark;
nav = menu: page:
nav = { menu, page }:
let
render-item = item:
if item ? menu then ''

View file

@ -46,7 +46,7 @@ in
body.content = [
(cfg.menus.main.outputs.html config)
{ section.heading.content = config.title; }
(cfg.templates.html.markdown config.name config.body)
(cfg.templates.html.markdown { inherit (config) name body; })
];
};
});

View file

@ -56,7 +56,9 @@ in
It must be a function that takes the page on which the navigation is to be shown, such that relative links get computed correctly.
'';
type = with types; attrsOf (functionTo str);
default.html = cfg.templates.html.nav config;
default.html = page: cfg.templates.html.nav {
menu = config; inherit page;
};
};
};
};

View file

@ -52,7 +52,7 @@ in
body.content = [
(cfg.menus.main.outputs.html config)
{ section.heading.content = config.title; }
(cfg.templates.html.markdown config.name config.body)
(cfg.templates.html.markdown { inherit (config) name body; })
];
};
};