diff --git a/lib/default.nix b/lib/default.nix index bb93938..ade4d8e 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,4 +1,5 @@ { lib }: lib.makeExtensible (self: { pkgBinNoDep = pkgs: name: "${pkgs.${name}}/bin/${name}"; + html = import ./html.nix { format = true; }; }) diff --git a/lib/html.nix b/lib/html.nix new file mode 100644 index 0000000..72e3100 --- /dev/null +++ b/lib/html.nix @@ -0,0 +1,22 @@ +{ format ? false }: +let + inherit (builtins) isAttrs isList map; + + fmt = if format then "\n " else ""; + mapAttrsToList = f: attrs: map (name: f name attrs.${name}) (builtins.attrNames attrs); + concatStrings = builtins.concatStringsSep ""; + genAttrs = f: names: builtins.listToAttrs (map (n: { name = n; value = (f n); }) names); + + evalAttrs = attrs: concatStrings (mapAttrsToList (name: value: " ${name}=\"${value}\"") attrs); + 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}") + else tag name { } maybeAttrs; + + tags = (genAttrs tag [ "html" "head" "body" "div" "p" "a" ]); +in +tags // { + inherit tag; + link = url: tags.a { href = url; }; +}