render content via structured DOM representation

This commit is contained in:
Valentin Gagarin 2024-11-13 15:24:41 +01:00 committed by Valentin Gagarin
parent e531e861ce
commit 5bef87be1f
4 changed files with 42 additions and 37 deletions

View file

@ -6,6 +6,7 @@ in
menus.main = { menus.main = {
label = "Main"; label = "Main";
items = [ items = [
{ page = pages.index // { title = "Start"; }; }
{ {
menu.label = "For you"; menu.label = "For you";
menu.items = map (page: { inherit page; }) menu.items = map (page: { inherit page; })

View file

@ -5,6 +5,14 @@ let
types types
; ;
templates = import ./templates.nix { inherit lib; }; templates = import ./templates.nix { inherit lib; };
render-html = document:
let
eval = lib.evalModules {
class = "DOM";
modules = [ document (import ./dom.nix { inherit lib; }).document ];
};
in
toString eval.config;
in in
{ {
options.templates = options.templates =
@ -38,30 +46,38 @@ in
in in
{ {
nav = lib.mkDefault templates.nav; nav = lib.mkDefault templates.nav;
page = lib.mkDefault (config: page: templates.html { page = lib.mkDefault (config: page: render-html {
head = '' html = {
<title>${page.title}</title> head = {
<meta name="description" content="${page.description}" /> title.text = page.title;
<link rel="canonical" href="${lib.head page.locations}" /> meta.description = page.description;
''; link.canonical = lib.head page.locations;
body = '' };
${config.menus.main.outputs.html page} body.content = ''
${builtins.readFile (commonmark page.name page.body)} ${config.menus.main.outputs.html page}
'';
<h1>${page.title}</h1>
${builtins.readFile (commonmark page.name page.body)}
'';
};
}); });
article = lib.mkDefault (config: page: templates.html { article = lib.mkDefault (config: page: render-html {
head = '' html = {
<title>${page.title}</title> head = {
<meta name="description" content="${page.description}" /> title.text = page.title;
${with lib; join "\n" (map meta.description = page.description;
(author: ''<meta name="author" content="${author}" />'') meta.authors = if lib.isList page.author then page.author else [ page.author ];
(if isList page.author then page.author else [page.author])) link.canonical = lib.head page.locations;
} };
''; body.content = ''
body = '' ${config.menus.main.outputs.html page}
${config.menus.main.outputs.html page}
${builtins.readFile (commonmark page.name page.body)} <h1>${page.title}</h1>
'';
${builtins.readFile (commonmark page.name page.body)}
'';
};
}); });
}; };

View file

@ -1,19 +1,5 @@
{ lib }: { lib }:
rec { 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: page: nav = menu: page:
let let
render-item = item: render-item = item:

View file

@ -46,6 +46,8 @@ in
default = target: with lib; "${relativePath (head config.locations) (head target.locations)}.html"; default = target: with lib; "${relativePath (head config.locations) (head target.locations)}.html";
}; };
outputs = mkOption { outputs = mkOption {
# TODO: figure out how to make this overridable at any granularity.
# it should be possible with the DOM module now.
description = '' description = ''
Representations of the document in different formats Representations of the document in different formats
''; '';