Skip to content

Commit

Permalink
Add utility functions to read a template from a channel/file
Browse files Browse the repository at this point in the history
  • Loading branch information
Armaël Guéneau committed Aug 24, 2018
1 parent 3b9a188 commit 778191d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/mustache.ml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ let parse_lx (lexbuf: Lexing.lexbuf) : Locs.t =
Mustache_lexer.(handle_standalone mustache lexbuf)

let of_string s = parse_lx (Lexing.from_string s)
let of_channel c = parse_lx (Lexing.from_channel c)
let of_file f =
let c = open_in f in
let ret = of_channel c in
close_in c;
ret

(* Utility module, that helps looking up values in the json data during the
rendering phase. *)
Expand Down Expand Up @@ -209,6 +215,8 @@ module Without_locations = struct

let parse_lx lexbuf = erase_locs (parse_lx lexbuf)
let of_string s = erase_locs (of_string s)
let of_channel c = erase_locs (of_channel c)
let of_file f = erase_locs (of_file f)

let pp = pp
let to_formatter = pp
Expand Down
2 changes: 2 additions & 0 deletions lib/mustache.mli
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ and partial =
(** Read *)
val parse_lx : Lexing.lexbuf -> t
val of_string : string -> t
val of_channel : in_channel -> t
val of_file : string -> t

(** [pp fmt template] print a template as raw mustache to
the formatter [fmt]. *)
Expand Down

0 comments on commit 778191d

Please sign in to comment.