feat: html lib

This commit is contained in:
dusk 2021-05-14 23:05:08 +03:00
parent 1614ff6866
commit aa0a25956c
Signed by: dusk
GPG Key ID: 1D8F8FAF2294D6EA
2 changed files with 23 additions and 0 deletions

View File

@ -1,4 +1,5 @@
{ lib }: { lib }:
lib.makeExtensible (self: { lib.makeExtensible (self: {
pkgBinNoDep = pkgs: name: "${pkgs.${name}}/bin/${name}"; pkgBinNoDep = pkgs: name: "${pkgs.${name}}/bin/${name}";
html = import ./html.nix { format = true; };
}) })

22
lib/html.nix Normal file
View File

@ -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}</${name}>")
else tag name { } maybeAttrs;
tags = (genAttrs tag [ "html" "head" "body" "div" "p" "a" ]);
in
tags // {
inherit tag;
link = url: tags.a { href = url; };
}