html.nix/templaters/basic.nix

223 lines
5.3 KiB
Nix
Raw Normal View History

2022-07-30 12:42:09 +03:00
{
config,
2023-04-06 01:43:12 +03:00
lib,
2022-07-30 12:42:09 +03:00
...
2023-04-06 01:43:12 +03:00
}: let
l = lib // builtins;
t = l.types;
inherit (config.html-nix.lib) html css;
2021-05-15 19:42:16 +03:00
2023-04-06 01:43:12 +03:00
func = ctx: let
stylesheets = l.map html.mkStylesheet [
"https://unpkg.com/purecss@3.0.0/build/pure-min.css"
"https://unpkg.com/purecss@3.0.0/build/grids-responsive-min.css"
"${ctx.baseurl}/site.css"
2022-07-30 12:42:09 +03:00
];
2023-04-06 01:43:12 +03:00
parsePostName = name: let
parts = l.splitString "_" name;
id = l.elemAt parts 1;
date = l.elemAt parts 0;
in {
inherit id date;
formatted = "${date} - ${id}";
};
2022-07-31 07:17:41 +03:00
2023-04-06 01:43:12 +03:00
renderPost = {
name,
value,
}: let
parsed = parsePostName name;
inherit (parsed) id date;
in
with html;
article [
(a {
href = "#${id}";
class = "postheader";
} (h2 {inherit id;} id))
(h3 ("date: " + date))
value
];
2023-04-06 01:43:12 +03:00
pagesSection = with html;
[
(div {class = "pure-u-1";} (a {
href = "${ctx.baseurl}/";
class = "pagelink";
} "home"))
2022-07-30 12:42:09 +03:00
]
2023-04-06 01:43:12 +03:00
++ (l.map
(name:
div {class = "pure-u-1";} (a {
href = "${ctx.baseurl}/${name}/";
class = "pagelink";
}
name))
2023-04-06 05:43:25 +03:00
(l.mapAttrsToList (name: _: name) ctx.pages))
2023-04-06 01:43:12 +03:00
++ [
(div {class = "pure-u-1";} (a {
href = "${ctx.baseurl}/posts/";
class = "pagelink";
} "posts"))
];
2021-05-16 02:56:54 +03:00
2023-04-06 01:43:12 +03:00
postsLinks = with html;
2023-04-06 05:43:25 +03:00
l.singleton
2023-04-06 01:43:12 +03:00
(ul (
l.map
(
post:
li (
a {href = "${ctx.baseurl}/${post.name}";}
(parsePostName post.name).formatted
)
)
2023-04-06 05:43:25 +03:00
ctx.posts
2023-04-06 01:43:12 +03:00
));
2021-05-15 21:29:30 +03:00
2023-04-06 01:43:12 +03:00
postsSectionContent = with html;
[
(a {
href = "#posts";
class = "postheader";
} (h1 "posts"))
]
++ postsLinks;
2023-04-06 01:43:12 +03:00
sidebarSection = l.optionalString ((l.length pagesSection) > 0) (
with html;
nav {class = "sidebar";} [
(div {class = "pure-g";} pagesSection)
]
2022-07-31 07:17:41 +03:00
);
2023-04-06 01:43:12 +03:00
mkPage = content:
with html; ''
<!DOCTYPE html>
2023-04-06 05:43:25 +03:00
${html.html [
2023-04-06 01:43:12 +03:00
(head (stylesheets
++ [
(title ctx.config.title)
(meta {
name = "viewport";
content = "width=device-width, initial-scale=1";
})
]))
(body ''
${script "0"}
${sidebarSection}
${div {class = "content";} content}
'')
]}
'';
indexPage = mkPage (ctx.indexContent or postsSectionContent);
pagesAndPosts =
ctx.pages
// l.listToAttrs (
map (post: l.nameValuePair post.name (renderPost post)) ctx.posts
);
stylesheet = let
marginMobile = {
margin-left = "3%";
margin-right = "3%";
};
in
css [
(css (
(
l.mapAttrs'
(
name: value:
l.nameValuePair
value
{
content = "\"${l.concatStrings (l.map (_: "#") (l.range 1 (l.toInt name)))} \"";
}
)
(
l.genAttrs
(l.map l.toString (l.range 1 6))
2023-04-06 05:43:25 +03:00
(n: "h${l.toString n}:before")
2023-04-06 01:43:12 +03:00
)
)
// {
body = {
font-family = ["Raleway" "Helvetica" "Arial" "sans-serif"];
background = "#111111";
color = "#eeeeee";
};
pre = {
font-family = ["Iosevka Term" "Iosevka" "monospace"];
background = "#171A21";
color = "#eeeeee";
};
"a,a:hover" = {
color = "#ffd814";
text-decoration = "none";
};
"a:hover" = {
text-decoration = "underline";
};
"a.postheader,a.postheader:hover" = {
color = "#fc6711";
};
"a.pagelink,a.pagelink:hover" = {
color = "#ffd814";
};
"div.content" = {
margin-top = "5%";
margin-bottom = "5%";
margin-left = "20%";
margin-right = "25%";
};
"nav.sidebar" = {
position = "fixed";
margin-left = "3%";
padding-top = 0;
z-index = 1000;
};
2022-07-30 12:42:09 +03:00
}
2023-04-06 01:43:12 +03:00
))
(css.media "max-width: 48em" {
"nav.sidebar" =
{
position = "relative";
margin-top = "5%";
}
// marginMobile;
"div.content" =
{
margin-top = 0;
}
// marginMobile;
})
];
in {
inherit stylesheets sidebarSection mkPage stylesheet;
2021-05-16 02:56:54 +03:00
2023-04-06 01:43:12 +03:00
site =
ctx.site
// {
"index.html" = indexPage;
"posts"."index.html" = mkPage postsSectionContent;
"404.html" = mkPage (html.h1 "No such page");
"site.css" = stylesheet;
}
// (l.mapAttrs (name: value: {"index.html" = mkPage value;}) pagesAndPosts)
// l.optionalAttrs (ctx ? resources) {inherit (ctx) resources;};
};
in {
options = {
html-nix.lib.templaters.basic = l.mkOption {
type = t.functionTo t.attrs;
};
};
config = {
html-nix.lib.templaters.basic = func;
};
2021-05-15 19:42:16 +03:00
}