123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138(*
* Copyright (c) 2014 Leo White <leo@lpw25.net>
*
* Permission to use, copy, modify, and 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.
*)openStdLabelsopenOr_errorletto_html_tree_page?theme_uri~syntaxv=matchsyntaxwith|Odoc_html.Tree.Reason->Odoc_html.Generator.Reason.page?theme_uriv|Odoc_html.Tree.OCaml->Odoc_html.Generator.ML.page?theme_urivletto_html_tree_compilation_unit?theme_uri~syntaxv=matchsyntaxwith|Odoc_html.Tree.Reason->Odoc_html.Generator.Reason.compilation_unit?theme_uriv|Odoc_html.Tree.OCaml->Odoc_html.Generator.ML.compilation_unit?theme_urivletfrom_odoc~env?(syntax=Odoc_html.Tree.OCaml)?theme_uri~output:root_dirinput=Root.readinput>>=funroot->matchroot.filewith|Pagepage_name->Page.loadinput>>=funpage->letresolve_env=Env.buildenv(`Pagepage)inOdoc_xref.resolve_page(Env.resolverresolve_env)page>>=funodoctree->letpkg_name=root.packageinletpages=to_html_tree_page?theme_uri~syntaxodoctreeinletpkg_dir=Fs.Directory.reach_from~dir:root_dirpkg_nameinFs.Directory.mkdir_ppkg_dir;Odoc_html.Tree.traversepages~f:(fun~parents_pkg_namecontent->assert(parents=[]);letoc=letf=Fs.File.create~directory:pkg_dir~name:(page_name^".html")inopen_out(Fs.File.to_stringf)inletfmt=Format.formatter_of_out_channelocinFormat.fprintffmt"%a@?"(Tyxml.Html.pp())content;close_outoc);Ok()|Compilation_unit{hidden=_;_}->(* If hidden, we should not generate HTML. See
https://github.com/ocaml/odoc/issues/99. *)Compilation_unit.loadinput>>=fununit->letunit=Odoc_xref.Lookup.lookupunitinbegin(* See comment in compile for explanation regarding the env duplication. *)letresolve_env=Env.buildenv(`Unitunit)inOdoc_xref.resolve(Env.resolverresolve_env)unit>>=funresolved->letexpand_env=Env.buildenv(`Unitresolved)inOdoc_xref.expand(Env.expanderexpand_env)resolved>>=funexpanded->Odoc_xref.Lookup.lookupexpanded|>Odoc_xref.resolve(Env.resolverexpand_env)(* Yes, again. *)end>>=funodoctree->letpkg_dir=Fs.Directory.reach_from~dir:root_dirroot.packageinletpages=to_html_tree_compilation_unit?theme_uri~syntaxodoctreeinOdoc_html.Tree.traversepages~f:(fun~parentsnamecontent->letdirectory=letdir=List.fold_right~f:(funnamedir->Fs.Directory.reach_from~dirname)parents~init:pkg_dirinFs.Directory.reach_from~dirnameinletoc=Fs.Directory.mkdir_pdirectory;letfile=Fs.File.create~directory~name:"index.html"inopen_out(Fs.File.to_stringfile)inletfmt=Format.formatter_of_out_channelocinFormat.fprintffmt"%a@?"(Tyxml.Html.pp())content;close_outoc);Ok()(* Used only for [--index-for] which is deprecated and available only for
backward compatibility. It should be removed whenever. *)letfrom_mld~env?(syntax=Odoc_html.Tree.OCaml)~package~output:root_dir~warn_errorinput=Odoc_model.Error.set_warn_errorwarn_error;letroot_name="index"inletdigest=Digest.file(Fs.File.to_stringinput)inletroot=letfile=Odoc_model.Root.Odoc_file.create_pageroot_namein{Odoc_model.Root.package;file;digest}inletname=`Page(root,Odoc_model.Names.PageName.of_stringroot_name)inletlocation=letpos=Lexing.{pos_fname=Fs.File.to_stringinput;pos_lnum=0;pos_cnum=0;pos_bol=0}inLocation.{loc_start=pos;loc_end=pos;loc_ghost=true}inletto_htmlcontent=(* This is a mess. *)letpage=Odoc_model.Lang.Page.{name;content;digest}inletpage=Odoc_xref.Lookup.lookup_pagepageinletenv=Env.buildenv(`Pagepage)inOdoc_xref.resolve_page(Env.resolverenv)page>>=funresolved->letpages=to_html_tree_page~syntaxresolvedinletpkg_dir=Fs.Directory.reach_from~dir:root_dirroot.packageinFs.Directory.mkdir_ppkg_dir;Odoc_html.Tree.traversepages~f:(fun~parents_pkg_namecontent->assert(parents=[]);letoc=letf=Fs.File.create~directory:pkg_dir~name:"index.html"inopen_out(Fs.File.to_stringf)inletfmt=Format.formatter_of_out_channelocinFormat.fprintffmt"%a@?"(Tyxml.Html.pp())content;close_outoc);Ok()inmatchFs.File.readinputwith|Error_ase->e|Okstr->matchOdoc_loader.read_stringnamelocationstrwith|Errore->Error(`Msg(Odoc_model.Error.to_stringe))|Ok(`Docscontent)->to_htmlcontent|Ok`Stop->to_html[](* TODO: Error? *)