From e98176b28cbf6f28cae9816f8f115fe3f4d634a7 Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Sat, 15 May 2021 19:42:16 +0300 Subject: [PATCH] feat: get (very) basic ssg ready --- examples/site/config.toml | 1 + examples/site/posts/Testing more.md | 3 +++ examples/site/posts/test.md | 8 ++++++++ flake.nix | 22 ++++++++++++++------- pkgs-lib.nix | 30 ++++++++++++++++++++++++++++- tags.nix | 11 +++++------ templaters/basic.nix | 21 ++++++++++++++++++++ utils.nix | 8 +++++++- 8 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 examples/site/config.toml create mode 100644 examples/site/posts/Testing more.md create mode 100644 examples/site/posts/test.md create mode 100644 templaters/basic.nix diff --git a/examples/site/config.toml b/examples/site/config.toml new file mode 100644 index 0000000..1481ae6 --- /dev/null +++ b/examples/site/config.toml @@ -0,0 +1 @@ +title = "test site" \ No newline at end of file diff --git a/examples/site/posts/Testing more.md b/examples/site/posts/Testing more.md new file mode 100644 index 0000000..2554e2a --- /dev/null +++ b/examples/site/posts/Testing more.md @@ -0,0 +1,3 @@ +#### another document + +Woo hoo! \ No newline at end of file diff --git a/examples/site/posts/test.md b/examples/site/posts/test.md new file mode 100644 index 0000000..99c431c --- /dev/null +++ b/examples/site/posts/test.md @@ -0,0 +1,8 @@ +Welcome! + +### This is a test document + +``` +echo "some code!" +echo "more!" +``` \ No newline at end of file diff --git a/flake.nix b/flake.nix index bdaabc4..b72465f 100644 --- a/flake.nix +++ b/flake.nix @@ -3,23 +3,31 @@ let utils = import ./utils.nix; - tags = import ./tags.nix { format = false; inherit utils; }; + lib = { + tags = import ./tags.nix { inherit utils; }; + + templaters = { + basic = import ./templaters/basic.nix; + }; + }; + pkgsLib = (final: prev: { - htmlNix = import ./pkgs-lib.nix { pkgs = prev; inherit utils; }; + htmlNix = import ./pkgs-lib.nix { pkgs = prev; utils = utils // { inherit (lib) tags; }; }; }); in { - lib = { - inherit tags; - }; + inherit lib; overlays = { inherit pkgsLib; }; examples = { - tags = import ./examples/tags.nix tags; - serve = import ./examples/serve.nix { inherit tags pkgsLib; }; # needs --impure + siteServe = + let inherit (import { overlays = [ pkgsLib ]; }) htmlNix; in + htmlNix.mkServeFromSite (htmlNix.mkSiteFrom { src = ./examples/site; templater = lib.templaters.basic; }); # needs --impure + tags = import ./examples/tags.nix lib.tags; + serve = import ./examples/serve.nix { inherit (lib) tags; inherit pkgsLib; }; # needs --impure }; }; } diff --git a/pkgs-lib.nix b/pkgs-lib.nix index 00963d7..ab093b3 100644 --- a/pkgs-lib.nix +++ b/pkgs-lib.nix @@ -21,9 +21,37 @@ let mkdir -p $out ${concatStringsSep "\n" createFileCmds} ''; + + parseMarkdown = name: contents: + pkgs.runCommand name { } '' + printf "${contents}" | ${pkgBin "lowdown"} -o $out - + ''; in { - inherit mkServePathScript mkSitePath; + inherit mkServePathScript mkSitePath parseMarkdown; mkServeFromSite = site: mkServePathScript (mkSitePath site); + mkSiteFrom = { src, templater }: + let + inherit (utils) readDir readFile fromTOML; + inherit (pkgs.lib) mapAttrs' nameValuePair head splitString; + + postsRendered = + let path = src + "/posts"; in + mapAttrs' + (name: _: + nameValuePair + (head (splitString "." name)) + (parseMarkdown name (readFile (path + "/${name}"))) + ) + (readDir path); + siteConfig = fromTOML (readFile (src + "/config.toml")); + + context = { + inherit utils pkgs; + config = siteConfig; + posts = postsRendered; + }; + in + templater context; } diff --git a/tags.nix b/tags.nix index c086a2c..4d90f2e 100644 --- a/tags.nix +++ b/tags.nix @@ -1,17 +1,16 @@ -{ format ? false, utils }: +{ utils }: let - inherit (utils) concatStrings mapAttrsToList genAttrs isAttrs isList; - - fmt = if format then "\n " else ""; + inherit (utils) concatStrings mapAttrsToList genAttrs isAttrs isList range toString; evalAttrs = attrs: concatStrings (mapAttrsToList (name: value: " ${name}=\"${value}\"") attrs); evalChildren = children: if isList children then concatStrings children else children; tag = name: maybeAttrs: if isAttrs maybeAttrs - then (children: "<${name}${evalAttrs maybeAttrs}>${fmt}${evalChildren children}${fmt}") + then (children: "<${name}${evalAttrs maybeAttrs}>\n ${evalChildren children}\n\n") else tag name { } maybeAttrs; - tags = (genAttrs tag [ "html" "head" "body" "div" "p" "a" ]); + tagsToGen = [ "html" "head" "body" "div" "p" "a" "title" "meta" "code" "pre" ] ++ (map (n: "h${toString n}") (range 1 6)); + tags = genAttrs tag tagsToGen; in tags // { inherit tag; diff --git a/templaters/basic.nix b/templaters/basic.nix new file mode 100644 index 0000000..093d5f6 --- /dev/null +++ b/templaters/basic.nix @@ -0,0 +1,21 @@ +{ utils, posts, pkgs, config, ... }@context: +let + inherit (utils) readFile mapAttrsToList; + inherit (pkgs.lib) flatten; + + renderPost = name: value: with utils.tags; [ + (h2 ("# " + name)) + (readFile value) + ]; + + allPosts = flatten (mapAttrsToList renderPost posts); +in +{ + "index.html" = with utils.tags; + html [ + (head [ + (title config.title) + ]) + (body allPosts) + ]; +} diff --git a/utils.nix b/utils.nix index 1d39851..1a95d2a 100644 --- a/utils.nix +++ b/utils.nix @@ -1,5 +1,5 @@ let - inherit (builtins) isAttrs isList map any concatMap concatStringsSep listToAttrs; + inherit (builtins) isAttrs isList map any concatMap concatStringsSep listToAttrs genList; mapAttrsToList = f: attrs: map (name: f name attrs.${name}) (builtins.attrNames attrs); in @@ -27,4 +27,10 @@ in concatStrings = concatStringsSep ""; genAttrs = f: names: listToAttrs (map (n: { name = n; value = (f n); }) names); + + range = first: last: + if first > last then + [ ] + else + genList (n: first + n) (last - first + 1); } // builtins