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

View file

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

View file

@ -64,15 +64,24 @@ in rec {
|| builtins.isList tag || builtins.isList tag
|| (tag ? __toString && builtins.isFunction tag.__toString)); || (tag ? __toString && builtins.isFunction tag.__toString));
addToHead = page: heads: addToHead = doc: heads:
page doc
// { // {
child = head = doc.head ++ heads;
map };
(e:
if e.tag == "head" document = {
then e // {child = e.child ++ heads;} head ? [],
else e) body ? [],
page.child; 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 then flatten page
else page) (builtins.attrValues site); 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 eachPage site
(content: html.addToHead content [(elems.Stylesheet "/style.css")]); (content: html.addToHead content [(elems.Stylesheet path)]);
eachPage = site: callback: (builtins.mapAttrs (name: content: eachPage = site: callback: (builtins.mapAttrs (name: content:
if builtins.isAttrs content && content ? "__toString" if builtins.isAttrs content && content ? "__toString"

View file

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

View file

@ -5,7 +5,8 @@
}: let }: let
html = import ../nixite/html.nix; html = import ../nixite/html.nix;
in in
with html; with html; [
(
describe "tag" [ describe "tag" [
(it "keeps info in the tag" (let (it "keeps info in the tag" (let
p = tag "p"; p = tag "p";
@ -140,16 +141,54 @@ in
expected = "<p on></p>"; 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 { in {
actual = addToHead page ["bar"]; actual = addToHead page ["bar"];
expected = { expected = html.document {
tag = "html"; head = ["foo" "bar"];
attrs = {};
child = [(tag "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; md = import ../nixite/md.nix;
elems = import ../nixite/elems.nix; elems = import ../nixite/elems.nix;
in in
with md; with md; [
(
describe "list" [ describe "list" [
(it "matches a list of one element" { (it "matches a list of one element" {
actual = list [ actual = list [
@ -72,7 +73,9 @@ in
expected = [str]; expected = [str];
})) }))
] ]
++ describe "process string" [ )
(
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.
@ -104,20 +107,26 @@ in
asString = true; asString = true;
}) })
] ]
++ describe "fix appendix" [ )
(
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" [ )
(
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" [ )
(
describe "recReadMd" [
(it "recursively reads dir" { (it "recursively reads dir" {
actual = recReadMd ./blog; actual = recReadMd ./blog;
expected = { expected = {
@ -127,7 +136,9 @@ in
asJSON = true; asJSON = true;
}) })
] ]
++ describe "recFixAppendix" [ )
(
describe "recFixAppendix" [
(it "recursively fixes filename" { (it "recursively fixes filename" {
actual = recFixAppendix { actual = recFixAppendix {
"index.md" = "something"; "index.md" = "something";
@ -139,7 +150,9 @@ in
}; };
}) })
] ]
++ describe "readDir" [ )
(
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 {
@ -148,3 +161,5 @@ in
}; };
}) })
] ]
)
]

View file

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

View file

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