From 166a48f8cd877207de004ca76c6b723d9c81bc32 Mon Sep 17 00:00:00 2001 From: valentin gagarin Date: Wed, 13 Nov 2024 15:24:41 +0100 Subject: [PATCH] unify template parameters --- presentation/default.nix | 24 ++++++++++-------------- structure/article.nix | 2 +- structure/navigation.nix | 4 +++- structure/page.nix | 2 +- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/presentation/default.nix b/presentation/default.nix index 1b24b7e0..39ec3593 100644 --- a/presentation/default.nix +++ b/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/structure/article.nix b/structure/article.nix index aac3a398..67ec6045 100644 --- a/structure/article.nix +++ b/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/structure/navigation.nix b/structure/navigation.nix index 97ae9480..5f47d239 100644 --- a/structure/navigation.nix +++ b/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/structure/page.nix b/structure/page.nix index a4352f12..748dd3a7 100644 --- a/structure/page.nix +++ b/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; }) ]; }; };