xml-nix/to-xml.nix

22 lines
726 B
Nix
Raw Normal View History

2024-03-23 11:24:18 -07:00
{ lib, ... }:
2022-06-21 11:56:49 -07:00
with lib;
let
isEmpty = lst: (length lst) == 0;
isEmptyAttrs = attrs: (length (attrNames attrs)) == 0;
renderAttr = name: value: ''${name}="${value}"'';
renderAttrs = attrs:
if (isEmptyAttrs attrs) then
""
else
" " + concatStringsSep " " (mapAttrsToList renderAttr attrs);
renderTag = tag: attrs: body: "<${tag}${renderAttrs attrs}>${body}</${tag}>";
# renderLoneTag = tag: attrs: "<${tag}${renderAttrs attrs} />";
toXML = { tag, attrs ? { }, children ? [ ] }:
let
renderElement = el: if isString el then el else toXML el;
body = concatStringsSep "" (map renderElement children);
in renderTag tag attrs body;
in { inherit toXML toXMLFile toFormattedXML; }