From 46b3656161aab4fe38b7aba089341648c2d91b85 Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Thu, 6 Apr 2023 05:43:25 +0300 Subject: [PATCH] write new templater --- css.nix | 2 +- default.nix | 2 +- examples/default.nix | 3 + examples/flake.nix | 15 --- examples/site.nix | 6 +- examples/site/posts/2021-07-01_asdf.md | 2 + flake.lock | 73 +----------- flake.nix | 6 +- html.nix | 8 +- pkgs-lib.nix | 68 +++++++---- templaters/basic.nix | 10 +- templaters/default.nix | 1 + templaters/simple/default.nix | 151 +++++++++++++++++++++++++ templaters/simple/stylesheet.nix | 88 ++++++++++++++ 14 files changed, 313 insertions(+), 122 deletions(-) create mode 100644 examples/default.nix delete mode 100644 examples/flake.nix create mode 100644 templaters/simple/default.nix create mode 100644 templaters/simple/stylesheet.nix diff --git a/css.nix b/css.nix index c161ad8..d164636 100644 --- a/css.nix +++ b/css.nix @@ -34,7 +34,7 @@ in { options = { html-nix.lib.css = l.mkOption { - type = t.functionTo t.str; + type = t.raw; }; }; config = { diff --git a/default.nix b/default.nix index 71e44ce..d8cb34f 100644 --- a/default.nix +++ b/default.nix @@ -3,6 +3,6 @@ ./html.nix ./css.nix ./pkgs-lib.nix - ./templaters/default.nix + ./templaters ]; } diff --git a/examples/default.nix b/examples/default.nix new file mode 100644 index 0000000..ca20fe6 --- /dev/null +++ b/examples/default.nix @@ -0,0 +1,3 @@ +{ + imports = [./site.nix]; +} diff --git a/examples/flake.nix b/examples/flake.nix deleted file mode 100644 index 91eed36..0000000 --- a/examples/flake.nix +++ /dev/null @@ -1,15 +0,0 @@ -{ - inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; - parts.url = "github:hercules-ci/flake-parts"; - }; - - outputs = inp: - inp.parts.lib.mkFlake {inputs = inp;} { - systems = ["x86_64-linux"]; - imports = [ - ../default.nix - ./site.nix - ]; - }; -} diff --git a/examples/site.nix b/examples/site.nix index 671b62f..8c597c3 100644 --- a/examples/site.nix +++ b/examples/site.nix @@ -1,9 +1,9 @@ -{ +topArgs: { perSystem = {config, ...}: let - html-nix = config.html-nix; + html-nix = config.html-nix.lib; siteServe = html-nix.mkServeFromSite (html-nix.mkSiteFrom { src = ./site; - templater = html-nix.lib.templaters.basic; + templater = topArgs.config.html-nix.lib.templaters.simple; local = true; }); in { diff --git a/examples/site/posts/2021-07-01_asdf.md b/examples/site/posts/2021-07-01_asdf.md index e670b66..ddd0f42 100644 --- a/examples/site/posts/2021-07-01_asdf.md +++ b/examples/site/posts/2021-07-01_asdf.md @@ -6,4 +6,6 @@ fn main() { } ``` +AAAAAAAAAAa `rust` `test` aaaaaaaaaaaa + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur condimentum felis risus, ac tempor eros mollis ac. Curabitur dictum justo magna, a tristique nibh euismod at. Nullam at laoreet quam, a facilisis massa. Vestibulum iaculis euismod lorem non consequat. Quisque elementum pharetra egestas. Mauris in enim facilisis, pretium lacus vitae, accumsan diam. Praesent ut finibus mauris, eget iaculis lorem. Cras pharetra lectus varius, volutpat odio sit amet, pulvinar lectus. Aliquam malesuada erat a enim rutrum viverra. Nullam blandit nec enim eu egestas. Duis venenatis aliquet cursus. Phasellus non posuere lectus, sed rhoncus mi. Quisque ut ornare dui. Ut eget elit ac leo aliquam aliquam. \ No newline at end of file diff --git a/flake.lock b/flake.lock index 15d95dd..0cafd6b 100644 --- a/flake.lock +++ b/flake.lock @@ -1,21 +1,5 @@ { "nodes": { - "examples": { - "inputs": { - "nixpkgs": "nixpkgs", - "parts": "parts" - }, - "locked": { - "lastModified": 1, - "narHash": "sha256-agjsV0F6nVUimGUciKlOSF35XvTOQKKEez0xZ8X0JBI=", - "path": "./examples", - "type": "path" - }, - "original": { - "path": "./examples", - "type": "path" - } - }, "nixpkgs": { "locked": { "lastModified": 1680668850, @@ -50,40 +34,6 @@ "type": "github" } }, - "nixpkgs-lib_2": { - "locked": { - "dir": "lib", - "lastModified": 1680213900, - "narHash": "sha256-cIDr5WZIj3EkKyCgj/6j3HBH4Jj1W296z7HTcWj1aMA=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "e3652e0735fbec227f342712f180f4f21f0594f2", - "type": "github" - }, - "original": { - "dir": "lib", - "owner": "NixOS", - "ref": "nixos-unstable", - "repo": "nixpkgs", - "type": "github" - } - }, - "nixpkgs_2": { - "locked": { - "lastModified": 1680668850, - "narHash": "sha256-mQMg13yRsS0LXVzaeoSPwqgPO6yhkGzGewPgMSqXSv8=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "4a65e9f64e53fdca6eed31adba836717a11247d2", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixpkgs-unstable", - "repo": "nixpkgs", - "type": "github" - } - }, "parts": { "inputs": { "nixpkgs-lib": "nixpkgs-lib" @@ -102,29 +52,10 @@ "type": "github" } }, - "parts_2": { - "inputs": { - "nixpkgs-lib": "nixpkgs-lib_2" - }, - "locked": { - "lastModified": 1680392223, - "narHash": "sha256-n3g7QFr85lDODKt250rkZj2IFS3i4/8HBU2yKHO3tqw=", - "owner": "hercules-ci", - "repo": "flake-parts", - "rev": "dcc36e45d054d7bb554c9cdab69093debd91a0b5", - "type": "github" - }, - "original": { - "owner": "hercules-ci", - "repo": "flake-parts", - "type": "github" - } - }, "root": { "inputs": { - "examples": "examples", - "nixpkgs": "nixpkgs_2", - "parts": "parts_2" + "nixpkgs": "nixpkgs", + "parts": "parts" } } }, diff --git a/flake.nix b/flake.nix index a58a298..646c6b8 100644 --- a/flake.nix +++ b/flake.nix @@ -2,16 +2,18 @@ inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; parts.url = "github:hercules-ci/flake-parts"; - examples.url = "path:./examples"; }; outputs = inp: inp.parts.lib.mkFlake {inputs = inp;} { debug = true; systems = ["x86_64-linux"]; + imports = [ + ./default.nix + ./examples + ]; flake = { flakeModule = ./default.nix; - inherit (inp.examples) apps; }; }; } diff --git a/html.nix b/html.nix index 6bb43d0..d373bca 100644 --- a/html.nix +++ b/html.nix @@ -19,13 +19,13 @@ noChildrenTag = name: attrs: "<${name} ${evalAttrs attrs}>"; tagsToGen = - (l.map (n: "h${toString n}") (l.range 1 6)) + (l.map (n: "h${l.toString n}") (l.range 1 6)) ++ ["ul" "li" "html" "head" "body" "div" "p"] ++ ["a" "title" "code" "pre" "nav" "article" "script"]; - tags = l.genAttrs tag tagsToGen; + tags = l.genAttrs tagsToGen tag; - noChildrenTagsToGen = ["link" "meta"]; - noChildrenTags = l.genAttrs noChildrenTag noChildrenTagsToGen; + noChildrenTagsToGen = ["link" "meta" "hr"]; + noChildrenTags = l.genAttrs noChildrenTagsToGen noChildrenTag; in { options = { html-nix.lib.html = l.mkOption { diff --git a/pkgs-lib.nix b/pkgs-lib.nix index 3801eab..85ebdf1 100644 --- a/pkgs-lib.nix +++ b/pkgs-lib.nix @@ -4,17 +4,37 @@ ... }: let l = lib // builtins; + recursiveAttrPaths = set: let + flattenIfHasList = x: + if (l.isList x) && (l.any l.isList x) + then l.concatMap flattenIfHasList x + else [x]; + + recurse = path: set: let + g = name: value: + if l.isAttrs value + then recurse (path ++ [name]) value + else path ++ [name]; + in + l.mapAttrsToList g set; + in + flattenIfHasList (recurse [] set); in { options = { perSystem = flake-parts-lib.mkPerSystemOption ({...}: { - html-nix.lib = { - mkServeFromSite = l.mkOption { - type = with l.types; functionTo package; - }; - mkSiteFrom = l.mkOption { - type = with l.types; functionTo attrs; + options = { + html-nix.lib = { + mkServeFromSite = l.mkOption { + type = with l.types; functionTo package; + }; + mkSiteFrom = l.mkOption { + type = with l.types; functionTo attrs; + }; + mkSitePathFrom = l.mkOption { + type = l.types.raw; + }; }; }; }); @@ -33,7 +53,7 @@ in { if builtins.isPath value then value else pkgs.writeText (l.concatStringsSep "-" path) value; - fileAttrPaths = l.recursiveAttrPaths site; + fileAttrPaths = recursiveAttrPaths site; texts = l.mapAttrsRecursive convertToPath site; mkCreateFileCmd = path: value: let p = l.concatStringsSep "/" (l.init path); @@ -54,6 +74,7 @@ in { ''; in { html-nix.lib = { + mkSitePathFrom = mkSitePath; mkServeFromSite = site: mkServePathScript (mkSitePath site); mkSiteFrom = { src, @@ -65,17 +86,23 @@ in { in l.pipe (l.readDir path) [ (l.mapAttrsToList ( - name: _: - l.nameValuePair - (l.head (l.splitString "." name)) - (l.readFile (parseMarkdown name (l.readFile (path + "/${name}")))) + name: _: let + __displayName = l.head (l.splitString "." name); + _displayName = l.splitString "_" __displayName; + id = l.replaceStrings [" "] ["_"] __displayName; + in { + inherit id; + displayName = l.last _displayName; + date = l.head _displayName; + content = l.readFile (parseMarkdown id (l.readFile (path + "/${name}"))); + } )) (l.sort ( p: op: let - extractDate = name: l.splitString "-" (l.head (l.splitString "_" name)); - getPart = name: el: l.removeSuffix "0" (l.elemAt (extractDate name) el); - d = getPart p.name; - od = getPart op.name; + extractDate = date: l.splitString "-" date; + getPart = date: el: l.removeSuffix "0" (l.elemAt (extractDate date) el); + d = getPart p.date; + od = getPart op.date; in !(((d 0) > (od 0)) && ((d 1) > (od 1)) && ((d 2) > (od 2))) )) @@ -83,12 +110,13 @@ in { pagesRendered = let path = src + "/pages"; in - l.mapAttrs' + l.mapAttrsToList ( - name: _: - l.nameValuePair - (l.head (l.splitString "." name)) - (l.readFile (parseMarkdown name (l.readFile (path + "/${name}")))) + name: _: rec { + displayName = l.head (l.splitString "." name); + id = l.replaceStrings [" "] ["_"] displayName; + content = l.readFile (parseMarkdown id (l.readFile (path + "/${name}"))); + } ) (l.readDir path); siteConfig = l.fromTOML (l.readFile (src + "/config.toml")); diff --git a/templaters/basic.nix b/templaters/basic.nix index d370919..48bd167 100644 --- a/templaters/basic.nix +++ b/templaters/basic.nix @@ -54,7 +54,7 @@ class = "pagelink"; } name)) - (l.mapAttrsToList (name: _: name) pages)) + (l.mapAttrsToList (name: _: name) ctx.pages)) ++ [ (div {class = "pure-u-1";} (a { href = "${ctx.baseurl}/posts/"; @@ -63,7 +63,7 @@ ]; postsLinks = with html; - singleton + l.singleton (ul ( l.map ( @@ -73,7 +73,7 @@ (parsePostName post.name).formatted ) ) - posts + ctx.posts )); postsSectionContent = with html; @@ -95,7 +95,7 @@ mkPage = content: with html; '' - ${html [ + ${html.html [ (head (stylesheets ++ [ (title ctx.config.title) @@ -140,8 +140,8 @@ ) ( l.genAttrs - (n: "h${l.toString n}:before") (l.map l.toString (l.range 1 6)) + (n: "h${l.toString n}:before") ) ) // { diff --git a/templaters/default.nix b/templaters/default.nix index f9bc9f7..be75aee 100644 --- a/templaters/default.nix +++ b/templaters/default.nix @@ -1,5 +1,6 @@ { imports = [ ./basic.nix + ./simple ]; } diff --git a/templaters/simple/default.nix b/templaters/simple/default.nix new file mode 100644 index 0000000..aee71be --- /dev/null +++ b/templaters/simple/default.nix @@ -0,0 +1,151 @@ +{ + config, + lib, + ... +}: let + l = lib // builtins; + t = l.types; + inherit (config.html-nix.lib) html css; + + func = ctx: let + stylesheets = l.map html.mkStylesheet ["${ctx.baseurl}/site.css"]; + stylesheet = import ./stylesheet.nix {inherit css l;}; + + renderPost = post: + with html; + article [ + (h1 {inherit (post) id;} post.displayName) + (h4 {class = "nohashtag";} ("date: " + post.date)) + post.content + ]; + + mkPage = { + content, + titleStr ? ctx.config.title, + }: + with html; '' + + ${html.html [ + (head (stylesheets + ++ [ + (title titleStr) + (meta { + name = "viewport"; + content = "width=device-width, initial-scale=1"; + }) + ])) + (body '' + ${script "0"} + ${div (l.flatten [ + navBar + (hr {}) + content + ])} + '') + ]} + ''; + + navBar = with html; + nav ( + [ + (a { + href = "${ctx.baseurl}/"; + class = "novisited"; + } "home") + ] + ++ ( + l.map + ( + page: + " " + + ( + a { + href = "${ctx.baseurl}/${page.id}/"; + class = "novisited"; + } + page.displayName + ) + ) + ctx.pages + ) + ); + + postsLinks = with html; + l.singleton + (ul ( + l.map + ( + post: + li ( + a {href = "${ctx.baseurl}/${post.id}";} + "${post.date} - ${post.displayName}" + ) + ) + ctx.posts + )); + + postsSectionContent = [(html.h1 "posts")] ++ postsLinks; + + postsRendered = l.listToAttrs ( + l.map + (post: + l.nameValuePair post.id { + content = renderPost post; + name = post.displayName; + }) + ctx.posts + ); + pagesRendered = l.listToAttrs ( + l.map + (page: + l.nameValuePair page.id { + content = page.content; + name = page.displayName; + }) + ctx.pages + ); + + indexPage = mkPage { + content = ctx.indexContent or postsSectionContent; + }; + in { + inherit stylesheets mkPage stylesheet; + + site = + ctx.site + // { + "index.html" = indexPage; + "posts"."index.html" = mkPage { + content = postsSectionContent; + titleStr = "posts - ${ctx.config.title}"; + }; + "404.html" = mkPage { + content = html.h1 {class = "nohashtag";} "page not found"; + titleStr = "page not found - ${ctx.config.title}"; + }; + "site.css" = stylesheet; + } + // ( + l.mapAttrs + ( + name: value: { + "index.html" = mkPage { + content = value.content; + titleStr = "${value.name} - ${ctx.config.title}"; + }; + } + ) + (pagesRendered // postsRendered) + ) + // l.optionalAttrs (ctx ? resources) {inherit (ctx) resources;}; + }; +in { + options = { + html-nix.lib.templaters.simple = l.mkOption { + type = t.uniq (t.functionTo t.attrs); + }; + }; + config = { + html-nix.lib.templaters.simple = func; + }; +} diff --git a/templaters/simple/stylesheet.nix b/templaters/simple/stylesheet.nix new file mode 100644 index 0000000..8f7abe5 --- /dev/null +++ b/templaters/simple/stylesheet.nix @@ -0,0 +1,88 @@ +{ + css, + l, + ... +}: let + colors = { + light = rec { + fg = "#000"; + bg = "#faf9f6"; + code-bg = fg; + code-fg = bg; + link = "#1f51ff"; + link-visited = "#9d00ff"; + }; + dark = rec { + fg = "#eee"; + bg = "#111"; + code-bg = "#333"; + code-fg = fg; + link = "#007fff"; + link-visited = "#bf40bf"; + }; + }; + headers = extra: + l.genAttrs + (l.map l.toString (l.range 1 6)) + (n: "h${l.toString n}${extra}"); +in + css [ + (css ( + l.mapAttrs' + ( + name: value: + l.nameValuePair + value + { + content = ''"${l.concatStrings (l.map (_: "#") (l.range 1 (l.toInt name)))} "''; + } + ) + (headers ":before") + )) + (css ( + l.mapAttrs' + (_: value: l.nameValuePair value {content = ''""'';}) + (headers ".nohashtag:before") + )) + (css { + body = { + font-family = ["sans-serif"]; + color = colors.light.fg; + background = colors.light.bg; + max-width = "650px"; + margin = "40px auto"; + }; + "pre,code" = { + font-family = ["monospace"]; + background = colors.light.code-bg; + color = colors.light.code-fg; + padding = "4px"; + border-radius = "4px"; + }; + "pre code" = { + padding = 0; + border-radius = 0; + }; + a = { + color = colors.light.link; + text-decoration = "none"; + }; + "a:hover".text-decoration = "underline"; + "a:visited".color = colors.light.link-visited; + "a.novisited:visited".color = colors.light.link; + "h1,h2,h3".line-height = "1.2"; + }) + (css.media "prefers-color-scheme: dark" { + body = { + color = colors.dark.fg; + background = colors.dark.bg; + }; + "pre,code" = { + color = colors.dark.code-fg; + background = colors.dark.code-bg; + }; + a.color = colors.dark.link; + "a:visited".color = colors.dark.link-visited; + "a.novisited:visited".color = colors.dark.link; + }) + ]