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

View file

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