Skip to content

Commit e56e89a

Browse files
authored
Merge pull request #62 from gasche/minor-changes
Minor changes
2 parents 750f7f0 + 696488f commit e56e89a

File tree

9 files changed

+103
-108
lines changed

9 files changed

+103
-108
lines changed

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ Example usage
99

1010
```ocaml
1111
let tmpl =
12-
Mustache.of_string "Hello {{name}}\n\
13-
Mustache is:\n\
14-
{{#qualities}}\
15-
* {{name}}\n\
16-
{{/qualities}}"
12+
try
13+
Mustache.of_string "Hello {{name}}\n\
14+
Mustache is:\n\
15+
{{#qualities}}\
16+
* {{name}}\n\
17+
{{/qualities}}"
18+
with Mustache.Parse_error err ->
19+
Format.eprintf "%a@."
20+
Mustache.pp_template_parse_error err;
21+
exit 3
1722
1823
let json =
1924
`O [ "name", `String "OCaml"
@@ -24,7 +29,11 @@ let json =
2429
]
2530
2631
let rendered =
27-
Mustache.render tmpl json
32+
try Mustache.render tmpl json
33+
with Mustache.Render_error err ->
34+
Format.eprintf "%a@."
35+
Mustache.pp_render_error err;
36+
exit 2
2837
```
2938

3039
Spec compliance

bin/mustache_cli.ml

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ let load_template template_filename =
2828
lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = template_filename };
2929
in
3030
try Mustache.parse_lx lexbuf
31-
with Mustache.Template_parse_error err ->
32-
Format.eprintf "Template parse error:@\n%a@."
31+
with Mustache.Parse_error err ->
32+
Format.eprintf "%a@."
3333
Mustache.pp_template_parse_error err;
3434
exit 3
3535

@@ -49,48 +49,47 @@ let run search_path json_filename template_filename =
4949
print_string output;
5050
flush stdout
5151
with Mustache.Render_error err ->
52-
Format.eprintf "Template render error:@\n%a@."
52+
Format.eprintf "%a@."
5353
Mustache.pp_render_error err;
5454
exit 2
5555

56-
let run_command =
57-
let open Cmdliner in
58-
let doc = "renders Mustache template from JSON data files" in
59-
let man = [
60-
`S Manpage.s_description;
61-
`P "$(tname) is a command-line tool coming with the $(i,ocaml-mustache) library,
62-
an OCaml implementation of the Mustache template format.
63-
$(tname) takes a data file and a template file as command-line parameters;
64-
it renders the populated template on standard output.";
65-
66-
`P "Mustache is a simple and popular template format,
67-
with library implementations in many programming languages.
68-
It is named from its {{..}} delimiters.";
69-
70-
`I ("Mustache website",
71-
"https://mustache.github.io/");
72-
`I ("Mustache templates documentation",
73-
"https://mustache.github.io/mustache.5.html");
74-
`I ("ocaml-mustache website:",
75-
"https://github.com/rgrinberg/ocaml-mustache");
76-
77-
`P "The $(i,ocaml-mustache) implementation is tested against
78-
the Mustache specification testsuite.
79-
All features are supported, except for lambdas and setting delimiter tags.";
80-
`S Manpage.s_options;
81-
`S "PARTIALS";
82-
`P "The $(i,ocaml-mustache) library gives programmatic control over the meaning of partials {{>foo}}.
83-
For the $(tname) tool, partials are interpreted as template file inclusion: '{{>foo}}' includes
84-
the template file 'foo.mustache'.";
85-
`P "Included files are resolved in a search path, which contains the current working directory
86-
(unless the $(b,--no-working-dir) option is used)
87-
and include directories passed through $(b,-I DIR) options.";
88-
`P "If a file exists in several directories of the search path, the directory included first
89-
(leftmost $(b,-I) option) has precedence, and the current working directory has precedence
90-
over include directories.";
91-
`S Manpage.s_examples;
92-
`Pre
93-
{|
56+
let manpage = Cmdliner.[
57+
`S Manpage.s_description;
58+
`P "$(tname) is a command-line tool coming with the $(i,ocaml-mustache) library,
59+
an OCaml implementation of the Mustache template format.
60+
$(tname) takes a data file and a template file as command-line parameters;
61+
it renders the populated template on standard output.";
62+
63+
`P "Mustache is a simple and popular template format,
64+
with library implementations in many programming languages.
65+
It is named from its {{..}} delimiters.";
66+
67+
`I ("Mustache website",
68+
"https://mustache.github.io/");
69+
`I ("Mustache templates documentation",
70+
"https://mustache.github.io/mustache.5.html");
71+
`I ("$(i,ocaml-mustache) website:",
72+
"https://github.com/rgrinberg/ocaml-mustache");
73+
74+
`S Manpage.s_options;
75+
(* The content of this section is filled by Cmdliner; it is used here
76+
to enforce the placement of the non-standard sections below. *)
77+
78+
`S "PARTIALS";
79+
`P "The $(i,ocaml-mustache) library gives programmatic control over the meaning of partials {{>foo}}.
80+
For the $(tname) tool, partials are interpreted as template file inclusion: '{{>foo}}' includes
81+
the template file 'foo.mustache'.";
82+
`P "Included files are resolved in a search path, which contains the current working directory
83+
(unless the $(b,--no-working-dir) option is used)
84+
and include directories passed through $(b,-I DIR) options.";
85+
`P "If a file exists in several directories of the search path, the directory included first
86+
(leftmost $(b,-I) option) has precedence, and the current working directory has precedence
87+
over include directories.";
88+
89+
`S Manpage.s_examples;
90+
`Pre {|
91+
## Simple usage.
92+
9493
\$ cat data.json
9594
{ "name": "OCaml",
9695
"qualities": [{"name": "simple"}, {"name": "fun"}] }
@@ -109,6 +108,8 @@ Mustache is:
109108
- fun
110109

111110

111+
## Using a partial to include a subpage; see $(b,PARTIALS).
112+
112113
\$ cat page.mustache
113114
<html>
114115
<body>
@@ -124,13 +125,24 @@ Mustache is:
124125
- simple
125126
- fun
126127
</body>
127-
</html>
128+
</html>|};
128129

129-
|};
130-
`S Manpage.s_bugs;
131-
`P "Report bugs on https://github.com/rgrinberg/ocaml-mustache/issues";
132-
]
133-
in
130+
`S "CONFORMING TO";
131+
132+
`P "The $(i,ocaml-mustache) implementation is tested against
133+
the Mustache specification testsuite.
134+
All features are supported, except for lambdas and setting delimiter tags.";
135+
136+
`I ("Mustache specification testsuite",
137+
"https://github.com/mustache/spec");
138+
139+
`S "REPORTING BUGS";
140+
`P "Report bugs on https://github.com/rgrinberg/ocaml-mustache/issues";
141+
]
142+
143+
let run_command =
144+
let open Cmdliner in
145+
let doc = "renders Mustache template from JSON data files" in
134146
let json_file =
135147
let doc = "data file in JSON format" in
136148
Arg.(required & pos 0 (some file) None & info [] ~docv:"DATA.json" ~doc)
@@ -156,7 +168,7 @@ Mustache is:
156168
Term.(const search_path $ includes $ no_working_dir)
157169
in
158170
Term.(const run $ search_path $ json_file $ template_file),
159-
Term.info "mustache" ~doc ~man
171+
Term.info "mustache" ~doc ~man:manpage
160172

161173
let () =
162174
let open Cmdliner in

bin/test/errors/parsing-errors.t

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,61 @@ Delimiter problems:
44
$ PROBLEM=no-closing-mustache.mustache
55
$ echo "{{foo" > $PROBLEM
66
$ mustache foo.json $PROBLEM
7-
Template parse error:
87
File "no-closing-mustache.mustache", line 2, character 0: '}}' expected.
98
[3]
109
1110
$ PROBLEM=one-closing-mustache.mustache
1211
$ echo "{{foo}" > $PROBLEM
1312
$ mustache foo.json $PROBLEM
14-
Template parse error:
1513
File "one-closing-mustache.mustache", line 1, character 5: '}}' expected.
1614
[3]
1715
1816
$ PROBLEM=eof-before-variable.mustache
1917
$ echo "{{" > $PROBLEM
2018
$ mustache foo.json $PROBLEM
21-
Template parse error:
2219
File "eof-before-variable.mustache", line 2, character 0: ident expected.
2320
[3]
2421
2522
$ PROBLEM=eof-before-section.mustache
2623
$ echo "{{#" > $PROBLEM
2724
$ mustache foo.json $PROBLEM
28-
Template parse error:
2925
File "eof-before-section.mustache", line 2, character 0: ident expected.
3026
[3]
3127
3228
$ PROBLEM=eof-before-section-end.mustache
3329
$ echo "{{#foo}} {{.}} {{/" > $PROBLEM
3430
$ mustache foo.json $PROBLEM
35-
Template parse error:
3631
File "eof-before-section-end.mustache", line 2, character 0: ident expected.
3732
[3]
3833
3934
$ PROBLEM=eof-before-inverted-section.mustache
4035
$ echo "{{^" > $PROBLEM
4136
$ mustache foo.json $PROBLEM
42-
Template parse error:
4337
File "eof-before-inverted-section.mustache", line 2, character 0:
4438
ident expected.
4539
[3]
4640
4741
$ PROBLEM=eof-before-unescape.mustache
4842
$ echo "{{{" > $PROBLEM
4943
$ mustache foo.json $PROBLEM
50-
Template parse error:
5144
File "eof-before-unescape.mustache", line 2, character 0: ident expected.
5245
[3]
5346
5447
$ PROBLEM=eof-before-unescape.mustache
5548
$ echo "{{&" > $PROBLEM
5649
$ mustache foo.json $PROBLEM
57-
Template parse error:
5850
File "eof-before-unescape.mustache", line 2, character 0: ident expected.
5951
[3]
6052
6153
$ PROBLEM=eof-before-partial.mustache
6254
$ echo "{{>" > $PROBLEM
6355
$ mustache foo.json $PROBLEM
64-
Template parse error:
6556
File "eof-before-partial.mustache", line 2, character 0: '}}' expected.
6657
[3]
6758
6859
$ PROBLEM=eof-in-comment.mustache
6960
$ echo "{{! non-terminated comment" > $PROBLEM
7061
$ mustache foo.json $PROBLEM
71-
Template parse error:
7262
File "eof-in-comment.mustache", line 2, character 0: non-terminated comment.
7363
[3]
7464
@@ -78,14 +68,12 @@ Mismatches between opening and closing mustaches:
7868
$ PROBLEM=two-three.mustache
7969
$ echo "{{ foo }}}" > $PROBLEM
8070
$ mustache foo.json $PROBLEM
81-
Template parse error:
8271
File "two-three.mustache", line 1, characters 7-10: '}}' expected.
8372
[3]
8473
8574
$ PROBLEM=three-two.mustache
8675
$ echo "{{{ foo }}" > $PROBLEM
8776
$ mustache foo.json $PROBLEM
88-
Template parse error:
8977
File "three-two.mustache", line 1, characters 8-10: '}}}' expected.
9078
[3]
9179
@@ -95,23 +83,20 @@ Mismatch between section-start and section-end:
9583
$ PROBLEM=foo-bar.mustache
9684
$ echo "{{#foo}} {{.}} {{/bar}}" > $PROBLEM
9785
$ mustache foo.json $PROBLEM
98-
Template parse error:
99-
File "foo-bar.mustache", lines 1-2, characters 23-0:
86+
File "foo-bar.mustache", line 1, characters 0-23:
10087
Section mismatch: {{#foo}} is closed by {{/bar}}.
10188
[3]
10289
10390
$ PROBLEM=foo-not-closed.mustache
10491
$ echo "{{#foo}} {{.}} {{foo}}" > $PROBLEM
10592
$ mustache foo.json $PROBLEM
106-
Template parse error:
10793
File "foo-not-closed.mustache", line 2, character 0: syntax error.
10894
[3]
10995
11096
$ PROBLEM=wrong-nesting.mustache
11197
$ echo "{{#bar}} {{#foo}} {{.}} {{/bar}} {{/foo}}" > $PROBLEM
11298
$ mustache foo.json $PROBLEM
113-
Template parse error:
114-
File "wrong-nesting.mustache", lines 1-2, characters 41-0:
99+
File "wrong-nesting.mustache", line 1, characters 9-32:
115100
Section mismatch: {{#foo}} is closed by {{/bar}}.
116101
[3]
117102
@@ -121,6 +106,5 @@ Weird cases that may confuse our lexer or parser:
121106
$ PROBLEM=weird-tag-name.mustache
122107
$ echo "{{.weird}} foo bar" > $PROBLEM
123108
$ mustache foo.json $PROBLEM
124-
Template parse error:
125109
File "weird-tag-name.mustache", line 1, character 3: '}}' expected.
126110
[3]

bin/test/errors/render-errors.t/run.t

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,67 +27,57 @@ one possible source of error, or both, or none.
2727
Invalid variable name:
2828

2929
$ mustache reference.json missing-variable.mustache
30-
Template render error:
3130
File "missing-variable.mustache", line 14, characters 40-46:
3231
the variable 'na' is missing.
3332
[2]
3433

3534
$ mustache missing-variable.json reference.mustache
36-
Template render error:
3735
File "reference.mustache", line 5, characters 4-12:
3836
the variable 'data' is missing.
3937
[2]
4038

4139
Invalid section name:
4240

4341
$ mustache reference.json missing-section.mustache
44-
Template render error:
4542
File "missing-section.mustache", line 14, characters 0-55:
4643
the section 'na' is missing.
4744
[2]
4845

4946
$ mustache missing-section.json reference.mustache
50-
Template render error:
5147
File "reference.mustache", lines 9-12, characters 0-10:
5248
the section 'group' is missing.
5349
[2]
5450

5551
Error in a dotted path foo.bar (one case for the first component, the other in the second).
5652

5753
$ mustache reference.json invalid-dotted-name-1.mustache
58-
Template render error:
5954
File "invalid-dotted-name-1.mustache", line 10, characters 2-15:
6055
the variable 'gro' is missing.
6156
[2]
6257

6358
$ mustache invalid-dotted-name-1.json reference.mustache
64-
Template render error:
6559
File "reference.mustache", lines 9-12, characters 0-10:
6660
the section 'group' is missing.
6761
[2]
6862

6963
$ mustache reference.json invalid-dotted-name-2.mustache
70-
Template render error:
7164
File "invalid-dotted-name-2.mustache", line 10, characters 2-15:
7265
the variable 'group.fir' is missing.
7366
[2]
7467

7568
$ mustache invalid-dotted-name-2.json reference.mustache
76-
Template render error:
7769
File "reference.mustache", line 10, characters 2-17:
7870
the variable 'group.first' is missing.
7971
[2]
8072

8173
Non-scalar used as a scalar:
8274

8375
$ mustache reference.json non-scalar.mustache
84-
Template render error:
8576
File "non-scalar.mustache", line 4, characters 0-8:
8677
the value of 'list' is not a valid scalar.
8778
[2]
8879

8980
$ mustache non-scalar.json reference.mustache
90-
Template render error:
9181
File "reference.mustache", line 1, characters 7-16:
9282
the value of 'title' is not a valid scalar.
9383
[2]
@@ -98,7 +88,6 @@ Missing partial (currently the CLI does not support any partial anyway):
9888
in better `ls` output).
9989

10090
$ mustache reference.json z-missing-partial.mustache
101-
Template render error:
10291
File "z-missing-partial.mustache", line 11, characters 2-13:
10392
the partial 'second' is missing.
10493
[2]

bin/test/partials.t/run.t

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ we need to set the search path to locate their included partials.
2121

2222
This fails:
2323
$ (cd subdir; mustache ../data.json ../foo.mustache)
24-
Template render error:
2524
File "../foo.mustache", line 2, characters 23-31:
2625
the partial 'bar' is missing.
2726
[2]

0 commit comments

Comments
 (0)