@@ -3,26 +3,6 @@ module Mustache = struct
33 include With_locations
44end
55
6- let apply_mustache ~json_data ~template_filename ~template_data =
7- let env = Ezjsonm. from_string json_data
8- and tmpl =
9- let lexbuf = Lexing. from_string template_data in
10- let () =
11- let open Lexing in
12- lexbuf.lex_curr_p < - { lexbuf.lex_curr_p with pos_fname = template_filename };
13- in
14- try Mustache. parse_lx lexbuf
15- with Mustache. Template_parse_error err ->
16- Format. eprintf " Template parse error:@\n %a@."
17- Mustache. pp_template_parse_error err;
18- exit 3
19- in
20- try Mustache. render tmpl env |> print_endline
21- with Mustache. Render_error err ->
22- Format. eprintf " Template render error:@\n %a@."
23- Mustache. pp_render_error err;
24- exit 2
25-
266let load_file f =
277 let ic = open_in f in
288 let n = in_channel_length ic in
@@ -31,11 +11,34 @@ let load_file f =
3111 close_in ic;
3212 (Bytes. to_string s)
3313
34- let run json_filename template_filename =
35- let json_data = load_file json_filename
36- and template_data = load_file template_filename
14+ let load_template template_filename =
15+ let template_data = load_file template_filename in
16+ let lexbuf = Lexing. from_string template_data in
17+ let () =
18+ let open Lexing in
19+ lexbuf.lex_curr_p < - { lexbuf.lex_curr_p with pos_fname = template_filename };
3720 in
38- apply_mustache ~json_data ~template_filename ~template_data
21+ try Mustache. parse_lx lexbuf
22+ with Mustache. Template_parse_error err ->
23+ Format. eprintf " Template parse error:@\n %a@."
24+ Mustache. pp_template_parse_error err;
25+ exit 3
26+
27+ let load_json json_filename =
28+ Ezjsonm. from_string (load_file json_filename)
29+
30+ let run json_filename template_filename =
31+ let env = load_json json_filename in
32+ let tmpl = load_template template_filename in
33+ 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
37+ try Mustache. render ~partials tmpl env |> print_endline
38+ with Mustache. Render_error err ->
39+ Format. eprintf " Template render error:@\n %a@."
40+ Mustache. pp_render_error err;
41+ exit 2
3942
4043let usage () =
4144 print_endline " Usage: mustache-cli json_filename template_filename"
0 commit comments