ark/lib/html.nix

34 lines
909 B
Nix
Raw Normal View History

2021-05-14 23:05:08 +03:00
{ format ? false }:
let
inherit (builtins) isAttrs isList map;
2022-02-18 20:31:01 +03:00
fmt =
if format
then "\n "
else "";
2021-05-14 23:05:08 +03:00
mapAttrsToList = f: attrs: map (name: f name attrs.${name}) (builtins.attrNames attrs);
concatStrings = builtins.concatStringsSep "";
evalAttrs = attrs: concatStrings (mapAttrsToList (name: value: " ${name}=\"${value}\"") attrs);
2022-02-18 20:31:01 +03:00
genAttrs = f: names:
builtins.listToAttrs (map
(n: {
name = n;
value = (f n);
})
names);
evalChildren = children:
if isList children
then concatStrings children
else children;
tag =
name: maybeAttrs:
if isAttrs maybeAttrs
then (children: "<${name}${evalAttrs maybeAttrs}>${fmt}${evalChildren children}${fmt}</${name}>")
else tag name {} maybeAttrs;
tags = (genAttrs tag ["html" "head" "body" "div" "p" "a"]);
2021-05-14 23:05:08 +03:00
in
2022-02-18 20:31:01 +03:00
tags
// {
inherit tag;
link = url: tags.a { href = url; };
}