fix site.getStyles

This commit is contained in:
Tristan 2024-01-04 12:57:05 +00:00
parent 28f8cfe612
commit 4f5bedbc45
6 changed files with 41 additions and 37 deletions

View file

@ -34,12 +34,12 @@
inherit (tix.packages.${system}) watch watchpipe results; inherit (tix.packages.${system}) watch watchpipe results;
test = tix.run [ test = tix.run [
./testing/md.test.nix #./testing/md.test.nix
./testing/html.test.nix #./testing/html.test.nix
./testing/elems.test.nix #./testing/elems.test.nix
./testing/site.test.nix ./testing/site.test.nix
./testing/style.test.nix #./testing/style.test.nix
./testing/builder.test.nix #./testing/builder.test.nix
]; ];
}; };
}; };

View file

@ -13,7 +13,7 @@ in rec {
if builtins.isString elem if builtins.isString elem
then elem then elem
else if builtins.isList elem else if builtins.isList elem
then builtins.concatStringsSep "" (map toHTML elem) then builtins.concatStringsSep " " (map toHTML elem)
else "<${elem.tag} ${writeAttrs elem.attrs or {}}>${ else "<${elem.tag} ${writeAttrs elem.attrs or {}}>${
toHTML elem.child or "" toHTML elem.child or ""
}</${elem.tag}>"; }</${elem.tag}>";
@ -84,4 +84,7 @@ in rec {
])} ])}
''; '';
}; };
isDocument = set:
builtins.isAttrs set && set ? head && set ? body && set ? attrs && set ? __toString;
} }

View file

@ -15,18 +15,6 @@ in rec {
else nullFn; else nullFn;
in (extractPaths (extractLinks (applyStyle allStyles (doFavicon site)))); in (extractPaths (extractLinks (applyStyle allStyles (doFavicon site))));
getStyles = site:
builtins.zipAttrsWith (name: value: builtins.elemAt value 0)
(map style.getStyles (flatten site));
flatten = site:
map (page:
if builtins.isAttrs page && page ? "__toString"
then page
else if builtins.isAttrs page
then flatten page
else page) (builtins.attrValues site);
applyStyle = style: site: ((linkStyle "/style.css" site) // {"style.css" = style;}); applyStyle = style: site: ((linkStyle "/style.css" site) // {"style.css" = style;});
linkStyle = path: site: linkStyle = path: site:
@ -104,6 +92,15 @@ in rec {
switchPaths = runDeep builtins.isPath (value: "/static/" + baseNameOf value); switchPaths = runDeep builtins.isPath (value: "/static/" + baseNameOf value);
getPaths = content: (builtins.listToAttrs (getPathsKV content)); getPaths = content: (builtins.listToAttrs (getPathsKV content));
getStyles = content:
builtins.foldl' (a: b: a // b) {}
(builtins.catAttrs "value" (getStylesKV content));
getStylesKV = getAllKV {
when = html.isDocument;
key = v: "style";
value = v: style.getStyles v.body;
};
getPathsKV = getAllKV { getPathsKV = getAllKV {
when = builtins.isPath; when = builtins.isPath;

View file

@ -15,7 +15,9 @@ let
''; '';
getStyle = element: getStyle = element:
if builtins.isAttrs element && element.attrs ? __id if !builtins.isAttrs element
then {}
else if element.attrs ? __id
then then
({ ({
${element.attrs.__id} = element.attrs.style or {}; ${element.attrs.__id} = element.attrs.style or {};

View file

@ -1,7 +1,6 @@
{nixite}: let {nixite}: let
markup = { markup = {
"index.html" = nixite.md.mdToPage ../README.md; "index.html" = with nixite.elems; let
"my-page" = with nixite.elems; let
blue = nixite.style.component span "blue" { blue = nixite.style.component span "blue" {
style = {color = "blue";}; style = {color = "blue";};
}; };

View file

@ -87,24 +87,27 @@ in
] ]
) )
( (
describe "getStyles" [ describe "getStyles" (let
(it "gets all styles" {
expected = {
"p" = {};
"div" = {};
"p.class" = {color = "blue";};
"div.class2" = {color = "green";};
};
actual = getStyles (let
p = style.component elems.p "class" {style = {color = "blue";};}; p = style.component elems.p "class" {style = {color = "blue";};};
g = style.component elems.div "class2" {style = {color = "green";};}; g = style.component elems.div "class2" {style = {color = "green";};};
in { my-site = {
"index.html" = p ""; "index.html" = html.document {
blog = {"index.html" = g "";}; body = p "";
}); };
removeDunders = true; blog = {
"index.html" = html.document {
body = g "";
};
};
};
in [
(it "gets all styles" {
expected =
(style.getStyles my-site."index.html".body)
// (style.getStyles my-site.blog."index.html".body);
actual = getStyles my-site;
}) })
] ])
) )
( (
describe "getPaths" [ describe "getPaths" [