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;
test = tix.run [
./testing/md.test.nix
./testing/html.test.nix
./testing/elems.test.nix
#./testing/md.test.nix
#./testing/html.test.nix
#./testing/elems.test.nix
./testing/site.test.nix
./testing/style.test.nix
./testing/builder.test.nix
#./testing/style.test.nix
#./testing/builder.test.nix
];
};
};

View file

@ -13,7 +13,7 @@ in rec {
if builtins.isString elem
then 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 {}}>${
toHTML elem.child or ""
}</${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;
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;});
linkStyle = path: site:
@ -104,6 +92,15 @@ in rec {
switchPaths = runDeep builtins.isPath (value: "/static/" + baseNameOf value);
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 {
when = builtins.isPath;

View file

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

View file

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

View file

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