Skip to content

Commit e8925e2

Browse files
committed
define rendering on templates with locations
1 parent bbe0d94 commit e8925e2

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

lib/mustache.ml

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ end
302302
module Render = struct
303303
(* Rendering is defined on the ast without locations. *)
304304

305-
open No_locs
305+
open Locs
306306

307307
(* Render a template whose partials have already been expanded.
308308
@@ -316,7 +316,7 @@ module Render = struct
316316
partial-resolution function. *)
317317
let render_expanded
318318
?(strict = true)
319-
(buf : Buffer.t) (m : No_locs.t) (js : Json.t)
319+
(buf : Buffer.t) (m : Locs.t) (js : Json.t)
320320
=
321321
let print_indent indent =
322322
for _ = 0 to indent - 1 do
@@ -346,7 +346,7 @@ module Render = struct
346346
) (List.tl lines)
347347
in
348348

349-
let rec render indent m (ctxs : Contexts.t) = match m with
349+
let rec render indent m (ctxs : Contexts.t) = match m.desc with
350350

351351
| String s ->
352352
print_indented_string indent s
@@ -448,7 +448,8 @@ module Without_locations = struct
448448

449449

450450
let render_buf ?strict ?(partials = fun _ -> None) buf (m : t) (js : Json.t) =
451-
Render.render_expanded buf ?strict (expand_partials partials m) js
451+
let m = add_dummy_locs (expand_partials partials m) in
452+
Render.render_expanded buf ?strict m js
452453
453454
let render ?strict ?partials (m : t) (js : Json.t) =
454455
let buf = Buffer.create 0 in
@@ -473,27 +474,6 @@ module With_locations = struct
473474
474475
let to_string x = to_string (erase_locs x)
475476
476-
let partials_erase_locs partials =
477-
option_map partials (fun f name -> option_map (f name) erase_locs)
478-
479-
let render_fmt ?strict ?partials fmt m js =
480-
Without_locations.render_fmt
481-
?strict
482-
?partials:(partials_erase_locs partials)
483-
fmt (erase_locs m) js
484-
485-
let render_buf ?strict ?partials fmt m js =
486-
Without_locations.render_buf
487-
?strict
488-
?partials:(partials_erase_locs partials)
489-
fmt (erase_locs m) js
490-
491-
let render ?strict ?partials m js =
492-
Without_locations.render
493-
?strict
494-
?partials:(partials_erase_locs partials)
495-
(erase_locs m) js
496-
497477
let rec fold ~string ~section ~escaped ~unescaped ~partial ~comment ~concat t =
498478
let go = fold ~string ~section ~escaped ~unescaped ~partial ~comment ~concat in
499479
let { desc; loc } = t in
@@ -543,6 +523,20 @@ module With_locations = struct
543523
partial ~loc ~indent name contents'
544524
in
545525
fold ~string:raw ~section ~escaped ~unescaped ~partial ~comment ~concat
526+
527+
let render_buf ?strict ?(partials = fun _ -> None) buf (m : t) (js : Json.t) =
528+
let m = expand_partials partials m in
529+
Render.render_expanded buf ?strict m js
530+
531+
let render ?strict ?partials (m : t) (js : Json.t) =
532+
let buf = Buffer.create 0 in
533+
render_buf ?strict ?partials buf m js ;
534+
Buffer.contents buf
535+
536+
let render_fmt ?strict ?partials fmt m js =
537+
let str = render ?strict ?partials m js in
538+
Format.pp_print_string fmt str;
539+
Format.pp_print_flush fmt ()
546540
end
547541
548542

0 commit comments

Comments
 (0)