md refactorm
This commit is contained in:
parent
89b29b2e46
commit
3c82f5f9bc
|
@ -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
|
||||||
|
|
|
@ -58,40 +58,41 @@ in rec {
|
||||||
];
|
];
|
||||||
|
|
||||||
replace = regex: apply: block:
|
replace = regex: apply: block:
|
||||||
assert builtins.isString block; (let
|
if toString block == ""
|
||||||
m = builtins.match regex block;
|
then ""
|
||||||
before = builtins.elemAt m 0;
|
else
|
||||||
after = toString (builtins.elemAt m (matchCount - 1));
|
(let
|
||||||
matchCount = builtins.length m;
|
match = builtins.match regex block;
|
||||||
|
before = builtins.elemAt match 0;
|
||||||
|
matchCount = builtins.length match;
|
||||||
|
after = builtins.elemAt match (matchCount - 1);
|
||||||
in
|
in
|
||||||
if m == null
|
if match == null
|
||||||
then block
|
then block
|
||||||
else
|
else
|
||||||
(
|
(
|
||||||
if before == null
|
replace regex apply before
|
||||||
then ""
|
|
||||||
else replace regex apply before
|
|
||||||
)
|
)
|
||||||
+ (apply m)
|
+ (apply match)
|
||||||
+ after);
|
+ after);
|
||||||
|
|
||||||
rule = matcher: apply: blocks:
|
rule = matcher: apply: blocks:
|
||||||
map (block:
|
if builtins.isString blocks
|
||||||
if builtins.isString block
|
then replace matcher apply blocks
|
||||||
then replace matcher apply block
|
else if builtins.isList blocks
|
||||||
else block)
|
then map (replace matcher apply) blocks
|
||||||
blocks;
|
else throw "replace rule should be applied to string or list of strings";
|
||||||
|
|
||||||
applyRules = i: rules: input: let
|
applyRules = index: 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 i;
|
rule = builtins.elemAt rules index;
|
||||||
next = i + 1;
|
next = index + 1;
|
||||||
in
|
in
|
||||||
assert i < len;
|
assert index < 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;
|
||||||
|
@ -124,15 +125,22 @@ in rec {
|
||||||
(basicRule (containsBreak "<(${linkmatcher})>") (m: elems.a m m))
|
(basicRule (containsBreak "<(${linkmatcher})>") (m: elems.a m m))
|
||||||
];
|
];
|
||||||
|
|
||||||
list = rule "((.*)(\n([^-\n][^\n]+)?\n))?((- [^\n]+\n)+)(.*)" (
|
list = let
|
||||||
l: (elems.ul (basicRule "(.*\n)?- ([^\n]+)\n(.*)" (m:
|
addCheckboxes = basicRule "()\\[(.)] (.*)" (check:
|
||||||
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}(.*)";
|
||||||
|
|
Loading…
Reference in a new issue