123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296(*---------------------------------------------------------------------------
Copyright (c) 2018 The b0 programmers. All rights reserved.
Distributed under the ISC license, see terms at the end of the file.
---------------------------------------------------------------------------*)openB0_stdopenB0_std.Fut.SyntaxopenB00letread_path_writesmfile=letparse_pathnpacc=matchFpath.of_stringpwith|Errore->B00_lines.errn"%s"e|Okp->p::accinletparselines=B00_lines.fold~file(String.trimlines)parse_path[]inlet*lines=Memo.readmfileinFut.return(Memo.fail_if_errorm(parselines))lettool=Tool.by_name"odoc"(* FIXME conf ! *)letfiles_to_includesfs=(* So sad https://github.com/ocaml/odoc/issues/81 *)letadd_diraccf=Fpath.Set.add(Fpath.parentf)accinletdirs=List.fold_leftadd_dirFpath.Set.emptyfsinFpath.Set.fold(fundiracc->Cmd.(acc%"-I"%%pathdir))dirsCmd.emptymoduleCompile=structmoduleDep=structtypet=string*Digest.tletname=fstletdigest=sndletppppf(n,d)=Fmt.pfppf"@[%s %s@]"n(Digest.to_hexd)letparse_depnlineacc=matchString.cut_right~sep:" "linewith|None->B00_lines.errn"Could not parse line %S"line|Some(name,digest)->letdigest=tryDigest.from_hexdigestwith|Invalid_argument_(* sic *)->B00_lines.errn"Could not parse digest %S"digestin(name,digest)::accletwritemcobj~o=letodoc=Memo.toolmtoolinMemo.spawnm~reads:[cobj]~writes:[o]~stdout:(`Fileo)@@odocCmd.(atom"compile-deps"%%pathcobj)letreadmfile=letparselines=B00_lines.fold~file(String.trimlines)parse_dep[]inlet*lines=Memo.readmfileinFut.return(Memo.fail_if_errorm(parselines))endmoduleWrites=structletwritemcobj~to_odoc~o=letodoc=Memo.toolmtoolinMemo.spawnm~reads:[cobj]~writes:[o]~stdout:(`Fileo)@@odocCmd.(atom"compile-targets"%"-o"%%pathto_odoc%%pathcobj)letread=read_path_writesendletcmd?(resolve_forward_deps=false)?(hidden=false)m~odoc_deps~writes~pkgcobj~o=letodoc=Memo.toolmtoolinletincs=files_to_includesodoc_depsinMemo.spawnm~reads:(cobj::odoc_deps)~writes@@odocCmd.(atom"compile"%"--pkg"%pkg%%if'hidden(atom"--hidden")%%if'resolve_forward_deps(atom"--resolve-fwd-refs")%"-o"%%patho%%pathcobj%%incs)letto_odocm?hidden~pkg~odoc_depsobj~o:odoc=ignore@@(* FIXME maybe remove that. *)letwrites=Fpath.(odoc+".writes")inWrites.writemobj~to_odoc:odoc~o:writes;let*writes=Writes.readmwritesincmdm?hidden~odoc_deps~writes~pkgobj~o:odoc;Fut.return()endmoduleHtml=structmoduleDep=structtypet=string*string*Digest.tletpkg(p,_,_)=pletname(_,n,_)=nletdigest(_,_,d)=dletto_compile_dep(_,n,d)=(n,d)letparse_depnlineacc=matchString.cut_right~sep:" "linewith|None->B00_lines.errn"Could not parse line %S"line|Some(rest,digest)->letdigest=tryDigest.from_hexdigestwith|Invalid_argument_(* sic *)->B00_lines.errn"Could not parse digest %S"digestinmatchString.cut_right~sep:" "restwith|Some(pkg,name)->(pkg,name,digest)::acc|None->B00_lines.errn"Could not parse pkg and mod names %S"restletwritem~odoc_filespkg_odoc_dir~o=letodoc=Memo.toolmtoolinMemo.spawnm~reads:odoc_files~writes:[o]~stdout:(`Fileo)@@odocCmd.(atom"html-deps"%%pathpkg_odoc_dir)letreadmfile=letparselines=B00_lines.fold~file(String.trimlines)parse_dep[]inlet*lines=Memo.readmfileinFut.return(Memo.fail_if_errorm(parselines))endmoduleWrites=structletwritem~odoc_depsodoc_file~to_dir~o=letodoc=Memo.toolmtoolinletincs=files_to_includesodoc_depsinletreads=odoc_file::odoc_depsinMemo.spawnm~reads~writes:[o]~stdout:(`Fileo)@@odocCmd.(atom"html-targets"%%incs%"-o"%%pathto_dir%%pathodoc_file)letread=read_path_writesendletcmd?(hidden=false)?theme_urim~odoc_deps~writesodoc_file~to_dir=letodoc=Memo.toolmtoolinletincs=files_to_includesodoc_depsinlettheme_uri=matchtheme_uriwith|None->Cmd.empty|Someu->Cmd.(atom"--theme-uri"%u)inMemo.spawnm~reads:(odoc_file::odoc_deps)~writes@@odocCmd.(atom"html"%%if'hidden(atom"--hidden")%%theme_uri%"-o"%%pathto_dir%%pathodoc_file%%incs)letwritem?theme_uri~html_dir~odoc_depsodoc=ignore@@(* FIXME maybe remove that *)letwrites=Fpath.(odoc-+".html.writes")inletodoc_deps=matchodoc_depswith|[]->(* Hack to work around https://github.com/ocaml/odoc/issues/290.
If we have only mld's html-deps returns nothing. This
will at least include the package directory. *)[odoc]|deps->(* This will also be wrong in general for mld pages. So we add
again the odoc's file directory by default. *)(odoc::deps)inWrites.writem~odoc_depsodoc~to_dir:html_dir~o:writes;let*writes=Writes.readmwritesincmdm?theme_uri~odoc_deps~writesodoc~to_dir:html_dir;Fut.return()endmoduleHtml_fragment=structletcmdm~odoc_depsmld_file~o=letodoc=Memo.toolmtoolinletincs=files_to_includesodoc_depsinMemo.spawnm~reads:(mld_file::odoc_deps)~writes:[o]@@odocCmd.(atom"html-fragment"%"-o"%%patho%%pathmld_file%%incs)endmoduleSupport_files=structmoduleWrites=structletwrite?(without_theme=false)m~to_dir~o=letodoc=Memo.toolmtoolinMemo.spawnm~reads:[]~writes:[o]~stdout:(`Fileo)@@odocCmd.(atom"support-files-targets"%%if'without_theme(atom"--without-theme")%"-o"%%pathto_dir)letread=read_path_writesendletcmd?(without_theme=false)m~writes~to_dir=letodoc=Memo.toolmtoolinlettheme=Cmd.(if'without_theme(atom"--without-theme"))inMemo.spawnm~reads:[]~writes@@odocCmd.(atom"support-files"%%theme%"-o"%%pathto_dir)letwritem~without_theme~html_dir~build_dir=ignore@@(* FIXME maybe remove that *)leto=Fpath.(build_dir/"odoc-support-files.writes")inletto_dir=html_dirinWrites.writem~without_theme~to_dir~o;let*writes=Writes.readmoincmdm~writes~without_theme~to_dir;Fut.return()endmoduleTheme=structletdefault_uri="_odoc-theme"(* Theme names *)typename=stringletodoc_default="odoc.default"letodig_default="odig.default"(* User preference *)letconfig_file=Fpath.v"odig/odoc-theme"letset_user_preferencename=tryletconfig=Os.Dir.config()|>Result.to_failureinletconfig_file=Fpath.(config//config_file)inmatchnamewith|None->Result.bind(Os.File.deleteconfig_file)@@fun_->Ok()|Somename->Os.File.write~force:true~make_path:trueconfig_filenamewithFailuree->Erroreletget_user_preference()=tryletconfig=Os.Dir.config()|>Result.to_failureinletfile=Fpath.(config//config_file)inmatchOs.File.existsfile|>Result.to_failurewith|false->OkNone|true->letname=String.trim(Os.File.readfile|>Result.to_failure)inOk(Somename)withFailuree->Errore(* Theme *)typet=name*Fpath.tletname(n,_)=nletpath(_,p)=pletppppf(n,p)=Fmt.pfppf"@[<h>%s %a@]"n(Fmt.tty[`Faint]Fpath.pp_unquoted)pletpp_nameppf(n,_)=Fmt.stringppfnletof_dirdir=Log.time(fun_m->m"theme list of %a"Fpath.pp_quoteddir)@@fun()->tryletadd_themes_pkgdiracc=lettdir=Fpath.(dir/"odoc-theme")inmatchOs.Dir.existstdir|>Result.to_failurewith|false->acc|true->letnamepkgname=Fmt.str"%s.%s"pkgnameinletadd_theme_subdiracc=(namepkgsub,dir)::accinResult.to_failure@@Os.Dir.fold_dirs~recurse:falseadd_themetdiraccinletts=Os.Dir.fold_dirs~recurse:falseadd_themesdir[]inletcompare(n0,_)(n1,_)=compare(String.Ascii.lowercasen0)(String.Ascii.lowercasen1)inList.sortcompare(Result.to_failurets)withFailuree->Log.err(funm->m"theme list: %s"e);[]letfind~fallbacknts=matchList.find(funt->namet=n)tswith|t->Okt|exceptionNot_found->letpp_name=Fmt.(codestring)inletss=String.suggest(List.rev_mapnamets)ninletpp_fallbackppf=function|None->()|Somef->Fmt.pfppf"@ using %a instead"pp_namefinFmt.error"@[Unknown theme %a%a.@ %a@]"pp_namenpp_fallbackfallback(Fmt.did_you_meanpp_name)ss(* Writing *)letwritemtheme~to_dir=(* XXX this is basically a copy dir we likely want to provide
that in the API somewhere *)letcopy_filem~src_root~dst_rootsrc=letdst=Fpath.reroot~root:src_root~dst:dst_rootsrcinB00.Memo.file_readymsrc;B00.Memo.copym~srcdstinletsrc_root=paththemeinletfiles=Os.Dir.fold_files~recurse:trueOs.Dir.path_listsrc_root[]inletfiles=Memo.fail_if_errormfilesinList.iter(copy_filem~src_root~dst_root:to_dir)filesend(*---------------------------------------------------------------------------
Copyright (c) 2018 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.
---------------------------------------------------------------------------*)