use styled components and improve error handling

This commit is contained in:
tristan 2023-12-31 04:27:09 +00:00
parent 9d0d54c2e4
commit 29f79c67d8
10 changed files with 117 additions and 86 deletions

View file

@ -3,6 +3,8 @@
A static site generator made in Nix. A static site generator made in Nix.
This is currently very experimental / subject to change.
## Why ## Why
Clearly there aren't already enough web frameworks out there. Clearly there aren't already enough web frameworks out there.

View file

@ -21,7 +21,7 @@
packages.${system} = { packages.${system} = {
default = nixite.mkSite (nixite.site.applyStyle ./testing/src/style.css { default = nixite.mkSite (nixite.site.applyStyle ./testing/src/style.css {
"index.html" = with nixite.elems; (doc "index.html" = with nixite.elems; (doc [
[ [
(title "Nixite") (title "Nixite")
] ]
@ -33,9 +33,10 @@
"item 2" "item 2"
"item 3" "item 3"
]) ])
])); ])
]);
blog = { blog = {
"index.html" = with nixite.elems; (doc "index.html" = with nixite.elems; (doc [
[ [
(title "A post") (title "A post")
] ]
@ -44,7 +45,8 @@
This is a post This is a post
'') '')
(link "/" "Home") (link "/" "Home")
])); ])
]);
}; };
}); });

View file

@ -1,27 +1,35 @@
let let
html = import ./html.nix; html = import ./html.nix;
in { s = import ./style.nix;
p = child: in
html.tag "p" {} child; (s.styled "p" "p" {} {})
(s.styled "div" "div" {} {})
div = child: (s.styled "section" "section" {} {})
html.tag "div" {} child; (s.styled "span" "span" {} {})
(s.styled "main" "main" {} {})
section = child: (s.styled "article" "article" {} {})
html.tag "section" {} child; (s.styled "title" "title" {} {})
(s.styled "h1" "h1" {} {})
span = child: (s.styled "h2" "h2" {} {})
html.tag "span" {} child; (s.styled "h3" "h3" {} {})
(s.styled "list" "ul" {
main = child: __child = child:
html.tag "main" {} child; assert builtins.isList child; (map (html.tag "li" {}) child);
} {})
(s.styled "doc" "html" {
__child = child:
assert builtins.isList child;
assert builtins.length child == 2;
assert builtins.isList (builtins.elemAt child 0); [
(html.tag "head" {} (builtins.elemAt child 0))
(html.tag "body" {} (builtins.elemAt child 1))
];
lang = "en";
} {})
// {
h = v: child: h = v: child:
html.tag "h${toString v}" {} child; html.tag "h${toString v}" {} child;
title = child:
html.tag "title" {} child;
link = href: child: link = href: child:
html.tag "a" {inherit href;} child; html.tag "a" {inherit href;} child;
@ -30,13 +38,4 @@ in {
rel = "stylesheet"; rel = "stylesheet";
href = path; href = path;
} ""; } "";
}
list = elems: html.tag "ul" {} (map (e: html.tag "li" {} e) elems);
doc = head: body:
assert builtins.isList head;
html.tag "html" {lang = "en";} [
(html.tag "head" {} head)
(html.tag "body" {} body)
];
}

View file

@ -9,8 +9,10 @@ rec {
writeAttrs = attrs: writeAttrs = attrs:
toString (builtins.attrValues ( toString (builtins.attrValues (
builtins.mapAttrs (key: value: builtins.mapAttrs (key: value:
if (builtins.substring 0 2 key) == "__" then "" if (builtins.substring 0 2 key) == "__"
else ''${key}="${toString value}"'') attrs then ""
else ''${key}="${toString value}"'')
attrs
)); ));
tag = tag: attrs: child: { tag = tag: attrs: child: {

View file

@ -18,7 +18,7 @@ in rec {
else builtins.stringLength (builtins.elemAt m 0); else builtins.stringLength (builtins.elemAt m 0);
in in
if m == null if m == null
then elems.p block then elems.p {} block
else elems.h h (builtins.elemAt m 1); else elems.h h (builtins.elemAt m 1);
heading = block: builtins.match "(#+) (.*)" block; heading = block: builtins.match "(#+) (.*)" block;

View file

@ -8,7 +8,10 @@ let
// {style = self.style + new.style;}; // {style = self.style + new.style;};
}; };
mkStyle = identifier: styles: if styles == {} then "" else '' mkStyle = identifier: styles:
if styles == {}
then ""
else ''
${identifier} { ${identifier} {
${toString (builtins.attrValues ( ${toString (builtins.attrValues (
builtins.mapAttrs ( builtins.mapAttrs (
@ -33,11 +36,16 @@ let
) )
+ id; + id;
in { in {
styled = name: tag: cprops: styles: styled = name: tag: cprops: styles: let
let
custom = cprops.__child or (child: child); custom = cprops.__child or (child: child);
in{ in
${name} = props: child: (html.tag tag (props // cprops) (custom child)); {
${name} = props:
if builtins.isAttrs props
then (child: (html.tag tag (props // cprops) (custom child)))
else if builtins.isString props || builtins.isList props
then (html.tag tag cprops (custom props))
else throw "Call element with attributes and child.";
style = mkStyle (mkIdentifier tag cprops) styles; style = mkStyle (mkIdentifier tag cprops) styles;
} }

View file

@ -6,23 +6,23 @@ in
with elems; [ with elems; [
(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";
}) })
(it "makes a div tag" { (it "makes a div tag" {
expected = html.tag "div" {} "foobar"; expected = html.tag "div" {} "foobar";
actual = div "foobar"; actual = div {} "foobar";
}) })
(it "makes a section tag" { (it "makes a section tag" {
expected = html.tag "section" {} "foobar"; expected = html.tag "section" {} "foobar";
actual = section "foobar"; actual = section {} "foobar";
}) })
(it "makes a span tag" { (it "makes a span tag" {
expected = html.tag "span" {} "foobar"; expected = html.tag "span" {} "foobar";
actual = span "foobar"; actual = span {} "foobar";
}) })
(it "makes a main tag" { (it "makes a main tag" {
expected = html.tag "main" {} ["yeet"]; expected = html.tag "main" {} ["yeet"];
actual = main ["yeet"]; actual = main {} ["yeet"];
}) })
(it "makes an h1 tag" { (it "makes an h1 tag" {
expected = html.tag "h1" {} "foobar"; expected = html.tag "h1" {} "foobar";
@ -34,7 +34,7 @@ in
}) })
(it "makes a title tag" { (it "makes a title tag" {
expected = html.tag "title" {} "foobar"; expected = html.tag "title" {} "foobar";
actual = title "foobar"; actual = title {} "foobar";
}) })
(it "makes an a tag" { (it "makes an a tag" {
expected = html.tag "a" {href = "https://example.com";} "example"; expected = html.tag "a" {href = "https://example.com";} "example";
@ -48,18 +48,21 @@ in
actual = stylesheet "/style"; actual = stylesheet "/style";
}) })
(it "makes a list" { (it "makes a list" {
expected = html.tag "ul" {} [ expected = toString (html.tag "ul" {} [
(html.tag "li" {} "foo") (html.tag "li" {} "foo")
(html.tag "li" {} "bar") (html.tag "li" {} "bar")
(html.tag "li" {} "baz") (html.tag "li" {} "baz")
]; ]);
actual = list ["foo" "bar" "baz"]; actual = toString (list {} ["foo" "bar" "baz"]);
}) })
(it "makes an html doc" { (it "makes an html doc" {
expected = html.tag "html" {lang = "en";} [ expected = toString (html.tag "html" {
__child = "";
lang = "en";
} [
(html.tag "head" {} ["foo"]) (html.tag "head" {} ["foo"])
(html.tag "body" {} "bar") (html.tag "body" {} "bar")
]; ]);
actual = doc ["foo"] "bar"; actual = toString (doc {} [["foo"] "bar"]);
}) })
] ]

View file

@ -10,7 +10,7 @@ in
(assert mdBlock "# heading 1" == elems.h 1 "heading 1"; "makes h1 tag") (assert mdBlock "# heading 1" == elems.h 1 "heading 1"; "makes h1 tag")
(assert mdBlock "## subheading" == elems.h 2 "subheading"; "makes h2 tag") (assert mdBlock "## subheading" == elems.h 2 "subheading"; "makes h2 tag")
(assert mdBlock "some paragraph" == elems.p "some paragraph"; "makes p tag") (assert mdBlock "some paragraph" == elems.p {} "some paragraph"; "makes p tag")
(it "processes md block" { (it "processes md block" {
actual = readMd '' actual = readMd ''
@ -21,7 +21,7 @@ in
expected = [ expected = [
(elems.h 1 "foo bar") (elems.h 1 "foo bar")
"" ""
(elems.p "lorem ipsum\n") (elems.p {} "lorem ipsum\n")
]; ];
}) })
] ]

View file

@ -1,4 +1,5 @@
let let
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;
it = import ./it.nix; it = import ./it.nix;
@ -6,9 +7,15 @@ in
with site; [ with site; [
(it "applies a style" { (it "applies a style" {
expected = { expected = {
"index.html" = with elems; (doc [(title "foobar") (stylesheet "/style.css")] [(main "something")]); "index.html" = html.tag "html" {} [
(html.tag "head" {} [(elems.title {} "foobar") (elems.stylesheet "/style.css")])
(elems.main {} "something")
];
blog = { blog = {
"index.html" = with elems; (doc [(title "foobar") (stylesheet "/style.css")] [(main "blogy blog")]); "index.html" = html.tag "html" {} [
(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
@ -18,9 +25,15 @@ in
applyStyle '' applyStyle ''
this is a stylesheet this is a stylesheet
'' { '' {
"index.html" = with elems; (doc [(title "foobar")] [(main "something")]); "index.html" = html.tag "html" {} [
(html.tag "head" {} [(elems.title {} "foobar")])
(elems.main {} "something")
];
blog = { blog = {
"index.html" = with elems; (doc [(title "foobar")] [(main "blogy blog")]); "index.html" = html.tag "html" {} [
(html.tag "head" {} [(elems.title {} "foobar")])
(elems.main {} "blogy blog")
];
}; };
}; };
}) })

View file

@ -24,15 +24,17 @@ let
}) })
(style.styled "list" "ul" { (style.styled "list" "ul" {
__child = child: __child = child:
assert builtins.isList child; assert builtins.isList child; (map (html.tag "li" {}) child);
(map (html.tag "li" {}) child); } {});
} {})
;
in [ in [
(it "makes a p component" { (it "makes a p component" {
expected = html.tag "p" {} "yes"; expected = html.tag "p" {} "yes";
actual = my.p {} "yes"; actual = my.p {} "yes";
}) })
(it "does not error without attrs" {
expected = html.tag "p" {} "yes";
actual = my.p "yes";
})
(it "makes a component" { (it "makes a component" {
expected = html.tag "div" {class = ["something"];} "foobar"; expected = html.tag "div" {class = ["something"];} "foobar";
actual = my.div {} "foobar"; actual = my.div {} "foobar";
@ -53,7 +55,7 @@ in [
(html.tag "li" {} "1") (html.tag "li" {} "1")
(html.tag "li" {} "2") (html.tag "li" {} "2")
]); ]);
actual = toString ( my.list {} ["1" "2"] ); actual = toString (my.list {} ["1" "2"]);
}) })
(it "combines attrs" { (it "combines attrs" {
expected = html.tag "div" { expected = html.tag "div" {