write new templater

This commit is contained in:
dusk 2023-04-06 05:43:25 +03:00
parent ea64bc70fc
commit 46b3656161
Signed by: dusk
GPG Key ID: 1D8F8FAF2294D6EA
14 changed files with 313 additions and 122 deletions

View File

@ -34,7 +34,7 @@
in { in {
options = { options = {
html-nix.lib.css = l.mkOption { html-nix.lib.css = l.mkOption {
type = t.functionTo t.str; type = t.raw;
}; };
}; };
config = { config = {

View File

@ -3,6 +3,6 @@
./html.nix ./html.nix
./css.nix ./css.nix
./pkgs-lib.nix ./pkgs-lib.nix
./templaters/default.nix ./templaters
]; ];
} }

3
examples/default.nix Normal file
View File

@ -0,0 +1,3 @@
{
imports = [./site.nix];
}

View File

@ -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
];
};
}

View File

@ -1,9 +1,9 @@
{ topArgs: {
perSystem = {config, ...}: let perSystem = {config, ...}: let
html-nix = config.html-nix; html-nix = config.html-nix.lib;
siteServe = html-nix.mkServeFromSite (html-nix.mkSiteFrom { siteServe = html-nix.mkServeFromSite (html-nix.mkSiteFrom {
src = ./site; src = ./site;
templater = html-nix.lib.templaters.basic; templater = topArgs.config.html-nix.lib.templaters.simple;
local = true; local = true;
}); });
in { in {

View File

@ -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. 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.

View File

@ -1,21 +1,5 @@
{ {
"nodes": { "nodes": {
"examples": {
"inputs": {
"nixpkgs": "nixpkgs",
"parts": "parts"
},
"locked": {
"lastModified": 1,
"narHash": "sha256-agjsV0F6nVUimGUciKlOSF35XvTOQKKEez0xZ8X0JBI=",
"path": "./examples",
"type": "path"
},
"original": {
"path": "./examples",
"type": "path"
}
},
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1680668850, "lastModified": 1680668850,
@ -50,40 +34,6 @@
"type": "github" "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": { "parts": {
"inputs": { "inputs": {
"nixpkgs-lib": "nixpkgs-lib" "nixpkgs-lib": "nixpkgs-lib"
@ -102,29 +52,10 @@
"type": "github" "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": { "root": {
"inputs": { "inputs": {
"examples": "examples", "nixpkgs": "nixpkgs",
"nixpkgs": "nixpkgs_2", "parts": "parts"
"parts": "parts_2"
} }
} }
}, },

View File

@ -2,16 +2,18 @@
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
parts.url = "github:hercules-ci/flake-parts"; parts.url = "github:hercules-ci/flake-parts";
examples.url = "path:./examples";
}; };
outputs = inp: outputs = inp:
inp.parts.lib.mkFlake {inputs = inp;} { inp.parts.lib.mkFlake {inputs = inp;} {
debug = true; debug = true;
systems = ["x86_64-linux"]; systems = ["x86_64-linux"];
imports = [
./default.nix
./examples
];
flake = { flake = {
flakeModule = ./default.nix; flakeModule = ./default.nix;
inherit (inp.examples) apps;
}; };
}; };
} }

View File

@ -19,13 +19,13 @@
noChildrenTag = name: attrs: "<${name} ${evalAttrs attrs}>"; noChildrenTag = name: attrs: "<${name} ${evalAttrs attrs}>";
tagsToGen = 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"] ++ ["ul" "li" "html" "head" "body" "div" "p"]
++ ["a" "title" "code" "pre" "nav" "article" "script"]; ++ ["a" "title" "code" "pre" "nav" "article" "script"];
tags = l.genAttrs tag tagsToGen; tags = l.genAttrs tagsToGen tag;
noChildrenTagsToGen = ["link" "meta"]; noChildrenTagsToGen = ["link" "meta" "hr"];
noChildrenTags = l.genAttrs noChildrenTag noChildrenTagsToGen; noChildrenTags = l.genAttrs noChildrenTagsToGen noChildrenTag;
in { in {
options = { options = {
html-nix.lib.html = l.mkOption { html-nix.lib.html = l.mkOption {

View File

@ -4,17 +4,37 @@
... ...
}: let }: let
l = lib // builtins; 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 { in {
options = { options = {
perSystem = perSystem =
flake-parts-lib.mkPerSystemOption flake-parts-lib.mkPerSystemOption
({...}: { ({...}: {
html-nix.lib = { options = {
mkServeFromSite = l.mkOption { html-nix.lib = {
type = with l.types; functionTo package; mkServeFromSite = l.mkOption {
}; type = with l.types; functionTo package;
mkSiteFrom = l.mkOption { };
type = with l.types; functionTo attrs; 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 if builtins.isPath value
then value then value
else pkgs.writeText (l.concatStringsSep "-" path) value; else pkgs.writeText (l.concatStringsSep "-" path) value;
fileAttrPaths = l.recursiveAttrPaths site; fileAttrPaths = recursiveAttrPaths site;
texts = l.mapAttrsRecursive convertToPath site; texts = l.mapAttrsRecursive convertToPath site;
mkCreateFileCmd = path: value: let mkCreateFileCmd = path: value: let
p = l.concatStringsSep "/" (l.init path); p = l.concatStringsSep "/" (l.init path);
@ -54,6 +74,7 @@ in {
''; '';
in { in {
html-nix.lib = { html-nix.lib = {
mkSitePathFrom = mkSitePath;
mkServeFromSite = site: mkServePathScript (mkSitePath site); mkServeFromSite = site: mkServePathScript (mkSitePath site);
mkSiteFrom = { mkSiteFrom = {
src, src,
@ -65,17 +86,23 @@ in {
in in
l.pipe (l.readDir path) [ l.pipe (l.readDir path) [
(l.mapAttrsToList ( (l.mapAttrsToList (
name: _: name: _: let
l.nameValuePair __displayName = l.head (l.splitString "." name);
(l.head (l.splitString "." name)) _displayName = l.splitString "_" __displayName;
(l.readFile (parseMarkdown name (l.readFile (path + "/${name}")))) 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 ( (l.sort (
p: op: let p: op: let
extractDate = name: l.splitString "-" (l.head (l.splitString "_" name)); extractDate = date: l.splitString "-" date;
getPart = name: el: l.removeSuffix "0" (l.elemAt (extractDate name) el); getPart = date: el: l.removeSuffix "0" (l.elemAt (extractDate date) el);
d = getPart p.name; d = getPart p.date;
od = getPart op.name; od = getPart op.date;
in in
!(((d 0) > (od 0)) && ((d 1) > (od 1)) && ((d 2) > (od 2))) !(((d 0) > (od 0)) && ((d 1) > (od 1)) && ((d 2) > (od 2)))
)) ))
@ -83,12 +110,13 @@ in {
pagesRendered = let pagesRendered = let
path = src + "/pages"; path = src + "/pages";
in in
l.mapAttrs' l.mapAttrsToList
( (
name: _: name: _: rec {
l.nameValuePair displayName = l.head (l.splitString "." name);
(l.head (l.splitString "." name)) id = l.replaceStrings [" "] ["_"] displayName;
(l.readFile (parseMarkdown name (l.readFile (path + "/${name}")))) content = l.readFile (parseMarkdown id (l.readFile (path + "/${name}")));
}
) )
(l.readDir path); (l.readDir path);
siteConfig = l.fromTOML (l.readFile (src + "/config.toml")); siteConfig = l.fromTOML (l.readFile (src + "/config.toml"));

View File

@ -54,7 +54,7 @@
class = "pagelink"; class = "pagelink";
} }
name)) name))
(l.mapAttrsToList (name: _: name) pages)) (l.mapAttrsToList (name: _: name) ctx.pages))
++ [ ++ [
(div {class = "pure-u-1";} (a { (div {class = "pure-u-1";} (a {
href = "${ctx.baseurl}/posts/"; href = "${ctx.baseurl}/posts/";
@ -63,7 +63,7 @@
]; ];
postsLinks = with html; postsLinks = with html;
singleton l.singleton
(ul ( (ul (
l.map l.map
( (
@ -73,7 +73,7 @@
(parsePostName post.name).formatted (parsePostName post.name).formatted
) )
) )
posts ctx.posts
)); ));
postsSectionContent = with html; postsSectionContent = with html;
@ -95,7 +95,7 @@
mkPage = content: mkPage = content:
with html; '' with html; ''
<!DOCTYPE html> <!DOCTYPE html>
${html [ ${html.html [
(head (stylesheets (head (stylesheets
++ [ ++ [
(title ctx.config.title) (title ctx.config.title)
@ -140,8 +140,8 @@
) )
( (
l.genAttrs l.genAttrs
(n: "h${l.toString n}:before")
(l.map l.toString (l.range 1 6)) (l.map l.toString (l.range 1 6))
(n: "h${l.toString n}:before")
) )
) )
// { // {

View File

@ -1,5 +1,6 @@
{ {
imports = [ imports = [
./basic.nix ./basic.nix
./simple
]; ];
} }

View File

@ -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; ''
<!DOCTYPE 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;
};
}

View File

@ -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;
})
]