From 67603cd2794de92f5059c9c26e81dd4cff566fdc Mon Sep 17 00:00:00 2001 From: poacher Date: Wed, 8 Apr 2026 09:13:10 -0400 Subject: [PATCH] proper templating system --- README.md | 82 ++++++++++++------- default.nix | 84 +++++++++++--------- lib/{recursiveImport.nix => importPosts.nix} | 4 +- lib/importTemplates.nix | 42 ++++++++++ lib/mkIndex.nix | 5 +- lib/mkPost.nix | 4 +- 6 files changed, 149 insertions(+), 72 deletions(-) rename lib/{recursiveImport.nix => importPosts.nix} (94%) create mode 100644 lib/importTemplates.nix diff --git a/README.md b/README.md index e9dca5f..7a77cad 100644 --- a/README.md +++ b/README.md @@ -7,43 +7,67 @@ - Fast, cached compilation # Usage +## Using mkSite +`mkSite` provides the core functionality of `spoonful`, creating and combining +the post, index, and RSS derivations. It expects the following arguments: +1. `metadata ? {}` an attribute set with your website's title, description, and link. +2. `posts ? []` a list of posts to be generated. +3. `generateRSS ? true` if an RSS feed should be generated. +4. `static ? null` an optional folder to be included in your website as-is. +5. `lib` just `spoonful.lib`, you can inherit this or use it to override functions +6. `templates ? {}` the HTML templates used to generate posts. + +### Example +```nix +spoonful.mkSite (lib: { + inherit lib; + static = ./static; + posts = builtins.sort lib.sorting.byDate (lib.recursiveImport ./content); + templates = lib.recursiveImportAttrs ./templates; + metadata = { + title = "My Blog"; + description = "A blog."; + link = "http://127.0.0.1:9999" + }; +}; +``` + ## Creating a post -Posts are Nix attribute sets with the following parameters: +Posts are Nix functions which take all the arguments passed to `mksite` and +return an attribute set with the following parameters: 1. `title ? "Title"` the post's title. 2. `description ? "Description"` a description of the post. 3. `content ? ""` the markdown of the post either as a file path or a string. 4. `date` an attribute set with the `day`, `month`, and `year` the post was uploaded. 5. `slug ? ` the post's slug, optionally generated from the post's title. +6. `template` the HTML template used to generate the post. -When exposed in `mkIndexHTML` and `mkPostHTML`, posts have the following -additional attributes: -1. `slug` assuming no slug was defined by the user. +When exposed in a template, posts have the following additional attributes: +1. `slug` assuming no slug was defined by the user one will be generated from `title`. 2. `content` the HTML representation of your post's markdown. 3. `next` the next post in the list. 4. `prev` the previous post in the list. -## Using mkSite -`mkSite` provides the core functionality of `spoonful`, creating and combining -the post, index, and RSS derivations. It expects the following arguments: -1. `title ? "Title"` your website name. -2. `link ? "http://127.0.0.1:9999"` the link to your website. -3. `description ? "Description"` what you do on your website. -4. `posts ? []` a list of posts to be generated. -5. `generateRSS ? true` if an RSS feed should be generated. -6. `mkIndexHTML ? _: null` a function that determines how your website's index.html is generated. -7. `mkPostHTML ? _: null` a function that determines how each of your post's HTML is generated. -8. `static ? null` an optional folder to be included in your website as-is. - -Additionally, you may override any of the functions provided by `lib` by -passing them into `mkSite`. +### Example +```nix +{templates, ...}: { + title = "Post Title"; + description = "Post description"; + content = ./content.md; + template = templates.post; + date = { + day = 26; + month = 02; + year = 2026; + }; +} +``` -## Writing a mkIndexHTML -`mkIndexHTML` is function which takes `posts`, the list of your posts, and -returns the finished index.html of your website as a string. - -## Writing a mkPostHTML -`mkPostHTML` is a function which takes a post and generates the finished HTML -of said post. +## Writing Templates +Templates are functions that take all the attributes passed to `spoonful.mkSite` +as arguments and the current post, then return the HTML for the page as a string. +The only required template is `index` which defines how `index.html` should be +generated. ## Helper Functions `spoonful` also provides various helper functions to make your life easier. @@ -51,9 +75,11 @@ of said post. ### lib.sorting.byDate Provides a condition for `builtins.sort` which sorts posts by date. -### lib.recursiveImport -Recursively imports all Nix modules in a directory. Useful for automatically -importing your posts. +### lib.importPosts +Recursively imports all posts in a directory. + +### lib.importTemplates +Recursively imports all templates in a directory. ### lib.toKebabCase Converts any string to one in kebab-case. Used internally for slug generation. diff --git a/default.nix b/default.nix index e5f8260..0480214 100644 --- a/default.nix +++ b/default.nix @@ -1,6 +1,7 @@ {pkgs}: let - lib = { - recursiveImport = import ./lib/recursiveImport.nix; + lib = args: { + importPosts = import ./lib/importPosts.nix args; + importTemplates = import ./lib/importTemplates.nix; sorting.byDate = import ./lib/sortByDate.nix; toKebabCase = import ./lib/toKebabCase.nix {inherit (pkgs) lib;}; mkRSS = pkgs.callPackage ./lib/mkRSS.nix {}; @@ -11,55 +12,38 @@ pandocArgs = "-f commonmark_x"; }; }; -in { - inherit lib; - mkSite = { - title ? "Title", - link ? "http://127.0.0.1:9999", - description ? "Description", + + mkDerivation = { posts ? [], generateRSS ? true, - mkIndexHTML ? _: null, - mkPostHTML ? _: null, static ? null, - mkHTML ? lib.mkHTML, - mkPost ? lib.mkPost, - mkIndex ? lib.mkIndex, - mkRSS ? lib.mkRSS, + metadata ? { + title = "Title"; + link = "http://127.0.0.1:9999"; + description = "Description"; + }, + templates ? { + index = _: null; + post = _: null; + }, + lib, }: let - # Add `content`, `slug`, `prev`, and `next` attributes to each post - finalPosts = builtins.genList (i: let - post = builtins.elemAt posts i; - in - post - // { - content = builtins.readFile "${mkHTML.override {source = post.content;}}/page.html"; - slug = post.slug or (lib.toKebabCase post.title); - next = - if i == 0 - then null - else builtins.elemAt finalPosts (i - 1); - prev = - if i == (builtins.length posts) - 1 - then null - else builtins.elemAt finalPosts (i + 1); - }) (builtins.length posts); in pkgs.symlinkJoin { name = "website"; paths = [ - (mkIndex.override { - inherit mkIndexHTML; - posts = finalPosts; + (lib.mkIndex.override { + inherit metadata posts; + indexTemplate = templates.index; }) ] ++ ( if generateRSS then [ - (mkRSS.override { - inherit title link description; - posts = finalPosts; + (lib.mkRSS.override { + inherit posts; + inherit (metadata) title link description; }) ] else [] @@ -69,6 +53,30 @@ in { then [static] else [] ) - ++ map (post: mkPost.override {inherit mkPostHTML post;}) finalPosts; + ++ map (post: lib.mkPost.override {inherit post metadata;}) posts; }; +in { + mkSite = args: let + libWithArgs = lib preArgs; + preArgs = args libWithArgs; + + # Add `content`, `slug`, `prev`, and `next` attributes to each post + finalPosts = builtins.genList (i: let + post = builtins.elemAt preArgs.posts i; + in + post + // { + content = builtins.readFile "${preArgs.lib.mkHTML.override {source = post.content;}}/page.html"; + slug = post.slug or (preArgs.lib.toKebabCase post.title); + next = + if i == 0 + then null + else builtins.elemAt finalPosts (i - 1); + prev = + if i == (builtins.length preArgs.posts) - 1 + then null + else builtins.elemAt finalPosts (i + 1); + }) (builtins.length preArgs.posts); + in + mkDerivation (preArgs // {posts = finalPosts;}); } diff --git a/lib/recursiveImport.nix b/lib/importPosts.nix similarity index 94% rename from lib/recursiveImport.nix rename to lib/importPosts.nix index 301a045..397e0d1 100644 --- a/lib/recursiveImport.nix +++ b/lib/importPosts.nix @@ -1,6 +1,6 @@ # Written by llakala (https://github.com/llakala) and given to me with # permission via Discord. -let +args: let inherit (builtins) readDir @@ -26,7 +26,7 @@ let type = contents.${filename}; in if type == "regular" && isNixFile path - then [(import path)] + then [(import path args)] else if type == "directory" then getNixFilesRecursive path else [] diff --git a/lib/importTemplates.nix b/lib/importTemplates.nix new file mode 100644 index 0000000..5a88c00 --- /dev/null +++ b/lib/importTemplates.nix @@ -0,0 +1,42 @@ +# Written by llakala (https://github.com/llakala) and given to me with +# permission via Discord. +let + inherit + (builtins) + readDir + concatMap + attrNames + stringLength + substring + listToAttrs + ; + + isNixFile = path: let + str = toString path; + len = stringLength str; + in + len >= 4 && substring (len - 4) len str == ".nix"; + + # Returns a list of attrsets + getNixFilesRecursive = dir: let + contents = readDir dir; + in + concatMap ( + filename: let + path = dir + "/${filename}"; + type = contents.${filename}; + in + if type == "regular" && isNixFile path + then [ + { + name = substring 0 (stringLength filename - 4) filename; + value = import path; + } + ] + else if type == "directory" + then getNixFilesRecursive path + else [] + ) (attrNames contents); +in + dir: + listToAttrs (getNixFilesRecursive dir) diff --git a/lib/mkIndex.nix b/lib/mkIndex.nix index 0eb9f20..8e548cd 100644 --- a/lib/mkIndex.nix +++ b/lib/mkIndex.nix @@ -1,11 +1,12 @@ { stdenv, - mkIndexHTML ? _: "", + indexTemplate ? _: null, posts ? [], + metadata ? {}, }: stdenv.mkDerivation { name = "index"; - src = mkIndexHTML {inherit posts;}; + src = indexTemplate {inherit posts metadata;}; phases = ["buildPhase" "installPhase"]; buildPhase = '' echo -n "$src" >> index.html diff --git a/lib/mkPost.nix b/lib/mkPost.nix index 74c4d72..6a036ba 100644 --- a/lib/mkPost.nix +++ b/lib/mkPost.nix @@ -1,13 +1,13 @@ { stdenv, - mkPostHTML ? _: "", post ? {}, + metadata ? {}, }: let slug = post.slug or ''"$(echo '${post.title}' | tr '[:upper:]' '[:lower:]' | tr ' ' '-')"''; in stdenv.mkDerivation { name = "post"; - src = mkPostHTML {inherit post;}; + src = post.template {inherit post metadata;}; phases = ["buildPhase" "installPhase"]; buildPhase = '' echo -n "$src" >> ${slug}.html -- knotmirror 0.0.1