Compare commits

..

No commits in common. "md" and "master" have entirely different histories.
md ... master

2 changed files with 58 additions and 57 deletions

View file

@ -34,7 +34,7 @@
inherit (tix.packages.${system}) watch watchpipe results; inherit (tix.packages.${system}) watch watchpipe results;
test = tix.run [ test = tix.run [
./testing/md.test.nix # ./testing/md.test.nix
./testing/html.test.nix ./testing/html.test.nix
./testing/elems.test.nix ./testing/elems.test.nix
./testing/builder.test.nix ./testing/builder.test.nix

View file

@ -1,16 +1,19 @@
rec { let
processMd = elems: md: elems = import ./elems.nix;
html = import ./html.nix;
in rec {
processMd = md:
if builtins.isPath md if builtins.isPath md
then processStr elems (builtins.readFile md) then processStr (builtins.readFile md)
else processStr elems md; else processStr md;
recReadMd = toPage: elems: root: recReadMd = root:
assert builtins.isPath root; assert builtins.isPath root;
builtins.mapAttrs (path: type: builtins.mapAttrs (path: type:
if type == "directory" if type == "directory"
then recReadMd (root + (/. + path)) then recReadMd (root + (/. + path))
else if type == "regular" else if type == "regular"
then toPage (processMd elems (root + (/. + path))) then mdToPage (root + (/. + path))
else throw "Cannot read ${path}, file type ${type}") (builtins.readDir root); else throw "Cannot read ${path}, file type ${type}") (builtins.readDir root);
recFixAppendix = site: recFixAppendix = site:
@ -25,13 +28,19 @@ rec {
fixAppendix = builtins.replaceStrings [".md"] [".html"]; fixAppendix = builtins.replaceStrings [".md"] [".html"];
readDir = toPage: elems: root: recFixAppendix (recReadMd toPage elems root); readDir = root: recFixAppendix (recReadMd root);
splitList = elems: block: mdToPage = md:
map listItem elems (builtins.filter (s: builtins.isString s && s != "") html.document {
head = [];
body = elems.main (elems.article (processMd md));
};
splitList = block:
map listItem (builtins.filter (s: builtins.isString s && s != "")
(builtins.split "\n" block)); (builtins.split "\n" block));
listItem = elems: str: let listItem = str: let
li = builtins.match "- (.*)" str; li = builtins.match "- (.*)" str;
checkbox = builtins.match "- \\[(.)] (.*)" str; checkbox = builtins.match "- \\[(.)] (.*)" str;
checked = builtins.elemAt checkbox 0; checked = builtins.elemAt checkbox 0;
@ -49,48 +58,47 @@ rec {
]; ];
replace = regex: apply: block: replace = regex: apply: block:
if toString block == "" assert builtins.isString block; (let
then "" m = builtins.match regex block;
else before = builtins.elemAt m 0;
(let after = toString (builtins.elemAt m (matchCount - 1));
match = builtins.match regex block; matchCount = builtins.length m;
before = builtins.elemAt match 0;
matchCount = builtins.length match;
after = builtins.elemAt match (matchCount - 1);
in in
if match == null if m == null
then block then block
else else
( (
replace regex apply before if before == null
then ""
else replace regex apply before
) )
+ (apply match) + (apply m)
+ after); + after);
rule = matcher: apply: blocks: rule = matcher: apply: blocks:
if builtins.isString blocks map (block:
then replace matcher apply blocks if builtins.isString block
else if builtins.isList blocks then replace matcher apply block
then map (replace matcher apply) blocks else block)
else throw "replace rule should be applied to string or list of strings"; blocks;
applyRules = index: rules: input: let applyRules = i: rules: input: let
group = group =
if builtins.isString input if builtins.isString input
then [input] then [input]
else input; else input;
len = builtins.length rules; len = builtins.length rules;
rule = builtins.elemAt rules index; rule = builtins.elemAt rules i;
next = index + 1; next = i + 1;
in in
assert index < len; assert i < len;
if next < len if next < len
then rule (applyRules next rules group) then rule (applyRules next rules group)
else rule group; else rule group;
basicRule = matcher: elem: rule matcher (m: elem (builtins.elemAt m 1)); basicRule = matcher: elem: rule matcher (m: elem (builtins.elemAt m 1));
processStr = elems: applyRules 0 [ processStr = applyRules 0 [
(basicRule (wrap "\\^") elems.sup) (basicRule (wrap "\\^") elems.sup)
(basicRule (wrap "~") elems.sub) (basicRule (wrap "~") elems.sub)
(basicRule (wrap "\\*") elems.em) (basicRule (wrap "\\*") elems.em)
@ -103,8 +111,8 @@ rec {
(rule (contains "\\[(.*)]\\((.*)\\)") (m: let (rule (contains "\\[(.*)]\\((.*)\\)") (m: let
href = builtins.elemAt m 2; href = builtins.elemAt m 2;
text = builtins.elemAt m 1; text = builtins.elemAt m 1;
in (elems.a {inherit href;} text))) in (elems.a href text)))
(list elems) list
(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))
@ -113,25 +121,18 @@ rec {
(basicRule (containsBreak "### ([^\n]+)") (elems.h3)) (basicRule (containsBreak "### ([^\n]+)") (elems.h3))
(basicRule (containsBreak "## ([^\n]+)") (elems.h2)) (basicRule (containsBreak "## ([^\n]+)") (elems.h2))
(basicRule (containsBreak "# ([^\n]+)") (elems.h1)) (basicRule (containsBreak "# ([^\n]+)") (elems.h1))
(basicRule (containsBreak "<(${linkmatcher})>") (m: elems.a {href = m;} m)) (basicRule (containsBreak "<(${linkmatcher})>") (m: elems.a m m))
]; ];
list = elems: let list = rule "((.*)(\n([^-\n][^\n]+)?\n))?((- [^\n]+\n)+)(.*)" (
addCheckboxes = basicRule "()\\[(.)] (.*)" (check: l: (elems.ul (basicRule "(.*\n)?- ([^\n]+)\n(.*)" (m:
elems.li (basicRule "()\\[(.)] (.*)" (check:
elems.input { elems.input {
type = "checkbox"; type = "checkbox";
checked = check != " "; checked = check != " ";
disabled = true; disabled = true;
}); }) [m])) [(builtins.elemAt l 4)]))
);
listitems =
basicRule "(.*\n)?- ([^\n]+)\n(.*)" (contents:
elems.li (addCheckboxes contents));
in (rule "((.*)(\n([^-\n][^\n]+)?\n))?((- [^\n]+\n)+)(.*)" (
match: let
listString = builtins.elemAt match 4;
in (elems.ul (listitems listString))
));
linkmatcher = "[-[:alnum:].%?&#=:/]+"; linkmatcher = "[-[:alnum:].%?&#=:/]+";
contains = matcher: "(.*)?${matcher}(.*)"; contains = matcher: "(.*)?${matcher}(.*)";