html.nix/pkgs-lib.nix

67 lines
2.2 KiB
Nix
Raw Normal View History

{ utils, pkgs }:
2021-05-15 04:15:00 +03:00
let
pkgBin = name: "${pkgs.${name}}/bin/${name}";
mkServePathScript = path: pkgs.writeScriptBin "serve" ''
#!${pkgs.stdenv.shell}
${pkgBin "miniserve"} --index index.html ${path}
'';
mkSitePath = site:
let
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;
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.runCommand "site-path" { } ''
mkdir -p $out
${concatStringsSep "\n" createFileCmds}
'';
2021-05-15 19:42:16 +03:00
parseMarkdown = name: contents:
pkgs.runCommand name { } ''
printf "${contents}" | ${pkgBin "lowdown"} -o $out -
'';
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-15 19:42:16 +03:00
mkSiteFrom = { src, templater }:
let
inherit (utils) readDir readFile fromTOML mapAttrsToList sort elemAt;
inherit (pkgs.lib) nameValuePair head splitString pipe removeSuffix;
2021-05-15 19:42:16 +03:00
postsRendered =
let path = src + "/posts"; in
pipe (readDir path) [
(mapAttrsToList (name: _:
2021-05-15 19:42:16 +03:00
nameValuePair
(head (splitString "." name))
(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)))
))
];
2021-05-15 19:42:16 +03:00
siteConfig = fromTOML (readFile (src + "/config.toml"));
context = {
inherit utils pkgs;
config = siteConfig;
posts = postsRendered;
};
in
2021-05-16 02:56:54 +03:00
(templater context).site;
}