links work

This commit is contained in:
tristan 2024-01-01 11:12:37 +00:00
parent 9fc7817e0e
commit 8e97ebe2ce
4 changed files with 111 additions and 42 deletions

View file

@ -9,8 +9,8 @@
in nixite // { in nixite // {
packages.${system} = { packages.${system} = {
raw = nixite.mkSite (let raw = nixite.mkSite (let
readme = _: { readme = {
type = "page"; type = "link";
name = "readme"; name = "readme";
content = nixite.md.mdToPage ./README.md; content = nixite.md.mdToPage ./README.md;
}; };
@ -26,7 +26,7 @@
in (Doc { } [ in (Doc { } [
[ (title { } "Nixite") ] [ (title { } "Nixite") ]
(main { } [ (main { } [
(a "/readme" "Readme") (a { href = readme; } "Readme")
(a "/blog" "blog") (a "/blog" "blog")
(List { } [ "item 1" "item 2" "item 3" ]) (List { } [ "item 1" "item 2" "item 3" ])
(p { } [ (p { } [
@ -38,11 +38,9 @@
]) ])
]); ]);
blog = nixite.md.readDir ./testing/blog; blog = nixite.md.readDir ./testing/blog;
"index.html" = (readme { }).content;
}; };
styles = nixite.site.getStyles markup; site = (nixite.site.prepare { favicon = ./testing/src/favicon.png; }
site = (nixite.site.extractPaths (nixite.site.applyStyle styles markup);
(nixite.site.applyFavicon ./testing/src/favicon.png markup)));
in site); in site);
default = nixite.serve self.packages.${system}.raw; default = nixite.serve self.packages.${system}.raw;

View file

@ -3,6 +3,14 @@ let
elems = import ./elems.nix; elems = import ./elems.nix;
style = import ./style.nix; style = import ./style.nix;
in rec { in rec {
prepare = { favicon ? null, styles ? { }, }:
site:
let
nullFn = p: p;
allStyles = styles // getStyles site;
doFavicon = if favicon != null then (applyFavicon favicon) else nullFn;
in (extractPaths (extractLinks (applyStyle allStyles (doFavicon site))));
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));
@ -16,7 +24,7 @@ in rec {
else else
page) (builtins.attrValues site); page) (builtins.attrValues site);
applyStyle = style: site: (linkStyle site) // { "style.css" = style; }; applyStyle = style: site: ((linkStyle site) // { "style.css" = style; });
linkStyle = site: linkStyle = site:
eachPage site eachPage site
@ -40,21 +48,29 @@ in rec {
}) })
]); ]);
extractPaths = content: switchPaths content // { static = getPaths content; }; extractPaths = content:
extractFunctions = content: switchPaths content //
switchFunctions content // { {
static = getFunctions content; static = ( content.static or { } ) // getPaths content;
}; };
switchFunctions = extractLinks = content:
runDeep builtins.isFunction (value: "/static/" + (value { }).name); switchLinks content //
{
static = ( content.static or { } ) // getLinks content;
};
getFunctions = content: (builtins.listToAttrs (getFunctionsKV content)); isLink = value:
builtins.isAttrs value && value ? type && value.type == "link";
getFunctionsKV = getAllKV { switchLinks = runDeep (isLink) (value: "/static/" + value.name);
when = f: (runLink f) != null;
key = f: (runLink f).name or null; getLinks = content: (builtins.listToAttrs (getLinksKV content));
value = f: (runLink f).content or null;
getLinksKV = getAllKV {
when = isLink;
key = l: l.name or null;
value = l: l.content or null;
}; };
runLink = f: runLink = f:

View file

@ -1,5 +1,6 @@
msg: msg:
{ actual, expected, asString ? false, asJSON ? false, removeDunders ? false, safeToPrint ? true }: { actual, expected, asString ? false, asJSON ? false, removeDunders ? false
, safeToPrint ? true }:
let let
preProcess = v: preProcess = v:
if removeDunders then if removeDunders then
@ -17,9 +18,8 @@ let
in if (a == e) then '' in if (a == e) then ''
echo 'it ${msg}' echo 'it ${msg}'
'' else '' else
builtins.trace actual builtins.trace "FAILED ${msg}" builtins.trace
builtins.trace expected (if safeToPrint then builtins.toJSON actual else actual) builtins.trace
'' (if safeToPrint then builtins.toJSON expected else expected) ''
echo FAILED ${msg} echo FAILED ${msg}
echo '${if safeToPrint then builtins.toJSON actual else ""}' ''
''

View file

@ -147,14 +147,14 @@ in with site; [
}; };
}) })
(it "switches functions" (let (it "switches links" (let
coolPage = _: { coolPage = {
type = "page"; type = "link";
name = "cool-page"; name = "cool-page";
content = ""; content = "";
}; };
in { in {
actual = switchFunctions { actual = switchLinks {
something = ""; something = "";
a-thing = { src = coolPage; }; a-thing = { src = coolPage; };
a-list = [{ thingy = coolPage; }]; a-list = [{ thingy = coolPage; }];
@ -166,19 +166,19 @@ in with site; [
}; };
})) }))
(it "gets functions" (let (it "gets links" (let
coolPage = _: { coolPage = {
type = "page"; type = "link";
name = "cool-page"; name = "cool-page";
content = "cool content"; content = "cool content";
}; };
otherPage = _: { otherPage = {
type = "page"; type = "link";
name = "page2"; name = "page2";
content = "stuff"; content = "stuff";
}; };
in { in {
actual = getFunctions { actual = getLinks {
something = "yes"; something = "yes";
a-list = a-list =
[ { thingy = coolPage; } [ (elems.img { src = otherPage; } "") ] ]; [ { thingy = coolPage; } [ (elems.img { src = otherPage; } "") ] ];
@ -189,20 +189,20 @@ in with site; [
}; };
})) }))
(it "extracts functions" (let (it "extracts links" (let
coolPage = _: { coolPage = {
type = "page"; type = "link";
name = "cool-page"; name = "cool-page";
content = "cool content"; content = "cool content";
}; };
otherPage = _: { otherPage = {
type = "page"; type = "link";
name = "page2"; name = "page2";
content = "stuff"; content = "stuff";
}; };
in { in {
actual = extractFunctions { actual = extractLinks {
something = ""; something = "";
a-thing = { src = coolPage; }; a-thing = { src = coolPage; };
a-list = [{ thingy = otherPage; }]; a-list = [{ thingy = otherPage; }];
@ -235,4 +235,59 @@ in with site; [
''; '';
})) }))
(it "prepares the site" ({
actual = prepare { favicon = ./src/favicon.png; } {
"index.html" = elems.html { } [
(elems.head { } [ (elems.title { } "foobar") ])
(elems.main { } [
(elems.a {
href = {
type = "link";
name = "a-page";
content = "this is another page";
};
} "A Page")
])
];
blog = {
"index.html" = elems.html { } [
(elems.head { } [ (elems.title { } "foobar") ])
(elems.main { } "something")
];
};
};
expected = {
"index.html" = elems.html { } [
(elems.head { } [
(elems.title { } "foobar")
(elems.link {
rel = "shortcut icon";
href = "/static/favicon.png";
})
(elems.Stylesheet "/style.css")
])
(elems.main { } [ (elems.a { href = "/static/a-page"; } "A Page") ])
];
blog = {
"index.html" = elems.html { } [
(elems.head { } [
(elems.title { } "foobar")
(elems.link {
rel = "shortcut icon";
href = "/static/favicon.png";
})
(elems.Stylesheet "/style.css")
])
(elems.main { } "something")
];
};
static = {
"favicon.png" = ./src/favicon.png;
"a-page" = "this is another page";
};
"style.css" = "";
};
asJSON = true;
}))
] ]