this repo has no description
at main 94 lines 3.0 kB view raw
1(* This exe will compile a markdown file, outputting a compiled `page-x.odoc` file. 2 This is tightly coupled with the internal representation of odoc files and thus needs 3 to be run with the exact same version of odoc that it is compiled with. *) 4 5open Odoc_model 6 7let parse id input_s = 8 let location = 9 Lexing.{ pos_fname = input_s; pos_lnum = 1; pos_cnum = 0; pos_bol = 0 } 10 in 11 let str = In_channel.(with_open_bin input_s input_all) in 12 let content, parser_warnings = 13 Doc_of_md.parse_comment ~location ~text:str () 14 in 15 let (content, ()), semantics_warnings = 16 Semantics.ast_to_comment ~internal_tags:Expect_none ~tags_allowed:false 17 ~parent_of_sections:(id :> Paths.Identifier.LabelParent.t) 18 content [] 19 |> Error.unpack_warnings 20 in 21 (content, List.map Error.t_of_parser_t parser_warnings @ semantics_warnings) 22 23let mk_page input_s id elements = 24 (* Construct the output file representation *) 25 let zero_heading = Comment.find_zero_heading elements in 26 let frontmatter = Frontmatter.empty in 27 let digest = Digest.file input_s in 28 let root = 29 let file = Root.Odoc_file.create_page input_s zero_heading frontmatter in 30 { Root.id = (id :> Paths.Identifier.OdocId.t); file; digest } 31 in 32 let children = [] in 33 { 34 Lang.Page.name = id; 35 root; 36 children; 37 content = { elements; warnings_tag = None }; 38 digest; 39 linked = false; 40 frontmatter; 41 } 42 43let run input_s parent_id_opt odoc_dir = 44 (* Construct the id of this page *) 45 let page_name = Filename.basename input_s |> Filename.chop_extension in 46 let parent_id = 47 match parent_id_opt with 48 | Some parent_id_str -> Odoc_odoc.Compile.mk_id parent_id_str 49 | None -> None 50 in 51 let id = 52 Odoc_model.Paths.Identifier.Mk.leaf_page 53 (parent_id, Odoc_model.Names.PageName.make_std page_name) 54 in 55 56 let content, warnings = parse id input_s in 57 let page = mk_page input_s id content in 58 59 let output = 60 let fname = "page-" ^ page_name ^ ".odoc" in 61 match parent_id_opt with 62 | None -> Fpath.(v odoc_dir / fname) 63 | Some parent_id_str -> Fpath.(v odoc_dir // v parent_id_str / fname) 64 in 65 Odoc_odoc.Odoc_file.save_page output ~warnings page 66 67open Cmdliner 68 69let input = 70 let doc = "Input markdown file." in 71 Arg.(required & pos 0 (some file) None & info ~doc ~docv:"FILE" []) 72 73let parent_id = 74 let doc = 75 "Parent id. This defines both the location of the resulting odoc file as \ 76 well as the location of the eventual html or other file." 77 in 78 Arg.( 79 value & opt (some string) None & info ~docv:"PARENT" ~doc [ "parent-id" ]) 80 81let output_dir = 82 let doc = 83 "Output file directory. The output file will be put in the parent-id path \ 84 below this." 85 in 86 Arg.( 87 required & opt (some string) None & info ~docv:"PATH" ~doc [ "output-dir" ]) 88 89let cmd = 90 let doc = "Compile a markdown file to an odoc page-*.odoc file." in 91 let info = Cmd.info "odoc-md" ~doc in 92 Cmd.v info Term.(const run $ input $ parent_id $ output_dir) 93 94let () = Cmdliner.(exit @@ Cmd.eval cmd)