@@ -11,6 +11,11 @@ let load_file f =
1111 close_in ic;
1212 (Bytes. to_string s)
1313
14+ let locate_template search_path relative_filename =
15+ search_path
16+ |> List. map (fun path -> Filename. concat path relative_filename)
17+ |> List. find_opt Sys. file_exists
18+
1419let load_template template_filename =
1520 let template_data = load_file template_filename in
1621 let lexbuf = Lexing. from_string template_data in
@@ -27,13 +32,14 @@ let load_template template_filename =
2732let load_json json_filename =
2833 Ezjsonm. from_string (load_file json_filename)
2934
30- let run json_filename template_filename =
35+ let run search_path json_filename template_filename =
3136 let env = load_json json_filename in
3237 let tmpl = load_template template_filename in
3338 let partials name =
34- let path = Printf. sprintf " %s.mustache" name in
35- if not (Sys. file_exists path) then None
36- else Some (load_template path) in
39+ let file = Printf. sprintf " %s.mustache" name in
40+ let path = locate_template search_path file in
41+ Option. map load_template path
42+ in
3743 try
3844 let output = Mustache. render ~partials tmpl env in
3945 print_string output;
@@ -99,10 +105,25 @@ Mustache is:
99105 let doc = "mustache template" in
100106 Arg. (required & pos 1 (some file ) None & info [] ~docv :"TEMPLATE.mustache" ~doc )
101107 in
102- Term. (const run $ json_file $ template_file ),
108+ let search_path =
109+ let includes =
110+ let doc = "Adds the directory $(docv) to the search path for partials." in
111+ Arg. (value & opt_all dir [] & info ["I" ] ~docv :"DIR" ~doc )
112+ in
113+ let no_working_dir =
114+ let doc = "Disable the implicit inclusion of the working directory
115+ in the search path for partials." in
116+ Arg. (value & flag & info ["no-working-dir" ] ~doc )
117+ in
118+ let search_path includes no_working_dir =
119+ if no_working_dir then includes
120+ else Filename. current_dir_name :: includes
121+ in
122+ Term. (const search_path $ includes $ no_working_dir )
123+ in
124+ Term. (const run $ search_path $ json_file $ template_file ),
103125 Term. info "mustache" ~doc ~man
104126
105-
106127let () =
107128 let open Cmdliner in
108129 Term. exit @@ Term. eval run_command
0 commit comments