diff --git a/website/content/default.nix b/website/content/default.nix index a696290..649b5d3 100644 --- a/website/content/default.nix +++ b/website/content/default.nix @@ -1,13 +1,14 @@ { config, lib, ... }: let inherit (config) pages; + cfg = config; in { imports = lib.nixFiles ./.; - collections.news.type = config.content-types.article; + collections.news.type = cfg.content-types.article; - pages.index = { link, ... }: { + pages.index = { config, link, ... }: { title = "Fediversity"; description = "Fediversity web site"; summary = '' @@ -52,12 +53,19 @@ in ${ let - sorted = with lib; reverseList (sortOn (entry: entry.date) config.collections.news.entry); + sorted = with lib; reverseList (sortOn (entry: entry.date) cfg.collections.news.entry); in lib.join "\n" (map (article: '' - ${article.date} [${article.title}](${link article}) '') sorted) } ''; + outputs.html = (cfg.templates.html.page config).override { + html.body.content = lib.mkForce [ + # don't show the page title as a heading + (cfg.menus.main.outputs.html config) + (cfg.templates.html.markdown { inherit (config) name body; }) + ]; + }; }; } diff --git a/website/lib.nix b/website/lib.nix index f3cb7e4..aa8cdaa 100644 --- a/website/lib.nix +++ b/website/lib.nix @@ -1,5 +1,8 @@ { lib }: rec { + template = g: f: x: + (g (f x)) // { override = o: g (lib.recursiveUpdate (f x) o); }; + /** Recursively replace occurrences of `from` with `to` within `string` diff --git a/website/presentation/default.nix b/website/presentation/default.nix index 3e9e266..8f74b91 100644 --- a/website/presentation/default.nix +++ b/website/presentation/default.nix @@ -21,7 +21,7 @@ in description = '' Collection of named helper functions for conversion different structured representations which can be rendered to a string ''; - type = recursiveAttrs (with types; functionTo (coercedTo attrs toString str)); + type = recursiveAttrs (with types; functionTo (either str attrs)); }; options.files = mkOption { diff --git a/website/presentation/templates.nix b/website/presentation/templates.nix index c76cd3a..7a688f7 100644 --- a/website/presentation/templates.nix +++ b/website/presentation/templates.nix @@ -4,19 +4,21 @@ let mkOption types ; - # TODO: optionally run the whole thing through the validator - # https://github.com/validator/validator - render-html = document: - let - eval = lib.evalModules { - class = "DOM"; - modules = [ document (import ./dom.nix) ]; - }; - in - toString eval.config; in { config.templates.html = { + dom = document: + let + eval = lib.evalModules { + class = "DOM"; + modules = [ document (import ./dom.nix) ]; + }; + in + { + __toString = _: toString eval.config; + value = eval.config; + }; + markdown = { name, body }: let commonmark = pkgs.runCommand "${name}.html" diff --git a/website/structure/article.nix b/website/structure/article.nix index 67ec604..04c6e65 100644 --- a/website/structure/article.nix +++ b/website/structure/article.nix @@ -5,14 +5,6 @@ let types ; cfg = config; - render-html = document: - let - eval = lib.evalModules { - class = "DOM"; - modules = [ document (import ../presentation/dom.nix) ]; - }; - in - toString eval.config; in { content-types.article = { config, collection, ... }: { @@ -35,7 +27,7 @@ in }; }; config.name = lib.slug config.title; - config.outputs.html = lib.mkForce (render-html { + config.outputs.html = lib.mkForce (cfg.templates.html.dom { html = { head = { title.text = config.title; diff --git a/website/structure/collections.nix b/website/structure/collections.nix index b171c1f..7dd1261 100644 --- a/website/structure/collections.nix +++ b/website/structure/collections.nix @@ -68,7 +68,7 @@ in in with lib; foldl (acc: elem: acc // { - "${head elem.locations}.html" = builtins.toFile "${elem.name}.html" elem.outputs.html; + "${head elem.locations}.html" = builtins.toFile "${elem.name}.html" "${elem.outputs.html}"; }) { } collections; diff --git a/website/structure/default.nix b/website/structure/default.nix index 4e1cdb9..6210292 100644 --- a/website/structure/default.nix +++ b/website/structure/default.nix @@ -53,13 +53,11 @@ in # names is soft. default = target: with lib; "${relativePath (head config.locations) (head target.locations)}.html"; }; - outputs.html = mkOption { - # TODO: make this of type DOM and convert to string at the output. - # the output aggregator then only needs something string-coercible + outputs = mkOption { description = '' Representations of the document in different formats ''; - type = with types; str; + type = with types; attrsOf (either str attrs); }; }; }; diff --git a/website/structure/page.nix b/website/structure/page.nix index 8eef14e..8d255c1 100644 --- a/website/structure/page.nix +++ b/website/structure/page.nix @@ -5,14 +5,6 @@ let types ; cfg = config; - render-html = document: - let - eval = lib.evalModules { - class = "DOM"; - modules = [ document (import ../presentation/dom.nix) ]; - }; - in - toString eval.config; in { # TODO: enable i18n, e.g. via a nested attribute for language-specific content @@ -27,7 +19,7 @@ in (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; + "${head elem.locations}.html" = builtins.toFile "${elem.name}.html" "${elem.outputs.html}"; }) { } (attrValues config.pages); @@ -59,19 +51,22 @@ in type = types.str; }; }; - config.outputs.html = render-html { - html = { - head = { - title.text = config.title; - meta.description = config.description; - link.canonical = lib.head config.locations; - }; - body.content = [ - (cfg.menus.main.outputs.html config) - { section.heading.content = config.title; } - (cfg.templates.html.markdown { inherit (config) name body; }) - ]; - }; - }; + + config.outputs.html = cfg.templates.html.page config; }; + + config.templates.html.page = lib.template cfg.templates.html.dom (page: { + html = { + head = { + title.text = page.title; + meta.description = page.description; + link.canonical = lib.head page.locations; + }; + body.content = [ + (cfg.menus.main.outputs.html page) + { section.heading.content = page.title; } + (cfg.templates.html.markdown { inherit (page) name body; }) + ]; + }; + }); }