From aa0a25956c4a50bfadc3285a55f20e8910d7450e Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Fri, 14 May 2021 23:05:08 +0300 Subject: [PATCH] feat: html lib --- lib/default.nix | 1 + lib/html.nix | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 lib/html.nix 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; }; +}