improvements

This commit is contained in:
dusk 2022-07-30 12:42:09 +03:00
parent 5bca7064e4
commit 8c562cd257
Signed by: dusk
GPG Key ID: 1D8F8FAF2294D6EA
9 changed files with 303 additions and 226 deletions

11
css.nix
View File

@ -1,15 +1,16 @@
{ utils }: {utils}: let
let
inherit (utils) mapAttrsToList concatStringsSep isList toString map; inherit (utils) mapAttrsToList concatStringsSep isList toString map;
evalCssValue = value: if isList value then concatStringsSep ", " (map toString value) else toString value; evalCssValue = value:
if isList value
then concatStringsSep ", " (map toString value)
else toString value;
evalInner = inner: concatStringsSep " " (mapAttrsToList (name: value: "${name}: ${evalCssValue value};") inner); evalInner = inner: concatStringsSep " " (mapAttrsToList (name: value: "${name}: ${evalCssValue value};") inner);
css = maybeAttrs: css = maybeAttrs:
if isList maybeAttrs if isList maybeAttrs
then concatStringsSep " " maybeAttrs then concatStringsSep " " maybeAttrs
else concatStringsSep " " (mapAttrsToList (name: inner: "${name} { ${evalInner inner} }") maybeAttrs); else concatStringsSep " " (mapAttrsToList (name: inner: "${name} { ${evalInner inner} }") maybeAttrs);
in in {
{
inherit css; inherit css;
media = rule: inner: '' media = rule: inner: ''

View File

@ -1,6 +1,8 @@
{ tags, pkgs }: {
with pkgs.htmlNix; tags,
let pkgs,
}:
with pkgs.htmlNix; let
index = with tags; index = with tags;
html [ html [
(body [ (body [
@ -17,6 +19,9 @@ let
]) ])
]; ];
site = { "index.html" = index; "ex.html" = ex; }; site = {
"index.html" = index;
"ex.html" = ex;
};
in in
mkServeFromSite site mkServeFromSite site

View File

@ -1,10 +1,12 @@
{ pkgs, lib }: {
let pkgs,
lib,
}: let
inherit (pkgs) htmlNix; inherit (pkgs) htmlNix;
src = ./site; src = ./site;
in in
htmlNix.mkServeFromSite (htmlNix.mkSiteFrom { htmlNix.mkServeFromSite (htmlNix.mkSiteFrom {
inherit src; inherit src;
templater = lib.templaters.basic; templater = lib.templaters.basic;
local = true; local = true;
}) })

View File

@ -1,7 +1,8 @@
tags: with tags; tags:
html [ with tags;
html [
(body [ (body [
(p "Hello,") (p "Hello,")
(p "world!") (p "world!")
]) ])
] ]

View File

@ -4,15 +4,18 @@
flakeUtils.url = "github:numtide/flake-utils"; flakeUtils.url = "github:numtide/flake-utils";
}; };
outputs = { self, flakeUtils, nixpkgs }: outputs = {
let self,
flakeUtils,
nixpkgs,
}: let
utils = import ./utils.nix; utils = import ./utils.nix;
lib = { lib = {
# Convert Nix expressions to HTML # Convert Nix expressions to HTML
tags = import ./tags.nix { inherit utils; }; tags = import ./tags.nix {inherit utils;};
# Convert Nix expressions to CSS # Convert Nix expressions to CSS
css = import ./css.nix { inherit utils; }; css = import ./css.nix {inherit utils;};
# Various site templaters # Various site templaters
templaters = { templaters = {
@ -21,27 +24,26 @@
}; };
}; };
overlay = final: prev: { outputs = flakeUtils.lib.eachDefaultSystem (system: let
htmlNix = (import ./pkgs-lib.nix { pkgs = prev; utils = utils // { inherit (lib) tags css; }; }) // lib; pkgs = nixpkgs.legacyPackagse.${system};
in {
lib = {
pkgsLib = import ./pkgs-lib.nix {
inherit pkgs;
utils = utils // {inherit (lib) tags css;};
}; };
in
{ inherit overlay; } //
(flakeUtils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; overlays = [ overlay ]; };
in
{
lib = lib // {
pkgsLib = import ./pkgs-lib.nix { inherit pkgs; utils = utils // { inherit (lib) tags css; }; };
}; };
apps = with flakeUtils.lib; { apps = with flakeUtils.lib; {
site = mkApp { site = mkApp {
drv = import ./examples/site.nix { inherit lib pkgs; }; drv = import ./examples/site.nix {inherit lib pkgs;};
name = "serve"; name = "serve";
}; };
basicServe = mkApp { basicServe = mkApp {
drv = import ./examples/serve.nix { inherit (lib) tags; inherit pkgs; }; drv = import ./examples/serve.nix {
inherit (lib) tags;
inherit pkgs;
};
name = "serve"; name = "serve";
}; };
}; };
@ -49,5 +51,7 @@
examples = { examples = {
tags = import ./examples/tags.nix lib.tags; tags = import ./examples/tags.nix lib.tags;
}; };
})); });
in
outputs // {lib = outputs.lib // lib;};
} }

View File

@ -1,15 +1,17 @@
{ utils, pkgs }: {
let utils,
pkgs,
}: let
pkgBin = name: "${pkgs.${name}}/bin/${name}"; pkgBin = name: "${pkgs.${name}}/bin/${name}";
mkServePathScript = path: pkgs.writeScriptBin "serve" '' mkServePathScript = path:
pkgs.writeScriptBin "serve" ''
#!${pkgs.stdenv.shell} #!${pkgs.stdenv.shell}
cd ${path} cd ${path}
${pkgBin "caddy"} run --config Caddyfile ${pkgBin "caddy"} run --config Caddyfile
''; '';
mkSitePath = site: mkSitePath = site: let
let
inherit (utils) recursiveAttrPaths concatStringsSep map; inherit (utils) recursiveAttrPaths concatStringsSep map;
inherit (pkgs.lib) mapAttrsRecursive init last getAttrFromPath; inherit (pkgs.lib) mapAttrsRecursive init last getAttrFromPath;
@ -18,35 +20,39 @@ let
mkCreateFileCmd = path: value: let p = concatStringsSep "/" (init path); in "mkdir -p \"$out/${p}\" && ln -s \"${value}\" \"$out/${p}/${last path}\""; 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; createFileCmds = map (path: mkCreateFileCmd path (getAttrFromPath path texts)) fileAttrPaths;
in in
pkgs.runCommandLocal "site-path" { } '' pkgs.runCommandLocal "site-path" {} ''
mkdir -p $out mkdir -p $out
${concatStringsSep "\n" createFileCmds} ${concatStringsSep "\n" createFileCmds}
''; '';
parseMarkdown = name: contents: parseMarkdown = name: contents:
pkgs.runCommandLocal name { } '' pkgs.runCommandLocal name {} ''
printf ${pkgs.lib.escapeShellArg contents} | ${pkgBin "pandoc"} -f gfm > $out printf ${pkgs.lib.escapeShellArg contents} | ${pkgBin "pandoc"} -f gfm > $out
''; '';
in in {
{
inherit mkServePathScript mkSitePath parseMarkdown; inherit mkServePathScript mkSitePath parseMarkdown;
mkServeFromSite = site: mkServePathScript (mkSitePath site); mkServeFromSite = site: mkServePathScript (mkSitePath site);
mkSiteFrom = { src, templater, local ? false }: mkSiteFrom = {
let src,
templater,
local ? false,
}: let
inherit (utils) readDir readFile fromTOML mapAttrsToList sort elemAt; inherit (utils) readDir readFile fromTOML mapAttrsToList sort elemAt;
inherit (pkgs.lib) nameValuePair head splitString pipe removeSuffix mapAttrs'; inherit (pkgs.lib) nameValuePair head splitString pipe removeSuffix mapAttrs';
postsRendered = postsRendered = let
let path = src + "/posts"; in path = src + "/posts";
in
pipe (readDir path) [ pipe (readDir path) [
(mapAttrsToList (name: _: (mapAttrsToList (
name: _:
nameValuePair nameValuePair
(head (splitString "." name)) (head (splitString "." name))
(readFile (parseMarkdown name (readFile (path + "/${name}")))) (readFile (parseMarkdown name (readFile (path + "/${name}"))))
)) ))
(sort (p: op: (sort (
let p: op: let
extractDate = name: splitString "-" (head (splitString "_" name)); extractDate = name: splitString "-" (head (splitString "_" name));
getPart = name: el: removeSuffix "0" (elemAt (extractDate name) el); getPart = name: el: removeSuffix "0" (elemAt (extractDate name) el);
d = getPart p.name; d = getPart p.name;
@ -55,17 +61,22 @@ in
!(((d 0) > (od 0)) && ((d 1) > (od 1)) && ((d 2) > (od 2))) !(((d 0) > (od 0)) && ((d 1) > (od 1)) && ((d 2) > (od 2)))
)) ))
]; ];
pagesRendered = pagesRendered = let
let path = src + "/pages"; in path = src + "/pages";
in
mapAttrs' mapAttrs'
(name: _: (
name: _:
nameValuePair nameValuePair
(head (splitString "." name)) (head (splitString "." name))
(readFile (parseMarkdown name (readFile (path + "/${name}")))) (readFile (parseMarkdown name (readFile (path + "/${name}"))))
) )
(readDir path); (readDir path);
siteConfig = fromTOML (readFile (src + "/config.toml")); siteConfig = fromTOML (readFile (src + "/config.toml"));
baseurl = if local then "http://localhost:8080" else siteConfig.baseurl or (throw "Need baseurl"); baseurl =
if local
then "http://localhost:8080"
else siteConfig.baseurl or (throw "Need baseurl");
context = { context = {
inherit utils pkgs baseurl; inherit utils pkgs baseurl;

View File

@ -1,23 +1,31 @@
{ utils }: {utils}: let
let
inherit (utils) concatStrings mapAttrsToList genAttrs isAttrs isList range toString; inherit (utils) concatStrings mapAttrsToList genAttrs isAttrs isList range toString;
evalAttrs = attrs: concatStrings (mapAttrsToList (name: value: " ${name}=\"${value}\"") attrs); evalAttrs = attrs: concatStrings (mapAttrsToList (name: value: " ${name}=\"${value}\"") attrs);
evalChildren = children: if isList children then concatStrings children else children; evalChildren = children:
if isList children
then concatStrings children
else children;
tag = name: maybeAttrs: tag = name: maybeAttrs:
if isAttrs maybeAttrs if isAttrs maybeAttrs
then (children: "<${name}${evalAttrs maybeAttrs}>${evalChildren children}</${name}>") then (children: "<${name}${evalAttrs maybeAttrs}>${evalChildren children}</${name}>")
else tag name { } maybeAttrs; else tag name {} maybeAttrs;
noChildrenTag = name: attrs: "<${name} ${evalAttrs attrs}>"; noChildrenTag = name: attrs: "<${name} ${evalAttrs attrs}>";
tagsToGen = [ "html" "head" "body" "div" "p" "a" "title" "code" "pre" "nav" "article" ] ++ (map (n: "h${toString n}") (range 1 6)); tagsToGen = ["html" "head" "body" "div" "p" "a" "title" "code" "pre" "nav" "article"] ++ (map (n: "h${toString n}") (range 1 6));
tags = genAttrs tag tagsToGen; tags = genAttrs tag tagsToGen;
noChildrenTagsToGen = [ "link" "meta" ]; noChildrenTagsToGen = ["link" "meta"];
noChildrenTags = genAttrs noChildrenTag noChildrenTagsToGen; noChildrenTags = genAttrs noChildrenTag noChildrenTagsToGen;
in in
tags // noChildrenTags // { tags
// noChildrenTags
// {
inherit tag; inherit tag;
mkLink = url: tags.a { href = url; }; mkLink = url: tags.a {href = url;};
mkStylesheet = url: noChildrenTags.link { rel = "stylesheet"; href = url; }; mkStylesheet = url:
} noChildrenTags.link {
rel = "stylesheet";
href = url;
};
}

View File

@ -1,5 +1,13 @@
{ utils, posts, pkgs, config, pages, site, baseurl, ... }@context: {
let utils,
posts,
pkgs,
config,
pages,
site,
baseurl,
...
} @ context: let
inherit (utils) readFile mapAttrsToList mapAttrs tags fetchGit map elemAt foldl' concatStrings genAttrs toString; inherit (utils) readFile mapAttrsToList mapAttrs tags fetchGit map elemAt foldl' concatStrings genAttrs toString;
inherit (pkgs.lib) optional length splitString nameValuePair toInt range mapAttrs'; inherit (pkgs.lib) optional length splitString nameValuePair toInt range mapAttrs';
@ -9,48 +17,76 @@ let
"${baseurl}/site.css" "${baseurl}/site.css"
]; ];
renderPost = { name, value }: renderPost = {
let name,
value,
}: let
parts = splitString "_" name; parts = splitString "_" name;
id = elemAt parts 1; id = elemAt parts 1;
in in
with tags; article [ with tags;
(a { href = "#${id}"; class = "postheader"; } (h2 { inherit id; } id)) article [
(a {
href = "#${id}";
class = "postheader";
} (h2 {inherit id;} id))
(h3 ("date: " + (elemAt parts 0))) (h3 ("date: " + (elemAt parts 0)))
value value
]; ];
pagesSection = pagesSection =
(map (map
(name: tags.div { class = "pure-u-1"; } (tags.a { href = "${baseurl}/${name}/"; class = "pagelink"; } name)) (name:
(mapAttrsToList (name: _: name) pages)) ++ [ (tags.div { class = "pure-u-1"; } (tags.a { href = "${baseurl}/"; class = "pagelink"; } "posts")) ]; tags.div {class = "pure-u-1";} (tags.a {
href = "${baseurl}/${name}/";
class = "pagelink";
}
name))
(mapAttrsToList (name: _: name) pages))
++ [
(tags.div {class = "pure-u-1";} (tags.a {
href = "${baseurl}/";
class = "pagelink";
} "posts"))
];
postsSectionContent = with tags; [ postsSectionContent = with tags;
(a { href = "#posts"; class = "postheader"; } (h1 "posts")) [
] ++ (map renderPost posts); (a {
href = "#posts";
class = "postheader";
} (h1 "posts"))
]
++ (map renderPost posts);
sidebarSection = optional ((length pagesSection) > 0) ( sidebarSection = optional ((length pagesSection) > 0) (
with tags; nav { class = "sidebar"; } ([ with tags;
(a { href = "#pages"; class = "postheader"; } (h1 "pages")) nav {class = "sidebar";} [
(div { class = "pure-g"; } pagesSection) (a {
]) href = "#pages";
class = "postheader";
} (h1 "pages"))
(div {class = "pure-g";} pagesSection)
]
); );
mkPage = content: with tags; mkPage = content:
'' with tags; ''
<!DOCTYPE html> <!DOCTYPE html>
${html [ ${html [
(head (stylesheets ++ [ (head (stylesheets
++ [
(title config.title) (title config.title)
(meta { name = "viewport"; content = "width=device-width, initial-scale=1"; }) (meta {
name = "viewport";
content = "width=device-width, initial-scale=1";
})
])) ]))
(body (sidebarSection ++ [ (div { class = "content"; } content) ])) (body (sidebarSection ++ [(div {class = "content";} content)]))
]} ]}
''; '';
stylesheet = stylesheet = with utils.css; let
with utils.css;
let
marginMobile = { marginMobile = {
margin-left = "3%"; margin-left = "3%";
margin-right = "3%"; margin-right = "3%";
@ -60,16 +96,17 @@ let
(css ( (css (
( (
mapAttrs' mapAttrs'
(name: value: nameValuePair value { content = "\"${concatStrings (map (_: "#") (range 1 (toInt name)))} \""; }) (name: value: nameValuePair value {content = "\"${concatStrings (map (_: "#") (range 1 (toInt name)))} \"";})
(genAttrs (n: "h${toString n}:before") (map toString (range 1 6))) (genAttrs (n: "h${toString n}:before") (map toString (range 1 6)))
) // { )
// {
body = { body = {
font-family = [ "Raleway" "Helvetica" "Arial" "sans-serif" ]; font-family = ["Raleway" "Helvetica" "Arial" "sans-serif"];
background = "#111111"; background = "#111111";
color = "#eeeeee"; color = "#eeeeee";
}; };
pre = { pre = {
font-family = [ "Iosevka Term" "Iosevka" "monospace" ]; font-family = ["Iosevka Term" "Iosevka" "monospace"];
background = "#171A21"; background = "#171A21";
color = "#eeeeee"; color = "#eeeeee";
}; };
@ -101,22 +138,28 @@ let
} }
)) ))
(media "max-width: 48em" { (media "max-width: 48em" {
"nav.sidebar" = { "nav.sidebar" =
{
position = "relative"; position = "relative";
margin-top = "5%"; margin-top = "5%";
} // marginMobile; }
"div.content" = { // marginMobile;
"div.content" =
{
margin-top = 0; margin-top = 0;
} // marginMobile; }
// marginMobile;
}) })
]; ];
in in {
{
inherit stylesheets sidebarSection mkPage stylesheet; inherit stylesheets sidebarSection mkPage stylesheet;
site = site // { site =
site
// {
"index.html" = mkPage postsSectionContent; "index.html" = mkPage postsSectionContent;
"404.html" = mkPage (tags.h1 "No such page"); "404.html" = mkPage (tags.h1 "No such page");
"site.css" = stylesheet; "site.css" = stylesheet;
} // (mapAttrs (name: value: { "index.html" = mkPage value; }) pages); }
// (mapAttrs (name: value: {"index.html" = mkPage value;}) pages);
} }

View File

@ -3,34 +3,36 @@ let
mapAttrsToList = f: attrs: map (name: f name attrs.${name}) (builtins.attrNames attrs); mapAttrsToList = f: attrs: map (name: f name attrs.${name}) (builtins.attrNames attrs);
in in
{ {
inherit mapAttrsToList; inherit mapAttrsToList;
recursiveAttrPaths = set: recursiveAttrPaths = set: let
let
flattenIfHasList = x: flattenIfHasList = x:
if (isList x) && (any isList x) if (isList x) && (any isList x)
then concatMap flattenIfHasList x then concatMap flattenIfHasList x
else [ x ]; else [x];
recurse = path: set: recurse = path: set: let
let g = name: value:
g =
name: value:
if isAttrs value if isAttrs value
then recurse (path ++ [ name ]) value then recurse (path ++ [name]) value
else path ++ [ name ]; else path ++ [name];
in in
mapAttrsToList g set; mapAttrsToList g set;
in in
flattenIfHasList (recurse [ ] set); flattenIfHasList (recurse [] set);
concatStrings = concatStringsSep ""; concatStrings = concatStringsSep "";
genAttrs = f: names: listToAttrs (map (n: { name = n; value = (f n); }) names); genAttrs = f: names:
listToAttrs (map (n: {
name = n;
value = f n;
})
names);
range = first: last: range = first: last:
if first > last then if first > last
[ ] then []
else else genList (n: first + n) (last - first + 1);
genList (n: first + n) (last - first + 1); }
} // builtins // builtins