reusable, extendable tags

This commit is contained in:
tristan 2024-01-01 12:42:18 +00:00
parent 8e97ebe2ce
commit e76305f71a
9 changed files with 85 additions and 76 deletions

View file

@ -28,27 +28,29 @@ in rec {
else
keyvalue key value) attrs)));
tag = tag: {
inherit tag;
__toString = self: toString (self { } "");
__functor = self: attrs:
if !(builtins.isAttrs attrs) then
throw "HTML tag requires attribute set"
tag = t: {
tag = t;
attrs = {};
__toString = self: toString (self "");
__functor = self: child:
(if !(isTag child) then
(if isSet child then
self // ({attrs = self.attrs // child;})
else
throw "tag child must be tag, list, or string, got ${
builtins.typeOf child
}"
)
else {
inherit tag attrs;
__toString = self: toString (self "");
__functor = self: child:
if !(isTag child) then
throw "tag child must be tag, list, or string, got ${
builtins.typeOf child
}"
else {
inherit tag attrs child;
__toString = toHTML;
};
};
tag = t;
attrs = self.attrs;
inherit child;
__toString = toHTML;
});
};
isSet = a: builtins.isAttrs a && !a ? __toString;
isTag = tag:
(builtins.isString tag || builtins.isList tag
|| (tag ? __toString && builtins.isFunction tag.__toString));