make it all work
This commit is contained in:
parent
74670edee1
commit
c10587f0c5
46
flake.nix
46
flake.nix
|
@ -11,25 +11,47 @@
|
|||
};
|
||||
nixite = import ./nixite/. {inherit pkgs;};
|
||||
in {
|
||||
packages.${system} = with nixite; {
|
||||
# default = nixite.mkSite (nixite.layout (nixite.md.readMd ./src/index.md));
|
||||
default = nixite.mkSite {
|
||||
index =
|
||||
html.toHTML
|
||||
(
|
||||
elems.doc
|
||||
packages.${system} = {
|
||||
default = with nixite;
|
||||
mkSite (site.applyStyle ./testing/src/style.css {
|
||||
"index.html" = with 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")
|
||||
(elems.main
|
||||
(elems.p "hello"))
|
||||
(title "A post")
|
||||
]
|
||||
);
|
||||
(main [
|
||||
(p ''
|
||||
This is a post
|
||||
'')
|
||||
(link "/" "Home")
|
||||
]));
|
||||
};
|
||||
});
|
||||
|
||||
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}
|
||||
'';
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
{pkgs, ...}: {
|
||||
mkSite = import ./site.nix {inherit pkgs;};
|
||||
mkSite = import ./make-site.nix {inherit pkgs;};
|
||||
serve = site: (pkgs.writeShellScriptBin "serve" ''
|
||||
${pkgs.caddy}/bin/caddy file-server --root ${site}
|
||||
'');
|
||||
md = import ./md.nix;
|
||||
html = import ./html.nix;
|
||||
elems = import ./elems.nix;
|
||||
site = import ./site.nix;
|
||||
}
|
||||
|
|
|
@ -1,13 +1,33 @@
|
|||
let
|
||||
html = import ./html.nix;
|
||||
in {
|
||||
p = child: html.tag "p" child;
|
||||
main = child: html.tag "main" child;
|
||||
h = v: child: html.tag "h${toString v}" child;
|
||||
title = child: html.tag "title" child;
|
||||
p = child:
|
||||
html.tag "p" {} 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:
|
||||
html.tag "html" [
|
||||
(html.tag "head" head)
|
||||
(html.tag "body" body)
|
||||
assert builtins.isList head;
|
||||
html.tag "html" {lang = "en";} [
|
||||
(html.tag "head" {} head)
|
||||
(html.tag "body" {} body)
|
||||
];
|
||||
}
|
||||
|
|
|
@ -4,15 +4,33 @@ rec {
|
|||
then elem
|
||||
else if builtins.typeOf elem == "list"
|
||||
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:
|
||||
toString builtins.attrValues (
|
||||
toString (builtins.attrValues (
|
||||
builtins.mapAttrs (key: value: ''${key}="${value}"'') attrs
|
||||
);
|
||||
));
|
||||
|
||||
tag = tag: child: {
|
||||
inherit tag child;
|
||||
tag = tag: attrs: child: {
|
||||
inherit tag child attrs;
|
||||
__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
16
nixite/make-site.nix
Normal 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
|
||||
'';
|
||||
}
|
|
@ -1,26 +1,25 @@
|
|||
rec {
|
||||
readMd = path: processMd (builtins.readFile path);
|
||||
processMd = md: {
|
||||
title = "idk";
|
||||
content = toString (map (c:
|
||||
if builtins.typeOf c == "string"
|
||||
then processMdBlock c
|
||||
else null) (builtins.split "\n\n" md));
|
||||
};
|
||||
let
|
||||
elems = import ./elems.nix;
|
||||
in rec {
|
||||
readMd = md:
|
||||
if builtins.isPath md
|
||||
then processMd (builtins.readFile md)
|
||||
else processMd 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;
|
||||
|
||||
h = content: ''
|
||||
<h1>
|
||||
${toString content}
|
||||
</h1>
|
||||
'';
|
||||
|
||||
p = content: ''
|
||||
<p>
|
||||
${toString content}
|
||||
</p>
|
||||
'';
|
||||
heading = block: builtins.match "(#+) (.*)" block;
|
||||
}
|
||||
|
|
|
@ -1,12 +1,44 @@
|
|||
{pkgs, ...}: site:
|
||||
pkgs.stdenv.mkDerivation {
|
||||
name = "site";
|
||||
src = ./.;
|
||||
buildPhase = ''
|
||||
mkdir dist
|
||||
echo "${toString site.index}" > dist/index.html
|
||||
'';
|
||||
installPhase = ''
|
||||
cp -r dist $out
|
||||
'';
|
||||
let
|
||||
html = import ./html.nix;
|
||||
elems = import ./elems.nix;
|
||||
in rec {
|
||||
applyStyle = style: site: (linkStyle site) // {"style.css" = style;};
|
||||
|
||||
linkStyle = site: (builtins.mapAttrs (
|
||||
name: content:
|
||||
if builtins.isAttrs content && content ? "__toString"
|
||||
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
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
# Hello World
|
||||
|
||||
Making a website in Nix? That sounds dumb.
|
53
testing/elems.test.nix
Normal file
53
testing/elems.test.nix
Normal 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
27
testing/html.test.nix
Normal 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
5
testing/import.nix
Normal file
|
@ -0,0 +1,5 @@
|
|||
path:
|
||||
toString (map (v: ''
|
||||
echo '${builtins.baseNameOf path} :: ${v}'
|
||||
'')
|
||||
(import path))
|
11
testing/it.nix
Normal file
11
testing/it.nix
Normal 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
27
testing/md.test.nix
Normal 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
27
testing/site.test.nix
Normal 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
11
testing/src/index.md
Normal 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
3
testing/src/style.css
Normal file
|
@ -0,0 +1,3 @@
|
|||
p {
|
||||
padding: 1rem;
|
||||
};
|
Loading…
Reference in a new issue