123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305openAstringopenOdoc_modelopenOdoc_model.NamesopenOr_error(*
* 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.
*)typeparent_spec=|ExplicitofPaths.Identifier.ContainerPage.t*Lang.Page.childlist|PackageofPaths.Identifier.ContainerPage.t|Noparenttypeparent_cli_spec=|CliParentofstring|CliPackageofstring|CliNoparentletcheck_is_nonemsg=functionNone->Ok()|Some_->Error(`Msgmsg)letcheck_is_emptymsg=function[]->Ok()|_::_->Error(`Msgmsg)(** Used to disambiguate child references. *)letis_module_namen=String.lengthn>0&&Char.Ascii.is_uppern.[0](** Accepted child references:
- [asset-foo] child is an arbitrary asset
- [module-Foo] child is a module.
- [module-foo], [Foo] child is a module, for backward compatibility.
- [page-foo] child is a container or leaf page.
- [src-foo] child is a source tree
Parses [...-"foo"] as [...-foo] for backward compatibility. *)letparse_parent_child_references=letunquotes=letlen=String.lengthsinifString.heads=Some'"'&&String.head~rev:trues=Some'"'&&len>1thenString.with_range~first:1~len:(len-2)selsesinmatchString.cut~sep:"-"swith|Some("page",n)->Ok(Lang.Page.Page_child(unquoten))|Some("src",n)->Ok(Source_tree_child(unquoten))|Some("asset",n)->Ok(Asset_child(unquoten))|Some("module",n)->Ok(Module_child(unquote(String.Ascii.capitalizen)))|Some(k,_)->Error(`Msg("Unrecognized kind: "^k))|None->ifis_module_namesthenOk(Module_childs)elseOk(Page_childs)letresolve_parent_pageresolverf=letfind_parent=function|Lang.Page.Page_childp->(matchResolver.lookup_pageresolverpwith|Somer->Okr|None->Error(`Msg"Couldn't find specified parent page"))|Source_tree_child_|Module_child_|Asset_child_->Error(`Msg"Expecting page as parent")inletextract_parent=function|{Paths.Identifier.iv=`Page_;_}ascontainer->Okcontainer|_->Error(`Msg"Specified parent is not a parent of this file")inparse_parent_child_referencef>>=funr->find_parentr>>=funpage->extract_parentpage.name>>=funparent->Ok(parent,page.children)letparentresolverparent_cli_spec=matchparent_cli_specwith|CliParentf->resolve_parent_pageresolverf>>=fun(parent,children)->Ok(Explicit(parent,children))|CliPackagepackage->Ok(Package(Paths.Identifier.Mk.page(None,PageName.make_stdpackage)))|CliNoparent->OkNoparentletresolve_importsresolverimports=List.map(function|Lang.Compilation_unit.Import.Resolved_asresolved->resolved|Unresolved(name,_)asunresolved->(matchResolver.resolve_importresolvernamewith|Someroot->Resolved(root,Names.ModuleName.make_stdname)|None->unresolved))imports(** Raises warnings and errors. *)letresolve_and_substitute~resolver~make_root~source_id_opt~cmt_filename_opt~hidden(parent:Paths.Identifier.ContainerPage.toption)input_fileinput_type=letfilename=Fs.File.to_stringinput_fileinletunit=matchinput_typewith|`Cmti->Odoc_loader.read_cmti~make_root~parent~filename~source_id_opt~cmt_filename_opt|>Error.raise_errors_and_warnings|`Cmt->Odoc_loader.read_cmt~make_root~parent~filename~source_id_opt|>Error.raise_errors_and_warnings|`Cmi->Odoc_loader.read_cmi~make_root~parent~filename|>Error.raise_errors_and_warningsinletunit={unitwithhidden=hidden||unit.hidden}inifnotunit.Lang.Compilation_unit.interfacethenPrintf.eprintf"WARNING: not processing the \"interface\" file.%s\n%!"(ifnot(Filename.check_suffixfilename"cmt")then""(* ? *)elsePrintf.sprintf" Using %S while you should use the .cmti file"filename);(* Resolve imports, used by the [link-deps] command. *)letunit={unitwithimports=resolve_importsresolverunit.imports}inletenv=Resolver.build_compile_env_for_unitresolverunitinletcompiled=Odoc_xref2.Compile.compile~filenameenvunit|>Error.raise_warningsin(* [expand unit] fetches [unit] from [env] to get the expansion of local, previously
defined, elements. We'd rather it got back the resolved bit so we rebuild an
environment with the resolved unit.
Note that this is bad and once rewritten expand should not fetch the unit it is
working on. *)(* let expand_env = Env.build env (`Unit resolved) in*)(* let expanded = Odoc_xref2.Expand.expand (Env.expander expand_env) resolved in *)compiledletroot_of_compilation_unit~parent_spec~hidden~output~module_name~digest=letopenRootinletfilename=Filename.chop_extensionFs.File.(to_string@@basenameoutput)inletresultparent=letfile=Odoc_file.create_unit~force_hidden:hiddenmodule_nameinOk{id=Paths.Identifier.Mk.root(parent,ModuleName.make_stdmodule_name);file;digest;}inletcheck_child=function|Lang.Page.Module_childn->String.Ascii.(uncapitalizen=uncapitalizefilename)|Asset_child_|Source_tree_child_|Page_child_->falseinmatchparent_specwith|Noparent->resultNone|Explicit(parent,children)->ifList.existscheck_childchildrenthenresult(Someparent)elseError(`Msg"Specified parent is not a parent of this file")|Packageparent->result(Someparent)letname_of_output~prefixoutput=letpage_dash_root=Filename.chop_extensionFs.File.(to_string@@basenameoutput)inString.drop~max:(String.lengthprefix)page_dash_rootletpage_name_of_output~is_parent_explicitoutput=letroot_name=name_of_output~prefix:"page-"outputin(ifis_parent_explicitthenmatchroot_namewith|"index"->Format.eprintf"Warning: Potential name clash - child page named 'index'\n%!"|_->());root_nameletmld~parent_spec~output~children~warnings_optionsinput=List.fold_left(funaccchild_str->match(acc,parse_parent_child_referencechild_str)with|Okacc,Okr->Ok(r::acc)|Errorm,_->Errorm|_,Error(`Msgm)->Error(`Msg("Failed to parse child reference: "^m))|_,Error_->Error(`Msg"Unknown failure parsing child reference"))(Ok[])children>>=funchildren->letroot_name=letis_parent_explicit=matchparent_specwithExplicit_->true|_->falseinpage_name_of_output~is_parent_explicitoutputinletinput_s=Fs.File.to_stringinputinletdigest=Digest.fileinput_sinletpage_name=PageName.make_stdroot_nameinletcheck_child=function|Lang.Page.Page_childn->root_name=n|Asset_child_|Source_tree_child_|Module_child_->falsein(ifchildren=[]then(* No children, this is a leaf page. *)matchparent_specwith|Explicit(p,_)->Ok(Paths.Identifier.Mk.leaf_page(Somep,page_name))|Packageparent->Ok(Paths.Identifier.Mk.leaf_page(Someparent,page_name))|Noparent->Ok(Paths.Identifier.Mk.leaf_page(None,page_name))else(* Has children, this is a container page. *)letcheckparents_childrenv=ifList.existscheck_childparents_childrenthenOkvelseError(`Msg"Specified parent is not a parent of this file")in(matchparent_specwith|Explicit(p,cs)->checkcs@@Paths.Identifier.Mk.page(Somep,page_name)|Packageparent->Ok(Paths.Identifier.Mk.page(Someparent,page_name))(* This is a bit odd *)|Noparent->Ok(Paths.Identifier.Mk.page(None,page_name)))>>=funid->Ok(id:>Paths.Identifier.Page.t))>>=funname->letroot=letfile=Root.Odoc_file.create_pageroot_namein{Root.id=(name:>Paths.Identifier.OdocId.t);file;digest}inletresolvecontent=letpage=Lang.Page.{name;root;children;content;digest;linked=false}inOdoc_file.save_pageoutput~warnings:[]page;Ok()inFs.File.readinput>>=funstr->Odoc_loader.read_string(name:>Paths.Identifier.LabelParent.t)input_sstr|>Error.handle_errors_and_warnings~warnings_options>>=function|`Stop->resolve[](* TODO: Error? *)|`Docscontent->resolvecontentlethandle_file_extext=matchextwith|".cmti"->Ok`Cmti|".cmt"->Ok`Cmt|".cmi"->Ok`Cmi|_->Error(`Msg"Unknown extension, expected one of: cmti, cmt, cmi or mld.")letcompile~resolver~parent_cli_spec~hidden~children~output~warnings_options~source~cmt_filename_optinput=parentresolverparent_cli_spec>>=funparent_spec->letext=Fs.File.get_extinputinifext=".mld"thencheck_is_none"Not expecting source (--source-*) when compiling pages."source>>=fun()->check_is_none"Not expecting cmt filename (--cmt) when compiling pages."cmt_filename_opt>>=fun()->mld~parent_spec~output~warnings_options~childreninputelsecheck_is_empty"Not expecting children (--child) when compiling modules."children>>=fun()->(matchsourcewith|Some(parent,name)->(Odoc_file.loadparent>>=funparent->leterr_not_parent()=Error(`Msg"Specified source-parent is not a parent of the source.")inmatchparent.Odoc_file.contentwith|Odoc_file.Source_tree_contentpage->(matchpage.Lang.SourceTree.namewith|{Paths.Identifier.iv=`Page_;_}asparent_id->letid=Paths.Identifier.Mk.source_page(parent_id,name)inifList.exists(Paths.Identifier.equalid)page.source_childrenthenOk(Someid)elseerr_not_parent()|{iv=`LeafPage_;_}->err_not_parent())|Unit_content_|Odoc_file.Page_content_->Error(`Msg"Specified source-parent should be a page but is a module."))|None->OkNone)>>=funsource_id_opt->handle_file_extext>>=funinput_type->letparent=matchparent_specwith|Noparent->None|Explicit(parent,_)->Someparent|Packageparent->Someparentinletmake_root=root_of_compilation_unit~parent_spec~hidden~outputinletresult=Error.catch_errors_and_warnings(fun()->resolve_and_substitute~resolver~make_root~hidden~source_id_opt~cmt_filename_optparentinputinput_type)in(* Extract warnings to write them into the output file *)let_,warnings=Error.unpack_warningsresultinError.handle_errors_and_warnings~warnings_optionsresult>>=fununit->Odoc_file.save_unitoutput~warningsunit;Ok()