From 6f90db7193a2a0f2524a57151d923ab8403a974a Mon Sep 17 00:00:00 2001 From: valentin gagarin Date: Wed, 13 Nov 2024 15:24:41 +0100 Subject: [PATCH] unify template parameters --- website/presentation/default.nix | 24 ++++++++++-------------- website/structure/article.nix | 2 +- website/structure/navigation.nix | 4 +++- website/structure/page.nix | 2 +- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/website/presentation/default.nix b/website/presentation/default.nix index 1b24b7e..39ec359 100644 --- a/website/presentation/default.nix +++ b/website/presentation/default.nix @@ -20,35 +20,31 @@ in options.templates = let # arbitrarily nested attribute set where the leaves are of type `type` - # 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)); + 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` + 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 '' diff --git a/website/structure/article.nix b/website/structure/article.nix index aac3a39..67ec604 100644 --- a/website/structure/article.nix +++ b/website/structure/article.nix @@ -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; }) ]; }; }); diff --git a/website/structure/navigation.nix b/website/structure/navigation.nix index 97ae948..5f47d23 100644 --- a/website/structure/navigation.nix +++ b/website/structure/navigation.nix @@ -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; + }; }; }; }; diff --git a/website/structure/page.nix b/website/structure/page.nix index a4352f1..748dd3a 100644 --- a/website/structure/page.nix +++ b/website/structure/page.nix @@ -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; }) ]; }; };