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 {
options = {
html-nix.lib.css = l.mkOption {
type = t.functionTo t.str;
type = t.raw;
};
};
config = {

View File

@ -3,6 +3,6 @@
./html.nix
./css.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
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 {

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.

View File

@ -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"
}
}
},

View File

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

View File

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

View File

@ -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"));

View File

@ -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; ''
<!DOCTYPE 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")
)
)
// {

View File

@ -1,5 +1,6 @@
{
imports = [
./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;
})
]