Skip to content

Commit

Permalink
odoc_index: Replace -P/-L with --root arguments
Browse files Browse the repository at this point in the history
Co-authored-by: Paul-Elliot <peada@free.fr>
  • Loading branch information
gpetiot and panglesd committed Nov 15, 2024
1 parent bcb5314 commit 5a79e19
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 105 deletions.
19 changes: 19 additions & 0 deletions src/index/page_hierarchy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,24 @@ let rec remove_common_root (v : t) =

let of_list l =
let dir = empty_t None in
let l =
List.filter_map
(fun (page : Lang.Page.t) ->
match page.name with
| { iv = #Paths.Identifier.LeafPage.t_pv; _ } as id ->
let title =
match Odoc_model.Comment.find_zero_heading page.content with
| None ->
[
Location_.at (Location_.span [])
(`Word (Paths.Identifier.name id));
]
| Some title -> title
in
let children_order = page.frontmatter.Frontmatter.children_order in
Some (id, title, children_order)
| _ -> None)
l
in
List.iter (add dir) l;
t_of_in_progress dir |> remove_common_root
4 changes: 1 addition & 3 deletions src/index/page_hierarchy.mli
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
open Odoc_model
open Odoc_model.Paths
open Odoc_utils

(** Page hierarchies represent a hierarchy of pages. *)
Expand All @@ -12,7 +11,6 @@ type index =

type t = index Tree.t

val of_list :
(Identifier.LeafPage.t * title * Frontmatter.children_order option) list -> t
val of_list : Lang.Page.t list -> t
(** Uses the convention that the [index] children passes its payload to the
container directory to output a payload *)
33 changes: 10 additions & 23 deletions src/odoc/bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -468,14 +468,12 @@ module Indexing = struct
| None, `JSON -> Ok (Fs.File.of_string "index.json")
| None, `Marshall -> Ok (Fs.File.of_string "index.odoc-index")

let index dst json warnings_options page_roots lib_roots inputs_in_file inputs
occurrences =
let index dst json warnings_options roots inputs_in_file inputs occurrences =
let marshall = if json then `JSON else `Marshall in
output_file ~dst marshall >>= fun output ->
Antichain.check (page_roots |> List.map ~f:snd) ~opt:"-P" >>= fun () ->
Antichain.check (lib_roots |> List.map ~f:snd) ~opt:"-L" >>= fun () ->
Indexing.compile marshall ~output ~warnings_options ~occurrences ~lib_roots
~page_roots ~inputs_in_file ~odocls:inputs
Antichain.check (roots |> List.map ~f:snd) ~opt:"--root" >>= fun () ->
Indexing.compile marshall ~output ~warnings_options ~occurrences ~roots
~inputs_in_file ~odocls:inputs
let cmd =
let dst =
let doc =
Expand Down Expand Up @@ -510,31 +508,20 @@ module Indexing = struct
let doc = ".odocl file to index" in
Arg.(value & pos_all convert_fpath [] & info ~doc ~docv:"FILE" [])
in
let page_roots =
let doc =
"Specifies a directory PATH containing pages that should be included \
in the sidebar, under the NAME section."
in
Arg.(
value
& opt_all convert_named_root []
& info ~docs ~docv:"NAME:PATH" ~doc [ "P" ])
in
let lib_roots =
let roots =
let doc =
"Specifies a directory PATH containing units that should be included \
in the sidebar, as part of the LIBNAME library."
"Specifies a directory PATH containing pages or units that should be \
included in the sidebar."
in

Arg.(
value
& opt_all convert_named_root []
& info ~docs ~docv:"LIBNAME:PATH" ~doc [ "L" ])
& info ~docs ~docv:"NAME:PATH" ~doc [ "root" ])
in
Term.(
const handle_error
$ (const index $ dst $ json $ warnings_options $ page_roots $ lib_roots
$ inputs_in_file $ inputs $ occurrences))
$ (const index $ dst $ json $ warnings_options $ roots $ inputs_in_file
$ inputs $ occurrences))

let info ~docs =
let doc =
Expand Down
97 changes: 34 additions & 63 deletions src/odoc/indexing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -98,68 +98,43 @@ let read_occurrences file =
let htbl : Odoc_occurrences.Table.t = Marshal.from_channel ic in
htbl

let pages resolver page_roots =
List.map
(fun (page_root, _) ->
let pages = Resolver.all_pages ~root:page_root resolver in
let p_hierarchy =
let page_toc_input =
(* To create a page toc, we need a list with id, title and children
order. We generate this list from *)
let prepare_input (id, title, frontmatter) =
(* We filter non-leaf pages *)
match id with
| { Id.iv = #Id.LeafPage.t_pv; _ } as id ->
(* We generate a title if needed *)
let title =
match title with
| None -> Location_.[ at (span []) (`Word (Id.name id)) ]
| Some x -> x
in
let children_order = frontmatter.Frontmatter.children_order in
Some (id, title, children_order)
| _ -> None
in
List.filter_map prepare_input pages
in
Odoc_index.Page_hierarchy.of_list page_toc_input
in
{ Odoc_index.p_name = page_root; p_hierarchy })
page_roots
let find_pages_and_units root =
Fs.Directory.fold_files_rec_result ~ext:".odocl"
(fun (pages, units) path ->
Odoc_file.load path >>= fun { Odoc_file.content; warnings = _ } ->
match content with
| Page_content p -> Ok (p :: pages, units)
| Impl_content _ | Asset_content _ -> Ok (pages, units)
| Unit_content u -> Ok (pages, u :: units))
([], []) root

let libs resolver lib_roots =
List.map
(fun (library, _) ->
let units = Resolver.all_units ~library resolver in
let l_hierarchies =
List.filter_map
(fun (file, _id) ->
match file () with
| Some unit -> Some (Odoc_index.Skeleton.from_unit unit)
| None -> None)
units
in
{ Odoc_index.l_name = library; l_hierarchies })
lib_roots
let page_index ~name = function
| [] -> None
| pages ->
let p_hierarchy = Odoc_index.Page_hierarchy.of_list pages in
Some { Odoc_index.p_name = name; p_hierarchy }

let compile out_format ~output ~warnings_options ~occurrences ~lib_roots
~page_roots ~inputs_in_file ~odocls =
let lib_index ~name = function
| [] -> None
| units ->
let l_hierarchies = List.map Odoc_index.Skeleton.from_unit units in
Some { Odoc_index.l_name = name; l_hierarchies }

let compile out_format ~output ~warnings_options ~occurrences ~roots
~inputs_in_file ~odocls =
let handle_warnings f =
let res = Error.catch_warnings f in
Error.handle_warnings ~warnings_options res |> Result.join
in
handle_warnings @@ fun () ->
let current_dir = Fs.File.dirname output in
parse_input_files inputs_in_file >>= fun files ->
let files = List.rev_append odocls files in
let occurrences =
match occurrences with
| None -> None
| Some occurrences -> Some (read_occurrences (Fpath.to_string occurrences))
in
let includes_rec =
List.rev_append (List.map snd page_roots) (List.map snd lib_roots)
in
let includes_rec = List.map snd roots in
let files =
List.rev_append files
(includes_rec
Expand All @@ -172,19 +147,15 @@ let compile out_format ~output ~warnings_options ~occurrences ~lib_roots
match out_format with
| `JSON -> compile_to_json ~output ~occurrences files
| `Marshall ->
let resolver =
Resolver.create ~important_digests:false ~directories:[]
~roots:
(Some
{
page_roots;
lib_roots;
current_lib = None;
current_package = None;
current_dir;
})
~open_modules:[]
let indexes =
List.fold_left
(fun (pages_acc, libs_acc) (name, root) ->
match find_pages_and_units root with
| Ok (p, m) ->
let p = Option.to_list @@ page_index ~name p in
let l = Option.to_list @@ lib_index ~name m in
(pages_acc @ p, libs_acc @ l)
| Error _ -> (pages_acc, libs_acc))
([], []) roots
in
let pages = pages resolver page_roots in
let libs = libs resolver lib_roots in
compile_to_marshall ~output (pages, libs) files
compile_to_marshall ~output indexes files
3 changes: 1 addition & 2 deletions src/odoc/indexing.mli
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ val compile :
output:Fs.file ->
warnings_options:Odoc_model.Error.warnings_options ->
occurrences:Fs.file option ->
lib_roots:(string * Fs.directory) list ->
page_roots:(string * Fs.directory) list ->
roots:(string * Fs.directory) list ->
inputs_in_file:Fs.file list ->
odocls:Fs.file list ->
(unit, [> msg ]) result
2 changes: 2 additions & 0 deletions src/utils/odoc_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ module Option = struct
let map f = function None -> None | Some x -> Some (f x)

let is_some = function None -> false | Some _ -> true

let to_list = function None -> [] | Some x -> [ x ]
end

module Result = struct
Expand Down
2 changes: 1 addition & 1 deletion test/pages/toc_order.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
$ odoc link _odoc/pkg/dir1/page-content_in_dir.odoc
$ odoc link _odoc/pkg/dir1/page-dontent.odoc

$ odoc compile-index -P test:_odoc/pkg
$ odoc compile-index --root test:_odoc/pkg
File "index.mld", line 5, character 7 to line 7, character 0:
Warning: Duplicate 'dir1/' in (children).
File "index.mld", line 5, character 7 to line 7, character 0:
Expand Down
2 changes: 1 addition & 1 deletion test/parent_id/missing_indexes.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
$ odoc link _odoc/page-foo.odoc
$ odoc link _odoc/page-bar.odoc
$ odoc link _odoc/baz/page-bli.odoc
$ odoc compile-index -P _:_odoc
$ odoc compile-index --root _:_odoc

$ odoc html-generate --index index.odoc-index --indent --output-dir _html _odoc/page-foo.odocl

Expand Down
2 changes: 1 addition & 1 deletion test/roots_and_hierarchy/canonical_hierarchy.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Let's link it:

Let's html-generate it (with a sidebar):

$ odoc compile-index -P pkg:_odoc/pkg/ -L libname:_odoc/pkg/libname -o sidebar.odoc-index
$ odoc compile-index --root pkg:_odoc/pkg/ -o sidebar.odoc-index
$ odoc html-generate --indent --index sidebar.odoc-index -o html _odoc/pkg/page-file.odocl
$ odoc html-generate --indent --index sidebar.odoc-index -o html _odoc/pkg/dir1/page-my_page.odocl
$ odoc html-generate --indent --index sidebar.odoc-index -o html _odoc/pkg/dir1/page-index.odocl
Expand Down
2 changes: 1 addition & 1 deletion test/roots_and_hierarchy/separate_doc_lib_folders.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Let's link it:

Let's html-generate it (with a sidebar):

$ odoc compile-index -P pkg:_odoc/pkg/doc/ -L libname:_odoc/pkg/lib/libname -o sidebar.odoc-index
$ odoc compile-index --root pkg:_odoc/pkg/doc/ --root libname:_odoc/pkg/lib/libname -o sidebar.odoc-index
$ odoc html-generate --indent --index sidebar.odoc-index -o html _odoc/pkg/doc/page-file.odocl
$ odoc html-generate --indent --index sidebar.odoc-index -o html _odoc/pkg/doc/dir1/page-my_page.odocl
$ odoc html-generate --indent --index sidebar.odoc-index -o html _odoc/pkg/doc/dir1/page-index.odocl
Expand Down
2 changes: 1 addition & 1 deletion test/roots_and_hierarchy/sidebar.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
$ odoc link -P pkg:_odoc/pkg/ _odoc/pkg/page-index.odoc
$ odoc link -P pkg:_odoc/pkg/ _odoc/pkg/libname/unit.odoc

$ odoc compile-index -P pkg:_odoc/pkg/ -L libname:_odoc/pkg/libname -o sidebar.odoc-index
$ odoc compile-index --root libname:_odoc/pkg/ -o sidebar.odoc-index
$ odoc html-generate --indent --index sidebar.odoc-index -o html _odoc/pkg/page-file.odocl
$ odoc html-generate --indent --index sidebar.odoc-index -o html _odoc/pkg/dir1/page-my_page.odocl
$ odoc html-generate --indent --index sidebar.odoc-index -o html _odoc/pkg/dir1/page-index.odocl
Expand Down
5 changes: 3 additions & 2 deletions test/roots_and_hierarchy/sidebar_with_indexes.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ Since -L subfolders are omitted from -P roots, the index page should not be adde
$ odoc link -P pkg:_odoc/pkg/ _odoc/pkg/libname/unit.odoc
$ odoc link -P pkg:_odoc/pkg/ _odoc/pkg/libname/page-index.odoc

$ odoc compile-index -P pkg:_odoc/pkg/ -L libname:_odoc/pkg/libname -o sidebar.odoc-index
$ odoc compile-index --root libname:_odoc/pkg/ -o sidebar.odoc-index
$ odoc html-generate --indent --index sidebar.odoc-index -o html _odoc/pkg/page-file.odocl
$ odoc html-generate --indent --index sidebar.odoc-index -o html _odoc/pkg/dir1/page-my_page.odocl
$ odoc html-generate --indent --index sidebar.odoc-index -o html _odoc/pkg/dir1/page-index.odocl
$ odoc html-generate --indent --index sidebar.odoc-index -o html _odoc/pkg/page-index.odocl
$ odoc html-generate --indent --index sidebar.odoc-index -o html _odoc/pkg/libname/unit.odocl
$ odoc html-generate --indent --index sidebar.odoc-index -o html _odoc/pkg/libname/page-index.odocl

$ cat html/pkg/index.html | grep odoc-global-toc -A 15
$ cat html/pkg/index.html | grep odoc-global-toc -A 16
<nav class="odoc-toc odoc-global-toc">
<ul class="odoc-modules">
<li><b>Library <code>libname</code></b>
Expand All @@ -39,6 +39,7 @@ Since -L subfolders are omitted from -P roots, the index page should not be adde
<li><a href="dir1/index.html">A directory</a>
<ul><li><a href="dir1/my_page.html">My page</a></li></ul>
</li><li><a href="file.html">File</a></li>
<li><a href="libname/index.html">Library landing page</a></li>
</ul>
</li>
</ul>
Expand Down
10 changes: 5 additions & 5 deletions test/search/html_search.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ the --marshall flag is passed. Odocl files can be given either in a list (using
passing directly the name of the files.

$ printf "main.odocl\npage-page.odocl\nj.odocl\n" > index_map
$ odoc compile-index --json -o index.json -P pkgname:.
$ odoc compile-index --json -o index.json --root pkgname:.

$ odoc compile-index -o index-main.odoc-index -P pkgname:.
$ odoc compile-index -o index-main.odoc-index --root pkgname:.

The json index file contains a json array, each element of the array corresponding to
a search entry.
Expand Down Expand Up @@ -214,7 +214,7 @@ Testing the warnings/errors for the `compile-index` command:
Passing an inexistent file:
$ odoc compile-index -P pkgname:babar
$ odoc compile-index --root pkgname:babar
$ odoc compile-index --file-list babar
odoc: option '--file-list': no 'babar' file or directory
Usage: odoc compile-index [--file-list=FILE] [--json] [OPTION]… [FILE]…
Expand All @@ -224,7 +224,7 @@ Passing an inexistent file:
Passing an empty folder is allowed:
$ mkdir foo
$ odoc compile-index -P pkgname:foo
$ odoc compile-index --root pkgname:foo
Wrong file extensions:
Expand All @@ -241,7 +241,7 @@ Wrong file extensions:
Passing a file which is not a correctly marshalled one:
$ echo hello > my_file.odocl
$ odoc compile-index -P pkgname:.
$ odoc compile-index --root pkgname:.
File "./my_file.odocl":
Warning: Error while unmarshalling "./my_file.odocl": End_of_file
Expand Down
2 changes: 1 addition & 1 deletion test/search/id_standalone_comments.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Compile and link the documentation
$ odoc compile -I . main.cmt
$ odoc link -I . main.odoc

$ odoc compile-index --json -P pkgname:./
$ odoc compile-index --json --root pkgname:./
We test that you can also pass a .odocl file directly.
$ odoc compile-index --json main.odocl -o index2.json
Indexes should be the same no matter how the inputs were passed.
Expand Down
2 changes: 1 addition & 1 deletion test/search/module_aliases.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Compile and link the documentation

$ odoc compile main.cmt
$ odoc link main.odoc
$ odoc compile-index --json -P pkgname:.
$ odoc compile-index --json --root pkgname:.

Search results only redirect to their definition point (not the
expansions). Comments link to the expansion they are in.
Expand Down

0 comments on commit 5a79e19

Please sign in to comment.