feat: get (very) basic ssg ready

This commit is contained in:
dusk 2021-05-15 19:42:16 +03:00
parent 39f6eb39be
commit e98176b28c
Signed by: dusk
GPG Key ID: 1D8F8FAF2294D6EA
8 changed files with 89 additions and 15 deletions

View File

@ -0,0 +1 @@
title = "test site"

View File

@ -0,0 +1,3 @@
#### another document
Woo hoo!

View File

@ -0,0 +1,8 @@
Welcome!
### This is a test document
```
echo "some code!"
echo "more!"
```

View File

@ -3,23 +3,31 @@
let
utils = import ./utils.nix;
tags = import ./tags.nix { format = false; inherit utils; };
lib = {
tags = import ./tags.nix { inherit utils; };
templaters = {
basic = import ./templaters/basic.nix;
};
};
pkgsLib = (final: prev: {
htmlNix = import ./pkgs-lib.nix { pkgs = prev; inherit utils; };
htmlNix = import ./pkgs-lib.nix { pkgs = prev; utils = utils // { inherit (lib) tags; }; };
});
in
{
lib = {
inherit tags;
};
inherit lib;
overlays = {
inherit pkgsLib;
};
examples = {
tags = import ./examples/tags.nix tags;
serve = import ./examples/serve.nix { inherit tags pkgsLib; }; # needs --impure
siteServe =
let inherit (import <nixpkgs> { overlays = [ pkgsLib ]; }) htmlNix; in
htmlNix.mkServeFromSite (htmlNix.mkSiteFrom { src = ./examples/site; templater = lib.templaters.basic; }); # needs --impure
tags = import ./examples/tags.nix lib.tags;
serve = import ./examples/serve.nix { inherit (lib) tags; inherit pkgsLib; }; # needs --impure
};
};
}

View File

@ -21,9 +21,37 @@ let
mkdir -p $out
${concatStringsSep "\n" createFileCmds}
'';
parseMarkdown = name: contents:
pkgs.runCommand name { } ''
printf "${contents}" | ${pkgBin "lowdown"} -o $out -
'';
in
{
inherit mkServePathScript mkSitePath;
inherit mkServePathScript mkSitePath parseMarkdown;
mkServeFromSite = site: mkServePathScript (mkSitePath site);
mkSiteFrom = { src, templater }:
let
inherit (utils) readDir readFile fromTOML;
inherit (pkgs.lib) mapAttrs' nameValuePair head splitString;
postsRendered =
let path = src + "/posts"; in
mapAttrs'
(name: _:
nameValuePair
(head (splitString "." name))
(parseMarkdown name (readFile (path + "/${name}")))
)
(readDir path);
siteConfig = fromTOML (readFile (src + "/config.toml"));
context = {
inherit utils pkgs;
config = siteConfig;
posts = postsRendered;
};
in
templater context;
}

View File

@ -1,17 +1,16 @@
{ format ? false, utils }:
{ utils }:
let
inherit (utils) concatStrings mapAttrsToList genAttrs isAttrs isList;
fmt = if format then "\n " else "";
inherit (utils) concatStrings mapAttrsToList genAttrs isAttrs isList range toString;
evalAttrs = attrs: concatStrings (mapAttrsToList (name: value: " ${name}=\"${value}\"") attrs);
evalChildren = children: if isList children then concatStrings children else children;
tag = name: maybeAttrs:
if isAttrs maybeAttrs
then (children: "<${name}${evalAttrs maybeAttrs}>${fmt}${evalChildren children}${fmt}</${name}>")
then (children: "<${name}${evalAttrs maybeAttrs}>\n ${evalChildren children}\n</${name}>\n")
else tag name { } maybeAttrs;
tags = (genAttrs tag [ "html" "head" "body" "div" "p" "a" ]);
tagsToGen = [ "html" "head" "body" "div" "p" "a" "title" "meta" "code" "pre" ] ++ (map (n: "h${toString n}") (range 1 6));
tags = genAttrs tag tagsToGen;
in
tags // {
inherit tag;

21
templaters/basic.nix Normal file
View File

@ -0,0 +1,21 @@
{ utils, posts, pkgs, config, ... }@context:
let
inherit (utils) readFile mapAttrsToList;
inherit (pkgs.lib) flatten;
renderPost = name: value: with utils.tags; [
(h2 ("# " + name))
(readFile value)
];
allPosts = flatten (mapAttrsToList renderPost posts);
in
{
"index.html" = with utils.tags;
html [
(head [
(title config.title)
])
(body allPosts)
];
}

View File

@ -1,5 +1,5 @@
let
inherit (builtins) isAttrs isList map any concatMap concatStringsSep listToAttrs;
inherit (builtins) isAttrs isList map any concatMap concatStringsSep listToAttrs genList;
mapAttrsToList = f: attrs: map (name: f name attrs.${name}) (builtins.attrNames attrs);
in
@ -27,4 +27,10 @@ in
concatStrings = concatStringsSep "";
genAttrs = f: names: listToAttrs (map (n: { name = n; value = (f n); }) names);
range = first: last:
if first > last then
[ ]
else
genList (n: first + n) (last - first + 1);
} // builtins