diff --git a/flake.nix b/flake.nix index 269b8ad..e428ab3 100644 --- a/flake.nix +++ b/flake.nix @@ -9,6 +9,11 @@ in nixite // { packages.${system} = { raw = nixite.mkSite (let + readme = _: { + type = "page"; + name = "readme"; + content = nixite.md.mdToPage ./README.md; + }; markup = { "test" = with nixite.elems; let @@ -33,7 +38,7 @@ ]) ]); blog = nixite.md.readDir ./testing/blog; - "index.html" = nixite.md.mdToPage ./README.md; + "index.html" = (readme { }).content; }; styles = nixite.site.getStyles markup; site = (nixite.site.extractPaths (nixite.site.applyStyle styles diff --git a/nixite/site.nix b/nixite/site.nix index 4d137f9..62e5a26 100644 --- a/nixite/site.nix +++ b/nixite/site.nix @@ -5,16 +5,16 @@ let in rec { getStyles = site: builtins.zipAttrsWith (name: value: builtins.elemAt value 0) - (map style.getStyles ( flatten site )); + (map style.getStyles (flatten site)); - flatten = site: map ( - page: if builtins.isAttrs page && page ? "__toString" then + 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); + page) (builtins.attrValues site); applyStyle = style: site: (linkStyle site) // { "style.css" = style; }; @@ -41,29 +41,58 @@ in rec { ]); extractPaths = content: switchPaths content // { static = getPaths content; }; + extractFunctions = content: + switchFunctions content // { + static = getFunctions content; + }; - switchPaths = content: - (if builtins.isAttrs content then - builtins.mapAttrs (key: value: - if builtins.isPath value then - ("/static/" + baseNameOf value) - else - switchPaths value) content - else if builtins.isList content then - (map switchPaths content) + switchFunctions = + runDeep builtins.isFunction (value: "/static/" + (value { }).name); + + getFunctions = content: (builtins.listToAttrs (getFunctionsKV content)); + + 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 - content); + (let res = f { }; in (if !builtins.isAttrs res then null else res)); + + 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 + set); + + switchPaths = runDeep builtins.isPath (value: "/static/" + baseNameOf value); getPaths = content: (builtins.listToAttrs (getPathsKV content)); - getPathsKV = path: - (if builtins.isPath path then [{ - name = baseNameOf path; - value = path; + getPathsKV = getAllKV { + when = builtins.isPath; + key = baseNameOf; + 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 - 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 - builtins.concatLists (map getPathsKV path) + builtins.concatMap (getAllKV params) path else [ ]); diff --git a/testing/it.nix b/testing/it.nix index 7936006..194b97b 100644 --- a/testing/it.nix +++ b/testing/it.nix @@ -1,25 +1,24 @@ msg: { actual, expected, asString ? false, asJSON ? false, removeDunders ? false, }: let - preProcess = v: - if removeDunders then - builtins.removeAttrs v [ "__toString" "__functor" ] - else if asString then - toString v - else if asJSON then - builtins.toJSON v - else v; - + preProcess = v: + if removeDunders then + builtins.removeAttrs v [ "__toString" "__functor" ] + else if asString then + toString v + else if asJSON then + builtins.toJSON v + else + v; + a = preProcess actual; e = preProcess expected; -in -if (a == e) then '' - echo 'it ${msg}' - '' -else - '' - echo 'FAILED: ${msg}' - echo '${builtins.toJSON expected}' - echo '${builtins.toJSON actual}' - '' +in if (a == e) then '' + echo 'it ${msg}' +'' else +builtins.trace actual +builtins.trace expected +'' + echo FAILED ${msg} +'' diff --git a/testing/site.test.nix b/testing/site.test.nix index 0839c37..734b7d5 100644 --- a/testing/site.test.nix +++ b/testing/site.test.nix @@ -84,31 +84,29 @@ in with site; [ asJSON = true; }) - (it "extracts all styles" { + (it "gets all styles" { expected = { - "p.class" = {color = "blue";}; - "a.class2" = {color = "green";}; + "p.class" = { color = "blue"; }; + "a.class2" = { color = "green"; }; }; actual = getStyles (let - p = style.tag "p" "class" {style = {color = "blue";};}; - g = style.tag "a" "class2" {style = {color = "green";};}; + p = style.tag "p" "class" { style = { color = "blue"; }; }; + g = style.tag "a" "class2" { style = { color = "green"; }; }; in { "index.html" = p ""; - blog = { - "index.html" = g ""; - }; + blog = { "index.html" = g ""; }; }); removeDunders = true; }) - (it "extracts top level paths" { + (it "gets top level paths" { actual = getPaths { something = ""; src = ./src/index.md; }; expected = { "index.md" = ./src/index.md; }; }) - (it "extracts lower level paths" { + (it "gets lower level paths" { actual = getPaths { something = "yes"; a-list = [ @@ -121,6 +119,7 @@ in with site; [ "favicon.png" = ./src/favicon.png; }; }) + (it "switches paths" { actual = switchPaths { something = ""; @@ -147,4 +146,76 @@ in with site; [ 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"; + }; + }; + })) + ]