package yocaml_mustache

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file yocaml_mustache.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
(* YOCaml a static blog generator.
   Copyright (C) 2024 The Funkyworkers and The YOCaml's developers

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>. *)

module Tpl = struct
  type t = Mustache.Json.value

  let rec from = function
    | Yocaml.Data.Null -> `Null
    | Yocaml.Data.Bool b -> `Bool b
    | Yocaml.Data.Int i -> `Float (float_of_int i)
    | Yocaml.Data.Float f -> `Float f
    | Yocaml.Data.String s -> `String s
    | Yocaml.Data.List l -> `A (List.map from l)
    | Yocaml.Data.Record r -> `O (List.map (fun (k, v) -> (k, from v)) r)

  let render ?(strict = true) parameters content =
    let layout = Mustache.of_string content in
    Mustache.render ~strict layout (`O parameters)
end

let read_template ?snapshot ?strict template =
  Yocaml.Pipeline.read_template (module Tpl) ?snapshot ?strict template

let read_templates ?snapshot ?strict templates =
  Yocaml.Pipeline.read_templates (module Tpl) ?snapshot ?strict templates

module Pipeline = struct
  let as_template (type a)
      (module I : Yocaml.Required.DATA_INJECTABLE with type t = a) ?snapshot
      ?strict template =
    Yocaml.Pipeline.as_template
      (module Tpl)
      (module I)
      ?snapshot ?strict template
end

include Tpl