use describe, apply alejandra

This commit is contained in:
tristan 2024-01-03 07:52:35 +00:00
parent 3f16ae203a
commit 819efaddf9
7 changed files with 791 additions and 759 deletions

View file

@ -37,11 +37,11 @@
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1704192724,
"narHash": "sha256-Cx9os5FQU4UTjxt/uE1trPHxegXyhvV/wSiIiBWTDt0=",
"lastModified": 1704202884,
"narHash": "sha256-+Zbr66+jP5Rdn95JAtKw25eyUnhNxS3eUOyIv01MLB8=",
"ref": "refs/heads/master",
"rev": "b800bb46955d69cf7693d7c788a4d624eff2717f",
"revCount": 3,
"rev": "32a3950d16e8a1c9413940d940732115cbe13bbd",
"revCount": 7,
"type": "git",
"url": "https://git.tristans.cloud/tristan/tix"
},

View file

@ -1,12 +1,10 @@
let
elems = import ./elems.nix;
in rec {
readMd = md:
processMd = md:
if builtins.isPath md
then processMd (builtins.readFile md)
else processMd md;
processMd = processStr;
then processStr (builtins.readFile md)
else processStr md;
recReadMd = root:
assert builtins.isPath root;
@ -32,11 +30,7 @@ in rec {
readDir = root: recFixAppendix (recReadMd root);
mdToPage = md:
elems.Doc {} [[(elems.title {} "markdown file")] (readMd md)];
list = block:
matchThen "(- .+\n)*(- .+\n?)"
block (m: elems.List (splitList block));
elems.Doc {} [[(elems.title {} "markdown file")] (processMd md)];
splitList = block:
map listItem (builtins.filter (s: builtins.isString s && s != "")
@ -111,15 +105,7 @@ in rec {
href = builtins.elemAt m 2;
text = builtins.elemAt m 1;
in (elems.a href text)))
(rule "(.*\n)([^-][^\n]+\n)((- [^\n]+\n)+)(.*)" (
l: (elems.ul (basicRule "(.*\n)?- ([^\n]+)\n(.*)" (m:
elems.li (basicRule "()\\[(.)](.*)" (check:
elems.input {
type = "checkbox";
checked = check != " ";
disabled = true;
}) [m])) [(builtins.elemAt l 2)]))
))
list
(basicRule "(.*\n\n)?(.+)\n(.*)?" elems.p)
(basicRule "(.*\n\n)?```(.*)```(.*)?" (elems.textarea {readonly = true;}))
(basicRule (containsBreak "###### ([^\n]+)") (elems.h6))
@ -131,6 +117,16 @@ in rec {
(basicRule (containsBreak "<(${linkmatcher})>") (m: elems.a m m))
];
list = rule "(.*\n)([^-][^\n]+\n)((- [^\n]+\n)+)(.*)" (
l: (elems.ul (basicRule "(.*\n)?- ([^\n]+)\n(.*)" (m:
elems.li (basicRule "()\\[(.)](.*)" (check:
elems.input {
type = "checkbox";
checked = check != " ";
disabled = true;
}) [m])) [(builtins.elemAt l 2)]))
);
linkmatcher = "[-[:alnum:].%?&#=:/]+";
contains = matcher: "(.*)?${matcher}(.*)";
wrap = matcher: contains "${matcher}([^${matcher}]+)${matcher}";

View file

@ -1,9 +1,13 @@
{it, ...}: let
{
describe,
it,
...
}: let
elems = import ../nixite/elems.nix;
style = import ../nixite/style.nix;
html = import ../nixite/html.nix;
in
with elems; [
with elems;
describe "html tag" [
(it "makes a p tag" {
expected = html.tag "p" {} "foobar";
actual = p {} "foobar";
@ -47,15 +51,16 @@ in
asString = true;
})
(it "lets you extend the a tag" {
(it "lets you extend the a tag" (let
extA = a {class = ["linky"];};
in {
expected = ["linky"];
actual =
((style.component a "linky" {}) "https://example.com"
"example")
(extA "https://example.com" "example")
.attrs
.class;
asString = true;
})
}))
(it "makes a stylesheet link" {
expected = html.tag "link" {

View file

@ -1,7 +1,12 @@
{it, ...}: let
{
describe,
it,
...
}: let
html = import ../nixite/html.nix;
in
with html; [
with html;
describe "tag" [
(it "keeps info in the tag" (let
p = tag "p";
in {
@ -127,6 +132,15 @@ in
expected = ''<p class="class1 class2">Hello</p>'';
})
(it "renders on / off attrs" {
actual = toString (tag "p" {
on = true;
off = false;
});
expected = "<p on></p>";
})
]
++ describe "addToHead" [
(it "applies style" (let
page = tag "html" {} [(tag "head" {} ["foo"])];
in {
@ -138,12 +152,4 @@ in
};
removeDunders = true;
}))
(it "renders on / off attrs" {
actual = toString (tag "p" {
on = true;
off = false;
});
expected = "<p on></p>";
})
]

View file

@ -1,39 +1,41 @@
{it, ...}: let
{
describe,
it,
...
}: let
md = import ../nixite/md.nix;
elems = import ../nixite/elems.nix;
in
with md; [
with md;
describe "list" [
(it "matches a list of one element" {
actual = list ''
actual = list [
''
- something
'';
expected = {
matched = true;
block = elems.List ["something"];
};
asJSON = true;
''
];
expected = [(elems.List ["something"])];
})
(it "makes a list of many elements" {
actual = list ''
actual = list [
''
- something
- something else
'';
expected = {
matched = true;
block = elems.List ["something" "something else"];
};
asJSON = true;
''
];
expected = [(elems.List ["something" "something else"])];
})
(it "makes a list of many checkboxes" {
actual = list ''
actual = list [
''
- [ ] something
- [X] something else
'';
expected = {
matched = true;
block = elems.List [
''
];
expected = [
(elems.List [
[
(elems.input {
type = "checkbox";
@ -50,32 +52,27 @@ in
} "")
"something else"
]
])
];
};
asJSON = true;
})
(it "matches a list with no whitespace around" {
actual = list ''
actual = list [
''
- something
- something else'';
expected = {
matched = true;
block = elems.List ["something" "something else"];
};
asJSON = true;
- something else''
];
expected = [(elems.List ["something" "something else"])];
})
(it "doesnt match not a list" (let
str = "blah blah";
in {
actual = list str;
expected = {
matched = false;
block = str;
};
actual = list [str];
expected = [str];
}))
]
++ describe "process string" [
(it "processes whole string with all rules" {
actual = processStr ''
this text **may** *or may not* contain **bold** words *inside* it.
@ -94,7 +91,7 @@ in
})
(it "makes paragraphs" {
actual = readMd ''
actual = processStr ''
lorem ipsum
dolor sit
@ -106,18 +103,21 @@ in
</p><p >foo bar</p>'';
asString = true;
})
]
++ describe "fix appendix" [
(it "can fix file appendixes" {
actual = fixAppendix "index.md";
expected = "index.html";
})
]
++ describe "mdToPage" [
(it "converts markdown to a page" {
actual = toString (mdToPage ./blog/index.md) + "\n\n"; # inflation
expected = builtins.readFile ./out/index.html;
asString = true;
})
]
++ describe "recReadMd" [
(it "recursively reads dir" {
actual = recReadMd ./blog;
expected = {
@ -126,7 +126,8 @@ in
};
asJSON = true;
})
]
++ describe "recFixAppendix" [
(it "recursively fixes filename" {
actual = recFixAppendix {
"index.md" = "something";
@ -137,7 +138,8 @@ in
dir = {"index.html" = "something else";};
};
})
]
++ describe "readDir" [
(it "recursively translates md to html" {
actual = builtins.toJSON (readDir ./blog);
expected = builtins.toJSON {

View file

@ -1,10 +1,15 @@
{it, ...}: let
{
describe,
it,
...
}: let
html = import ../nixite/html.nix;
elems = import ../nixite/elems.nix;
site = import ../nixite/site.nix;
style = import ../nixite/style.nix;
in
with site; [
with site;
describe "applyStyle" [
(it "applies a style" {
expected = {
"index.html" = html.tag "html" {} [
@ -44,7 +49,8 @@ in
};
asJSON = true;
})
]
++ describe "applyFavicon" [
(it "applies a favicon" {
expected = {
"index.html" = elems.html {} [
@ -84,7 +90,8 @@ in
};
asJSON = true;
})
]
++ describe "getStyles" [
(it "gets all styles" {
expected = {
"p" = {};
@ -101,7 +108,8 @@ in
});
removeDunders = true;
})
]
++ describe "getPaths" [
(it "gets top level paths" {
actual = getPaths {
something = "";
@ -122,7 +130,8 @@ in
"favicon.png" = ./src/favicon.png;
};
})
]
++ describe "switchPaths" [
(it "switches paths" {
actual = switchPaths {
something = "";
@ -135,7 +144,8 @@ in
a-list = [{thingy = "/static/index.md";}];
};
})
]
++ describe "extractPaths" [
(it "extracts paths" {
actual = extractPaths {
something = "";
@ -149,7 +159,8 @@ in
static = {"index.md" = ./src/index.md;};
};
})
]
++ describe "switchLinks" [
(it "switches links" (let
coolPage = {
type = "link";
@ -168,7 +179,8 @@ in
a-list = [{thingy = "/static/cool-page";}];
};
}))
]
++ describe "getLinks" [
(it "gets links" (let
coolPage = {
type = "link";
@ -190,7 +202,8 @@ in
"page2" = "stuff";
};
}))
]
++ describe "extractLinks" [
(it "extracts links" (let
coolPage = {
type = "link";
@ -218,7 +231,8 @@ in
};
};
}))
]
++ describe "copyTo" [
(it "copies all the files" {
actual = copyTo "." {
page = "this is a page";
@ -255,7 +269,8 @@ in
actual = copyTo "." {page = 5;};
throws = true;
})
]
++ describe "prepare" [
(it "prepares the site" {
actual = prepare {favicon = ./src/favicon.png;} {
"index.html" = elems.html {} [

View file

@ -1,7 +1,12 @@
{it, ...}: let
{
describe,
it,
...
}: let
style = import ../nixite/style.nix;
elems = import ../nixite/elems.nix;
in [
in
describe "getStyle" [
(it "fetches empty style" (let
para = style.component elems.p "para" {};
in {
@ -23,7 +28,19 @@ in [
actual = style.getStyle (para "");
}))
(it "appliess class" (let
(it "fetches style for class" (let
s = {foo = "bar";};
para = style.component elems.p "para" {style = s;};
in {
expected = {
"p" = {};
"p.para" = s;
};
actual = style.getStyle (para "");
}))
]
++ describe "component" [
(it "applies class" (let
attrs = {style = {foo = "bar";};};
para = style.component elems.p "para" attrs;
in {
@ -42,17 +59,21 @@ in [
actual = (para "").attrs.class;
}))
(it "fetches style for class" (let
s = {foo = "bar";};
para = style.component elems.p "para" {style = s;};
in {
expected = {
(it "extends styled tags classes" (let
s = {
"p" = {};
"p.para" = s;
"div" = {};
"p.para" = {foo = "bar";};
"p.para.oof" = {oof = "yes";};
};
actual = style.getStyle (para "");
para = style.component elems.p "para" {style = s."p.para";};
para2 = style.component para "oof" {style = s."p.para.oof";};
in {
expected = ["para" "oof"];
actual = (para2 "").attrs.class;
}))
]
++ describe "getStyles" [
(it "fetches style recursively" (let
s = {
"p" = {};
@ -98,6 +119,20 @@ in [
removeDunders = true;
}))
(it "extends styled tags" (let
s = {
"p.para" = {foo = "bar";};
"p.oof" = {oof = "yes";};
};
para = style.component elems.p "para" {style = s."p.para";};
para2 = style.component para "oof" {style = s."p.oof";};
in {
expected = s;
actual = style.getStyles (para2 "");
removeDunders = true;
}))
]
++ describe "stylesToString" [
(it "converts styles to string" (let
s = {
"p" = {};
@ -115,31 +150,4 @@ in [
'';
actual = style.stylesToString s;
}))
(it "extends styled tags" (let
s = {
"p.para" = {foo = "bar";};
"p.oof" = {oof = "yes";};
};
para = style.component elems.p "para" {style = s."p.para";};
para2 = style.component para "oof" {style = s."p.oof";};
in {
expected = s;
actual = style.getStyles (para2 "");
removeDunders = true;
}))
(it "extends styled tags classes" (let
s = {
"p" = {};
"div" = {};
"p.para" = {foo = "bar";};
"p.para.oof" = {oof = "yes";};
};
para = style.component elems.p "para" {style = s."p.para";};
para2 = style.component para "oof" {style = s."p.para.oof";};
in {
expected = ["para" "oof"];
actual = (para2 "").attrs.class;
}))
]
]