html.nix/pkgs-lib.nix

98 lines
2.8 KiB
Nix
Raw Normal View History

2022-07-30 12:42:09 +03:00
{
utils,
pkgs,
}: let
2021-05-15 04:15:00 +03:00
pkgBin = name: "${pkgs.${name}}/bin/${name}";
2022-07-30 12:42:09 +03:00
mkServePathScript = path:
pkgs.writeScriptBin "serve" ''
2022-07-31 03:51:59 +03:00
${pkgs.nodePackages.http-server}/bin/http-server -c-1 ${path}
2022-07-30 12:42:09 +03:00
'';
2022-07-30 12:42:09 +03:00
mkSitePath = site: let
inherit (utils) recursiveAttrPaths concatStringsSep map;
inherit (pkgs.lib) mapAttrsRecursive init last getAttrFromPath;
2022-07-30 13:55:09 +03:00
convertToPath = path: value:
2022-07-30 13:53:42 +03:00
if builtins.isPath value
then value
else pkgs.writeText (concatStringsSep "-" path) value;
2022-07-30 12:42:09 +03:00
fileAttrPaths = recursiveAttrPaths site;
2022-07-30 13:55:09 +03:00
texts = mapAttrsRecursive convertToPath site;
2022-07-30 12:42:09 +03:00
mkCreateFileCmd = path: value: let p = concatStringsSep "/" (init path); in "mkdir -p \"$out/${p}\" && ln -s \"${value}\" \"$out/${p}/${last path}\"";
createFileCmds = map (path: mkCreateFileCmd path (getAttrFromPath path texts)) fileAttrPaths;
in
pkgs.runCommandLocal "site-path" {} ''
mkdir -p $out
${concatStringsSep "\n" createFileCmds}
'';
2021-05-15 19:42:16 +03:00
parseMarkdown = name: contents:
2022-07-30 12:42:09 +03:00
pkgs.runCommandLocal name {} ''
2021-05-17 18:06:05 +03:00
printf ${pkgs.lib.escapeShellArg contents} | ${pkgBin "pandoc"} -f gfm > $out
2021-05-15 19:42:16 +03:00
'';
2022-07-30 12:42:09 +03:00
in {
2021-05-15 19:42:16 +03:00
inherit mkServePathScript mkSitePath parseMarkdown;
2021-05-15 04:15:00 +03:00
mkServeFromSite = site: mkServePathScript (mkSitePath site);
2022-07-30 12:42:09 +03:00
mkSiteFrom = {
src,
templater,
local ? false,
}: let
inherit (utils) readDir readFile fromTOML mapAttrsToList sort elemAt;
inherit (pkgs.lib) nameValuePair head splitString pipe removeSuffix mapAttrs';
2021-05-15 19:42:16 +03:00
2022-07-30 12:42:09 +03:00
postsRendered = let
path = src + "/posts";
in
pipe (readDir path) [
(mapAttrsToList (
name: _:
2021-05-16 22:47:53 +03:00
nameValuePair
2022-07-30 12:42:09 +03:00
(head (splitString "." name))
(readFile (parseMarkdown name (readFile (path + "/${name}"))))
))
(sort (
p: op: let
extractDate = name: splitString "-" (head (splitString "_" name));
getPart = name: el: removeSuffix "0" (elemAt (extractDate name) el);
d = getPart p.name;
od = getPart op.name;
in
!(((d 0) > (od 0)) && ((d 1) > (od 1)) && ((d 2) > (od 2)))
))
];
pagesRendered = let
path = src + "/pages";
in
mapAttrs'
(
name: _:
nameValuePair
(head (splitString "." name))
(readFile (parseMarkdown name (readFile (path + "/${name}"))))
)
(readDir path);
siteConfig = fromTOML (readFile (src + "/config.toml"));
baseurl =
if local
then "http://localhost:8080"
else siteConfig.baseurl or (throw "Need baseurl");
2021-05-15 19:42:16 +03:00
2022-07-30 12:42:09 +03:00
context = {
inherit utils pkgs baseurl;
config = siteConfig;
posts = postsRendered;
pages = pagesRendered;
site = {
"robots.txt" = ''
User-agent: *
Allow: /
'';
2021-05-15 19:42:16 +03:00
};
2022-07-30 12:42:09 +03:00
};
in
2021-05-16 02:56:54 +03:00
(templater context).site;
}