insert elems into md

This commit is contained in:
tristan 2024-02-11 08:25:09 +00:00
parent 62f3749709
commit 6509c8c827

View file

@ -1,19 +1,16 @@
let rec {
elems = import ./elems.nix; processMd = elems: md:
html = import ./html.nix;
in rec {
processMd = md:
if builtins.isPath md if builtins.isPath md
then processStr (builtins.readFile md) then processStr elems (builtins.readFile md)
else processStr md; else processStr elems md;
recReadMd = toPage: root: recReadMd = toPage: elems: 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 (root + (/. + path))) then toPage (processMd elems (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:
@ -28,13 +25,13 @@ in rec {
fixAppendix = builtins.replaceStrings [".md"] [".html"]; fixAppendix = builtins.replaceStrings [".md"] [".html"];
readDir = toPage: root: recFixAppendix (recReadMd toPage root); readDir = toPage: elems: root: recFixAppendix (recReadMd toPage elems root);
splitList = block: splitList = elems: block:
map listItem (builtins.filter (s: builtins.isString s && s != "") map listItem elems (builtins.filter (s: builtins.isString s && s != "")
(builtins.split "\n" block)); (builtins.split "\n" block));
listItem = str: let listItem = elems: 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;
@ -93,7 +90,7 @@ in rec {
basicRule = matcher: elem: rule matcher (m: elem (builtins.elemAt m 1)); basicRule = matcher: elem: rule matcher (m: elem (builtins.elemAt m 1));
processStr = applyRules 0 [ processStr = elems: 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)
@ -106,8 +103,8 @@ in 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 href text))) in (elems.a {inherit href;} text)))
list (list elems)
(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))
@ -116,10 +113,10 @@ in 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 m m)) (basicRule (containsBreak "<(${linkmatcher})>") (m: elems.a {href = m;} m))
]; ];
list = let list = elems: let
addCheckboxes = basicRule "()\\[(.)] (.*)" (check: addCheckboxes = basicRule "()\\[(.)] (.*)" (check:
elems.input { elems.input {
type = "checkbox"; type = "checkbox";