Compare commits

..

4 commits

Author SHA1 Message Date
Valentin Gagarin 3689353603 add type for list of unique elements 2024-11-03 17:52:36 +01:00
Valentin Gagarin c6f44d5f8e don't use outPath any more
- links are constructed explicitly, relative to the current page's
location
- templates are called explicitly by output type
2024-10-16 18:23:55 +02:00
Valentin Gagarin c25cd72ee9 let navigation have its own template 2024-10-16 18:20:48 +02:00
Valentin Gagarin 0d5db72310 make a proper link 2024-10-16 18:19:41 +02:00
6 changed files with 54 additions and 26 deletions

View file

@ -55,7 +55,7 @@ in
sorted = with lib; reverseList (sortOn (entry: entry.date) config.collections.news.entry);
in
lib.join "\n" (map (article: ''
- ${article.date} [${article.title}](${article})
- ${article.date} [${article.title}](${link article})
'') sorted)
}
'';

27
lib.nix
View file

@ -113,5 +113,32 @@ rec {
};
nestedTypes.elemType = elemType;
};
listOfUnique = elemType:
let
baseType = lib.types.listOf elemType;
in
baseType // {
merge = loc: defs:
let
# Keep track of which definition each value came from
defsWithValues = map
(def:
map (v: { inherit (def) file; value = v; }) def.value
)
defs;
flatDefs = lib.flatten defsWithValues;
# Check for duplicates while preserving source info
seen = builtins.foldl'
(acc: def:
if lib.lists.any (v: v.value == def.value) acc
then throw "The option `${lib.options.showOption loc}` has duplicate values (${toString def.value}) defined in ${def.file}"
else acc ++ [ def ]
) [ ]
flatDefs;
in
map (def: def.value) seen;
};
};
}

View file

@ -21,6 +21,9 @@ in
Each template function takes the complete site `config` and the document's data structure.
'';
# 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));
};
@ -34,14 +37,15 @@ in
'';
in
{
nav = lib.mkDefault templates.nav;
page = lib.mkDefault (config: page: templates.html {
head = ''
<title>${page.title}</title>
<meta name="description" content="${page.description}" />
<link rel="canonical" href="${page.outPath}" />
<link rel="canonical" href="${lib.head page.locations}" />
'';
body = ''
${templates.nav { inherit page; menu = { menu = config.menus.main; }; }}
${config.menus.main.outputs.html page}
${builtins.readFile (commonmark page.name page.body)}
'';
});
@ -55,7 +59,7 @@ in
}
'';
body = ''
${templates.nav { inherit page; menu = { menu = config.menus.main; }; }}
${config.menus.main.outputs.html page}
${builtins.readFile (commonmark page.name page.body)}
'';
});

View file

@ -14,17 +14,16 @@ rec {
<body>
</html>
'';
nav = { page, menu }:
nav = menu: page:
let
render-item = item:
if item ? menu then
''
<li>${item.menu.label}
${lib.indent " " (nav { inherit page; menu = item; })}
''
else
if item ? page then ''<li><a href="${page.link item.page}">${item.page.title}</a></li>''
else ''<li><a href="${item.link.url}">${item.link.label}</a></li>''
if item ? menu then ''
<li>${item.menu.label}
${lib.indent " " (item.menu.outputs.html page)}
</li>
''
else if item ? page then ''<li><a href="${page.link item.page}">${item.page.title}</a></li>''
else ''<li><a href="${item.link.url}">${item.link.label}</a></li>''
;
in
''

View file

@ -14,9 +14,6 @@ in
type = types.str;
default = name;
};
# TODO: reconsider using `page.outPath` and what to put into `locations`.
# maybe we can avoid having ".html" suffixes there.
# since templates can output multiple files, `html` is merely one of many things we *could* produce.
locations = mkOption {
description = ''
List of historic output locations for the resulting file
@ -46,15 +43,7 @@ in
# 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 config.outPath target.outPath}.html";
};
# TODO: may not need it when using `link`; could repurpose it to render the default template
outPath = mkOption {
description = ''
Location of the page, used for transparently creating links
'';
type = types.str;
default = lib.head config.locations;
default = target: with lib; "${relativePath (head config.locations) (head target.locations)}.html";
};
outputs = mkOption {
description = ''

View file

@ -29,7 +29,7 @@ in
};
};
content-types.navigation = { name, ... }: {
content-types.navigation = { name, config, ... }: {
options = {
name = mkOption {
description = "Symbolic name, used as a human-readable identifier";
@ -49,6 +49,15 @@ in
link = mkOption { type = submodule cfg.content-types.named-link; };
});
};
outputs = mkOption {
description = ''
Representations of the navigation structure in different formats
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 { menu = config; };
};
};
};
}