feat: get (very) basic ssg ready
This commit is contained in:
parent
39f6eb39be
commit
e98176b28c
1
examples/site/config.toml
Normal file
1
examples/site/config.toml
Normal file
@ -0,0 +1 @@
|
||||
title = "test site"
|
3
examples/site/posts/Testing more.md
Normal file
3
examples/site/posts/Testing more.md
Normal file
@ -0,0 +1,3 @@
|
||||
#### another document
|
||||
|
||||
Woo hoo!
|
8
examples/site/posts/test.md
Normal file
8
examples/site/posts/test.md
Normal file
@ -0,0 +1,8 @@
|
||||
Welcome!
|
||||
|
||||
### This is a test document
|
||||
|
||||
```
|
||||
echo "some code!"
|
||||
echo "more!"
|
||||
```
|
22
flake.nix
22
flake.nix
@ -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
|
||||
};
|
||||
};
|
||||
}
|
||||
|
30
pkgs-lib.nix
30
pkgs-lib.nix
@ -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;
|
||||
}
|
||||
|
11
tags.nix
11
tags.nix
@ -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
21
templaters/basic.nix
Normal 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)
|
||||
];
|
||||
}
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user