make it all work

This commit is contained in:
tristan 2023-12-31 00:24:48 +00:00
parent 74670edee1
commit c10587f0c5
16 changed files with 335 additions and 66 deletions

View file

@ -11,25 +11,47 @@
}; };
nixite = import ./nixite/. {inherit pkgs;}; nixite = import ./nixite/. {inherit pkgs;};
in { in {
packages.${system} = with nixite; { packages.${system} = {
# default = nixite.mkSite (nixite.layout (nixite.md.readMd ./src/index.md)); default = with nixite;
default = nixite.mkSite { mkSite (site.applyStyle ./testing/src/style.css {
index = "index.html" = with elems; (doc
html.toHTML
(
elems.doc
[ [
(elems.title "Nixite") (title "Nixite")
] ]
(main [
(md.readMd ./testing/src/index.md)
(link "/blog" "blog")
(list [
"item 1"
"item 2"
"item 3"
])
]));
blog = {
"index.html" = with elems; (doc
[ [
(elems.h 1 "Nixite") (title "A post")
(elems.main
(elems.p "hello"))
] ]
); (main [
(p ''
This is a post
'')
(link "/" "Home")
]));
}; };
});
serve = nixite.serve self.packages.${system}.default; serve = nixite.serve self.packages.${system}.default;
test = let
test = import ./testing/import.nix;
in
pkgs.writeShellScriptBin "test" ''
${test ./testing/md.test.nix}
${test ./testing/html.test.nix}
${test ./testing/elems.test.nix}
${test ./testing/site.test.nix}
'';
}; };
}; };
} }

View file

@ -1,9 +1,10 @@
{pkgs, ...}: { {pkgs, ...}: {
mkSite = import ./site.nix {inherit pkgs;}; mkSite = import ./make-site.nix {inherit pkgs;};
serve = site: (pkgs.writeShellScriptBin "serve" '' serve = site: (pkgs.writeShellScriptBin "serve" ''
${pkgs.caddy}/bin/caddy file-server --root ${site} ${pkgs.caddy}/bin/caddy file-server --root ${site}
''); '');
md = import ./md.nix; md = import ./md.nix;
html = import ./html.nix; html = import ./html.nix;
elems = import ./elems.nix; elems = import ./elems.nix;
site = import ./site.nix;
} }

View file

@ -1,13 +1,33 @@
let let
html = import ./html.nix; html = import ./html.nix;
in { in {
p = child: html.tag "p" child; p = child:
main = child: html.tag "main" child; html.tag "p" {} child;
h = v: child: html.tag "h${toString v}" child;
title = child: html.tag "title" child; main = child:
html.tag "main" {} child;
h = v: child:
html.tag "h${toString v}" {} child;
title = child:
html.tag "title" {} child;
link = href: child:
html.tag "a" {inherit href;} child;
stylesheet = path:
html.tag "link" {
rel = "stylesheet";
href = path;
} "";
list = elems: html.tag "ul" {} (map (e: html.tag "li" {} e) elems);
doc = head: body: doc = head: body:
html.tag "html" [ assert builtins.isList head;
(html.tag "head" head) html.tag "html" {lang = "en";} [
(html.tag "body" body) (html.tag "head" {} head)
(html.tag "body" {} body)
]; ];
} }

View file

@ -4,15 +4,33 @@ rec {
then elem then elem
else if builtins.typeOf elem == "list" else if builtins.typeOf elem == "list"
then builtins.toString (map toHTML elem) then builtins.toString (map toHTML elem)
else ''<${elem.tag}>${toHTML elem.child}</${elem.tag}>''; else ''<${elem.tag} ${writeAttrs elem.attrs}>${toHTML elem.child}</${elem.tag}>'';
writeAttrs = attrs: writeAttrs = attrs:
toString builtins.attrValues ( toString (builtins.attrValues (
builtins.mapAttrs (key: value: ''${key}="${value}"'') attrs builtins.mapAttrs (key: value: ''${key}="${value}"'') attrs
); ));
tag = tag: child: { tag = tag: attrs: child: {
inherit tag child; inherit tag child attrs;
__toString = toHTML; __toString = toHTML;
}; };
addToHead = page: heads:
page
// {
child =
map
(
e:
if e.tag == "head"
then
e
// {
child = e.child ++ heads;
}
else e
)
page.child;
};
} }

16
nixite/make-site.nix Normal file
View file

@ -0,0 +1,16 @@
{pkgs, ...}: raw: let
site = import ./site.nix;
in
pkgs.stdenv.mkDerivation {
name = "site";
src = ./.;
buildPhase = ''
mkdir dist
${site.copyTo "dist" raw}
'';
installPhase = ''
cp -r dist $out
'';
}

View file

@ -1,26 +1,25 @@
rec { let
readMd = path: processMd (builtins.readFile path); elems = import ./elems.nix;
processMd = md: { in rec {
title = "idk"; readMd = md:
content = toString (map (c: if builtins.isPath md
if builtins.typeOf c == "string" then processMd (builtins.readFile md)
then processMdBlock c else processMd md;
else null) (builtins.split "\n\n" md)); processMd = md: (map (c:
}; if builtins.isString c
then mdBlock c
else "") (builtins.split "\n\n" md));
processMdBlock = block: p block; mdBlock = block: let
m = heading block;
h =
if m == null
then 0
else builtins.stringLength (builtins.elemAt m 0);
in
if m == null
then elems.p block
else elems.h h (builtins.elemAt m 1);
isHeading = block: builtins.match "#+ .*" block; heading = block: builtins.match "(#+) (.*)" block;
h = content: ''
<h1>
${toString content}
</h1>
'';
p = content: ''
<p>
${toString content}
</p>
'';
} }

View file

@ -1,12 +1,44 @@
{pkgs, ...}: site: let
pkgs.stdenv.mkDerivation { html = import ./html.nix;
name = "site"; elems = import ./elems.nix;
src = ./.; in rec {
buildPhase = '' applyStyle = style: site: (linkStyle site) // {"style.css" = style;};
mkdir dist
echo "${toString site.index}" > dist/index.html linkStyle = site: (builtins.mapAttrs (
''; name: content:
installPhase = '' if builtins.isAttrs content && content ? "__toString"
cp -r dist $out then html.addToHead content [(elems.stylesheet "/style.css")]
''; else if builtins.isAttrs content
then linkStyle content
else content
)
site);
copyTo = prefix: site:
builtins.toString (
builtins.attrValues (
builtins.mapAttrs (
name: content:
if builtins.isString content
then ''
cp ${builtins.toFile name content} ${prefix}/${name}
''
else if builtins.isPath content
then ''
cp ${content} ${prefix}/${name}
''
else if builtins.isAttrs content && content ? "__toString"
then ''
cp ${builtins.toFile name (toString content)} ${prefix}/${name}
''
else if builtins.isAttrs content
then ''
mkdir -p ${prefix}/${name}
${copyTo "${prefix}/${name}" content}
''
else throw "Site page must be string, path or attrset"
)
site
)
);
} }

View file

@ -1,3 +0,0 @@
# Hello World
Making a website in Nix? That sounds dumb.

53
testing/elems.test.nix Normal file
View file

@ -0,0 +1,53 @@
let
elems = import ../nixite/elems.nix;
html = import ../nixite/html.nix;
it = import ./it.nix;
in
with elems; [
(it "makes a p tag" {
expected = html.tag "p" {} "foobar";
actual = p "foobar";
})
(it "makes a main tag" {
expected = html.tag "main" {} ["yeet"];
actual = main ["yeet"];
})
(it "makes an h1 tag" {
expected = html.tag "h1" {} "foobar";
actual = h 1 "foobar";
})
(it "makes an h2 tag" {
expected = html.tag "h2" {} "foobar";
actual = h 2 "foobar";
})
(it "makes a title tag" {
expected = html.tag "title" {} "foobar";
actual = title "foobar";
})
(it "makes an a tag" {
expected = html.tag "a" {href = "https://example.com";} "example";
actual = link "https://example.com" "example";
})
(it "makes a stylesheet link" {
expected = html.tag "link" {
href = "/style";
rel = "stylesheet";
} "";
actual = stylesheet "/style";
})
(it "makes a list" {
expected = html.tag "ul" {} [
(html.tag "li" {} "foo")
(html.tag "li" {} "bar")
(html.tag "li" {} "baz")
];
actual = list ["foo" "bar" "baz"];
})
(it "makes an html doc" {
expected = html.tag "html" {lang = "en";} [
(html.tag "head" {} ["foo"])
(html.tag "body" {} "bar")
];
actual = doc ["foo"] "bar";
})
]

27
testing/html.test.nix Normal file
View file

@ -0,0 +1,27 @@
let
html = import ../nixite/html.nix;
it = import ./it.nix;
in
with html; [
(it "makes a p tag" {
actual = tag "p" {} "Hello";
expected = {
tag = "p";
attrs = {};
child = "Hello";
__toString = toHTML;
};
})
(it "applies style" (let
page = tag "html" {} [(tag "head" {} ["foo"])];
in {
actual = addToHead page ["bar"];
expected = {
tag = "html";
attrs = {};
child = [(tag "head" {} ["foo" "bar"])];
__toString = toHTML;
};
}))
]

5
testing/import.nix Normal file
View file

@ -0,0 +1,5 @@
path:
toString (map (v: ''
echo '${builtins.baseNameOf path} :: ${v}'
'')
(import path))

11
testing/it.nix Normal file
View file

@ -0,0 +1,11 @@
msg: {
actual,
expected,
}:
if actual == expected
then msg
else
throw
(builtins.toJSON {
inherit actual expected msg;
})

27
testing/md.test.nix Normal file
View file

@ -0,0 +1,27 @@
let
md = import ../nixite/md.nix;
elems = import ../nixite/elems.nix;
it = import ./it.nix;
in
with md; [
(assert heading "# heading 1" == ["#" "heading 1"]; "gets heading 1")
(assert heading "## subheading" == ["##" "subheading"]; "gets heading 2")
(assert heading "some paragraph" == null; "paragraph is heading 0")
(assert mdBlock "# heading 1" == elems.h 1 "heading 1"; "makes h1 tag")
(assert mdBlock "## subheading" == elems.h 2 "subheading"; "makes h2 tag")
(assert mdBlock "some paragraph" == elems.p "some paragraph"; "makes p tag")
(it "processes md block" {
actual = readMd ''
# foo bar
lorem ipsum
'';
expected = [
(elems.h 1 "foo bar")
""
(elems.p "lorem ipsum\n")
];
})
]

27
testing/site.test.nix Normal file
View file

@ -0,0 +1,27 @@
let
elems = import ../nixite/elems.nix;
site = import ../nixite/site.nix;
it = import ./it.nix;
in
with site; [
(it "applies a style" {
expected = {
"index.html" = with elems; (doc [(title "foobar") (stylesheet "/style.css")] [(main "something")]);
blog = {
"index.html" = with elems; (doc [(title "foobar") (stylesheet "/style.css")] [(main "blogy blog")]);
};
"style.css" = ''
this is a stylesheet
'';
};
actual =
applyStyle ''
this is a stylesheet
'' {
"index.html" = with elems; (doc [(title "foobar")] [(main "something")]);
blog = {
"index.html" = with elems; (doc [(title "foobar")] [(main "blogy blog")]);
};
};
})
]

11
testing/src/index.md Normal file
View file

@ -0,0 +1,11 @@
# Hello World
Making a website in Nix? That sounds dumb.
## but why?
great question!
### you are dumb.
yes I am.

3
testing/src/style.css Normal file
View file

@ -0,0 +1,3 @@
p {
padding: 1rem;
};