mirror of
https://github.com/RGBCube/HTMNIX
synced 2025-06-13 19:02:07 +00:00
Add escapix
This commit is contained in:
parent
02c480acc4
commit
682ed00b4f
2 changed files with 61 additions and 1 deletions
57
escape.nix
Normal file
57
escape.nix
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
# Taken from https://github.com/nrabulinski/cursed-nix. Huge thanks!
|
||||||
|
|
||||||
|
let
|
||||||
|
lib = (import <nixpkgs> {}).lib;
|
||||||
|
|
||||||
|
startMarker = "__START__";
|
||||||
|
endMarker = "__END__";
|
||||||
|
|
||||||
|
dropFirst = lib.drop 1;
|
||||||
|
dropLast = list: lib.sublist 0 (lib.length list - 1) list;
|
||||||
|
|
||||||
|
yeet = string: builtins.unsafeDiscardStringContext (builtins.unsafeDiscardOutputDependency string);
|
||||||
|
|
||||||
|
getContent = drvPath: let
|
||||||
|
drv = builtins.readFile drvPath;
|
||||||
|
isValid = builtins.match ".*${lib.escapeRegex startMarker}.*${lib.escapeRegex endMarker}.*" drv != null;
|
||||||
|
content = lib.pipe drv [
|
||||||
|
(lib.splitString startMarker)
|
||||||
|
dropFirst
|
||||||
|
(lib.concatStringsSep startMarker)
|
||||||
|
(lib.splitString endMarker)
|
||||||
|
dropLast
|
||||||
|
(lib.concatStringsSep endMarker)
|
||||||
|
];
|
||||||
|
in if isValid then content else toString (import drvPath);
|
||||||
|
|
||||||
|
escape = string: let
|
||||||
|
ctx = builtins.getContext string;
|
||||||
|
ctxNames = lib.attrNames ctx;
|
||||||
|
|
||||||
|
recurseSubDrv = restNames: strings: let
|
||||||
|
head = lib.head restNames;
|
||||||
|
headDrv = import head;
|
||||||
|
last = (lib.length restNames) == 1;
|
||||||
|
headContent = getContent head;
|
||||||
|
in map (string: let
|
||||||
|
m = lib.splitString (yeet headDrv.outPath) string;
|
||||||
|
__m = if last then map lib.strings.escapeXML else recurseSubDrv (lib.tail restNames);
|
||||||
|
out = __m m;
|
||||||
|
in lib.concatStringsSep headContent out) strings;
|
||||||
|
|
||||||
|
final = recurseSubDrv ctxNames [ string ];
|
||||||
|
final' = assert (lib.length final == 1); lib.head final;
|
||||||
|
in if builtins.hasContext string then
|
||||||
|
yeet final'
|
||||||
|
else
|
||||||
|
lib.strings.escapeXML string;
|
||||||
|
|
||||||
|
raw = string: derivation {
|
||||||
|
name = "_";
|
||||||
|
system = "_";
|
||||||
|
builder = "_";
|
||||||
|
rawContent = "${startMarker}${yeet string}${endMarker}";
|
||||||
|
};
|
||||||
|
in {
|
||||||
|
inherit escape raw;
|
||||||
|
}
|
|
@ -8,7 +8,8 @@
|
||||||
last = n: string: builtins.substring (builtins.stringLength string - n) n string;
|
last = n: string: builtins.substring (builtins.stringLength string - n) n string;
|
||||||
dropLast = n: string: builtins.substring 0 (builtins.stringLength string - n) string;
|
dropLast = n: string: builtins.substring 0 (builtins.stringLength string - n) string;
|
||||||
|
|
||||||
escape = string: string; # TODO
|
escapix = import ./escape.nix;
|
||||||
|
inherit (escapix) escape;
|
||||||
|
|
||||||
attrsetToHtmlAttrs = attrs:
|
attrsetToHtmlAttrs = attrs:
|
||||||
builtins.concatStringsSep " "
|
builtins.concatStringsSep " "
|
||||||
|
@ -24,6 +25,8 @@
|
||||||
|
|
||||||
else "<${name}>";
|
else "<${name}>";
|
||||||
in {
|
in {
|
||||||
|
inherit (escapix) raw;
|
||||||
|
|
||||||
__findFile = _: name: {
|
__findFile = _: name: {
|
||||||
outPath = dottedNameToTag name;
|
outPath = dottedNameToTag name;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue