diff --git a/flake.nix b/flake.nix index e428ab3..c9aec85 100644 --- a/flake.nix +++ b/flake.nix @@ -9,8 +9,8 @@ in nixite // { packages.${system} = { raw = nixite.mkSite (let - readme = _: { - type = "page"; + readme = { + type = "link"; name = "readme"; content = nixite.md.mdToPage ./README.md; }; @@ -26,7 +26,7 @@ in (Doc { } [ [ (title { } "Nixite") ] (main { } [ - (a "/readme" "Readme") + (a { href = readme; } "Readme") (a "/blog" "blog") (List { } [ "item 1" "item 2" "item 3" ]) (p { } [ @@ -38,11 +38,9 @@ ]) ]); blog = nixite.md.readDir ./testing/blog; - "index.html" = (readme { }).content; }; - styles = nixite.site.getStyles markup; - site = (nixite.site.extractPaths (nixite.site.applyStyle styles - (nixite.site.applyFavicon ./testing/src/favicon.png markup))); + site = (nixite.site.prepare { favicon = ./testing/src/favicon.png; } + markup); in site); default = nixite.serve self.packages.${system}.raw; diff --git a/nixite/site.nix b/nixite/site.nix index 62e5a26..72cfe1e 100644 --- a/nixite/site.nix +++ b/nixite/site.nix @@ -3,6 +3,14 @@ let elems = import ./elems.nix; style = import ./style.nix; 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: builtins.zipAttrsWith (name: value: builtins.elemAt value 0) (map style.getStyles (flatten site)); @@ -16,7 +24,7 @@ in rec { else page) (builtins.attrValues site); - applyStyle = style: site: (linkStyle site) // { "style.css" = style; }; + applyStyle = style: site: ((linkStyle site) // { "style.css" = style; }); linkStyle = site: eachPage site @@ -40,21 +48,29 @@ in rec { }) ]); - extractPaths = content: switchPaths content // { static = getPaths content; }; - extractFunctions = content: - switchFunctions content // { - static = getFunctions content; + extractPaths = content: + switchPaths content // + { + static = ( content.static or { } ) // getPaths content; }; - switchFunctions = - runDeep builtins.isFunction (value: "/static/" + (value { }).name); + extractLinks = content: + 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 { - when = f: (runLink f) != null; - key = f: (runLink f).name or null; - value = f: (runLink f).content or null; + switchLinks = runDeep (isLink) (value: "/static/" + value.name); + + getLinks = content: (builtins.listToAttrs (getLinksKV content)); + + getLinksKV = getAllKV { + when = isLink; + key = l: l.name or null; + value = l: l.content or null; }; runLink = f: diff --git a/testing/it.nix b/testing/it.nix index c733c8f..4324a02 100644 --- a/testing/it.nix +++ b/testing/it.nix @@ -1,5 +1,6 @@ msg: -{ actual, expected, asString ? false, asJSON ? false, removeDunders ? false, safeToPrint ? true }: +{ actual, expected, asString ? false, asJSON ? false, removeDunders ? false +, safeToPrint ? true }: let preProcess = v: if removeDunders then @@ -17,9 +18,8 @@ let in if (a == e) then '' echo 'it ${msg}' '' else -builtins.trace actual -builtins.trace expected -'' - echo FAILED ${msg} - echo '${if safeToPrint then builtins.toJSON actual else ""}' -'' + builtins.trace "FAILED ${msg}" builtins.trace + (if safeToPrint then builtins.toJSON actual else actual) builtins.trace + (if safeToPrint then builtins.toJSON expected else expected) '' + echo FAILED ${msg} + '' diff --git a/testing/site.test.nix b/testing/site.test.nix index b5d8c16..fdc38d8 100644 --- a/testing/site.test.nix +++ b/testing/site.test.nix @@ -147,14 +147,14 @@ in with site; [ }; }) - (it "switches functions" (let - coolPage = _: { - type = "page"; + (it "switches links" (let + coolPage = { + type = "link"; name = "cool-page"; content = ""; }; in { - actual = switchFunctions { + actual = switchLinks { something = ""; a-thing = { src = coolPage; }; a-list = [{ thingy = coolPage; }]; @@ -166,19 +166,19 @@ in with site; [ }; })) - (it "gets functions" (let - coolPage = _: { - type = "page"; + (it "gets links" (let + coolPage = { + type = "link"; name = "cool-page"; content = "cool content"; }; - otherPage = _: { - type = "page"; + otherPage = { + type = "link"; name = "page2"; content = "stuff"; }; in { - actual = getFunctions { + actual = getLinks { something = "yes"; a-list = [ { thingy = coolPage; } [ (elems.img { src = otherPage; } "") ] ]; @@ -189,20 +189,20 @@ in with site; [ }; })) - (it "extracts functions" (let - coolPage = _: { - type = "page"; + (it "extracts links" (let + coolPage = { + type = "link"; name = "cool-page"; content = "cool content"; }; - otherPage = _: { - type = "page"; + otherPage = { + type = "link"; name = "page2"; content = "stuff"; }; in { - actual = extractFunctions { + actual = extractLinks { something = ""; a-thing = { src = coolPage; }; 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; + })) + ]