diff --git a/flake.nix b/flake.nix index 38aed45..01c501c 100644 --- a/flake.nix +++ b/flake.nix @@ -29,20 +29,22 @@ underblue = nixite.style.component blue "under" { style = {text-decoration = "underline";}; }; - in (Doc {} [ - [(title "Nixite")] - (main [ - (a {href = nixite.site.link readme;} "Readme") - (a "/blog" "blog") - (List {} ["item 1" "item 2" "item 3"]) - (p [ - "check out my" - (blue "blue span") - "isn't it" - (underblue {onclick = "alert(1)";} "great!") - ]) - ]) - ]); + in ( + nixite.html.document { + head = [(title "Nixite")]; + body = main [ + (a {href = nixite.site.link readme;} "Readme") + (a "/blog" "blog") + (List {} ["item 1" "item 2" "item 3"]) + (p [ + "check out my" + (blue "blue span") + "isn't it" + (underblue {onclick = "alert(1)";} "great!") + ]) + ]; + } + ); blog = nixite.md.readDir ./testing/blog; }; site = diff --git a/nixite/elems.nix b/nixite/elems.nix index d91994e..95a9b4b 100644 --- a/nixite/elems.nix +++ b/nixite/elems.nix @@ -143,14 +143,6 @@ in { ); }; - Doc = params: child: - assert builtins.isList child; - assert builtins.length child == 2; - assert builtins.isList (builtins.elemAt child 0); - html.tag "html" ({lang = "en";} // params) [ - (html.tag "head" {} (builtins.elemAt child 0)) - (html.tag "body" {} (builtins.elemAt child 1)) - ]; Stylesheet = params: html.tag "link" ({ rel = "stylesheet"; diff --git a/nixite/html.nix b/nixite/html.nix index ca6948e..b203723 100644 --- a/nixite/html.nix +++ b/nixite/html.nix @@ -13,7 +13,7 @@ in rec { if builtins.isString elem then elem else if builtins.isList elem - then builtins.toString (map toHTML elem) + then builtins.concatStringsSep "" (map toHTML elem) else "<${elem.tag} ${writeAttrs elem.attrs or {}}>${ toHTML elem.child or "" }"; diff --git a/nixite/md.nix b/nixite/md.nix index e74bf91..7aa90af 100644 --- a/nixite/md.nix +++ b/nixite/md.nix @@ -1,5 +1,6 @@ let elems = import ./elems.nix; + html = import ./html.nix; in rec { processMd = md: if builtins.isPath md @@ -30,7 +31,10 @@ in rec { readDir = root: recFixAppendix (recReadMd root); mdToPage = md: - elems.Doc {} [[(elems.title {} "markdown file")] (processMd md)]; + html.document { + head = []; + body = elems.main (elems.article (processMd md)); + }; splitList = block: map listItem (builtins.filter (s: builtins.isString s && s != "") @@ -53,26 +57,29 @@ in rec { content ]; - replace = regex: apply: block: (let - m = builtins.match regex block; - before = let - v = builtins.elemAt m 0; + replace = regex: apply: block: + assert builtins.isString block; (let + m = builtins.match regex block; + before = builtins.elemAt m 0; + after = toString (builtins.elemAt m (matchCount - 1)); + matchCount = builtins.length m; in - if v == null - then "" - else v; - after = toString (builtins.elemAt m (matchCount - 1)); - matchCount = builtins.length m; - in - if m == null - then block - else (replace regex apply before) + (apply m) + after); + if m == null + then block + else + ( + if before == null + then "" + else replace regex apply before + ) + + (apply m) + + after); rule = matcher: apply: blocks: - map (b: - if builtins.isString b - then replace matcher apply b - else b) + map (block: + if builtins.isString block + then replace matcher apply block + else block) blocks; applyRules = i: rules: input: let @@ -117,14 +124,14 @@ in rec { (basicRule (containsBreak "<(${linkmatcher})>") (m: elems.a m m)) ]; - list = rule "(.*\n)([^-][^\n]+\n)((- [^\n]+\n)+)(.*)" ( + list = rule "((.*)(\n([^-\n][^\n]+)?\n))?((- [^\n]+\n)+)(.*)" ( l: (elems.ul (basicRule "(.*\n)?- ([^\n]+)\n(.*)" (m: - elems.li (basicRule "()\\[(.)](.*)" (check: + elems.li (basicRule "()\\[(.)] (.*)" (check: elems.input { type = "checkbox"; checked = check != " "; disabled = true; - }) [m])) [(builtins.elemAt l 2)])) + }) [m])) [(builtins.elemAt l 4)])) ); linkmatcher = "[-[:alnum:].%?&#=:/]+"; diff --git a/testing/elems.test.nix b/testing/elems.test.nix index 445b345..d04d97f 100644 --- a/testing/elems.test.nix +++ b/testing/elems.test.nix @@ -80,15 +80,6 @@ in actual = List {} ["foo" "bar" "baz"]; asString = true; }) - (it "makes an html doc" { - expected = html.tag "html" { - __child = ""; - class = []; - lang = "en"; - } [(html.tag "head" {} ["foo"]) (html.tag "body" {} "bar")]; - actual = Doc {} [["foo"] "bar"]; - asString = true; - }) ] ) ] diff --git a/testing/md.test.nix b/testing/md.test.nix index 22637e4..6373adb 100644 --- a/testing/md.test.nix +++ b/testing/md.test.nix @@ -4,6 +4,7 @@ ... }: let md = import ../nixite/md.nix; + html = import ../nixite/html.nix; elems = import ../nixite/elems.nix; in with md; [ @@ -16,6 +17,7 @@ in '' ]; expected = [(elems.List ["something"])]; + asJSON = true; }) (it "makes a list of many elements" { @@ -26,6 +28,7 @@ in '' ]; expected = [(elems.List ["something" "something else"])]; + asJSON = true; }) (it "makes a list of many checkboxes" { @@ -55,15 +58,7 @@ in ] ]) ]; - }) - - (it "matches a list with no whitespace around" { - actual = list [ - '' - - something - - something else'' - ]; - expected = [(elems.List ["something" "something else"])]; + asJSON = true; }) (it "doesnt match not a list" (let @@ -80,16 +75,16 @@ in actual = processStr '' this text **may** *or may not* contain **bold** words *inside* it. ''; - expected = elems.p [ - "this text" - (elems.strong "may") + expected = [(elems.p [ + "this text " + (elems.strong "may") " " (elems.em "or may not") - "contain" + " contain " (elems.strong "bold") - "words" + " words " (elems.em "inside") - "it." - ]; + " it." + ])]; asString = true; }) @@ -119,9 +114,25 @@ in ( 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; + actual = mdToPage '' + # Markdown + + This is *markdown*! + ''; + expected = html.document { + head = []; + body = [ + (elems.main (elems.article [ + (elems.h1 "Markdown") + (elems.p [ + "This is" + (elems.em "markdown") + "!" + ]) + ])) + ]; + }; }) ] )