From d7671bf1ba7b2430fe0937b33cab2a5772bd0604 Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Sat, 15 May 2021 02:30:42 +0300 Subject: [PATCH] chore: init --- flake.nix | 19 +++++++++++++++++++ lib.nix | 8 ++++++++ tags.nix | 17 +++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 flake.nix create mode 100644 lib.nix create mode 100644 tags.nix 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; }; +}