Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jonludlam committed Nov 5, 2024
1 parent 0ead601 commit 0045f1e
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 47 deletions.
5 changes: 1 addition & 4 deletions src/driver/odoc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,13 @@ let compile_md ~output_dir ~input_file:file ~parent_id =
let _, f = Fpath.split_base file in
Some Fpath.(output_dir // Id.to_fpath parent_id // set_ext "odoc" f)
in
let cmd =
!odoc_md % Fpath.to_string file % "--output-dir" % p output_dir
in
let cmd = !odoc_md % Fpath.to_string file % "--output-dir" % p output_dir in
let cmd = cmd % "--parent-id" % Id.to_string parent_id in
let desc = Printf.sprintf "Compiling Markdown %s" (Fpath.to_string file) in
let lines = Cmd_outputs.submit desc cmd output_file in
Cmd_outputs.(
add_prefixed_output cmd compile_output (Fpath.to_string file) lines)


let compile_asset ~output_dir ~name ~parent_id =
let open Cmd in
let output_file =
Expand Down
3 changes: 2 additions & 1 deletion src/driver/odoc.mli
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ val compile :
includes:Fpath.set ->
parent_id:Id.t ->
unit
val compile_md : output_dir:Fpath.t -> input_file:Fpath.t -> parent_id:Id.t -> unit
val compile_md :
output_dir:Fpath.t -> input_file:Fpath.t -> parent_id:Id.t -> unit

val compile_asset : output_dir:Fpath.t -> name:string -> parent_id:Id.t -> unit

Expand Down
25 changes: 15 additions & 10 deletions src/driver/odoc_unit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -350,19 +350,22 @@ let of_packages ~output_dir ~linked_dir ~index_dir ~extra_libs_paths
in
[ unit ]
in
let of_md pkg (md :Fpath.t) : md unit list =
let of_md pkg (md : Fpath.t) : md unit list =
let ext = Fpath.get_ext md in
match ext with
| ".md" ->
let rel_dir = doc_dir pkg in
let kind = `Md in
let name = md |> Fpath.rem_ext |> Fpath.basename |> ( ^ ) "page-" in
let lib_deps = Util.StringSet.empty in
let unit = make_unit ~name ~kind ~rel_dir ~input_file:md ~pkg ~include_dirs:Fpath.Set.empty ~lib_deps in
[ unit ]
let rel_dir = doc_dir pkg in
let kind = `Md in
let name = md |> Fpath.rem_ext |> Fpath.basename |> ( ^ ) "page-" in
let lib_deps = Util.StringSet.empty in
let unit =
make_unit ~name ~kind ~rel_dir ~input_file:md ~pkg
~include_dirs:Fpath.Set.empty ~lib_deps
in
[ unit ]
| _ ->
Logs.debug (fun m -> m "Skipping non-markdown doc file %a" Fpath.pp md);
[]
Logs.debug (fun m -> m "Skipping non-markdown doc file %a" Fpath.pp md);
[]
in
let of_asset pkg (asset : Packages.asset) : asset unit list =
let open Fpath in
Expand All @@ -384,7 +387,9 @@ let of_packages ~output_dir ~linked_dir ~index_dir ~extra_libs_paths
let lib_units :> t list list = List.map (of_lib pkg) pkg.libraries in
let mld_units :> t list list = List.map (of_mld pkg) pkg.mlds in
let asset_units :> t list list = List.map (of_asset pkg) pkg.assets in
let md_units :> t list list = Fpath.Set.fold (fun md acc -> of_md pkg md :: acc) pkg.other_docs [] in
let md_units :> t list list =
Fpath.Set.fold (fun md acc -> of_md pkg md :: acc) pkg.other_docs []
in
List.concat (lib_units @ mld_units @ asset_units @ md_units)
in
List.concat_map of_package pkgs
2 changes: 1 addition & 1 deletion src/driver/odoc_unit.mli
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type impl_extra = { src_id : Odoc.Id.t; src_path : Fpath.t }
type impl = [ `Impl of impl_extra ]

type mld = [ `Mld ]
type md = [`Md]
type md = [ `Md ]
type asset = [ `Asset ]

type t = [ impl | intf | mld | asset | md ] unit
Expand Down
1 change: 0 additions & 1 deletion src/markdown/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
(name odoc_md)
(package odoc-md)
(libraries cmarkit odoc.model odoc.odoc cmdliner))

68 changes: 40 additions & 28 deletions src/markdown/odoc_md.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(* This exe will compile a markdown file, outputting a compiled `page-x.odoc` file.
(* This exe will compile a markdown file, outputting a compiled `page-x.odoc` file.
This is tightly coupled with the internal representation of odoc files and thus needs
to be run with the exact same version of odoc that it is compiled with. *)

Expand All @@ -10,10 +10,13 @@ let parse id input_s =
in
let str = In_channel.(with_open_bin input_s input_all) in
let content, _warnings = Doc_of_md.parse_comment ~location ~text:str () in
let (content, ()) = Semantics.ast_to_comment ~internal_tags:Expect_none
~sections_allowed:`All ~tags_allowed:true
~parent_of_sections:(id :> Paths.Identifier.LabelParent.t) content []
|> Error.raise_warnings in
let content, () =
Semantics.ast_to_comment ~internal_tags:Expect_none ~sections_allowed:`All
~tags_allowed:true
~parent_of_sections:(id :> Paths.Identifier.LabelParent.t)
content []
|> Error.raise_warnings
in
content

let mk_page input_s id content =
Expand All @@ -22,26 +25,35 @@ let mk_page input_s id content =
let frontmatter, content = Comment.extract_frontmatter content in
let digest = Digest.file input_s in
let root =
let file =
Root.Odoc_file.create_page input_s zero_heading frontmatter
in
let file = Root.Odoc_file.create_page input_s zero_heading frontmatter in
{ Root.id = (id :> Paths.Identifier.OdocId.t); file; digest }
in
let children=[] in
{ Lang.Page.name=id; root; children; content; digest; linked = false; frontmatter }
let children = [] in
{
Lang.Page.name = id;
root;
children;
content;
digest;
linked = false;
frontmatter;
}

let run input_s parent_id_str odoc_dir =
(* Construct the id of this page *)
let page_name =
Filename.basename input_s |> Filename.chop_extension
in
let page_name = Filename.basename input_s |> Filename.chop_extension in
let parent_id = Odoc_odoc.Compile.mk_id parent_id_str in
let id = Odoc_model.Paths.Identifier.Mk.leaf_page (parent_id, Odoc_model.Names.PageName.make_std page_name) in
let id =
Odoc_model.Paths.Identifier.Mk.leaf_page
(parent_id, Odoc_model.Names.PageName.make_std page_name)
in

let content = parse id input_s in
let page = mk_page input_s id content in

let output = Fpath.(v odoc_dir // v parent_id_str / ("page-" ^ page_name ^ ".odoc")) in
let output =
Fpath.(v odoc_dir // v parent_id_str / ("page-" ^ page_name ^ ".odoc"))
in
Odoc_odoc.Odoc_file.save_page output ~warnings:[] page

open Cmdliner
Expand All @@ -51,24 +63,24 @@ let input =
Arg.(required & pos 0 (some file) None & info ~doc ~docv:"FILE" [])

let parent_id =
let doc = "Parent id. This defines both the location of the resulting odoc file as well as the \
location of the eventual html or other file." in
let doc =
"Parent id. This defines both the location of the resulting odoc file as \
well as the location of the eventual html or other file."
in
Arg.(
required
& opt (some string) None
& info ~docv:"PARENT" ~doc [ "parent-id" ])
required & opt (some string) None & info ~docv:"PARENT" ~doc [ "parent-id" ])

let output_dir =
let doc = "Output file directory. The output file will be put in the parent-id path below this." in
let doc =
"Output file directory. The output file will be put in the parent-id path \
below this."
in
Arg.(
required
& opt (some string) None
& info ~docv:"PATH" ~doc [ "output-dir" ])

required & opt (some string) None & info ~docv:"PATH" ~doc [ "output-dir" ])

let cmd =
let doc = "Compile a markdown file to an odoc page-*.odoc file." in
let info = Cmd.info "odoc-md" ~doc in
Cmd.v info
Term.(const run $ input $ parent_id $ output_dir)
Cmd.v info Term.(const run $ input $ parent_id $ output_dir)

let () = Cmdliner.(exit @@ Cmd.eval cmd)
let () = Cmdliner.(exit @@ Cmd.eval cmd)
11 changes: 9 additions & 2 deletions src/odoc/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,15 @@ let mld ~parent_id ~parents_children ~output ~children ~warnings_options input =
in
let page =
Lang.Page.
{ name=id; root; children; content; digest; linked = false; frontmatter }
{
name = id;
root;
children;
content;
digest;
linked = false;
frontmatter;
}
in
Odoc_file.save_page output ~warnings:[] page;
()
Expand All @@ -275,7 +283,6 @@ let mld ~parent_id ~parents_children ~output ~children ~warnings_options input =
|> function
| `Stop -> resolve [] (* TODO: Error? *)
| `Docs content -> resolve content


let handle_file_ext ext =
match ext with
Expand Down

0 comments on commit 0045f1e

Please sign in to comment.