commit d7671bf1ba7b2430fe0937b33cab2a5772bd0604 Author: Yusuf Bera Ertan Date: Sat May 15 02:30:42 2021 +0300 chore: init diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..08dc7f2 --- /dev/null +++ b/flake.nix @@ -0,0 +1,19 @@ +{ + inputs = { }; + + outputs = { self }: + let + tagsPath = ./tags.nix; + libPath = ./lib.nix; + + lib = import libPath; + in + { + lib = { + inherit tagsPath libPath; + + tags = import tagsPath { format = true; inherit lib; }; + core = lib; + }; + }; +} diff --git a/lib.nix b/lib.nix new file mode 100644 index 0000000..8a90d37 --- /dev/null +++ b/lib.nix @@ -0,0 +1,8 @@ +let + inherit (builtins) isAttrs isList map; +in +{ + 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); +} // builtins diff --git a/tags.nix b/tags.nix new file mode 100644 index 0000000..56dd736 --- /dev/null +++ b/tags.nix @@ -0,0 +1,17 @@ +{ format ? false, lib ? import ./lib.nix }: +with lib; let + fmt = if format then "\n " else ""; + + 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; }; +}