Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility functions to read a template from a channel/file #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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