convert functions into links

This commit is contained in:
tristan 2024-01-01 09:43:52 +00:00
parent d723140f84
commit b788aa4416
4 changed files with 155 additions and 51 deletions

View file

@ -9,6 +9,11 @@
in nixite // { in nixite // {
packages.${system} = { packages.${system} = {
raw = nixite.mkSite (let raw = nixite.mkSite (let
readme = _: {
type = "page";
name = "readme";
content = nixite.md.mdToPage ./README.md;
};
markup = { markup = {
"test" = with nixite.elems; "test" = with nixite.elems;
let let
@ -33,7 +38,7 @@
]) ])
]); ]);
blog = nixite.md.readDir ./testing/blog; blog = nixite.md.readDir ./testing/blog;
"index.html" = nixite.md.mdToPage ./README.md; "index.html" = (readme { }).content;
}; };
styles = nixite.site.getStyles markup; styles = nixite.site.getStyles markup;
site = (nixite.site.extractPaths (nixite.site.applyStyle styles site = (nixite.site.extractPaths (nixite.site.applyStyle styles

View file

@ -5,16 +5,16 @@ let
in rec { in rec {
getStyles = site: getStyles = site:
builtins.zipAttrsWith (name: value: builtins.elemAt value 0) builtins.zipAttrsWith (name: value: builtins.elemAt value 0)
(map style.getStyles ( flatten site )); (map style.getStyles (flatten site));
flatten = site: map ( flatten = site:
page: if builtins.isAttrs page && page ? "__toString" then map (page:
if builtins.isAttrs page && page ? "__toString" then
page page
else if builtins.isAttrs page then else if builtins.isAttrs page then
flatten page flatten page
else else
page page) (builtins.attrValues site);
) (builtins.attrValues site);
applyStyle = style: site: (linkStyle site) // { "style.css" = style; }; applyStyle = style: site: (linkStyle site) // { "style.css" = style; };
@ -41,29 +41,58 @@ in rec {
]); ]);
extractPaths = content: switchPaths content // { static = getPaths content; }; extractPaths = content: switchPaths content // { static = getPaths content; };
extractFunctions = content:
switchFunctions content // {
static = getFunctions content;
};
switchPaths = content: switchFunctions =
(if builtins.isAttrs content then runDeep builtins.isFunction (value: "/static/" + (value { }).name);
builtins.mapAttrs (key: value:
if builtins.isPath value then getFunctions = content: (builtins.listToAttrs (getFunctionsKV content));
("/static/" + baseNameOf value)
getFunctionsKV = getAllKV {
when = f: (runLink f) != null;
key = f: (runLink f).name or null;
value = f: (runLink f).content or null;
};
runLink = f:
if !builtins.isFunction f then
null
else else
switchPaths value) content (let res = f { }; in (if !builtins.isAttrs res then null else res));
else if builtins.isList content then
(map switchPaths content) runDeep = when: do: set:
(if builtins.isAttrs set then
builtins.mapAttrs
(key: value: if when value then do value else runDeep when do value) set
else if builtins.isList set then
(map (runDeep when do) set)
else else
content); set);
switchPaths = runDeep builtins.isPath (value: "/static/" + baseNameOf value);
getPaths = content: (builtins.listToAttrs (getPathsKV content)); getPaths = content: (builtins.listToAttrs (getPathsKV content));
getPathsKV = path: getPathsKV = getAllKV {
(if builtins.isPath path then [{ when = builtins.isPath;
name = baseNameOf path; key = baseNameOf;
value = path; value = v: v;
};
getAllKV = { when, key, value }@params:
path:
(if when path then [{
name = key path;
value = value path;
}] else if builtins.isAttrs path then }] else if builtins.isAttrs path then
builtins.concatLists (map getPathsKV (builtins.attrValues path)) builtins.concatMap (getAllKV params) (builtins.attrValues
(builtins.mapAttrs
(n: v: if (builtins.substring 0 2 n == "__") then null else v) path))
else if builtins.isList path then else if builtins.isList path then
builtins.concatLists (map getPathsKV path) builtins.concatMap (getAllKV params) path
else else
[ ]); [ ]);

View file

@ -8,18 +8,17 @@ let
toString v toString v
else if asJSON then else if asJSON then
builtins.toJSON v builtins.toJSON v
else v; else
v;
a = preProcess actual; a = preProcess actual;
e = preProcess expected; e = preProcess expected;
in in if (a == e) then ''
if (a == e) then ''
echo 'it ${msg}' echo 'it ${msg}'
'' '' else
else builtins.trace actual
'' builtins.trace expected
echo 'FAILED: ${msg}' ''
echo '${builtins.toJSON expected}' echo FAILED ${msg}
echo '${builtins.toJSON actual}' ''
''

View file

@ -84,31 +84,29 @@ in with site; [
asJSON = true; asJSON = true;
}) })
(it "extracts all styles" { (it "gets all styles" {
expected = { expected = {
"p.class" = {color = "blue";}; "p.class" = { color = "blue"; };
"a.class2" = {color = "green";}; "a.class2" = { color = "green"; };
}; };
actual = getStyles (let actual = getStyles (let
p = style.tag "p" "class" {style = {color = "blue";};}; p = style.tag "p" "class" { style = { color = "blue"; }; };
g = style.tag "a" "class2" {style = {color = "green";};}; g = style.tag "a" "class2" { style = { color = "green"; }; };
in { in {
"index.html" = p ""; "index.html" = p "";
blog = { blog = { "index.html" = g ""; };
"index.html" = g "";
};
}); });
removeDunders = true; removeDunders = true;
}) })
(it "extracts top level paths" { (it "gets top level paths" {
actual = getPaths { actual = getPaths {
something = ""; something = "";
src = ./src/index.md; src = ./src/index.md;
}; };
expected = { "index.md" = ./src/index.md; }; expected = { "index.md" = ./src/index.md; };
}) })
(it "extracts lower level paths" { (it "gets lower level paths" {
actual = getPaths { actual = getPaths {
something = "yes"; something = "yes";
a-list = [ a-list = [
@ -121,6 +119,7 @@ in with site; [
"favicon.png" = ./src/favicon.png; "favicon.png" = ./src/favicon.png;
}; };
}) })
(it "switches paths" { (it "switches paths" {
actual = switchPaths { actual = switchPaths {
something = ""; something = "";
@ -147,4 +146,76 @@ in with site; [
static = { "index.md" = ./src/index.md; }; static = { "index.md" = ./src/index.md; };
}; };
}) })
(it "switches functions" (let
coolPage = _: {
type = "page";
name = "cool-page";
content = "";
};
in {
actual = switchFunctions {
something = "";
a-thing = { src = coolPage; };
a-list = [{ thingy = coolPage; }];
};
expected = {
something = "";
a-thing = { src = "/static/cool-page"; };
a-list = [{ thingy = "/static/cool-page"; }];
};
}))
(it "gets functions" (let
coolPage = _: {
type = "page";
name = "cool-page";
content = "cool content";
};
otherPage = _: {
type = "page";
name = "page2";
content = "stuff";
};
in {
actual = getFunctions {
something = "yes";
a-list =
[ { thingy = coolPage; } [ (elems.img { src = otherPage; } "") ] ];
};
expected = {
"cool-page" = "cool content";
"page2" = "stuff";
};
}))
(it "extracts functions" (let
coolPage = _: {
type = "page";
name = "cool-page";
content = "cool content";
};
otherPage = _: {
type = "page";
name = "page2";
content = "stuff";
};
in {
actual = extractFunctions {
something = "";
a-thing = { src = coolPage; };
a-list = [{ thingy = otherPage; }];
};
expected = {
something = "";
a-thing = { src = "/static/cool-page"; };
a-list = [{ thingy = "/static/page2"; }];
static = {
"cool-page" = "cool content";
"page2" = "stuff";
};
};
}))
] ]