From c36fece77670a711415ee162e8d025015df50d99 Mon Sep 17 00:00:00 2001 From: tristan Date: Sun, 31 Dec 2023 09:15:25 +0000 Subject: [PATCH] extract direct paths --- flake.nix | 8 +++-- nixite/site.nix | 32 +++++++++++++++++++ testing/site.test.nix | 67 ++++++++++++++++++++++++++++++++++++++++ testing/src/favicon.png | Bin 0 -> 1205 bytes 4 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 testing/src/favicon.png diff --git a/flake.nix b/flake.nix index 9f0e471..202cbc8 100644 --- a/flake.nix +++ b/flake.nix @@ -20,14 +20,16 @@ style = nixite.style; packages.${system} = { - default = nixite.mkSite (nixite.site.applyStyle ./testing/src/style.css { + default = nixite.mkSite ( + nixite.site.applyStyle ./testing/src/style.css + (nixite.site.extractPaths { "index.html" = with nixite.elems; (doc [ [ (title "Nixite") ] (main [ - (nixite.md.readMd ./testing/src/index.md) (link "/blog" "blog") + (nixite.html.tag "img" {src = ./testing/src/favicon.png;} "") (list [ "item 1" "item 2" @@ -48,7 +50,7 @@ ]) ]); }; - }); + })); serve = self.serve self.packages.${system}.default; diff --git a/nixite/site.nix b/nixite/site.nix index 3b21173..59f2fc0 100644 --- a/nixite/site.nix +++ b/nixite/site.nix @@ -14,6 +14,38 @@ in rec { ) site); + extractPaths = content: + switchPaths content // {static = getPaths 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) + else content + ); + + getPaths = content: (builtins.listToAttrs (getPathsKV content)); + + getPathsKV = path: + (if builtins.isPath path + then [ + { + name = baseNameOf path; + value = path; + } + ] + else if builtins.isAttrs path + then builtins.concatLists (map getPathsKV ( builtins.attrValues path )) + else if builtins.isList path + then builtins.concatLists (map getPathsKV path) + else []); + copyTo = prefix: site: builtins.toString ( builtins.attrValues ( diff --git a/testing/site.test.nix b/testing/site.test.nix index 1a905d3..f3b9fe4 100644 --- a/testing/site.test.nix +++ b/testing/site.test.nix @@ -37,4 +37,71 @@ in }; }; }) + (it "extracts top level paths" { + actual = getPaths { + something = ""; + src = ./src/index.md; + }; + expected = { + "index.md" = ./src/index.md; + }; + }) + (it "extracts lower level paths" { + actual = getPaths { + something = "yes"; + a-list = [ + {thingy = ./src/index.md;} + [(html.tag "img" {src = ./src/favicon.png;} "")] + ]; + }; + expected = { + "index.md" = ./src/index.md; + "favicon.png" = ./src/favicon.png; + }; + }) + (it "switches paths" { + actual = switchPaths { + something = ""; + a-thing = { + src = ./src/index.md; + }; + a-list = [ + {thingy = ./src/index.md;} + ]; + }; + expected = { + something = ""; + a-thing = { + src = "/static/index.md"; + }; + a-list = [ + {thingy = "/static/index.md";} + ]; + }; + }) + + (it "extracts paths" { + actual = extractPaths { + something = ""; + a-thing = { + src = ./src/index.md; + }; + a-list = [ + {thingy = ./src/index.md;} + ]; + }; + expected = { + something = ""; + a-thing = { + src = "/static/index.md"; + }; + a-list = [ + {thingy = "/static/index.md";} + ]; + static = { + "index.md" = ./src/index.md; + }; + }; + }) + ] diff --git a/testing/src/favicon.png b/testing/src/favicon.png new file mode 100644 index 0000000000000000000000000000000000000000..1ed2b5fe0fdf7a6144adc5cdfa31b5f553df4610 GIT binary patch literal 1205 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!63?wyl`GbL!Lb6AYF9SoB8UsT^3j@P1pisjL z28L1t28LG&3=CE?7#PG0=IjczVPIf#2=EDU1u6(CKb+Wiwr=O+;uW{c*4&A1Jrz{8 z)4ynQ?dJPcYwy%;xnI8WmUr$t-~6qfnQMIVwv;WuQL*Yq-L5CatL}tUABt^18CZ5O zZ{c;IIY350$$p>|3flP?CbiEsc~jS!_w~D@-8-UT(J^iW{g;gG5$d2=oUQB>1m z@0`tUsjG?>Uj}-le#`xk$|De!wc8%H9(?})|Nq7v4}s!!o9``u`G4xw?{%B*m9M-R zQVF#5P*BAYs3W&O0$NnJ^+EmC2f#3iZ95)VdI0Dcpiyv-WY0P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