Initial commit
This commit is contained in:
commit
57cad0cd85
|
@ -0,0 +1,13 @@
|
||||||
|
{
|
||||||
|
description = "Nix-to-XML converter.";
|
||||||
|
|
||||||
|
inputs = {
|
||||||
|
nixpkgs.url = "nixpkgs/nixos-22.05";
|
||||||
|
utils.url = "github:numtide/flake-utils";
|
||||||
|
};
|
||||||
|
|
||||||
|
outputs = { self, nixpkgs, utils }: {
|
||||||
|
lib.toXML = import ./to-xml.nix { inherit (nixpkgs) lib; };
|
||||||
|
overlay = final: prev: { lib = prev.lib // { toXML = self.lib.toXML; }; };
|
||||||
|
};
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
{ lib, toXMLFile, ... }:
|
||||||
|
|
||||||
|
content:
|
||||||
|
let xmlFile = toXMLFile "formatted-xml" content;
|
||||||
|
in lib.readFile xmlFile
|
|
@ -0,0 +1,10 @@
|
||||||
|
{ lib, pkgs, toXML, ... }:
|
||||||
|
|
||||||
|
name: content:
|
||||||
|
let preformatXML = pkgs.writeText "${name}-preformat.xml" (toXML content);
|
||||||
|
in pkgs.stdenv.mkDerivation {
|
||||||
|
name = "${name}.xml";
|
||||||
|
phases = [ "installPhase" ];
|
||||||
|
buildInputs = with pkgs; [ xmlformat ];
|
||||||
|
installPhase = "xmlformat ${preformatXML} > $out";
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
{ config, lib, pkgs, ... }:
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
toXMLFile = name: content:
|
||||||
|
let preformatXML = pkgs.writeText "${name}-preformat.xml" (toXML content);
|
||||||
|
in pkgs.stdenv.mkDerivation {
|
||||||
|
name = "${name}.xml";
|
||||||
|
phases = [ "installPhase" ];
|
||||||
|
buildInputs = with pkgs; [ xmlformat ];
|
||||||
|
installPhase = "xmlformat ${preformatXML} > $out";
|
||||||
|
};
|
||||||
|
|
||||||
|
toFormattedXML = content:
|
||||||
|
let xmlFile = toXMLFile "formatted-xml" content;
|
||||||
|
in readFile xmlFile;
|
||||||
|
in { inherit toXML toXMLFile toFormattedXML; }
|
Loading…
Reference in New Issue