2021-05-15 04:11:35 +03:00
|
|
|
{ utils, pkgs }:
|
2021-05-15 04:15:00 +03:00
|
|
|
let
|
|
|
|
pkgBin = name: "${pkgs.${name}}/bin/${name}";
|
|
|
|
|
2021-05-15 04:02:06 +03:00
|
|
|
mkServePathScript = path: pkgs.writeScriptBin "serve" ''
|
|
|
|
#!${pkgs.stdenv.shell}
|
2021-05-17 03:26:48 +03:00
|
|
|
cd ${path}
|
|
|
|
${pkgBin "caddy"} run --config Caddyfile
|
2021-05-15 02:39:30 +03:00
|
|
|
'';
|
2021-05-15 04:02:06 +03:00
|
|
|
|
|
|
|
mkSitePath = site:
|
|
|
|
let
|
2021-05-15 04:11:35 +03:00
|
|
|
inherit (utils) recursiveAttrPaths concatStringsSep map;
|
|
|
|
inherit (pkgs.lib) mapAttrsRecursive init last getAttrFromPath;
|
|
|
|
|
|
|
|
fileAttrPaths = recursiveAttrPaths site;
|
|
|
|
texts = mapAttrsRecursive (path: value: pkgs.writeText (concatStringsSep "-" path) value) site;
|
2021-05-17 18:07:02 +03:00
|
|
|
mkCreateFileCmd = path: value: let p = concatStringsSep "/" (init path); in "mkdir -p \"$out/${p}\" && ln -s \"${value}\" \"$out/${p}/${last path}\"";
|
2021-05-15 04:11:35 +03:00
|
|
|
createFileCmds = map (path: mkCreateFileCmd path (getAttrFromPath path texts)) fileAttrPaths;
|
2021-05-15 04:02:06 +03:00
|
|
|
in
|
2022-02-17 01:35:47 +03:00
|
|
|
pkgs.runCommandLocal "site-path" { } ''
|
2021-05-15 04:02:06 +03:00
|
|
|
mkdir -p $out
|
2021-05-15 04:11:35 +03:00
|
|
|
${concatStringsSep "\n" createFileCmds}
|
2021-05-15 04:02:06 +03:00
|
|
|
'';
|
2021-05-15 19:42:16 +03:00
|
|
|
|
|
|
|
parseMarkdown = name: contents:
|
2022-02-17 01:35:47 +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
|
|
|
'';
|
2021-05-15 04:15:00 +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);
|
2021-05-17 00:08:42 +03:00
|
|
|
mkSiteFrom = { src, templater, local ? false }:
|
2021-05-15 19:42:16 +03:00
|
|
|
let
|
2021-05-16 03:57:35 +03:00
|
|
|
inherit (utils) readDir readFile fromTOML mapAttrsToList sort elemAt;
|
2021-05-16 22:47:53 +03:00
|
|
|
inherit (pkgs.lib) nameValuePair head splitString pipe removeSuffix mapAttrs';
|
2021-05-15 19:42:16 +03:00
|
|
|
|
|
|
|
postsRendered =
|
|
|
|
let path = src + "/posts"; in
|
2021-05-16 03:57:35 +03:00
|
|
|
pipe (readDir path) [
|
|
|
|
(mapAttrsToList (name: _:
|
2021-05-15 19:42:16 +03:00
|
|
|
nameValuePair
|
|
|
|
(head (splitString "." name))
|
2021-05-16 22:47:53 +03:00
|
|
|
(readFile (parseMarkdown name (readFile (path + "/${name}"))))
|
2021-05-16 03:57:35 +03:00
|
|
|
))
|
|
|
|
(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
|
2021-05-16 04:25:22 +03:00
|
|
|
!(((d 0) > (od 0)) && ((d 1) > (od 1)) && ((d 2) > (od 2)))
|
2021-05-16 03:57:35 +03:00
|
|
|
))
|
|
|
|
];
|
2021-05-16 22:47:53 +03:00
|
|
|
pagesRendered =
|
|
|
|
let path = src + "/pages"; in
|
|
|
|
mapAttrs'
|
|
|
|
(name: _:
|
|
|
|
nameValuePair
|
|
|
|
(head (splitString "." name))
|
|
|
|
(readFile (parseMarkdown name (readFile (path + "/${name}"))))
|
|
|
|
)
|
|
|
|
(readDir path);
|
2021-05-15 19:42:16 +03:00
|
|
|
siteConfig = fromTOML (readFile (src + "/config.toml"));
|
2021-05-17 03:26:48 +03:00
|
|
|
baseurl = if local then "http://localhost:8080" else siteConfig.baseurl or (throw "Need baseurl");
|
2021-05-15 19:42:16 +03:00
|
|
|
|
|
|
|
context = {
|
2021-05-16 23:51:59 +03:00
|
|
|
inherit utils pkgs baseurl;
|
2021-05-15 19:42:16 +03:00
|
|
|
config = siteConfig;
|
|
|
|
posts = postsRendered;
|
2021-05-16 22:47:53 +03:00
|
|
|
pages = pagesRendered;
|
2021-05-16 23:51:59 +03:00
|
|
|
site = {
|
|
|
|
"robots.txt" = ''
|
|
|
|
User-agent: *
|
|
|
|
Allow: /
|
|
|
|
'';
|
2021-05-17 03:26:48 +03:00
|
|
|
"Caddyfile" = ''
|
|
|
|
${baseurl}
|
|
|
|
|
|
|
|
handle_errors {
|
|
|
|
rewrite * /{http.error.status_code}.html
|
|
|
|
file_server
|
|
|
|
}
|
|
|
|
file_server
|
|
|
|
'';
|
2021-05-16 23:51:59 +03:00
|
|
|
};
|
2021-05-15 19:42:16 +03:00
|
|
|
};
|
|
|
|
in
|
2021-05-16 02:56:54 +03:00
|
|
|
(templater context).site;
|
2021-05-15 02:39:30 +03:00
|
|
|
}
|