fediversity.eu/site.nix

72 lines
1.4 KiB
Nix
Raw Normal View History

2024-10-11 12:28:57 +02:00
{ pkgs, lib, ... }:
2024-10-11 13:38:14 +02:00
let
join = lib.concatStringsSep;
in
2024-10-11 12:50:31 +02:00
rec {
2024-10-11 13:38:14 +02:00
/**
Build the web site
*/
2024-10-11 13:42:46 +02:00
build = name: dir:
2024-10-11 13:38:14 +02:00
let
script = ''
mkdir $out
'' + join "\n" copy;
copy = lib.mapAttrsToList
(
path: document: ''
mkdir -p $out/$(dirname ${path})
cp ${document} $out/${path}
''
)
(files (sources dir));
in
pkgs.runCommand name { } script;
/**
Get source files from a flat directory
*/
sources = dir: lib.mapAttrs'
(
attrname: value: {
name = lib.removeSuffix ".nix" attrname;
value = import (dir + "/${attrname}");
}
)
(builtins.readDir dir);
/**
Create a mapping from output file path to document contents
*/
files = documents: lib.mapAttrs'
(
name: document: {
name = document.outPath;
value = html document "${name}.html";
}
)
documents;
2024-10-11 12:50:31 +02:00
/**
Convert a Nix document to HTML
*/
html = document: name:
builtins.toFile "${name}.html" ''
<html>
<head>
<title>${document.title}</title>
</head>
<body>
${builtins.readFile (commonmark document.body name)}
<body>
</html>
'';
/**
Convert a commonmark string to HTML
*/
commonmark = markdown: name:
pkgs.runCommand "${name}.html" { buildInputs = [ pkgs.cmark ]; } ''
cmark ${builtins.toFile "${name}.md" markdown} > $out
'';
2024-10-11 12:28:57 +02:00
}