about summary refs log tree commit diff
path: root/users/sterni/nix/html/default.nix
blob: d25a7ab8dac0b3d5d75ab8c6359bcc001ab386f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Copyright © 2021 sterni
# SPDX-License-Identifier: MIT
#
# This file provides a cursed HTML DSL for nix which works by overloading
# the NIX_PATH lookup operation via angle bracket operations, e. g. `<nixpkgs>`.

{ ... }:

let
  /* Escape everything we have to escape in an HTML document if either
     in a normal context or an attribute string (`<>&"'`).

     A shorthand for this function called `esc` is also provided.

     Type: string -> string

     Example:

     escapeMinimal "<hello>"
     => "&lt;hello&gt;"
  */
  escapeMinimal = builtins.replaceStrings
    [ "<" ">" "&" "\"" "'" ]
    [ "&lt;" "&gt;" "&amp;" "&quot;" "&#039;" ];

  /* Return a string with a correctly rendered tag of the given name,
     with the given attributes which are automatically escaped.

     If the content argument is `null`, the tag will have no children nor a
     closing element. If the content argument is a string it is used as the
     content as is (unescaped). If the content argument is a list, its
     elements are concatenated.

     `renderTag` is only an internal function which is reexposed as `__findFile`
     to allow for much neater syntax than calling `renderTag` everywhere:

     ```nix
     { depot, ... }:
     let
       inherit (depot.users.sterni.nix.html) __findFile esc;
     in

     <html> {} [
       (<head> {} (<title> {} (esc "hello world")))
       (<body> {} [
         (<h1> {} (esc "hello world"))
         (<p> {} (esc "foo bar"))
       ])
     ]

     ```

     As you can see, the need to call a function disappears, instead the
     `NIX_PATH` lookup operation via `<foo>` is overloaded, so it becomes
     `renderTag "foo"` automatically.

     Since the content argument may contain the result of other `renderTag`
     calls, we can't escape it automatically. Instead this must be done manually
     using `esc`.

     Type: string -> attrs<string> -> (list<string> | string | null) -> string

     Example:

     <link> {
       rel = "stylesheet";
       href = "/css/main.css";
       type = "text/css";
     } null

     renderTag "link" {
       rel = "stylesheet";
       href = "/css/main.css";
       type = "text/css";
     } null

     => "<link href=\"/css/main.css\" rel=\"stylesheet\" type=\"text/css\"/>"

     <p> {} [
       "foo "
       (<strong> {} "bar")
     ]

     renderTag "p" {} "foo <strong>bar</strong>"
     => "<p>foo <strong>bar</strong></p>"
  */
  renderTag = tag: attrs: content:
    let
      attrs' = builtins.concatStringsSep "" (
        builtins.map
          (n:
            " ${escapeMinimal n}=\"${escapeMinimal (toString attrs.${n})}\""
          )
          (builtins.attrNames attrs)
      );
      content' =
        if builtins.isList content
        then builtins.concatStringsSep "" content
        else content;
    in
    if content == null
    then "<${tag}${attrs'}/>"
    else "<${tag}${attrs'}>${content'}</${tag}>";

  /* Prepend "<!DOCTYPE html>" to a string.

     Type: string -> string

     Example:

     withDoctype (<body> {} (esc "hello"))
     => "<!DOCTYPE html><body>hello</body>"
  */
  withDoctype = doc: "<!DOCTYPE html>" + doc;

in
{
  inherit escapeMinimal renderTag withDoctype;

  __findFile = _: renderTag;
  esc = escapeMinimal;
}