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

View file

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

View file

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

View file

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

View file

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

View file

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