html.nix/templaters/basic.nix

208 lines
4.9 KiB
Nix
Raw Normal View History

2022-07-30 12:42:09 +03:00
{
utils,
posts,
pkgs,
config,
pages,
site,
baseurl,
...
} @ context: let
2021-05-17 18:41:37 +03:00
inherit (utils) readFile mapAttrsToList mapAttrs tags fetchGit map elemAt foldl' concatStrings genAttrs toString;
2022-07-31 10:05:16 +03:00
inherit (pkgs.lib) optionalString optionalAttrs optional length splitString nameValuePair toInt range mapAttrs' singleton;
2022-07-31 07:17:41 +03:00
inherit (builtins) listToAttrs;
2021-05-15 19:42:16 +03:00
stylesheets = map tags.mkStylesheet [
"https://unpkg.com/purecss@2.0.6/build/pure-min.css"
"https://unpkg.com/purecss@2.0.6/build/grids-responsive-min.css"
2021-05-16 23:51:59 +03:00
"${baseurl}/site.css"
];
2022-07-31 07:17:41 +03:00
parsePostName = name: let
parts = splitString "_" name;
id = elemAt parts 1;
date = elemAt parts 0;
in {
inherit id date;
formatted = "${date} - ${id}";
};
2022-07-30 12:42:09 +03:00
renderPost = {
name,
value,
}: let
2022-07-31 07:17:41 +03:00
parsed = parsePostName name;
inherit (parsed) id date;
2022-07-30 12:42:09 +03:00
in
with tags;
article [
(a {
href = "#${id}";
class = "postheader";
} (h2 {inherit id;} id))
2022-07-31 07:17:41 +03:00
(h3 ("date: " + date))
2022-07-30 12:42:09 +03:00
value
];
2021-05-16 22:47:53 +03:00
pagesSection =
2022-07-31 07:17:41 +03:00
[
(tags.div {class = "pure-u-1";} (tags.a {
href = "${baseurl}/";
class = "pagelink";
} "home"))
]
++ (map
2022-07-30 12:42:09 +03:00
(name:
tags.div {class = "pure-u-1";} (tags.a {
href = "${baseurl}/${name}/";
class = "pagelink";
}
name))
(mapAttrsToList (name: _: name) pages))
++ [
(tags.div {class = "pure-u-1";} (tags.a {
2022-07-31 07:17:41 +03:00
href = "${baseurl}/posts/";
2022-07-30 12:42:09 +03:00
class = "pagelink";
} "posts"))
];
2022-07-31 07:17:41 +03:00
postsRendered = map renderPost posts;
postsLinks = with tags;
singleton
(ul (
map
(
post:
li (
a {href = "${baseurl}/${post.name}";}
(parsePostName post.name).formatted
)
)
posts
));
2022-07-30 12:42:09 +03:00
postsSectionContent = with tags;
[
(a {
href = "#posts";
class = "postheader";
} (h1 "posts"))
]
2022-07-31 07:17:41 +03:00
++ postsLinks;
2022-07-31 10:05:16 +03:00
sidebarSection = optionalString ((length pagesSection) > 0) (
2022-07-30 12:42:09 +03:00
with tags;
nav {class = "sidebar";} [
(div {class = "pure-g";} pagesSection)
]
);
2021-05-16 02:56:54 +03:00
2022-07-30 12:42:09 +03:00
mkPage = content:
with tags; ''
2021-05-16 23:51:59 +03:00
<!DOCTYPE html>
${html [
2022-07-30 12:42:09 +03:00
(head (stylesheets
++ [
(title config.title)
(meta {
name = "viewport";
content = "width=device-width, initial-scale=1";
})
]))
2022-07-31 10:05:16 +03:00
(body ''
2022-07-31 10:07:56 +03:00
${script "0"}
2022-07-31 10:05:16 +03:00
${sidebarSection}
${div {class = "content";} content}
'')
2021-05-16 23:51:59 +03:00
]}
'';
2021-05-15 21:29:30 +03:00
indexPage = mkPage (context.indexContent or postsSectionContent);
2022-07-31 07:17:41 +03:00
pagesAndPosts =
pages
// listToAttrs (
map (post: nameValuePair post.name (renderPost post)) posts
);
2022-07-30 12:42:09 +03:00
stylesheet = with utils.css; let
marginMobile = {
margin-left = "3%";
margin-right = "3%";
};
in
2021-05-16 02:56:54 +03:00
css [
2021-05-17 18:41:37 +03:00
(css (
(
mapAttrs'
2022-07-30 12:42:09 +03:00
(name: value: nameValuePair value {content = "\"${concatStrings (map (_: "#") (range 1 (toInt name)))} \"";})
(genAttrs (n: "h${toString n}:before") (map toString (range 1 6)))
)
// {
2021-05-17 18:41:37 +03:00
body = {
2022-07-30 12:42:09 +03:00
font-family = ["Raleway" "Helvetica" "Arial" "sans-serif"];
2021-05-17 18:41:37 +03:00
background = "#111111";
color = "#eeeeee";
};
pre = {
2022-07-30 12:42:09 +03:00
font-family = ["Iosevka Term" "Iosevka" "monospace"];
2021-05-17 18:41:37 +03:00
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%";
2022-07-31 08:34:09 +03:00
margin-right = "25%";
2021-05-17 18:41:37 +03:00
};
"nav.sidebar" = {
position = "fixed";
margin-left = "3%";
2022-07-30 14:28:37 +03:00
padding-top = 0;
2021-05-17 18:41:37 +03:00
z-index = 1000;
};
}
))
2021-05-16 02:56:54 +03:00
(media "max-width: 48em" {
2022-07-30 12:42:09 +03:00
"nav.sidebar" =
{
position = "relative";
margin-top = "5%";
}
// marginMobile;
"div.content" =
{
margin-top = 0;
}
// marginMobile;
2021-05-16 02:56:54 +03:00
})
];
2022-07-30 12:42:09 +03:00
in {
2021-05-16 02:56:54 +03:00
inherit stylesheets sidebarSection mkPage stylesheet;
2022-07-30 12:42:09 +03:00
site =
site
// {
"index.html" = indexPage;
2022-07-31 07:17:41 +03:00
"posts"."index.html" = mkPage postsSectionContent;
2022-07-30 12:42:09 +03:00
"404.html" = mkPage (tags.h1 "No such page");
"site.css" = stylesheet;
}
2022-07-31 07:17:41 +03:00
// (mapAttrs (name: value: {"index.html" = mkPage value;}) pagesAndPosts)
// optionalAttrs (context ? resources) {inherit (context) resources;};
2021-05-15 19:42:16 +03:00
}