From 4fdb56e43601716c8a2654057a0c9fbf426d641d Mon Sep 17 00:00:00 2001 From: valentin gagarin Date: Wed, 13 Nov 2024 15:24:41 +0100 Subject: [PATCH] implement raw assets this allows adding files to the output as they are --- presentation/templates.nix | 13 +++++++++++++ structure/assets.nix | 35 +++++++++++++++++++++++++++++++++++ structure/collections.nix | 12 ++++-------- structure/default.nix | 21 +++++++++++++++++++-- structure/page.nix | 11 ++--------- 5 files changed, 73 insertions(+), 19 deletions(-) create mode 100644 structure/assets.nix diff --git a/presentation/templates.nix b/presentation/templates.nix index 7a688f70..d23ee1fb 100644 --- a/presentation/templates.nix +++ b/presentation/templates.nix @@ -49,4 +49,17 @@ in ''; }; + + config.templates.files = fs: with lib; + foldl' + # TODO: create static redirects from `tail .locations` + (acc: elem: acc // (mapAttrs' (type: value: { + name = head elem.locations + optionalString (type != "") ".${type}"; + value = builtins.toFile + (elem.name + optionalString (type != "") ".${type}") + (toString value); + })) + elem.outputs) + { } + fs; } diff --git a/structure/assets.nix b/structure/assets.nix new file mode 100644 index 00000000..ff534286 --- /dev/null +++ b/structure/assets.nix @@ -0,0 +1,35 @@ +{ config, lib, ... }: +let + inherit (lib) + mkOption + types + ; + cfg = config; +in +{ + options.assets = mkOption { + description = '' + Collection of assets, i.e. static files that can be linked to from within documents + ''; + type = with types; attrsOf (submodule ({ config, ... }: { + imports = [ cfg.content-types.document ]; + options.path = mkOption { + type = types.path; + }; + config.name = builtins.baseNameOf config.path; + config.outputs."" = builtins.readFile config.path; + })); + default = { }; + }; + + config.files = with lib; + let + flatten = attrs: mapAttrsToList + (name: value: + # HACK: we somehow have to distinguish a module value from regular attributes. + # arbitrary choice: the outputs attribute + if value ? outputs then value else mapAttrsToList value) + attrs; + in + cfg.templates.files (flatten cfg.assets); +} diff --git a/structure/collections.nix b/structure/collections.nix index 7dd12610..a4f4fa66 100644 --- a/structure/collections.nix +++ b/structure/collections.nix @@ -61,15 +61,11 @@ in }; })); }; + config.files = - # TODO: create static redirects from `tail .locations` + with lib; let - collections = with lib; concatMap (collection: collection.entry) (attrValues config.collections); + collections = concatMap (collection: collection.entry) (attrValues config.collections); in - with lib; foldl - (acc: elem: acc // { - "${head elem.locations}.html" = builtins.toFile "${elem.name}.html" "${elem.outputs.html}"; - }) - { } - collections; + cfg.templates.files collections; } diff --git a/structure/default.nix b/structure/default.nix index 62102927..a13bb95f 100644 --- a/structure/default.nix +++ b/structure/default.nix @@ -47,11 +47,28 @@ in }; link = mkOption { description = "Helper function for transparent linking to other pages"; - type = with types; functionTo str; + type = with types; functionTo attrs; # TODO: we may want links to other representations, # and currently the mapping of output types to output file # names is soft. - default = target: with lib; "${relativePath (head config.locations) (head target.locations)}.html"; + default = with lib; target: + let + path = relativePath (head config.locations) (head target.locations); + links = mapAttrs + (type: output: + path + optionalString (type != "") ".${type}" + # ^^^^^^^^^^^^ + # convention for raw files + ) + target.outputs; + in + if length (attrValues links) == 0 + then throw "no output to link to for '${target.name}'" + else if length (attrValues links) == 1 + then links // { + __toString = _: head (attrValues links); + } + else links; }; outputs = mkOption { description = '' diff --git a/structure/page.nix b/structure/page.nix index 6c20719c..3578385b 100644 --- a/structure/page.nix +++ b/structure/page.nix @@ -14,15 +14,8 @@ in ''; type = with types; attrsOf (submodule config.content-types.page); }; - config.files = with lib; - foldl' - (acc: elem: acc // { - # TODO: create static redirects from `tail page.locations` - # TODO: the file name could correspond to the canonical location in the HTML representation - "${head elem.locations}.html" = builtins.toFile "${elem.name}.html" "${elem.outputs.html}"; - }) - { } - (attrValues config.pages); + + config.files = with lib; cfg.templates.files (attrValues config.pages); config.content-types.page = { name, config, ... }: { imports = [ cfg.content-types.document ];