Compare commits

..

No commits in common. "765e34754bc98da0ee121678c66e2edd463a9088" and "61709abeb59c72d3bf02e135fa7179f965d216ae" have entirely different histories.

6 changed files with 130 additions and 256 deletions

View file

@ -1,21 +0,0 @@
{ config, ... }:
let
inherit (config) pages;
in
{
menus.main = {
label = "Main";
items = [
{
menu.label = "Consortium";
menu.items = map (page: { inherit page; }) (with pages; [ nlnet oid tweag nordunet ]);
}
{
page = pages.fediversity;
}
{
page = pages.grants;
}
];
};
}

View file

@ -22,7 +22,6 @@ in
modules = [
./structure
./content
./presentation
{
_module.args = {
inherit pkgs;

View file

@ -1,115 +0,0 @@
{ config, options, lib, pkgs, ... }:
let
inherit (lib)
mkOption
types
;
templates = import ./templates.nix { inherit lib; };
in
{
options.templates = mkOption {
description = ''
Collection of named functions to convert page contents to files
Each template function takes the complete site `config` and the page data structure.
'';
type = with types; attrsOf (functionTo (functionTo options.files.type));
};
config.templates =
let
commonmark = name: markdown: pkgs.runCommand "${name}.html"
{
buildInputs = [ pkgs.cmark ];
} ''
cmark ${builtins.toFile "${name}.md" markdown} > $out
'';
in
{
page = lib.mkDefault (config: page: {
# TODO: create static redirects from `tail page.locations`
# 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.
# TODO: maybe it would even make sense to split routing and rendering altogether
${page.outPath} = builtins.toFile "${page.name}.html" (templates.html {
head = ''
<title>${page.title}</title>
<meta name="description" content="${page.description}" />
<link rel="canonical" href="${page.outPath}" />
'';
body = ''
${templates.nav { menu = { menu = config.menus.main; }; }}
${builtins.readFile (commonmark page.name page.body)}
'';
});
});
article = lib.mkDefault (config: page: {
# TODO: create static redirects from `tail page.locations`
${page.outPath} = builtins.toFile "${page.name}.html" (templates.html {
head = ''
<title>${page.title}</title>
<meta name="description" content="${page.description}" />
<meta name="author" content="${with lib; if isList page.author then join ", " page.author else page.author}" />
'';
body = ''
${templates.nav { menu = { menu = config.menus.main; }; }}
${builtins.readFile (commonmark page.name page.body)}
'';
});
});
};
options.files = mkOption {
description = ''
Files that make up the site, mapping from output path to contents
By default, all elements in `option`{pages} are converted to files using their template or the default template.
Add more files to the output by assigning to this attribute set.
'';
type = with types; attrsOf path;
};
config.files =
let
pages = lib.concatMapAttrs
(name: page: page.template config page)
config.pages;
collections =
let
byCollection = with lib; mapAttrs
(_: collection:
map (entry: entry.template config entry) collection.entry
)
config.collections;
in
with lib; concatMapAttrs
(collection: entries:
foldl' (acc: entry: acc // entry) { } entries
)
byCollection;
in
pages // collections;
options.build = mkOption {
description = ''
The final output of the web site
'';
type = types.package;
default =
let
script = ''
mkdir $out
'' + lib.join "\n" copy;
copy = lib.mapAttrsToList
(
path: file: ''
mkdir -p $out/$(dirname ${path})
cp -r ${file} $out/${path}
''
)
config.files;
in
pkgs.runCommand "source" { } script;
};
}

View file

@ -1,37 +0,0 @@
{ lib }:
rec {
html = { head, body }: ''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
${lib.indent " " head}
</head>
<body>
${lib.indent " " body}
<body>
</html>
'';
nav = { menu }:
let
render-item = item:
if item ? menu then
''
<li>${item.menu.label}
${lib.indent " " (nav { menu = item; })}
''
else
if item ? page then ''<li><a href="${item.page}">${item.page.title}</a></li>''
else ''<li><a href="${item.link.url}">${item.link.label}</a></li>''
;
in
''
<nav>
<ul>
${with lib; indent " " (join "\n" (map render-item menu.menu.items))}
</ul>
</nav>
'';
}

View file

@ -14,11 +14,15 @@ in
};
};
config.content-types = {
document = { name, config, ... }: {
page = { name, config, ... }: {
options = {
name = mkOption {
description = "Symbolic name, used as a human-readable identifier";
description = "Symbolic name for the page, used as a human-readable identifier";
type = types.str;
default = name;
};
title = mkOption {
description = "Page title";
type = types.str;
default = name;
};
@ -36,7 +40,6 @@ in
type = with types; functionTo str;
default = target: "TODO: compute the relative path based on `locations`";
};
# 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
@ -44,28 +47,6 @@ in
type = types.str;
default = lib.head config.locations;
};
# TODO: maybe it would even make sense to split routing and rendering altogether.
# in that case, templates would return strings, and a different
# piece of the machinery resolves rendering templates to files
# using `locations`.
# then we'd have e.g. `templates.html` and `templates.atom` for
# different output formats.
template = mkOption {
description = ''
Function that converts the page contents to files
'';
type = with types; functionTo (functionTo options.files.type);
};
};
};
page = { name, config, ... }: {
imports = [ cfg.content-types.document ];
options = {
title = mkOption {
description = "Page title";
type = types.str;
default = name;
};
description = mkOption {
description = ''
One-sentence description of page contents
@ -84,8 +65,14 @@ in
'';
type = types.str;
};
template = mkOption {
description = ''
Function that converts the page contents to files
'';
type = with types; functionTo (functionTo options.files.type);
default = cfg.templates.page;
};
};
config.template = cfg.templates.page;
};
article = { config, collectionName, ... }: {
@ -104,43 +91,7 @@ in
};
config.name = lib.slug config.title;
config.outPath = "${collectionName}/${lib.head config.locations}";
config.template = lib.mkForce cfg.templates.article;
};
named-link = { ... }: {
options = {
label = mkOption {
description = "Link label";
type = types.str;
};
url = mkOption {
description = "Link URL";
type = types.str;
};
};
};
navigation = { name, ... }: {
options = {
name = mkOption {
description = "Symbolic name, used as a human-readable identifier";
type = types.str;
default = name;
};
label = mkOption {
description = "Menu label";
type = types.str;
default = name;
};
items = mkOption {
description = "List of menu items";
type = with types; listOf (attrTag {
menu = mkOption { type = submodule cfg.content-types.navigation; };
page = mkOption { type = submodule cfg.content-types.page; };
link = mkOption { type = submodule cfg.content-types.named-link; };
});
};
};
config.template = cfg.templates.article;
};
};
}

View file

@ -9,7 +9,6 @@ in
{
imports = [ ./content-types.nix ];
# TODO: enable i18n, e.g. via a nested attribute for language-specific content
options.pages = mkOption {
description = ''
Collection of pages on the site
@ -21,20 +20,6 @@ in
{
description = ''
Named collections of unnamed pages
Define the content type of a new collection `example` to be `article`:
```nix
config.collections.example.type = config.types.article;
```
Add a new entry to the `example` collection:
```nix
config.collections.example.entry = {
# contents here
}
```
'';
type = with types; attrsOf (submodule ({ name, config, ... }: {
options = {
@ -54,10 +39,122 @@ in
}));
};
options.menus = mkOption {
options.templates = mkOption {
description = ''
Collection navigation menus
Collection of named functions to convert page contents to files
Each template function takes the complete site `config` and the page data structure.
'';
type = with types; attrsOf (submodule config.content-types.navigation);
type = with types; attrsOf (functionTo (functionTo options.files.type));
};
# TODO: split out templates and all related helper junk into `../presentation`
config.templates =
let
commonmark = name: markdown: pkgs.runCommand "${name}.html"
{
buildInputs = [ pkgs.cmark ];
} ''
cmark ${builtins.toFile "${name}.md" markdown} > $out
'';
in
{
page = lib.mkDefault (config: page: {
# TODO: create static redirects from `tail page.locations`
# 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.
${page.outPath} = builtins.toFile "${page.name}.html" ''
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>${page.title}</title>
<meta name="description" content="${page.description}" />
<link rel="canonical" href="${page.outPath}" />
</head>
<body>
${lib.indent " " (builtins.readFile (commonmark page.name page.body))}
<body>
</html>
'';
});
article = lib.mkDefault (config: page: {
# TODO: create static redirects from `tail page.locations`
${page.outPath} = builtins.toFile "${page.name}.html" ''
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>${page.title}</title>
<meta name="description" content="${page.description}" />
${with lib;
if ! isNull page.author then
''<meta name="author" content="${if isList page.author then join ", " page.author else page.author}" />''
else ""
}
<link rel="canonical" href="${page.outPath}" />
</head>
<body>
${lib.indent " " (builtins.readFile (commonmark page.name page.body))}
<body>
</html>
'';
});
};
options.files = mkOption {
description = ''
Files that make up the site, mapping from output path to contents
By default, all elements in `option`{pages} are converted to files using their template or the default template.
Add more files to the output by assigning to this attribute set.
'';
type = with types; attrsOf path;
};
config.files =
let
pages = lib.concatMapAttrs
(name: page: page.template config page)
config.pages;
collections =
let
byCollection = with lib; mapAttrs
(_: collection:
map (entry: entry.template config entry) collection.entry
)
config.collections;
in
with lib; concatMapAttrs
(collection: entries:
foldl' (acc: entry: acc // entry) { } entries
)
byCollection;
in
pages // collections;
options.build = mkOption {
description = ''
The final output of the web site
'';
type = types.package;
default =
let
script = ''
mkdir $out
'' + lib.join "\n" copy;
copy = lib.mapAttrsToList
(
path: file: ''
mkdir -p $out/$(dirname ${path})
cp -r ${file} $out/${path}
''
)
config.files;
in
pkgs.runCommand "source" { } script;
};
}