split out template library

This commit is contained in:
Valentin Gagarin 2024-11-13 15:24:41 +01:00 committed by Valentin Gagarin
parent 1469481829
commit 3a36a6cf29
2 changed files with 34 additions and 37 deletions

View file

@ -4,6 +4,7 @@ let
mkOption mkOption
types types
; ;
templates = import ./templates.nix { inherit lib; };
in in
{ {
options.templates = mkOption { options.templates = mkOption {
@ -30,46 +31,25 @@ in
# TODO: reconsider using `page.outPath` and what to put into `locations`. # TODO: reconsider using `page.outPath` and what to put into `locations`.
# maybe we can avoid having ".html" suffixes there. # maybe we can avoid having ".html" suffixes there.
# since templates can output multiple files, `html` is merely one of many things we *could* produce. # since templates can output multiple files, `html` is merely one of many things we *could* produce.
${page.outPath} = builtins.toFile "${page.name}.html" '' ${page.outPath} = builtins.toFile "${page.name}.html" (templates.html {
<html> head = ''
<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> <title>${page.title}</title>
<meta name="description" content="${page.description}" /> <meta name="description" content="${page.description}" />
<link rel="canonical" href="${page.outPath}" /> <link rel="canonical" href="${page.outPath}" />
</head>
<body>
${lib.indent " " (builtins.readFile (commonmark page.name page.body))}
<body>
</html>
''; '';
body = builtins.readFile (commonmark page.name page.body);
});
}); });
article = lib.mkDefault (config: page: { article = lib.mkDefault (config: page: {
# TODO: create static redirects from `tail page.locations` # TODO: create static redirects from `tail page.locations`
${page.outPath} = builtins.toFile "${page.name}.html" '' ${page.outPath} = builtins.toFile "${page.name}.html" (templates.html {
<html> head = ''
<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> <title>${page.title}</title>
<meta name="description" content="${page.description}" /> <meta name="description" content="${page.description}" />
${with lib; <meta name="author" content="${with lib; if isList page.author then join ", " page.author else page.author}" />
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>
''; '';
body = builtins.readFile (commonmark page.name page.body);
});
}); });
}; };

View file

@ -0,0 +1,17 @@
{ lib }:
{
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>
'';
}