From 3c82f5f9bc0fa737078b16a5a35ba6df80cf928e Mon Sep 17 00:00:00 2001 From: tristan Date: Sun, 11 Feb 2024 06:20:01 +0000 Subject: [PATCH] md refactorm --- flake.nix | 2 +- nixite/md.nix | 76 ++++++++++++++++++++++++++++----------------------- 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/flake.nix b/flake.nix index cc95dbe..a3791cd 100644 --- a/flake.nix +++ b/flake.nix @@ -34,7 +34,7 @@ inherit (tix.packages.${system}) watch watchpipe results; test = tix.run [ - # ./testing/md.test.nix + ./testing/md.test.nix ./testing/html.test.nix ./testing/elems.test.nix ./testing/builder.test.nix diff --git a/nixite/md.nix b/nixite/md.nix index 7aa90af..a466c80 100644 --- a/nixite/md.nix +++ b/nixite/md.nix @@ -58,40 +58,41 @@ in rec { ]; 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 m == null - then block - else - ( - if before == null - then "" - else replace regex apply before - ) - + (apply m) - + after); + if toString block == "" + then "" + else + (let + match = builtins.match regex block; + before = builtins.elemAt match 0; + matchCount = builtins.length match; + after = builtins.elemAt match (matchCount - 1); + in + if match == null + then block + else + ( + replace regex apply before + ) + + (apply match) + + after); rule = matcher: apply: blocks: - map (block: - if builtins.isString block - then replace matcher apply block - else block) - blocks; + if builtins.isString blocks + then replace matcher apply blocks + else if builtins.isList blocks + then map (replace matcher apply) 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 = if builtins.isString input then [input] else input; len = builtins.length rules; - rule = builtins.elemAt rules i; - next = i + 1; + rule = builtins.elemAt rules index; + next = index + 1; in - assert i < len; + assert index < len; if next < len then rule (applyRules next rules group) else rule group; @@ -124,15 +125,22 @@ in rec { (basicRule (containsBreak "<(${linkmatcher})>") (m: elems.a m m)) ]; - list = rule "((.*)(\n([^-\n][^\n]+)?\n))?((- [^\n]+\n)+)(.*)" ( - l: (elems.ul (basicRule "(.*\n)?- ([^\n]+)\n(.*)" (m: - elems.li (basicRule "()\\[(.)] (.*)" (check: - elems.input { - type = "checkbox"; - checked = check != " "; - disabled = true; - }) [m])) [(builtins.elemAt l 4)])) - ); + list = let + addCheckboxes = basicRule "()\\[(.)] (.*)" (check: + elems.input { + type = "checkbox"; + checked = check != " "; + disabled = true; + }); + + 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:].%?&#=:/]+"; contains = matcher: "(.*)?${matcher}(.*)";