123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356(*---------------------------------------------------------------------------
Copyright (c) 2016 The b0 programmers. All rights reserved.
Distributed under the ISC license, see terms at the end of the file.
---------------------------------------------------------------------------*)moduleAt=structtypename=stringtypet=name*stringletvnv=(n,v)lettrue'n=(n,"")letintni=(n,string_of_inti)letadd_ifbatl=ifbthenat::lelselletadd_if_somenameol=matchowithNone->l|Somea->(name,a)::lletto_pair=Fun.idmoduleName=structletaccesskey="accesskey"letautofocus="autofocus"letcharset="charset"letchecked="checked"letclass'="class"letcontent="content"letcontenteditable="contenteditable"letcols="cols"letdefer="defer"letdisabled="disabled"letdir="dir"letdraggable="draggable"letfor'="for"letheight="height"lethidden="hidden"lethref="href"letid="id"letlang="lang"letmedia="media"letname="name"letplaceholder="placeholder"letrel="rel"letrequired="required"letrows="rows"letsrc="src"letspellcheck="spellcheck"lettabindex="tabindex"lettitle="title"lettype'="type"letvalue="value"letwidth="width"letwrap="wrap"endtype'acons='a->tletaccesskeys=vName.accesskeysletautofocus=true'Name.autofocusletcharset=vName.charsetletchecked=true'Name.checkedletclass's=vName.class'sletcolsi=intName.colsiletcontents=vName.contentsletcontenteditables=true'Name.contenteditableletdefer=true'Name.deferletdisabled=true'Name.disabledletdirs=vName.dirsletdraggables=true'Name.draggableletfor's=vName.for'sletheighti=intName.heightilethidden=true'Name.hiddenlethrefs=vName.hrefsletids=vName.idsletlangs=vName.langsletmedias=vName.mediasletnames=vName.namesletplaceholders=vName.placeholdersletrels=vName.relsletrequired=true'Name.requiredletrowsi=intName.rowsiletsrcs=vName.srcsletspellcheck=vName.spellchecklettabindexi=intName.tabindexilettitles=vName.titleslettype's=vName.type'sletvalues=vName.valuesletwidthi=intName.widthiletwraps=vName.valuesendmoduleEl=structtypename=stringtypefrag=|Elofname*At.tlist*fraglist|Txtofstring|Spliceoffragoption*fraglist|Rawofstringletv?(at=[])ncs=El(n,at,cs)lettxtv=Txtvletsp=Txt" "letnbsp=Txt"\u{00A0}"letsplice?sepcs=Splice(sep,cs)letvoid=Splice(None,[])letrawf=Rawf(* Output *)letaddc=Buffer.add_charletadds=Buffer.add_stringletadds_escbs=(* N.B. we also escape @'s since ocamldoc trips over them. *)letlen=String.lengthsinletmax_idx=len-1inletflushbstarti=ifstart<lenthenBuffer.add_substringbsstart(i-start);inletrecloopstarti=ifi>max_idxthenflushbstartielseletnext=i+1inmatchString.getsiwith|'&'->flushbstarti;addsb"&";loopnextnext|'<'->flushbstarti;addsb"<";loopnextnext|'>'->flushbstarti;addsb">";loopnextnext|'\''->flushbstarti;addsb"'";loopnextnext|'\"'->flushbstarti;addsb""";loopnextnext|'@'->flushbstarti;addsb"@";loopnextnext|c->loopstartnextinloop00letvoid_els=B0_std.String.Set.of_list["area";"base";"br";"col";"embed";"hr";"img";"input";"link";"meta";"param";"source";"track";"wbr"]letrecadd_atsbcsatts=letadd_atbnv=addsbn;addsb"=\"";adds_escbv;addcb'\"'inmatchattswith|("class",c)::atts->add_atsb(c::cs)atts|(n,v)::atts->addcb' ';add_atbnv;add_atsbcsatts|[]whencs=[]->()|[]->addcb' ';add_atb"class"(String.concat" "(List.revcs))letrecadd_childb=function(* not T.R. *)|Rawr->addsbr|Txttxt->adds_escbtxt|Splice(sep,cs)->beginmatchsepwith|None->List.iter(add_childb)cs|Somesep->beginmatchcswith|[]->()|c::cs->letaddbc=add_childbsep;add_childbcinadd_childbc;List.iter(addb)csendend|El(n,atts,cs)->addcb'<';addsbn;add_atsb[]atts;addcb'>';ifnot(B0_std.String.Set.memnvoid_els)then(List.iter(add_childb)cs;addsb"</";addsbn;addcb'>')letadd_doc_typeb=addsb"<!DOCTYPE html>\n"letbuffer_add~doc_typebcs=ifdoc_typethenadd_doc_typeb;add_childbcsletto_string~doc_typeg=letb=Buffer.create65525inbuffer_add~doc_typebg;Buffer.contentsb(* Predefined element constructors *)typecons=?at:At.tlist->fraglist->fragtypevoid_cons=?at:At.tlist->unit->fraglet[@inline]conse?atels=v?ateelslet[@inline]void_conse?at()=ve?at[]leta=cons"a"letabbr=cons"abbr"letaddress=cons"address"letarea=void_cons"area"letarticle=cons"article"letaside=cons"aside"letaudio=cons"audio"letb=cons"b"letbase=void_cons"base"letbdi=cons"bdi"letbdo=cons"bdo"letblockquote=cons"blockquote"letbody=cons"body"letbr=void_cons"br"letbutton=cons"button"letcanvas=cons"canvas"letcaption=cons"caption"letcite=cons"cite"letcode=cons"code"letcol=void_cons"col"letcolgroup=cons"colgroup"letcommand=cons"command"letdatalist=cons"datalist"letdd=cons"dd"letdel=cons"del"letdetails=cons"details"letdfn=cons"dfn"letdiv=cons"div"letdl=cons"dl"letdt=cons"dt"letem=cons"em"letembed=void_cons"embed"letfieldset=cons"fieldset"letfigcaption=cons"figcaption"letfigure=cons"figure"letfooter=cons"footer"letform=cons"form"leth1=cons"h1"leth2=cons"h2"leth3=cons"h3"leth4=cons"h4"leth5=cons"h5"leth6=cons"h6"lethead=cons"head"letheader=cons"header"lethgroup=cons"hgroup"lethr=void_cons"hr"lethtml=cons"html"leti=cons"i"letiframe=cons"iframe"letimg=void_cons"img"letinput=void_cons"input"letins=cons"ins"letkbd=cons"kbd"letkeygen=cons"keygen"letlabel=cons"label"letlegend=cons"legend"letli=cons"li"letlink=void_cons"link"letmap=cons"map"letmark=cons"mark"letmenu=cons"menu"letmeta=void_cons"meta"letmeter=cons"meter"letnav=cons"nav"letnoscript=cons"noscript"letobject'=cons"object"letol=cons"ol"letoptgroup=cons"optgroup"letoption=cons"option"letoutput=cons"output"letp=cons"p"letparam=void_cons"param"letpre=cons"pre"letprogress=cons"progress"letq=cons"q"letrp=cons"rp"letrt=cons"rt"letruby=cons"ruby"lets=cons"s"letsamp=cons"samp"letscript=cons"script"letsection=cons"section"letselect=cons"select"letsmall=cons"small"letsource=void_cons"source"letspan=cons"span"letstrong=cons"strong"letstyle=cons"style"letsub=cons"sub"letsummary=cons"summary"letsup=cons"sup"lettable=cons"table"lettbody=cons"tbody"lettd=cons"td"lettextarea=cons"textarea"lettfoot=cons"tfoot"letth=cons"th"letthead=cons"thead"lettime=cons"time"lettitle=cons"title"lettr=cons"tr"lettrack=void_cons"track"letu=cons"u"letul=cons"ul"letvar=cons"var"letvideo=cons"video"letwbr=void_cons"wbr"(* Convenience *)lettitle_of_fpathfile=matchB0_std.Fpath.basename~no_ext:truefilewith|"index"|""->lettitle=B0_std.Fpath.(basename~no_ext:true(parentfile))iniftitle=""then"Untitled"elsetitle|title->titleletbasic_page?(lang="")?(generator="")?(styles=[])?(scripts=[])?(more_head=void)~title:tbody=letviewport="width=device-width, initial-scale=1.0"inletgenerator=matchgeneratorwith|""->void|g->meta~at:At.[name"generator";contentg]()inletstyleuri=link~at:At.[rel"stylesheet";type'"text/css";hrefuri]()inletscripturi=script~at:At.[type'"text/javascript";defer;srcuri][]inlethead=head[meta~at:At.[charset"utf-8"]();generator;meta~at:At.[name"viewport";contentviewport]();splice(List.mapstylestyles);splice(List.mapscriptscripts);more_head;title[txt(ifString.trimt=""then"Untilted"elset)]]inletat=iflang=""then[]else[At.langlang]inhtml~at[head;body]letwrite_page?(lang="")?(generator="")?(styles=[])?(scripts=[])?more_head?(title="")m~frag~o=(* FIXME Ideally we would like the read to be in write.
The write fun return a future but this has other impacts. *)letopenB0_std.Fut.Syntaxinignore@@(* FIXME maybe get rid of that. *)let*contents=B00.Memo.readmfraginlettitle=iftitle=""thentitle_of_fpathoelsetitleinletmore_head=matchmore_headwith|None->""|Somemore_head->to_string~doc_type:falsemore_headinletstamp=lang::generator::more_head::title::[]inletstamp=List.rev_appendstylesstampinletstamp=List.rev_appendscriptsstampinletstamp=String.concat""stampinB0_std.Fut.return@@(B00.Memo.writem~stamp~reads:[frag]o@@fun()->letmore_head=rawmore_headinletbody=body[rawcontents]inletpage=basic_page~lang~generator~styles~scripts~more_head~titlebodyinOk(to_string~doc_type:truepage))end(*---------------------------------------------------------------------------
Copyright (c) 2016 The b0 programmers
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
---------------------------------------------------------------------------*)