update tix and add html document

This commit is contained in:
tristan 2024-01-03 13:19:41 +00:00
parent baa5b6854e
commit 93799cac12
9 changed files with 759 additions and 699 deletions

View file

@ -37,11 +37,11 @@
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1704267472,
"narHash": "sha256-2QfrGFhkXEXRFVL6Lp14rOxNOH17fnZlY/NEqXUp26s=",
"lastModified": 1704284214,
"narHash": "sha256-YOKyHDSf6XUYJgdy8CrTGN+kPk9JGePksGENxn/gXPI=",
"ref": "refs/heads/master",
"rev": "6f4955581b37d2ae079941104df486fd390abd95",
"revCount": 15,
"rev": "cc834eda99ecaff5466bc826ac5b99f36b69c537",
"revCount": 19,
"type": "git",
"url": "https://git.tristans.cloud/tristan/tix"
},

View file

@ -14,6 +14,7 @@
in
nixite
// {
formatter.${system} = pkgs.alejandra;
packages.${system} = {
raw = nixite.mkSite (let
readme = {
@ -60,6 +61,10 @@
./testing/style.test.nix
];
watch = tix.packages.${system}.watch;
watchpipe = tix.packages.${system}.watchpipe;
results = tix.packages.${system}.results;
dev = tix.watch {
cmd = "nix run .# --show-trace";
stop = "pkill caddy";

View file

@ -64,15 +64,24 @@ in rec {
|| builtins.isList tag
|| (tag ? __toString && builtins.isFunction tag.__toString));
addToHead = page: heads:
page
addToHead = doc: heads:
doc
// {
child =
map
(e:
if e.tag == "head"
then e // {child = e.child ++ heads;}
else e)
page.child;
head = doc.head ++ heads;
};
document = {
head ? [],
body ? [],
attrs ? {},
}: {
inherit head body attrs;
__toString = self: ''
<!DOCTYPE html>
${(tag "html" [
(tag "head" self.head)
(tag "body" self.body)
])}
'';
};
}

View file

@ -27,11 +27,11 @@ in rec {
then flatten page
else page) (builtins.attrValues site);
applyStyle = style: site: ((linkStyle site) // {"style.css" = style;});
applyStyle = style: site: ((linkStyle "/style.css" site) // {"style.css" = style;});
linkStyle = site:
linkStyle = path: site:
eachPage site
(content: html.addToHead content [(elems.Stylesheet "/style.css")]);
(content: html.addToHead content [(elems.Stylesheet path)]);
eachPage = site: callback: (builtins.mapAttrs (name: content:
if builtins.isAttrs content && content ? "__toString"

View file

@ -6,7 +6,8 @@
elems = import ../nixite/elems.nix;
html = import ../nixite/html.nix;
in
with elems;
with elems; [
(
describe "html tag" [
(it "makes a p tag" {
expected = html.tag "p" {} "foobar";
@ -89,3 +90,5 @@ in
asString = true;
})
]
)
]

View file

@ -5,7 +5,8 @@
}: let
html = import ../nixite/html.nix;
in
with html;
with html; [
(
describe "tag" [
(it "keeps info in the tag" (let
p = tag "p";
@ -140,16 +141,54 @@ in
expected = "<p on></p>";
})
]
++ describe "addToHead" [
(it "applies style" (let
page = tag "html" {} [(tag "head" {} ["foo"])];
)
(
describe "addToHead" [
(it "adds a string to the head" (let
page = html.document {
head = ["foo"];
};
in {
actual = addToHead page ["bar"];
expected = {
tag = "html";
attrs = {};
child = [(tag "head" {} ["foo" "bar"])];
expected = html.document {
head = ["foo" "bar"];
};
removeDunders = true;
asJSON = true;
}))
]
)
(
describe "document" [
(it "has a head and body" {
actual =
(document {
head = [];
})
.head;
expected = [];
})
(it "has a head and body" {
actual =
(document {
body = [];
})
.body;
expected = [];
})
(it "produces a valid html document" {
actual = document {
head = [];
body = [];
};
expected = ''
<!DOCTYPE html>
${(tag "html" [
(tag "head" [])
(tag "body" [])
])}
'';
asString = true;
})
]
)
]

View file

@ -6,7 +6,8 @@
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 [
@ -72,7 +73,9 @@ in
expected = [str];
}))
]
++ describe "process string" [
)
(
describe "process string" [
(it "processes whole string with all rules" {
actual = processStr ''
this text **may** *or may not* contain **bold** words *inside* it.
@ -104,20 +107,26 @@ in
asString = true;
})
]
++ describe "fix appendix" [
)
(
describe "fix appendix" [
(it "can fix file appendixes" {
actual = fixAppendix "index.md";
expected = "index.html";
})
]
++ describe "mdToPage" [
)
(
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" [
)
(
describe "recReadMd" [
(it "recursively reads dir" {
actual = recReadMd ./blog;
expected = {
@ -127,7 +136,9 @@ in
asJSON = true;
})
]
++ describe "recFixAppendix" [
)
(
describe "recFixAppendix" [
(it "recursively fixes filename" {
actual = recFixAppendix {
"index.md" = "something";
@ -139,7 +150,9 @@ in
};
})
]
++ describe "readDir" [
)
(
describe "readDir" [
(it "recursively translates md to html" {
actual = builtins.toJSON (readDir ./blog);
expected = builtins.toJSON {
@ -148,3 +161,5 @@ in
};
})
]
)
]

View file

@ -7,26 +7,41 @@
elems = import ../nixite/elems.nix;
site = import ../nixite/site.nix;
style = import ../nixite/style.nix;
my-site = {
"index.html" = html.document {
head = [(elems.title {} "foobar")];
body = [(elems.main {} "something")];
};
blog = {
"index.html" = html.document {
head = [(elems.title {} "foobar")];
body = elems.main {} "blogy blog";
};
};
};
in
with site;
with site; [
(
describe "linkStyle" [
(it "links a stylesheet" {
actual = linkStyle "my-styles.css" my-site;
expected = {
"index.html" = html.addToHead my-site."index.html" [(elems.Stylesheet "my-styles.css")];
blog = {
"index.html" = html.addToHead my-site.blog."index.html" [(elems.Stylesheet "my-styles.css")];
};
};
asJSON = true;
})
]
)
(
describe "applyStyle" [
(it "applies a style" {
expected = {
"index.html" = html.tag "html" {} [
(html.tag "head" {} [
(elems.title {} "foobar")
(elems.Stylesheet "/style.css")
])
(elems.main {} "something")
];
"index.html" = html.addToHead my-site."index.html" [(elems.Stylesheet "/style.css")];
blog = {
"index.html" = html.tag "html" {} [
(html.tag "head" {} [
(elems.title {} "foobar")
(elems.Stylesheet "/style.css")
])
(elems.main {} "blogy blog")
];
"index.html" = html.addToHead my-site.blog."index.html" [(elems.Stylesheet "/style.css")];
};
"style.css" = ''
this is a stylesheet
@ -35,63 +50,44 @@ in
actual =
applyStyle ''
this is a stylesheet
'' {
"index.html" = html.tag "html" {} [
(html.tag "head" {} [(elems.title {} "foobar")])
(elems.main {} "something")
];
blog = {
"index.html" = html.tag "html" {} [
(html.tag "head" {} [(elems.title {} "foobar")])
(elems.main {} "blogy blog")
];
};
};
''
my-site;
asJSON = true;
})
]
++ describe "applyFavicon" [
(it "applies a favicon" {
)
(
describe "applyFavicon" [
(it "applies a favicon" (
let
href = "my-favicon.ico";
favicon-link = elems.link {
rel = "shortcut icon";
inherit href;
};
in {
expected = {
"index.html" = elems.html {} [
(elems.head {} [
(elems.title {} "foobar")
(elems.link {
rel = "shortcut icon";
href = ./src/favicon.png;
})
])
(elems.main {} "something")
];
"index.html" =
my-site."index.html"
// {
head = my-site."index.html".head ++ [favicon-link];
};
blog = {
"index.html" = elems.html {} [
(elems.head {} [
(elems.title {} "foobar")
(elems.link {
rel = "shortcut icon";
href = ./src/favicon.png;
})
])
(elems.main {} "something")
];
"index.html" =
my-site.blog."index.html"
// {
head = my-site.blog."index.html".head ++ [favicon-link];
};
};
actual = applyFavicon ./src/favicon.png {
"index.html" = elems.html {} [
(elems.head {} [(elems.title {} "foobar")])
(elems.main {} "something")
];
blog = {
"index.html" = elems.html {} [
(elems.head {} [(elems.title {} "foobar")])
(elems.main {} "something")
];
};
};
asJSON = true;
})
actual =
applyFavicon href my-site;
}
))
]
++ describe "getStyles" [
)
(
describe "getStyles" [
(it "gets all styles" {
expected = {
"p" = {};
@ -109,7 +105,9 @@ in
removeDunders = true;
})
]
++ describe "getPaths" [
)
(
describe "getPaths" [
(it "gets top level paths" {
actual = getPaths {
something = "";
@ -131,7 +129,9 @@ in
};
})
]
++ describe "switchPaths" [
)
(
describe "switchPaths" [
(it "switches paths" {
actual = switchPaths {
something = "";
@ -145,7 +145,9 @@ in
};
})
]
++ describe "extractPaths" [
)
(
describe "extractPaths" [
(it "extracts paths" {
actual = extractPaths {
something = "";
@ -160,7 +162,8 @@ in
};
})
]
++ describe "switchLinks" [
)
(describe "switchLinks" [
(it "switches links" (let
coolPage = {
type = "link";
@ -179,8 +182,8 @@ in
a-list = [{thingy = "/static/cool-page";}];
};
}))
]
++ describe "getLinks" [
])
(describe "getLinks" [
(it "gets links" (let
coolPage = {
type = "link";
@ -202,8 +205,8 @@ in
"page2" = "stuff";
};
}))
]
++ describe "extractLinks" [
])
(describe "extractLinks" [
(it "extracts links" (let
coolPage = {
type = "link";
@ -231,8 +234,8 @@ in
};
};
}))
]
++ describe "copyTo" [
])
(describe "copyTo" [
(it "copies all the files" {
actual = copyTo "." {
page = "this is a page";
@ -269,60 +272,37 @@ in
actual = copyTo "." {page = 5;};
throws = true;
})
]
++ describe "prepare" [
])
(describe "prepare" [
(it "prepares the site" {
actual = prepare {favicon = ./src/favicon.png;} {
"index.html" = elems.html {} [
(elems.head {} [(elems.title {} "foobar")])
(elems.main {} [
(elems.a {
href = {
type = "link";
name = "a-page";
content = "this is another page";
};
} "A Page")
])
];
blog = {
"index.html" = elems.html {} [
(elems.head {} [(elems.title {} "foobar")])
(elems.main {} "something")
];
};
};
expected = {
"index.html" = elems.html {} [
(elems.head {} [
(elems.title {} "foobar")
(elems.link {
actual = prepare {favicon = ./src/favicon.png;} my-site;
expected = let
my-favicon-link = elems.link {
rel = "shortcut icon";
href = "/static/favicon.png";
})
(elems.Stylesheet "/style.css")
])
(elems.main {} [(elems.a {href = "/static/a-page";} "A Page")])
];
};
my-style = elems.Stylesheet "/style.css";
in {
"index.html" = html.document {
head =
my-site."index.html".head
++ [my-favicon-link my-style];
body = my-site."index.html".body;
};
blog = {
"index.html" = elems.html {} [
(elems.head {} [
(elems.title {} "foobar")
(elems.link {
rel = "shortcut icon";
href = "/static/favicon.png";
})
(elems.Stylesheet "/style.css")
])
(elems.main {} "something")
];
"index.html" = html.document {
body = my-site.blog."index.html".body;
head =
my-site.blog."index.html".head
++ [my-favicon-link my-style];
};
};
static = {
"favicon.png" = ./src/favicon.png;
"a-page" = "this is another page";
};
"style.css" = "";
};
asJSON = true;
})
])
]

View file

@ -5,7 +5,8 @@
}: 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" {};
@ -39,7 +40,9 @@ in
actual = style.getStyle (para "");
}))
]
++ describe "component" [
)
(
describe "component" [
(it "applies class" (let
attrs = {style = {foo = "bar";};};
para = style.component elems.p "para" attrs;
@ -73,7 +76,9 @@ in
actual = (para2 "").attrs.class;
}))
]
++ describe "getStyles" [
)
(
describe "getStyles" [
(it "fetches style recursively" (let
s = {
"p" = {};
@ -132,7 +137,9 @@ in
removeDunders = true;
}))
]
++ describe "stylesToString" [
)
(
describe "stylesToString" [
(it "converts styles to string" (let
s = {
"p" = {};
@ -151,3 +158,5 @@ in
actual = style.stylesToString s;
}))
]
)
]