size: 48 KiB
| 1 | package.preload["djot.attributes"]=function()local e,t=string.find,string.sub;local a=0;local o=1;local i=2;local n=3;local s=4;local h=5;local r=6;local d=7;local l=8;local c=9;local u=10;local m=11;local w=12;local f=13;local g={};local y={};y[f]=function(t,o)if e(t.subject,"^{",o)then return a;else return m;end end;y[m]=function(e,e)return m;end;y[w]=function(e,e)return w;end;y[a]=function(s,h)local t=t(s.subject,h,h);if(t==" ")or(t=="\t")or(t=="\n")or(t=="\r")then return a;elseif t=="}"then return w;elseif t=="#"then s.begin=h;return o;elseif t=="%"then s.begin=h;return u;elseif t=="."then s.begin=h;return i;elseif e(t,"^[%a%d_:-]")then s.begin=h;return n;else return m;end end;y[u]=function(e,o)local e=t(e.subject,o,o);if e=="%"then return a;elseif e=="}"then return w;else return u;end end;y[o]=function(i,n)local t=t(i.subject,n,n);if e(t,"^[^%s%p]")or(t=="_")or(t=="-")or(t==":")then return o;elseif t=="}"then if i.lastpos>i.begin then i:add_match(i.begin+1,i.lastpos,"id");end i.begin=nil;return w;elseif e(t,"^%s")then if i.lastpos>i.begin then i:add_match(i.begin+1,i.lastpos,"id");end i.begin=nil;return a;else return m;end end;y[i]=function(o,n)local t=t(o.subject,n,n);if e(t,"^[^%s%p]")or(t=="_")or(t=="-")or(t==":")then return i;elseif t=="}"then if o.lastpos>o.begin then o:add_match(o.begin+1,o.lastpos,"class");end o.begin=nil;return w;elseif e(t,"^%s")then if o.lastpos>o.begin then o:add_match(o.begin+1,o.lastpos,"class");end o.begin=nil;return a;else return m;end end;y[n]=function(a,o)local t=t(a.subject,o,o);if t=="="then a:add_match(a.begin,a.lastpos,"key");a.begin=nil;return s;elseif e(t,"^[%a%d_:-]")then return n;else return m;end end;y[s]=function(a,o)local t=t(a.subject,o,o);if t=='"'then a.begin=o;return r;elseif e(t,"^[%a%d_:-]")then a.begin=o;return h;else return m;end end;y[h]=function(o,i)local t=t(o.subject,i,i);if e(t,"^[%a%d_:-]")then return h;elseif t=="}"then o:add_match(o.begin,o.lastpos,"value");o.begin=nil;return w;elseif e(t,"^%s")then o:add_match(o.begin,o.lastpos,"value");o.begin=nil;return a;else return m;end end;y[l]=function(e,e)return r;end;y[c]=function(e,e)return d;end;y[r]=function(e,o)local t=t(e.subject,o,o);if t=='"'then e:add_match(e.begin+1,e.lastpos,"value");e.begin=nil;return a;elseif t=="\n"then e:add_match(e.begin+1,e.lastpos,"value");e.begin=nil;return d;elseif t=="\\"then return l;else return r;end end;y[d]=function(e,o)local t=t(e.subject,o,o);if e.begin==nil then e.begin=o;end if t=='"'then e:add_match(e.begin,e.lastpos,"value");e.begin=nil;return a;elseif t=="\n"then e:add_match(e.begin,e.lastpos,"value");e.begin=nil;return d;elseif t=="\\"then return c;else return d;end end;function g.new(e,t)local t={subject=t,state=f,begin=nil,lastpos=nil,matches={}};setmetatable(t,e);e.__index=e;return t;end function g.add_match(e,t,a,o)e.matches[#e.matches+1]={t,a,o};end function g.get_matches(e)return e.matches;end function g.feed(e,t,a)local t=t;while t<=a do e.state=y[e.state](e,t);if e.state==w then return"done",t;elseif e.state==m then e.lastpos=t;return"fail",t;else e.lastpos=t;t=t+1;end end return"continue",a;end return{AttributeParser=g};end;package.preload["djot.inline"]=function()local e=unpack or table.unpack;local t=require("djot.attributes");local a,o=string.find,string.byte;local function i(e,t,o,i)local e,t,a,o,n=a(e,t,o);if t and(t<=i)then return e,t,a,o,n;end end local n={};function n.new(e,t,a)local t={warn=a or function()end,subject=t,matches={},openers={},verbatim=0,verbatim_type=nil,destination=false,firstpos=0,lastpos=0,allow_attributes=true,attribute_parser=nil,attribute_start=nil,attribute_slices=nil};setmetatable(t,e);e.__index=e;return t;end function n.add_match(e,t,a,o)e.matches[t]={t,a,o};end function n.add_opener(e,t,...)if not e.openers[t]then e.openers[t]={};end table.insert(e.openers[t],{...});end function n.clear_openers(t,a,o)for t,t in pairs(t.openers)do local i=#t;while t[i]do local e,n,s,s,h=e(t[i]);if(e>=a)and(n<=o)then t[i]=nil;elseif s and(s>=a)and h and(h<=o)then t[i][3]=nil;t[i][4]=nil;t[i][5]=nil;else break;end i=i-1;end end end function n.str_matches(t,a,o)for a=a,o do local o=t.matches[a];if o then local e,o,i=e(o);if(i~="str")and(i~="escape")then t.matches[a]={e,o,"str"};end end end end local function s(e,t)if e then return string.find(e[3],t);end end function n.between_matched(t,i,n,h)return function(r,d,l)n=n or"str";local c=r.subject;local u=a(c,"^%S",d+1);local a=a(c,"^%S",d-1);local s=s(r.matches[d-1],"^open%_marker");local o=((d+1)<=l)and(o(c,d+1)==125);local l=d;local c=d;if type(h)=="function"then u=u and h(r,d);end if s then u=true;a=false;c=d-1;end if not s and o then a=true;u=false;l=d+1;end if s and n:match("^right")then n=n:gsub("^right","left");elseif o and n:match("^left")then n=n:gsub("^left","right");end local h;if o then h="{"..t;else h=t;end local o=r.openers[h];if a and o and(#o>0)then local e,t=e(o[#o]);if t~=(d-1)then r:clear_openers(e,d);r:add_match(e,t,"+"..i);r:add_match(d,l,"-"..i);return l+1;end end if u then if s then h="{"..t;else h=t;end r:add_opener(h,c,d);r:add_match(c,d,n);return d+1;else r:add_match(d,l,n);return l+1;end end;end n.matchers={[96]=function(e,t,o)local n=e.subject;local o,o=i(n,"^`*",t,o);if not o then return nil;end if a(n,"^%$%$",t-2)and not a(n,"^\\",t-3)then e.matches[t-2]=nil;e.matches[t-1]=nil;e:add_match(t-2,o,"+display_math");e.verbatim_type="display_math";elseif a(n,"^%$",t-1)then e.matches[t-1]=nil;e:add_match(t-1,o,"+inline_math");e.verbatim_type="inline_math";else e:add_match(t,o,"+verbatim");e.verbatim_type="verbatim";end e.verbatim=(o-t)+1;return o+1;end,[92]=function(t,o,n)local s=t.subject;local h,h=i(s,"^[ \t]*\r?\n",o+1,n);t:add_match(o,o,"escape");if h then if#t.matches>0 then local e,a,o=e(t.matches[#t.matches]);if o=="str"then while(a>=e)and((s:byte(a)==32)or(s:byte(a)==9))do a=a-1;end if a<e then t.matches[#t.matches]=nil;else t:add_match(e,a,"str");end end end t:add_match(o+1,h,"hardbreak");return h+1;else local e,e=i(s,"^[%p ]",o+1,n);if not e then t:add_match(o,o,"str");return o+1;else t:add_match(o,o,"escape");if a(s,"^ ",o+1)then t:add_match(o+1,e,"nbsp");else t:add_match(o+1,e,"str");end return e+1;end end end,[60]=function(e,t,a)local o=e.subject;local a,n=i(o,"^%<[^<>%s]+%>",t,a);if a then local s=i(o,"^%a+:",t+1,n);local t=i(o,"^[^:]+%@",t+1,n);if t then e:add_match(a,a,"+email");e:add_match(a+1,n-1,"str");e:add_match(n,n,"-email");return n+1;elseif s then e:add_match(a,a,"+url");e:add_match(a+1,n-1,"str");e:add_match(n,n,"-url");return n+1;end end end,[126]=n.between_matched("~","subscript"),[94]=n.between_matched("^","superscript"),[91]=function(e,t,a)local a,o=i(e.subject,"^%^([^]]+)%]",t+1,a);if a then e:add_match(t,o,"footnote_reference");return o+1;else e:add_opener("[",t,t);e:add_match(t,t,"str");return t+1;end end,[93]=function(e,t,a)local o=e.openers["["];local n=e.subject;if o and(#o>0)then local o=o[#o];if o[3]=="reference_link"then local a=i(n,"^!",o[1]-1,a)and not i(n,"^[\\]",o[1]-2,a);if a then e:add_match(o[1]-1,o[1]-1,"image_marker");e:add_match(o[1],o[2],"+imagetext");e:add_match(o[4],o[4],"-imagetext");else e:add_match(o[1],o[2],"+linktext");e:add_match(o[4],o[4],"-linktext");end e:add_match(o[5],o[5],"+reference");e:add_match(t,t,"-reference");e:str_matches(o[5]+1,t-1);e:clear_openers(o[1],t);return t+1;elseif i(n,"^%[",t+1,a)then o[3]="reference_link";o[4]=t;o[5]=t+1;e:add_match(t,t+1,"str");e:clear_openers(o[1]+1,t-1);return t+2;elseif i(n,"^%(",t+1,a)then e.openers["("]={};o[3]="explicit_link";o[4]=t;o[5]=t+1;e.destination=true;e:add_match(t,t+1,"str");e:clear_openers(o[1]+1,t-1);return t+2;elseif i(n,"^%{",t+1,a)then e:add_match(o[1],o[2],"+span");e:add_match(t,t,"-span");e:clear_openers(o[1],t);return t+1;end end end,[40]=function(e,t)if not e.destination then return nil;end e:add_opener("(",t,t);e:add_match(t,t,"str");return t+1;end,[41]=function(e,t,a)if not e.destination then return nil;end local o=e.openers["("];if o and(#o>0)and o[#o][1]then o[#o]=nil;e:add_match(t,t,"str");return t+1;else local o=e.subject;local n=e.openers["["];if n and(#n>0)and(n[#n][3]=="explicit_link")then local n=n[#n];local a=i(o,"^!",n[1]-1,a)and not i(o,"^[\\]",n[1]-2,a);if a then e:add_match(n[1]-1,n[1]-1,"image_marker");e:add_match(n[1],n[2],"+imagetext");e:add_match(n[4],n[4],"-imagetext");else e:add_match(n[1],n[2],"+linktext");e:add_match(n[4],n[4],"-linktext");end e:add_match(n[5],n[5],"+destination");e:add_match(t,t,"-destination");e.destination=false;e:str_matches(n[5]+1,t-1);e:clear_openers(n[1],t);return t+1;end end end,[95]=n.between_matched("_","emph"),[42]=n.between_matched("*","strong"),[123]=function(e,a,o)if i(e.subject,"^[_*~^+='\"-]",a+1,o)then e:add_match(a,a,"open_marker");return a+1;elseif e.allow_attributes then e.attribute_parser=t.AttributeParser:new(e.subject);e.attribute_start=a;e.attribute_slices={};return a;else e:add_match(a,a,"str");return a+1;end end,[58]=function(e,t,a)local a,o=i(e.subject,"^%:[%w_+-]+%:",t,a);if a then e:add_match(a,o,"symbol");return o+1;else e:add_match(t,t,"str");return t+1;end end,[43]=n.between_matched("+","insert","str",function(e,t)return a(e.subject,"^%{",t-1)or a(e.subject,"^%}",t+1);end),[61]=n.between_matched("=","mark","str",function(e,t)return a(e.subject,"^%{",t-1)or a(e.subject,"^%}",t+1);end),[39]=n.between_matched("'","single_quoted","right_single_quote",function(e,t)return(t==1)or a(e.subject,"^[%s\"'-([]",t-1);end),[34]=n.between_matched('"',"double_quoted","left_double_quote"),[45]=function(e,t,i)local s=e.subject;local h;if(o(s,t-1)==123)or(o(s,t+1)==125)then h=n.between_matched("-","delete","str",function(e,t)return a(e.subject,"^%{",t-1)or a(e.subject,"^%}",t+1);end)(e,t,i);return h;end local a,a=a(s,"^%-*",t);if i<a then a=i;end local i=(1+a)-t;if o(s,a+1)==125 then i=i-1;end if i==0 then e:add_match(t,t+1,"str");return t+2;end local a=(i%3)==0;local o=(i%2)==0;while i>0 do if a then e:add_match(t,t+2,"em_dash");t=t+3;i=i-3;elseif o then e:add_match(t,t+1,"en_dash");t=t+2;i=i-2;elseif(i>=3)and(((i%2)~=0)or(i>4))then e:add_match(t,t+2,"em_dash");t=t+3;i=i-3;elseif i>=2 then e:add_match(t,t+1,"en_dash");t=t+2;i=i-2;else e:add_match(t,t,"str");t=t+1;i=i-1;end end return t;end,[46]=function(e,t,a)if i(e.subject,"^%.%.",t+1,a)then e:add_match(t,t+2,"ellipses");return t+3;end end};function n.single_char(e,t)e:add_match(t,t,"str");return t+1;end function n.reparse_attributes(t)local a=t.attribute_slices;if not a then return;end t.allow_attributes=false;t.attribute_parser=nil;t.attribute_start=nil;if a then for o=1,#a do t:feed(e(a[o]));end end t.allow_attributes=true;t.attribute_slices=nil;end function n.feed(t,a,n)local s="[][\\`{}_*()!<>~^:=+$\r\n'\".-]";local h=t.subject;local r=t.matchers;local d;if(t.firstpos==0)or(a<t.firstpos)then t.firstpos=a;end if(t.lastpos==0)or(n>t.lastpos)then t.lastpos=n;end d=a;while d<=n do if t.attribute_parser then local a=d;local o=i(h,s,d,n);if not o or(o>n)then o=n;end local o,i=t.attribute_parser:feed(a,o);if o=="done"then local a=t.attribute_start;t:add_match(a,a,"+attributes");t:add_match(i,i,"-attributes");local a=t.attribute_parser:get_matches();for o=1,#a do t:add_match(e(a[o]));end t.attribute_parser=nil;t.attribute_start=nil;t.attribute_slices=nil;d=i+1;elseif o=="fail"then t:reparse_attributes();d=a;elseif o=="continue"then if#t.attribute_slices==0 then t.attribute_slices={};end t.attribute_slices[#t.attribute_slices+1]={a,i};d=i+1;end else local e=i(h,s,d,n)or(n+1);if e>d then t:add_match(d,e-1,"str");d=e;if d>n then break;end end local e=o(h,d);if(e==13)or(e==10)then if(e==13)and i(h,"^[%n]",d+1,n)then t:add_match(d,d+1,"softbreak");d=d+2;else t:add_match(d,d,"softbreak");d=d+1;end elseif t.verbatim>0 then if e==96 then local e,e=i(h,"^`+",d,n);if e and(((e-d)+1)==t.verbatim)then local a,o=i(h,"^%{%=[^%s{}`]+%}",e+1,n);if a and(t.verbatim_type=="verbatim")then t:add_match(d,e,"-"..t.verbatim_type);t:add_match(a,o,"raw_format");d=o+1;else t:add_match(d,e,"-"..t.verbatim_type);d=e+1;end t.verbatim=0;t.verbatim_type=nil;else e=e or n;t:add_match(d,e,"str");d=e+1;end else t:add_match(d,d,"str");d=d+1;end else local e=r[e];d=(e and e(t,d,n))or t:single_char(d);end end end end function n.in_verbatim(e)return e.verbatim>0;end function n.get_matches(t)local a={};local i=t.subject;local n,s,h;if t.attribute_parser then t:reparse_attributes();end for o=t.firstpos,t.lastpos do if t.matches[o]then local e,i,r=e(t.matches[o]);if(r=="str")and(h=="str")and((s+1)==e)then a[#a]={n,i,r};n,s,h=n,i,r;else a[#a+1]=t.matches[o];n,s,h=e,i,r;end end end if#a>0 then local n=a[#a];local s,h,r=e(n);if r=="softbreak"then a[#a]=nil;n=a[#a];if not n then return a;end s,h,r=e(n);end if(r=="str")and(o(i,h)==32)then while(h>s)and(o(i,h)==32)do h=h-1;end a[#a]={s,h,r};end if t.verbatim>0 then t.warn({message="Unclosed verbatim",pos=h});a[#a+1]={h,h,"-"..t.verbatim_type};end end return a;end return{InlineParser=n};end;package.preload["djot.block"]=function()local e=require("djot.inline").InlineParser;local t=require("djot.attributes");local a=unpack or table.unpack;local o,i,n=string.find,string.sub,string.byte;local s={};function s.new(e,t,a)e=t;local t={};setmetatable(t,e);e.__index=e;if a then for e,a in pairs(a)do t[e]=a;end end return t;end local function h(e)if(e=="+")or(e=="-")or(e=="*")or(e==":")then return{e};elseif o(e,"^[+*-] %[[Xx ]%]")then return{"X"};elseif o(e,"^[(]?%d+[).]")then return{(e:gsub("%d+","1"))};elseif o(e,"^[(]?[ivxlcdm][).]")then return{(e:gsub("%a+","i")),(e:gsub("%a+","a"))};elseif o(e,"^[(]?[IVXLCDM][).]")then return{(e:gsub("%a+","I")),(e:gsub("%a+","A"))};elseif o(e,"^[(]?%l[).]")then return{(e:gsub("%l","a"))};elseif o(e,"^[(]?%u[).]")then return{(e:gsub("%u","A"))};elseif o(e,"^[(]?[ivxlcdm]+[).]")then return{(e:gsub("%a+","i"))};elseif o(e,"^[(]?[IVXLCDM]+[).]")then return{(e:gsub("%a+","I"))};else return{};end end local r={};function r.new(e,t,a)if not t:find("[\r\n]$")then t=t.."\n";end local t={warn=a or function()end,subject=t,indent=0,startline=nil,starteol=nil,endeol=nil,matches={},containers={},pos=1,last_matched_container=0,timer={},finished_line=false,returned=0};setmetatable(t,e);e.__index=e;return t;end function r.parse_table_row(t,i,s)local h=#t.matches;local r=t.pos;t:add_match(i,i,"+row");t.pos=o(t.subject,"%S",i+1);local d={};local l=t.pos;local c=false;while not c do local e,a,o,i,n=o(t.subject,"^(%:?)%-%-*(%:?)([ \t]*%|[ \t]*)",l);if a then local s="separator_default";if(#o>0)and(#i>0)then s="separator_center";elseif#i>0 then s="separator_right";elseif#o>0 then s="separator_left";end d[#d+1]={e,a-#n,s};l=a+1;if l==t.starteol then c=true;break;end else break;end end if c then for e=1,#d do t:add_match(a(d[e]));end t:add_match(t.starteol-1,t.starteol-1,"-row");t.pos=t.starteol;t.finished_line=true;return true;end local d=e:new(t.subject,t.warn);t:add_match(i,i,"+cell");local i=false;while t.pos<=s do local h,r;while not h do r,h=t:find("^[^|\r\n]*|");if not h then break;end if string.find(t.subject,"^\\",h-1)then d:feed(t.pos,h);t.pos=h+1;h=nil;else d:feed(t.pos,h-1);if d:in_verbatim()then d:feed(h,h);t.pos=h+1;h=nil;else t.pos=h+1;end end end i=h;if not i then break;end local i=d:get_matches();for e=1,#i do local a,o,s=a(i[e]);if(e==#i)and(s=="str")then while(n(t.subject,o)==32)and(o>=a)do o=o-1;end end t:add_match(a,o,s);end t:add_match(h,h,"-cell");if h<s then d=e:new(t.subject,t.warn);t:add_match(h,h,"+cell");t.pos=o(t.subject,"%S",t.pos);end end if not i then t.pos=r;for e=h,#t.matches do t.matches[e]=nil;end return false;else t:add_match(t.pos,t.pos,"-row");t.pos=t.starteol;t.finished_line=true;return true;end end function r.specs(n)return{{name="para",is_para=true,content="inline",continue=function()if n:find("^%S")then return true;else return false;end end,open=function(t)n:add_container(s:new(t,{inline_parser=e:new(n.subject,n.warn)}));n:add_match(n.pos,n.pos,"+para");return true;end,close=function()n:get_inline_matches();local e=n.matches[#n.matches]or{n.pos,n.pos,""};local e,e,t=a(e);n:add_match(e+1,e+1,"-para");n.containers[#n.containers]=nil;end},{name="caption",is_para=false,content="inline",continue=function()return n:find("^%S");end,open=function(t)local a,a=n:find("^%^[ \t]+");if a then n.pos=a+1;n:add_container(s:new(t,{inline_parser=e:new(n.subject,n.warn)}));n:add_match(n.pos,n.pos,"+caption");return true;end end,close=function()n:get_inline_matches();n:add_match(n.pos-1,n.pos-1,"-caption");n.containers[#n.containers]=nil;end},{name="blockquote",content="block",continue=function()if n:find("^%>%s")then n.pos=n.pos+1;return true;else return false;end end,open=function(e)if n:find("^%>%s")then n:add_container(s:new(e));n:add_match(n.pos,n.pos,"+blockquote");n.pos=n.pos+1;return true;end end,close=function()n:add_match(n.pos,n.pos,"-blockquote");n.containers[#n.containers]=nil;end},{name="footnote",content="block",continue=function(e)if(n.indent>e.indent)or n:find("^[\r\n]")then return true;else return false;end end,open=function(e)local t,a,o=n:find("^%[%^([^]]+)%]:%s");if not t then return nil;end n:add_container(s:new(e,{note_label=o,indent=n.indent}));n:add_match(t,t,"+footnote");n:add_match(t+2,a-3,"note_label");n.pos=a;return true;end,close=function(e)n:add_match(n.pos,n.pos,"-footnote");n.containers[#n.containers]=nil;end},{name="thematic_break",content=nil,continue=function()return false;end,open=function(e)local t,a=n:find("^[-*][ \t]*[-*][ \t]*[-*][-* \t]*[\r\n]");if a then n:add_container(s:new(e));n:add_match(t,a,"thematic_break");n.pos=a;return true;end end,close=function(e)n.containers[#n.containers]=nil;end},{name="list_item",content="block",continue=function(e)if(n.indent>e.indent)or n:find("^[\r\n]")then return true;else return false;end end,open=function(e)local t,a=n:find("^[-*+:]%s");if not t then t,a=n:find("^%d+[.)]%s");end if not t then t,a=n:find("^%(%d+%)%s");end if not t then t,a=n:find("^[ivxlcdmIVXLCDM]+[.)]%s");end if not t then t,a=n:find("^%([ivxlcdmIVXLCDM]+%)%s");end if not t then t,a=n:find("^%a[.)]%s");end if not t then t,a=n:find("^%(%a%)%s");end if not t then return nil;end local o=i(n.subject,t,a-1);local r=nil;if n:find("^[*+-] %[[Xx ]%]%s",t+1)then o=i(n.subject,t,t+4);r=i(n.subject,t+3,t+3);end local o=h(o);if#o==0 then return nil;end local i={styles=o,indent=n.indent};n:add_container(s:new(e,i));local e="+list_item";for t=1,#o do e=e.."|"..o[t];end n:add_match(t,a-1,e);n.pos=a;if r then if r==" "then n:add_match(t+2,t+4,"checkbox_unchecked");else n:add_match(t+2,t+4,"checkbox_checked");end n.pos=t+5;end return true;end,close=function(e)n:add_match(n.pos,n.pos,"-list_item");n.containers[#n.containers]=nil;end},{name="reference_definition",content=nil,continue=function(e)if e.indent>=n.indent then return false;end local e,e,t=n:find("^(%S+)");if e and(n.starteol==(e+1))then n:add_match((e-#t)+1,e,"reference_value");n.pos=e+1;return true;else return false;end end,open=function(e)local t,a,o,i=n:find("^%[([^]\r\n]*)%]:[ \t]*(%S*)");if a and(n.starteol==(a+1))then n:add_container(s:new(e,{key=o,indent=n.indent}));n:add_match(t,t,"+reference_definition");n:add_match(t,t+#o+1,"reference_key");if#i>0 then n:add_match((a-#i)+1,a,"reference_value");end n.pos=a+1;return true;end end,close=function(e)n:add_match(n.pos,n.pos,"-reference_definition");n.containers[#n.containers]=nil;end},{name="heading",content="inline",continue=function(e)local t,a=n:find("^%#+%s");if t and a and(e.level==(a-t))then n.pos=a;return true;else return false;end end,open=function(t)local a,i=n:find("^#+");if i and o(n.subject,"^%s",i+1)then local o=(i-a)+1;n:add_container(s:new(t,{level=o,inline_parser=e:new(n.subject,n.warn)}));n:add_match(a,i,"+heading");n.pos=i+1;return true;end end,close=function(e)n:get_inline_matches();local e=n.matches[#n.matches]or{n.pos,n.pos,""};local e,e,t=a(e);n:add_match(e+1,e+1,"-heading");n.containers[#n.containers]=nil;end},{name="code_block",content="text",continue=function(e)local t=i(e.border,1,1);local t,a,o=n:find("^("..e.border..t.."*)[ \t]*[\r\n]");if a then e.end_fence_sp=t;e.end_fence_ep=(t+#o)-1;n.pos=a;n.finished_line=true;return false;else return true;end end,open=function(e)local t,a,i,h,r=n:find("^(~~~~*)([ \t]*)(%S*)[ \t]*[\r\n]");if not a then t,a,i,h,r=n:find("^(````*)([ \t]*)([^%s`]*)[ \t]*[\r\n]");end if i then local o=(o(r,"^=")and true)or false;n:add_container(s:new(e,{border=i,indent=n.indent}));n:add_match(t,(t+#i)-1,"+code_block");if#r>0 then local e=t+#i+#h;if o then n:add_match(e,(e+#r)-1,"raw_format");else n:add_match(e,(e+#r)-1,"code_language");end end n.pos=a;n.finished_line=true;return true;end end,close=function(e)local t=e.end_fence_sp or n.pos;local e=e.end_fence_ep or n.pos;n:add_match(t,e,"-code_block");if t==e then n.warn({pos=n.pos,message="Unclosed code block"});end n.containers[#n.containers]=nil;end},{name="fenced_div",content="block",continue=function(e)if n.containers[#n.containers].name=="code_block"then return true;end local t,a,o=n:find("^(::::*)[ \t]*[\r\n]");if a and(#o>=e.equals)then e.end_fence_sp=t;e.end_fence_ep=(t+#o)-1;n.pos=a;return false;else return true;end end,open=function(e)local t,a,i=n:find("^(::::*)[ \t]*");if not a then return false;end local a,h=o(n.subject,"^[%w_-]*",a+1);local o,o=o(n.subject,"^[ \t]*[\r\n]",h+1);if o then n:add_container(s:new(e,{equals=#i}));n:add_match(t,h,"+div");if h>=a then n:add_match(a,h,"class");end n.pos=o+1;n.finished_line=true;return true;end end,close=function(e)local t=e.end_fence_sp or n.pos;local e=e.end_fence_ep or n.pos;n:add_match(t,e,"-div");if t==e then n.warn({pos=n.pos,message="Unclosed div"});end n.containers[#n.containers]=nil;end},{name="table",content="cells",continue=function(e)local e,t=n:find("^|[^\r\n]*|");local a=t and o(n.subject,"^[ \t]*[\r\n]",t+1);if a then return n:parse_table_row(e,t);end end,open=function(e)local t,a=n:find("^|[^\r\n]*|");local o=" *[\r\n]";if t and o then n:add_container(s:new(e,{columns=0}));n:add_match(t,t,"+table");if n:parse_table_row(t,a)then return true;else n.containers[#n.containers]=nil;return false;end end end,close=function(e)n:add_match(n.pos,n.pos,"-table");n.containers[#n.containers]=nil;end},{name="attributes",content="attributes",open=function(e)if n:find("^%{")then local t=t.AttributeParser:new(n.subject);local a,o=t:feed(n.pos,n.endeol);if(a=="fail")or((o+1)<n.endeol)then return false;else n:add_container(s:new(e,{status=a,indent=n.indent,startpos=n.pos,slices={},attribute_parser=t}));local e=n.containers[#n.containers];e.slices={{n.pos,n.endeol}};n.pos=n.starteol;return true;end end end,continue=function(t)if n.indent>t.indent then table.insert(t.slices,{n.pos,n.endeol});local e,a=t.attribute_parser:feed(n.pos,n.endeol);t.status=e;if(e~="fail")or((a+1)<n.endeol)then n.pos=n.starteol;return true;end end if t.status=="done"then return false;else local a=n:specs()[1];local e=s:new(a,{inline_parser=e:new(n.subject,n.warn)});n:add_match(t.startpos,t.startpos,"+para");n.containers[#n.containers]=e;e.inline_parser.attribute_slices=t.slices;e.inline_parser:reparse_attributes();n.pos=e.inline_parser.lastpos+1;return true;end end,close=function(e)local t=e.attribute_parser:get_matches();n:add_match(e.startpos,e.startpos,"+block_attributes");for e=1,#t do n:add_match(a(t[e]));end n:add_match(n.pos,n.pos,"-block_attributes");n.containers[#n.containers]=nil;end}};end function r.get_inline_matches(e)local t=e.containers[#e.containers].inline_parser:get_matches();for a=1,#t do e.matches[#e.matches+1]=t[a];end end function r.find(e,t)return o(e.subject,t,e.pos);end function r.add_match(e,t,a,o)e.matches[#e.matches+1]={t,a,o};end function r.add_container(e,t)local a=e.last_matched_container;while(#e.containers>a)or((#e.containers>0)and(e.containers[#e.containers].content~="block"))do e.containers[#e.containers]:close();end e.containers[#e.containers+1]=t;end function r.skip_space(e)local t,a=o(e.subject,"[^ \t]",e.pos);if t then e.indent=t-e.startline;e.pos=t;end end function r.get_eol(e)local t,a=o(e.subject,"[\r]?[\n]",e.pos);if not a then t,a=#e.subject,#e.subject;end e.starteol=t;e.endeol=a;end function r.events(e)local t=e:specs();local o=t[1];local i=#e.subject;return function()while e.pos<=i do if e.returned<#e.matches then e.returned=e.returned+1;return a(e.matches[e.returned]);end e.indent=0;e.startline=e.pos;e.finished_line=false;e:get_eol();e.last_matched_container=0;local a=0;while a<#e.containers do a=a+1;local t=e.containers[a];e:skip_space();if t:continue()then e.last_matched_container=a;else break;end end if e.finished_line then while#e.containers>e.last_matched_container do e.containers[#e.containers]:close();end end if not e.finished_line then e:skip_space();local a=e.pos==e.starteol;local i=false;local n=e.containers[e.last_matched_container];local n=not a and(not n or(n.content=="block"))and not e:find("^%a+%s");while n do n=false;for a=1,#t do local t=t[a];if not t.is_para then if t:open()then e.last_matched_container=#e.containers;if e.finished_line then n=false;else e:skip_space();i=true;n=t.content=="block";end break;end end end end if not e.finished_line then e:skip_space();a=e.pos==e.starteol;local t=not a and not i and(e.last_matched_container<#e.containers)and(e.containers[#e.containers].content=="inline");local n=e.last_matched_container;if not t then while(#e.containers>0)and(#e.containers>n)do e.containers[#e.containers]:close();end end local t=e.containers[#e.containers];if not t or(t.content=="block")then if a then if not i then e:add_match(e.pos,e.endeol,"blankline");end else o:open();end t=e.containers[#e.containers];end if t then if t.content=="text"then local a=e.pos;if t.indent and(e.indent>t.indent)then a=a-(e.indent-t.indent);end e:add_match(a,e.endeol,"str");elseif t.content=="inline"then if not a then t.inline_parser:feed(e.pos,e.endeol);end end end end end e.pos=e.endeol+1;end while#e.containers>0 do e.containers[#e.containers]:close();end if e.returned<#e.matches then e.returned=e.returned+1;return a(e.matches[e.returned]);end end;end return{Parser=r,Container=s};end;package.preload["djot.ast"]=function()if not utf8 then local e=pairs;function pairs(t)local a=getmetatable(t);if(type(a)=="table")and(type(a.__pairs)=="function")then return a.__pairs(t);else return e(t);end end end local e=unpack or table.unpack;local e,t,a,o,i=string.find,string.lower,string.sub,string.rep,string.format;local function n(e)local t={line={},col={},charpos={}};local a=1;local o=0;local i=0;local n=1;local s=string.byte(e,n);while s do o=o+1;i=i+1;local h;if s<192 then h=n+1;elseif s<224 then h=n+2;elseif s<240 then h=n+3;else h=n+4;end while n<h do t.line[n]=a;t.col[n]=o;t.charpos[n]=i;n=n+1;end if s==10 then a=a+1;o=0;end s=string.byte(e,n);end t.line[n]=a+1;t.col[n]=1;t.charpos[n]=i+1;return t;end local function s(e,t)if e.s then t[#t+1]=e.s;elseif e.t=="softbreak"then t[#t+1]="\n";elseif e.c then for a=1,#e.c do s(e.c[a],t);end end end local function h(e)local t={};s(e,t);return table.concat(t);end local s={i=1,v=5,x=10,l=50,c=100,d=500,m=1000};local function r(e)local o=0;local i=0;local n=#e;while n>0 do local t=t(a(e,n,n));local t=s[t];assert(t~=nil,"Encountered bad character in roman numeral "..e);if t<i then o=o-t;else o=o+t;end i=t;n=n-1;end return o;end local function t(e,t)local t=string.gsub(t,"%p","");local e=string.gsub(e,"%p","");if t=="1"then return tonumber(e);elseif t=="A"then return(string.byte(e)-string.byte("A"))+1;elseif t=="a"then return(string.byte(e)-string.byte("a"))+1;elseif t=="I"then return r(e);elseif t=="i"then return r(e);elseif t==""then return nil;end end local s={image_marker=true,escape=true,blankline=true};local function r(e,t)return function(a)local o={};local i=nil;i=next(a,i);while i do o[#o+1]=i;i=next(a,i);end table.sort(o,e);local e=0;local function i(a,i)e=e+1;local e=o[e];local t=t(e);if e then return t,a[e];else return nil;end end return i,a,nil;end;end local d={};local l={children="c",text="s",tag="t"};local c={c="children",s="text",t="tag"};function d.__index(e,t)local a=l[t];if a then return rawget(e,a);else return rawget(e,t);end end function d.__newindex(e,t,a)local o=l[t];if o then rawset(e,o,a);else rawset(e,t,a);end end d.__pairs=r(function(e,t)if e=="t"then return true;elseif e=="s"then return t~="t";elseif e=="c"then return(t=="references")or(t=="footnotes");elseif e=="references"then return t=="footnotes";elseif e=="footnotes"then return false;elseif(t=="t")or(t=="s")then return false;elseif(t=="c")or(t=="references")or(t=="footnotes")then return true;else return e<t;end end,function(e)return c[e]or e;end);local function l(e)local e={t=e,c=nil};setmetatable(e,d);return e;end local function d(e,t)if not e.c then e.c={t};else e.c[#e.c+1]=t;end end local function c(e)return e.c and(#e.c>0);end local function u(e)local e=e or{};setmetatable(e,{__pairs=r(function(e,t)return e<t;end,function(e)return e;end)});return e;end local function r(e,t,a)a=a:gsub("%s+"," ");if t=="class"then if e.class then e.class=e.class.." "..a;else e.class=a;end else e[t]=a;end end local function m(e,t)if t then for t,a in pairs(t)do r(e,t,a);end end end local function w(e,t)e.attr=e.attr or u();local a=1;while a<=#t do local o,i=t[a].t,t[a].s;if(o=="id")or(o=="class")then r(e.attr,o,i);elseif o=="key"then local o={};while t[a+1]and(t[a+1].t=="value")do o[#o+1]=t[a+1].s:gsub("\\(%p)","%1");a=a+1;end r(e.attr,i,table.concat(o,"\n"));end a=a+1;end end local function f(e)e.t="definition_list_item";if not c(e)then e.c={};end if e.c[1]and(e.c[1].t=="para")then e.c[1].t="term";else table.insert(e.c,1,l("term"));end if e.c[2]then local t=l("definition");t.c={};for a=2,#e.c do t.c[#t.c+1]=e.c[a];e.c[a]=nil;end e.c[2]=t;end end local function g(e)local a=nil;for e,t in pairs(e.styles)do if not a or(t<a.priority)then a={name=e,priority=t};end end e.style=a.name;e.styles=nil;e.start=t(e.startmarker,e.style);e.startmarker=nil;end local function t(t)local t=h(t);if e(t,"^ +`")then t=t:sub(2);end if e(t,"` +$")then t=t:sub(1,#t-1);end return t;end local function y(e)if not c(e)then return e;end local t=l("doc");local a={{sec=t,level=0}};for e,e in ipairs(e.c)do if e.t=="heading"then local t=e.level;local o=((#a>0)and a[#a].level)or 0;if o>=t then while a[#a].level>=t do local e=table.remove(a).sec;d(a[#a].sec,e);end end local o=l("section");o.attr=u({id=e.attr.id});e.attr.id=nil;d(o,e);a[#a+1]={sec=o,level=t};else d(a[#a].sec,e);end end while#a>1 do local e=table.remove(a).sec;d(a[#a].sec,e);end assert(a[1].sec==t);return t;end local function p(o,i)local m=o.subject;local p=o.warn;if not p then function p()end end local b;if i then b=n(m);end local i={};local n={};local v={};local function k(e)local e=e:gsub("[][~!@#$%^&*(){}`,.<>\\|=+/?]",""):gsub("^%s+",""):gsub("%s+$",""):gsub("%s+","-");local t=0;local a=e;while(a=="")or v[a]do t=t+1;if e==""then e="s";end a=e.."-"..tostring(t);end v[a]=true;return a;end local function x(e)if e then return string.format("%d:%d:%d",b.line[e],b.col[e],b.charpos[e]);end end local function j(e,t)if b then local t=x(t);if e.pos then e.pos[1]=t;else e.pos={t,nil};end end end local function q(e,t)if b and e.pos then local t=x(t);if e.pos then e.pos[2]=t;else e.pos={nil,t};end end end local x={heading=true,div=true,list=true,list_item=true,code_block=true,para=true,blockquote=true,table=true,thematic_break=true,raw_block=true,reference_definition=true,footnote=true};local z=nil;local function E(e)if z and x[e.t:gsub("%|.*","")]then for t=1,#z do w(e,z[t]);end if e.attr and e.attr.id then v[e.attr.id]=true;end z=nil;end end local v={};local x=0;local function T(e,t,a)local o=0;if a then while(v[t]=="blankline")or(v[t]=="-list_item")do t=t-1;end end for e=e,t do local t=v[e];if t=="blankline"then if not(string.find(v[e+1],"%+list_item")or(string.find(v[e+1],"%-list_item")and(a or string.find(v[e+2],"%-list_item"))))then o=o+1;end end end return o==0;end local function A(e,t)if(e[#e].t=="list")and not((t.t=="list_item")or(t.t=="definition_list_item"))then local t=table.remove(e);A(e,t);end if t.t=="list"then if t.pos then t.pos[2]=t.c[#t.c].pos[2];end local e=true;for a=1,#t.c do e=e and T(t.c[a].startidx,t.c[a].endidx,a==#t.c);t.c[a].startidx=nil;t.c[a].endidx=nil;end t.tight=e;g(t);end d(e[#e],t);end local function d(o,d,g,y)x=x+1;local b,T=string.match(y,"^([-+]?)(.+)");v[x]=y;if s[T]then return;end if b=="+"then local t=l(T);j(t,d);E(t);if T=="heading"then t.level=(g-d)+1;elseif e(T,"^list_item")then t.t="list_item";t.startidx=x;local e,e,e=string.find(T,"(%|.*)");local a={};if e then local t=1;for e in string.gmatch(e,"%|([^%|%]]*)")do a[e]=t;t=t+1;end end t.style_marker=e;local e=string.match(m,"^%S+",d);local i=o[#o];if i.t~="list"then local i=l("list");j(i,d);i.styles=a;i.attr=t.attr;i.startmarker=e;t.attr=nil;o[#o+1]=i;else local n={};local s=false;for e,t in pairs(a)do if i.styles[e]then s=true;n[e]=a[e];end end if s then i.styles=n;else local i=table.remove(o);A(o,i);local i=l("list");j(i,d);i.styles=a;i.attr=t.attr;i.startmarker=e;t.attr=nil;o[#o+1]=i;end end end o[#o+1]=t;elseif b=="-"then if o[#o].t=="list"then local e=table.remove(o);A(o,e);end if T==o[#o].t then local s=table.remove(o);q(s,g);if s.t=="block_attributes"then if not z then z={};end z[#z+1]=s.c;return;elseif s.t=="attributes"then local e=o[#o];local e=c(e)and e.c[#e.c];if e then local t=false;if e.t=="str"then local i=string.find(e.s,"[^%s]+$");if not i then t=true;elseif i>1 then local t=l("str");t.s=a(e.s,i,-1);e.s=a(e.s,1,i-1);A(o,t);e=t;end end if c(s)and not t then w(e,s.c);else p({message="Ignoring unattached attribute",pos=d});end else p({message="Ignoring unattached attribute",pos=d});end return;elseif T=="reference_definition"then local e="";local t;for a=1,#s.c do if s.c[a].t=="reference_key"then t=s.c[a].s;end if s.c[a].t=="reference_value"then e=e..s.c[a].s;end end i[t]=l("reference");i[t].destination=e;if s.attr then i[t].attr=s.attr;end return;elseif T=="footnote"then local e;if c(s)and(s.c[1].t=="note_label")then e=s.c[1].s;table.remove(s.c,1);end if e then n[e]=s;end return;elseif T=="table"then local t=1;local a={};while t<=#s.c do local o,i,n;if s.c[t].t=="row"then local h=s.c[t].c;for t=1,#h do o,n,i=e(h[t].t,"^separator_(.*)");if not o then break;end a[t]=i;end if o and(#a>0)then local e=s.c[t-1];if e and(e.t=="row")then e.head=true;for t=1,#e.c do e.c[t].head=true;if a[t]~="default"then e.c[t].align=a[t];end end end table.remove(s.c,t);else if#a>0 then for e=1,#s.c[t].c do if a[e]~="default"then s.c[t].c[e].align=a[e];end end end t=t+1;end end end elseif T=="code_block"then if c(s)then if s.c[1].t=="code_language"then s.lang=s.c[1].s;table.remove(s.c,1);elseif s.c[1].t=="raw_format"then local e=s.c[1].s:sub(2);table.remove(s.c,1);s.t="raw_block";s.format=e;end end s.s=h(s);s.c=nil;elseif e(T,"^list_item")then s.t="list_item";s.endidx=x;if s.style_marker=="|:"then f(s);end if(s.style_marker=="|X")and c(s)then if s.c[1].t=="checkbox_checked"then s.checkbox="checked";table.remove(s.c,1);elseif s.c[1].t=="checkbox_unchecked"then s.checkbox="unchecked";table.remove(s.c,1);end end s.style_marker=nil;elseif T=="inline_math"then s.t="math";s.s=t(s);s.c=nil;s.display=false;s.attr=u({class="math inline"});elseif T=="display_math"then s.t="math";s.s=t(s);s.c=nil;s.display=true;s.attr=u({class="math display"});elseif T=="imagetext"then s.t="image";elseif T=="linktext"then s.t="link";elseif T=="div"then s.c=s.c or{};if s.c[1]and(s.c[1].t=="class")then s.attr=u(s.attr);r(s.attr,"class",h(s.c[1]));table.remove(s.c,1);end elseif T=="verbatim"then s.s=t(s);s.c=nil;elseif T=="url"then s.destination=h(s);elseif T=="email"then s.destination="mailto:"..h(s);elseif T=="caption"then local e=o[#o];local e=c(e)and e.c[#e.c];if e and(e.t=="table")then table.insert(e.c,1,s);else p({message="Ignoring caption without preceding table",pos=d});end return;elseif T=="heading"then local e=h(s):gsub("^%s+",""):gsub("%s+$","");if not s.attr then s.attr=u({});end if not s.attr.id then r(s.attr,"id",k(e));end if not i[e]then i[e]=l("reference");i[e].destination="#"..s.attr.id;end elseif T=="destination"then local e=o[#o];local e=c(e)and e.c[#e.c];assert(e and((e.t=="image")or(e.t=="link")),"destination with no preceding link or image");e.destination=h(s):gsub("\r?\n","");return;elseif T=="reference"then local e=o[#o];local e=c(e)and e.c[#e.c];assert(e and((e.t=="image")or(e.t=="link")),"reference with no preceding link or image");if c(s)then e.reference=h(s):gsub("\r?\n"," ");else e.reference=h(e):gsub("\r?\n"," ");end return;end A(o,s);else assert(false,"unmatched "..y.." encountered at byte "..d);return;end else local e=l(T);E(e);j(e,d);q(e,g);if T=="softbreak"then e.s=nil;elseif T=="reference_key"then e.s=a(m,d+1,g-1);elseif T=="footnote_reference"then e.s=a(m,d+2,g-1);elseif T=="symbol"then e.alias=a(m,d+1,g-1);elseif T=="raw_format"then local t=o[#o];local t=c(t)and t.c[#t.c];if t and(t.t=="verbatim")then local e=h(t);t.t="raw_inline";t.s=e;t.c=nil;t.format=a(m,d+2,g-1);return;else e.s=a(m,d,g);end else e.s=a(m,d,g);end A(o,e);end end local e=l("doc");local t={e};for e,a,o in o:events()do d(t,e,a,o);end while#t>1 do local e=table.remove(t);A(t,e);if b and t[#t].pos then t[#t].pos[2]=e.pos[2];end end e=y(e);e.references=i;e.footnotes=n;return e;end local function e(t,a,n)n=n or 0;a:write(o(" ",n));if n>128 then a:write("(((DEEPLY NESTED CONTENT OMITTED)))\n");return;end if t.t then a:write(t.t);if t.pos then a:write(i(" (%s-%s)",t.pos[1],t.pos[2]));end for e,t in pairs(t)do if(type(e)=="string")and(e~="children")and(e~="tag")and(e~="pos")and(e~="attr")and(e~="references")and(e~="footnotes")then a:write(i(" %s=%q",e,tostring(t)));end end if t.attr then for e,t in pairs(t.attr)do a:write(i(" %s=%q",e,t));end end else io.stderr:write("Encountered node without tag:\n"..require("inspect")(t));os.exit(1);end a:write("\n");if t.c then for t,t in ipairs(t.c)do e(t,a,n+2);end end end local function t(t,a)e(t,a,0);if next(t.references)~=nil then a:write("references\n");for t,o in pairs(t.references)do a:write(i(" [%q] =\n",t));e(o,a,4);end end if next(t.footnotes)~=nil then a:write("footnotes\n");for t,o in pairs(t.footnotes)do a:write(i(" [%q] =\n",t));e(o,a,4);end end end return{to_ast=p,render=t,insert_attribute=r,copy_attributes=m,new_attributes=u,new_node=l,add_child=d,has_children=c};end;package.preload["djot.html"]=function()local e=require("djot.ast");local t=e.new_node;local a=e.new_attributes;local o=e.add_child;local i=unpack or table.unpack;local n,s=e.insert_attribute,e.copy_attributes;local h=string.format;local r,d=string.find,string.gsub;local function l(e)local t={};if e then for e,a in pairs(e)do local o=a;if type(a)=="table"then o=l(a);end t[e]=o;end end return t;end local function c(e)local t={};if e.t=="str"then t[#t+1]=e.s;elseif e.t=="nbsp"then t[#t+1]="\160";elseif e.t=="softbreak"then t[#t+1]=" ";elseif e.c and(#e.c>0)then for a=1,#e.c do t[#t+1]=c(e.c[a]);end end return table.concat(t);end local u={};function u.new(e)local t={out=function(e)io.stdout:write(e);end,tight=false,footnote_index={},next_footnote_index=1,references=nil,footnotes=nil};setmetatable(t,e);e.__index=e;return t;end u.html_escapes={["<"]="<",[">"]=">",["&"]="&",['"']="""};function u.escape_html(e,t)if r(t,"[<>&]")then return(d(t,"[<>&]",e.html_escapes));else return t;end end function u.escape_html_attribute(e,t)if r(t,'[<>&"]')then return(d(t,'[<>&"]',e.html_escapes));else return t;end end function u.render(e,t,a)e.references=t.references;e.footnotes=t.footnotes;if a then function e.out(e)a:write(e);end end e[t.t](e,t);end function u.render_children(e,t)local t,a=pcall(function()if t.c and(#t.c>0)then local a;if t.tight~=nil then a=e.tight;e.tight=t.tight;end for a=1,#t.c do e[t.c[a].t](e,t.c[a]);end if t.tight~=nil then e.tight=a;end end end);if not t and a:find("stack overflow")then e.out("(((DEEPLY NESTED CONTENT OMITTED)))\n");end end function u.render_attrs(e,t)if t.attr then for t,a in pairs(t.attr)do e.out(" "..t.."="..'"'..e:escape_html_attribute(a)..'"');end end if t.pos then local t,a=i(t.pos);e.out(' data-startpos="'..tostring(t)..'" data-endpos="'..tostring(a)..'"');end end function u.render_tag(e,t,a)e.out("<"..t);e:render_attrs(a);e.out(">");end function u.add_backlink(a,a,i)local n=t("link");n.destination="#fnref"..tostring(i);n.attr=e.new_attributes({role="doc-backlink"});local e=t("str");e.s="↩︎︎";o(n,e);if a.c[#a.c].t=="para"then o(a.c[#a.c],n);else local e=t("para");o(e,n);o(a,e);end end function u.doc(e,t)e:render_children(t);if e.next_footnote_index>1 then local t={};for a,o in pairs(e.footnotes)do if e.footnote_index[a]then t[e.footnote_index[a]]=o;end end e.out('<section role="doc-endnotes">\n<hr>\n<ol>\n');for a=1,#t do local t=t[a];if t then e.out(h('<li id="fn%d">\n',a));e:add_backlink(t,a);e:render_children(t);e.out("</li>\n");end end e.out("</ol>\n</section>\n");end end function u.raw_block(e,t)if t.format=="html"then e.out(t.s);end end function u.para(e,t)if not e.tight then e:render_tag("p",t);end e:render_children(t);if not e.tight then e.out("</p>");end e.out("\n");end function u.blockquote(e,t)e:render_tag("blockquote",t);e.out("\n");e:render_children(t);e.out("</blockquote>\n");end function u.div(e,t)e:render_tag("div",t);e.out("\n");e:render_children(t);e.out("</div>\n");end function u.section(e,t)e:render_tag("section",t);e.out("\n");e:render_children(t);e.out("</section>\n");end function u.heading(e,t)e:render_tag("h"..t.level,t);e:render_children(t);e.out("</h"..t.level..">\n");end function u.thematic_break(e,t)e:render_tag("hr",t);e.out("\n");end function u.code_block(e,t)e:render_tag("pre",t);e.out("<code");if t.lang and(#t.lang>0)then e.out(' class="language-'..t.lang..'"');end e.out(">");e.out(e:escape_html(t.s));e.out("</code></pre>\n");end function u.table(e,t)e:render_tag("table",t);e.out("\n");e:render_children(t);e.out("</table>\n");end function u.row(e,t)e:render_tag("tr",t);e.out("\n");e:render_children(t);e.out("</tr>\n");end function u.cell(e,t)local a;if t.head then a="th";else a="td";end local o=l(t.attr);if t.align then n(o,"style","text-align: "..t.align..";");end e:render_tag(a,{attr=o});e:render_children(t);e.out("</"..a..">\n");end function u.caption(e,t)e:render_tag("caption",t);e:render_children(t);e.out("</caption>\n");end function u.list(e,t)local a=t.style;if(a=="*")or(a=="+")or(a=="-")then e:render_tag("ul",t);e.out("\n");e:render_children(t);e.out("</ul>\n");elseif a=="X"then local a=l(t.attr);if a.class then a.class="task-list "..a.class;else n(a,"class","task-list");end e:render_tag("ul",{attr=a});e.out("\n");e:render_children(t);e.out("</ul>\n");elseif a==":"then e:render_tag("dl",t);e.out("\n");e:render_children(t);e.out("</dl>\n");else e.out("<ol");if t.start and(t.start>1)then e.out(' start="'..t.start..'"');end local a=d(t.style,"%p","");if a~="1"then e.out(' type="'..a..'"');end e:render_attrs(t);e.out(">\n");e:render_children(t);e.out("</ol>\n");end end function u.list_item(e,t)if t.checkbox then if t.checkbox=="checked"then e.out('<li class="checked">');elseif t.checkbox=="unchecked"then e.out('<li class="unchecked">');end else e:render_tag("li",t);end e.out("\n");e:render_children(t);e.out("</li>\n");end function u.term(e,t)e:render_tag("dt",t);e:render_children(t);e.out("</dt>\n");end function u.definition(e,t)e:render_tag("dd",t);e.out("\n");e:render_children(t);e.out("</dd>\n");end function u.definition_list_item(e,t)e:render_children(t);end function u.reference_definition(e)end function u.footnote_reference(e,t)local t=t.s;local a=e.footnote_index[t];if not a then a=e.next_footnote_index;e.footnote_index[t]=a;e.next_footnote_index=e.next_footnote_index+1;end e.out(h('<a id="fnref%d" href="#fn%d" role="doc-noteref"><sup>%d</sup></a>',a,a,a));end function u.raw_inline(e,t)if t.format=="html"then e.out(t.s);end end function u.str(e,t)if t.attr then e:render_tag("span",t);e.out(e:escape_html(t.s));e.out("</span>");else e.out(e:escape_html(t.s));end end function u.softbreak(e)e.out("\n");end function u.hardbreak(e)e.out("<br>\n");end function u.nbsp(e)e.out(" ");end function u.verbatim(e,t)e:render_tag("code",t);e.out(e:escape_html(t.s));e.out("</code>");end function u.link(e,t)local a=a({});if t.reference then local e=e.references[t.reference];if e then if e.attr then s(a,e.attr);end n(a,"href",e.destination);end elseif t.destination then n(a,"href",t.destination);end s(a,t.attr);e:render_tag("a",{attr=a});e:render_children(t);e.out("</a>");end u.url=u.link;u.email=u.link;function u.image(e,t)local a=a({});local o=c(t);if#o>0 then n(a,"alt",c(t));end if t.reference then local e=e.references[t.reference];if e then if e.attr then s(a,e.attr);end n(a,"src",e.destination);end elseif t.destination then n(a,"src",t.destination);end s(a,t.attr);e:render_tag("img",{attr=a});end function u.span(e,t)e:render_tag("span",t);e:render_children(t);e.out("</span>");end function u.mark(e,t)e:render_tag("mark",t);e:render_children(t);e.out("</mark>");end function u.insert(e,t)e:render_tag("ins",t);e:render_children(t);e.out("</ins>");end function u.delete(e,t)e:render_tag("del",t);e:render_children(t);e.out("</del>");end function u.subscript(e,t)e:render_tag("sub",t);e:render_children(t);e.out("</sub>");end function u.superscript(e,t)e:render_tag("sup",t);e:render_children(t);e.out("</sup>");end function u.emph(e,t)e:render_tag("em",t);e:render_children(t);e.out("</em>");end function u.strong(e,t)e:render_tag("strong",t);e:render_children(t);e.out("</strong>");end function u.double_quoted(e,t)e.out("“");e:render_children(t);e.out("”");end function u.single_quoted(e,t)e.out("‘");e:render_children(t);e.out("’");end function u.left_double_quote(e)e.out("“");end function u.right_double_quote(e)e.out("”");end function u.left_single_quote(e)e.out("‘");end function u.right_single_quote(e)e.out("’");end function u.ellipses(e)e.out("…");end function u.em_dash(e)e.out("—");end function u.en_dash(e)e.out("–");end function u.symbol(e,t)e.out(":"..t.alias..":");end function u.math(e,t)local a="inline";if r(t.attr.class,"display")then a="display";end e:render_tag("span",t);if a=="inline"then e.out("\\(");else e.out("\\[");end e.out(e:escape_html(t.s));if a=="inline"then e.out("\\)");else e.out("\\]");end e.out("</span>");end return{Renderer=u};end;package.preload["djot.filter"]=function()local function e(t,a)local o=a[t.t];local i,n;if type(o)=="table"then i=o.enter;n=o.exit;elseif type(o)=="function"then n=o;end if i then local e=i(t);if e then return;end end if t.c then for t,t in ipairs(t.c)do e(t,a);end end if t.footnotes then for t,t in pairs(t.footnotes)do e(t,a);end end if n then n(t);end end local function t(t,a)e(t,a);return t;end local function e(e,a)for a,a in ipairs(a)do t(e,a);end end local function t(e)local t=package.path;local e,t=pcall(function()package.path="./?.lua;"..package.path;local e=require(e:gsub("%.lua$",""));package.path=t;return e;end);if not e then return nil,t;elseif type(t)~="table"then return nil,"filter must be a table";end if#t==0 then return{t};else return t;end end local function a(e)local t,a;if _VERSION:match("5.1")then t,a=loadstring(e);else t,a=load(e);end if t then local e=t();if type(e)~="table"then return nil,"filter must be a table";end if#e==0 then return{e};else return e;end else return nil,a;end end return{apply_filter=e,require_filter=t,load_filter=a};end;package.preload["djot.json"]=function()local e={_version="0.1.2"};local t;local a={["\\"]="\\",['"']='"',["\b"]="b",["\f"]="f",["\n"]="n",["\r"]="r",["\t"]="t"};local o={["/"]="/"};for e,t in pairs(a)do o[t]=e;end local function o(e)return"\\"..(a[e]or string.format("u%04x",e:byte()));end local function a(e)return"null";end local function i(e,a)local o={};a=a or{};if a[e]then error("circular reference");end a[e]=true;if(rawget(e,1)~=nil)or(next(e)==nil)then local i=0;for e in pairs(e)do if type(e)~="number"then error("invalid table: mixed or invalid key types");end i=i+1;end if i~=#e then error("invalid table: sparse array");end for e,e in ipairs(e)do table.insert(o,t(e,a));end a[e]=nil;return"["..table.concat(o,",").."]";else for e,i in pairs(e)do if type(e)~="string"then error("invalid table: mixed or invalid key types");end table.insert(o,t(e,a)..":"..t(i,a));end a[e]=nil;return"{"..table.concat(o,",").."}";end end local function n(e)return'"'..e:gsub('[%z\1-\31\\"]',o)..'"';end local function o(e)if(e~=e)or(e<=-math.huge)or(e>=math.huge)then error("unexpected number value '"..tostring(e).."'");end return string.format("%.14g",e);end local a={["nil"]=a,table=i,string=n,number=o,boolean=tostring};function t(e,t)local o=type(e);local a=a[o];if a then return a(e,t);end error("unexpected type '"..o.."'");end function e.encode(e)return(t(e));end return e;end;function package.preload.djot()local e=unpack or table.unpack;local e=require("djot.block").Parser;local t=require("djot.ast");local a=require("djot.html");local o=require("djot.json");local i=require("djot.filter");local i={};function i.new(e)local e={};setmetatable(e,i);i.__index=i;return e;end function i.write(e,t)e[#e+1]=t;end function i.flush(e)return table.concat(e);end local function n(a,o,i)local e=e:new(a,i);return t.to_ast(e,o);end local function s(t,a)return e:new(t):events();end local function e(e)local a=i:new();t.render(e,a);return a:flush();end local function t(e)return o.encode(e).."\n";end local function o(e)local t=i:new();local a=a.Renderer:new();a:render(e,t);return t:flush();end local function a(e,t,a)return string.format("[%q,%d,%d]",a,e,t);end local function h(e,t)local o=i:new();local i=0;for e,t,n in s(e,t)do i=i+1;if i==1 then o:write("[");else o:write(",");end o:write(a(e,t,n).."\n");end o:write("]\n");return o:flush();end local i="0.2.1";local e={parse=n,parse_events=s,parse_and_render_events=h,render_html=o,render_ast_pretty=e,render_ast_json=t,render_event=a,version=i};setmetatable(e,{__index=function(e,t)local a=require("djot."..t);rawset(e,t,a);return e[t];end});return e;end local e=require("djot");return e; |