From b614348b893a564740fd753a524fd79236bb2674 Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Sun, 16 May 2021 22:47:53 +0300 Subject: [PATCH] feat: pages folder support --- examples/site.nix | 15 +-------------- examples/site/config.toml | 7 +------ examples/site/pages/about.md | 3 +++ pkgs-lib.nix | 14 ++++++++++++-- templaters/basic.nix | 22 +++++++++++----------- 5 files changed, 28 insertions(+), 33 deletions(-) create mode 100644 examples/site/pages/about.md diff --git a/examples/site.nix b/examples/site.nix index dfced44..c309e30 100644 --- a/examples/site.nix +++ b/examples/site.nix @@ -5,18 +5,5 @@ let in htmlNix.mkServeFromSite (htmlNix.mkSiteFrom { inherit src; - templater = context: pkgs.lib.pipe context [ - # Add basic template - lib.templaters.basic - - # Add about page - ({ site, mkPage, ... }@result: { - site = site // { - "about.html" = with lib.tags; mkPage [ - (h1 "About") - (p "testy test test") - ]; - }; - }) - ]; + templater = lib.templaters.basic; }) diff --git a/examples/site/config.toml b/examples/site/config.toml index cde8656..1481ae6 100644 --- a/examples/site/config.toml +++ b/examples/site/config.toml @@ -1,6 +1 @@ -title = "test site" - -[pages] -index = "index.html" -about = "about.html" -test = "404.html" \ No newline at end of file +title = "test site" \ No newline at end of file diff --git a/examples/site/pages/about.md b/examples/site/pages/about.md new file mode 100644 index 0000000..240f051 --- /dev/null +++ b/examples/site/pages/about.md @@ -0,0 +1,3 @@ +# About + +testy test \ No newline at end of file diff --git a/pkgs-lib.nix b/pkgs-lib.nix index 4371d76..633b469 100644 --- a/pkgs-lib.nix +++ b/pkgs-lib.nix @@ -34,7 +34,7 @@ in mkSiteFrom = { src, templater }: let inherit (utils) readDir readFile fromTOML mapAttrsToList sort elemAt; - inherit (pkgs.lib) nameValuePair head splitString pipe removeSuffix; + inherit (pkgs.lib) nameValuePair head splitString pipe removeSuffix mapAttrs'; postsRendered = let path = src + "/posts"; in @@ -42,7 +42,7 @@ in (mapAttrsToList (name: _: nameValuePair (head (splitString "." name)) - (parseMarkdown name (readFile (path + "/${name}"))) + (readFile (parseMarkdown name (readFile (path + "/${name}")))) )) (sort (p: op: let @@ -54,12 +54,22 @@ in !(((d 0) > (od 0)) && ((d 1) > (od 1)) && ((d 2) > (od 2))) )) ]; + pagesRendered = + let path = src + "/pages"; in + mapAttrs' + (name: _: + nameValuePair + (head (splitString "." name)) + (readFile (parseMarkdown name (readFile (path + "/${name}")))) + ) + (readDir path); siteConfig = fromTOML (readFile (src + "/config.toml")); context = { inherit utils pkgs; config = siteConfig; posts = postsRendered; + pages = pagesRendered; }; in (templater context).site; diff --git a/templaters/basic.nix b/templaters/basic.nix index 0e9026b..a83d08d 100644 --- a/templaters/basic.nix +++ b/templaters/basic.nix @@ -1,7 +1,7 @@ -{ utils, posts, pkgs, config, ... }@context: +{ utils, posts, pkgs, config, pages, ... }@context: let - inherit (utils) readFile mapAttrsToList tags fetchGit map elemAt; - inherit (pkgs.lib) optional length splitString; + inherit (utils) readFile mapAttrsToList tags fetchGit map elemAt foldl'; + inherit (pkgs.lib) optional length splitString mapAttrs' nameValuePair; stylesheets = map tags.mkStylesheet [ "https://unpkg.com/purecss@2.0.6/build/pure-min.css" @@ -17,22 +17,22 @@ let with tags; article [ (a { href = "#${id}"; class = "postheader"; } (h3 { inherit id; } ("## " + id))) (h6 ("date: " + (elemAt parts 0))) - (readFile value) + value ]; - pages = - mapAttrsToList - (name: relPath: tags.div { class = "pure-u-1"; } (tags.a { href = "./${relPath}"; class = "postheader"; } name)) - (config.pages or { }); + pagesSection = + map + (name: tags.div { class = "pure-u-1"; } (tags.a { href = "./${name}.html"; class = "postheader"; } name)) + ((mapAttrsToList (name: _: name) pages) ++ [ "index" ]); postsSectionContent = with tags; [ (a { href = "#posts"; class = "postheader"; } (h1 "# posts")) ] ++ (map renderPost posts); - sidebarSection = optional ((length pages) > 0) ( + sidebarSection = optional ((length pagesSection) > 0) ( with tags; nav { class = "sidebar"; } ([ (a { href = "#pages"; class = "postheader"; } (h1 "# pages")) - (div { class = "pure-g"; } pages) + (div { class = "pure-g"; } pagesSection) ]) ); @@ -100,5 +100,5 @@ in site = { "index.html" = mkPage postsSectionContent; "mine.css" = stylesheet; - }; + } // (mapAttrs' (name: value: nameValuePair (name + ".html") (mkPage value)) pages); }