alejandra
This commit is contained in:
parent
b1426fb1ca
commit
dc33fdf093
14 changed files with 1029 additions and 976 deletions
200
nixite/md.nix
200
nixite/md.nix
|
@ -1,57 +1,55 @@
|
|||
let elems = import ./elems.nix;
|
||||
let
|
||||
elems = import ./elems.nix;
|
||||
in rec {
|
||||
readMd = md:
|
||||
if builtins.isPath md then
|
||||
processMd (builtins.readFile md)
|
||||
else
|
||||
processMd md;
|
||||
if builtins.isPath md
|
||||
then processMd (builtins.readFile md)
|
||||
else processMd md;
|
||||
|
||||
processMd = processStr;
|
||||
|
||||
recReadMd = root:
|
||||
assert builtins.isPath root;
|
||||
builtins.mapAttrs (path: type:
|
||||
if type == "directory" then
|
||||
recReadMd (root + (/. + path))
|
||||
else if type == "regular" then
|
||||
mdToPage (root + (/. + path))
|
||||
else
|
||||
throw "Cannot read ${path}, file type ${type}") (builtins.readDir root);
|
||||
builtins.mapAttrs (path: type:
|
||||
if type == "directory"
|
||||
then recReadMd (root + (/. + path))
|
||||
else if type == "regular"
|
||||
then mdToPage (root + (/. + path))
|
||||
else throw "Cannot read ${path}, file type ${type}") (builtins.readDir root);
|
||||
|
||||
recFixAppendix = site:
|
||||
builtins.listToAttrs (builtins.attrValues (builtins.mapAttrs (name: value: {
|
||||
name = fixAppendix name;
|
||||
value = if builtins.isAttrs value then recFixAppendix value else value;
|
||||
}) site));
|
||||
name = fixAppendix name;
|
||||
value =
|
||||
if builtins.isAttrs value
|
||||
then recFixAppendix value
|
||||
else value;
|
||||
})
|
||||
site));
|
||||
|
||||
fixAppendix = builtins.replaceStrings [ ".md" ] [ ".html" ];
|
||||
fixAppendix = builtins.replaceStrings [".md"] [".html"];
|
||||
|
||||
readDir = root: recFixAppendix (recReadMd root);
|
||||
|
||||
mdToPage = md:
|
||||
elems.Doc { } [ [ (elems.title { } "markdown file") ] (readMd md) ];
|
||||
|
||||
code = block:
|
||||
matchThen "(```)(.*)(```)" block (m: elems.code (builtins.elemAt m 1));
|
||||
elems.Doc {} [[(elems.title {} "markdown file")] (readMd md)];
|
||||
|
||||
list = block:
|
||||
matchThen (''
|
||||
(- .+
|
||||
)*(- .+
|
||||
?)'') block (m: elems.List (splitList block));
|
||||
matchThen "(- .+\n)*(- .+\n?)"
|
||||
block (m: elems.List (splitList block));
|
||||
|
||||
splitList = block:
|
||||
map (listItem) (builtins.filter (s: builtins.isString s && s != "")
|
||||
map listItem (builtins.filter (s: builtins.isString s && s != "")
|
||||
(builtins.split "\n" block));
|
||||
|
||||
listItem = str:
|
||||
let
|
||||
li = builtins.match "- (.*)" str;
|
||||
checkbox = builtins.match "- \\[(.)] (.*)" str;
|
||||
checked = builtins.elemAt checkbox 0;
|
||||
content = builtins.elemAt checkbox 1;
|
||||
in if checkbox == null then
|
||||
li
|
||||
listItem = str: let
|
||||
li = builtins.match "- (.*)" str;
|
||||
checkbox = builtins.match "- \\[(.)] (.*)" str;
|
||||
checked = builtins.elemAt checkbox 0;
|
||||
content = builtins.elemAt checkbox 1;
|
||||
in
|
||||
if checkbox == null
|
||||
then li
|
||||
else [
|
||||
(elems.input {
|
||||
type = "checkbox";
|
||||
|
@ -61,28 +59,41 @@ in rec {
|
|||
content
|
||||
];
|
||||
|
||||
replace = regex: apply: block:
|
||||
(let
|
||||
m = builtins.match regex block;
|
||||
before = let v = builtins.elemAt m 0; 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);
|
||||
replace = regex: apply: block: (let
|
||||
m = builtins.match regex block;
|
||||
before = let
|
||||
v = builtins.elemAt m 0;
|
||||
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);
|
||||
|
||||
rule = matcher: apply: blocks:
|
||||
map (b: if builtins.isString b then replace matcher apply b else b) blocks;
|
||||
map (b:
|
||||
if builtins.isString b
|
||||
then replace matcher apply b
|
||||
else b)
|
||||
blocks;
|
||||
|
||||
applyRules = i: rules: input:
|
||||
let
|
||||
group = if builtins.isString input then [ input ] else input;
|
||||
len = builtins.length rules;
|
||||
rule = builtins.elemAt rules i;
|
||||
next = i + 1;
|
||||
in assert i < len;
|
||||
if next < len then rule (applyRules next rules group) else rule group;
|
||||
applyRules = i: rules: input: let
|
||||
group =
|
||||
if builtins.isString input
|
||||
then [input]
|
||||
else input;
|
||||
len = builtins.length rules;
|
||||
rule = builtins.elemAt rules i;
|
||||
next = i + 1;
|
||||
in
|
||||
assert i < len;
|
||||
if next < len
|
||||
then rule (applyRules next rules group)
|
||||
else rule group;
|
||||
|
||||
basicRule = matcher: elem: rule matcher (m: elem (builtins.elemAt m 1));
|
||||
|
||||
|
@ -96,60 +107,27 @@ in rec {
|
|||
(basicRule (wrap "~~") elems.del)
|
||||
(basicRule (wrap "\\*\\*") elems.strong)
|
||||
(basicRule (wrapBreak "__") elems.strong)
|
||||
(rule (contains "\\[(.*)]\\((.*)\\)") (m:
|
||||
let
|
||||
href = builtins.elemAt m 2;
|
||||
text = builtins.elemAt m 1;
|
||||
in (elems.a href text)))
|
||||
(rule (''
|
||||
(.*
|
||||
)([^-][^
|
||||
]+
|
||||
)((- [^
|
||||
]+
|
||||
)+)(.*)'') (
|
||||
|
||||
l:
|
||||
(elems.ul (basicRule (''
|
||||
(.*
|
||||
)?- ([^
|
||||
]+)
|
||||
(.*)'') (m:
|
||||
elems.li (basicRule ("()\\[(.)](.*)") (check:
|
||||
elems.input {
|
||||
type = "checkbox";
|
||||
checked = check != " ";
|
||||
disabled = true;
|
||||
}) [ m ])) [ (builtins.elemAt l 2) ]))
|
||||
|
||||
))
|
||||
(basicRule (''
|
||||
(.*
|
||||
|
||||
)?(.+)
|
||||
(.*)?'') elems.p)
|
||||
(basicRule (''
|
||||
(.*
|
||||
|
||||
)?```(.*)```(.*)?'') (elems.textarea { readonly = true; }))
|
||||
(basicRule (containsBreak ''
|
||||
###### ([^
|
||||
]+)'') (elems.h6))
|
||||
(basicRule (containsBreak ''
|
||||
##### ([^
|
||||
]+)'') (elems.h5))
|
||||
(basicRule (containsBreak ''
|
||||
#### ([^
|
||||
]+)'') (elems.h4))
|
||||
(basicRule (containsBreak ''
|
||||
### ([^
|
||||
]+)'') (elems.h3))
|
||||
(basicRule (containsBreak ''
|
||||
## ([^
|
||||
]+)'') (elems.h2))
|
||||
(basicRule (containsBreak ''
|
||||
# ([^
|
||||
]+)'') (elems.h1))
|
||||
(rule (contains "\\[(.*)]\\((.*)\\)") (m: let
|
||||
href = builtins.elemAt m 2;
|
||||
text = builtins.elemAt m 1;
|
||||
in (elems.a href text)))
|
||||
(rule "(.*\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 2)]))
|
||||
))
|
||||
(basicRule "(.*\n\n)?(.+)\n(.*)?" elems.p)
|
||||
(basicRule "(.*\n\n)?```(.*)```(.*)?" (elems.textarea {readonly = true;}))
|
||||
(basicRule (containsBreak "###### ([^\n]+)") (elems.h6))
|
||||
(basicRule (containsBreak "##### ([^\n]+)") (elems.h5))
|
||||
(basicRule (containsBreak "#### ([^\n]+)") (elems.h4))
|
||||
(basicRule (containsBreak "### ([^\n]+)") (elems.h3))
|
||||
(basicRule (containsBreak "## ([^\n]+)") (elems.h2))
|
||||
(basicRule (containsBreak "# ([^\n]+)") (elems.h1))
|
||||
(basicRule (containsBreak "<(${linkmatcher})>") (m: elems.a m m))
|
||||
];
|
||||
|
||||
|
@ -160,10 +138,11 @@ in rec {
|
|||
containsBreak = matcher: "(.*[[:space:]])?${matcher}(.*)";
|
||||
wrapBreak = matcher: containsBreak "${matcher}([^${matcher}]+)${matcher}";
|
||||
|
||||
matchThen = matcher: block: func:
|
||||
let m = builtins.match matcher block;
|
||||
in if m == null then
|
||||
dontMatch block
|
||||
matchThen = matcher: block: func: let
|
||||
m = builtins.match matcher block;
|
||||
in
|
||||
if m == null
|
||||
then dontMatch block
|
||||
else {
|
||||
matched = true;
|
||||
block = func m;
|
||||
|
@ -173,5 +152,4 @@ in rec {
|
|||
matched = false;
|
||||
inherit block;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue