Updated deps
[perl4caml.git] / wrappers / pl_Template.ml
1 (** Wrapper around Perl [Template] class (Template Toolkit).
2   * 
3   * Author: Dave Benjamin <dave@3dex.com>.
4   *)
5
6 open Perl
7
8 let _ = eval "use Template"
9
10 exception Not_implemented
11 exception Error of string
12
13 module Variant =
14
15 struct
16
17   type t =
18     | Null
19     | String of string
20     | Int of int
21     | Float of float
22     | Bool of bool
23     | Array of t list
24     | Hash of (string * t) list;;
25
26 end
27
28 let rec sv_of_variant = function
29   | Variant.Null -> sv_undef ()
30   | Variant.String s -> sv_of_string s
31   | Variant.Int i -> sv_of_int i
32   | Variant.Float f -> sv_of_float f
33   | Variant.Bool b -> sv_of_bool b
34   | Variant.Array xs -> arrayref (av_of_sv_list (List.map sv_of_variant xs))
35   | Variant.Hash xs -> hashref
36       (let hv = hv_empty () in
37          List.iter (fun (k, v) -> hv_set hv k (sv_of_variant v)) xs;
38          hv
39       );;
40
41 let hv_of_string_pair_list pairs =
42   let hv = hv_empty () in
43     List.iter (fun (k, v) -> hv_set hv k (sv_of_string v)) pairs;
44     hv
45
46 class template sv =
47
48 object (self)
49
50   method process file vars =
51     let output = sv_of_string "" in
52     let args = [sv_of_string file; sv_of_variant vars; scalarref output] in
53     let result = call_method sv "process" args in
54       if not (sv_is_true result) then
55         raise (Error self#error)
56       else
57         string_of_sv output
58
59   method error =
60     string_of_sv (call_method sv "error" [])
61
62 end
63
64 let may f = function None -> () | Some v -> f v
65
66 let new_ ?start_tag ?end_tag ?tag_style ?pre_chomp ?post_chomp ?trim
67   ?interpolate ?anycase ?include_path ?delimiter ?absolute ?relative
68   ?default ?blocks ?auto_reset ?recursion ?variables ?constants
69   ?constant_namespace ?namespace ?pre_process ?post_process ?process
70   ?wrapper ?error ?errors ?eval_perl ?output ?output_path ?debug ?debug_format
71   ?cache_size ?compile_ext ?compile_dir ?plugins ?plugin_base ?load_perl
72   ?filters ?v1dollar ?load_templates ?load_plugins ?load_filters
73   ?tolerant ?service ?context ?stash ?parser ?grammar () =
74
75   let args = ref [] in
76     may (fun v ->
77            args := sv_of_string "START_TAG" :: sv_of_string v :: !args) start_tag;
78     may (fun v ->
79            args := sv_of_string "END_TAG" :: sv_of_string v :: !args) end_tag;
80     may (fun v ->
81            args := sv_of_string "TAG_STYLE" :: sv_of_string v :: !args) tag_style;
82     may (fun v ->
83            args := sv_of_string "PRE_CHOMP" :: sv_of_bool v :: !args) pre_chomp;
84     may (fun v ->
85            args := sv_of_string "POST_CHOMP" :: sv_of_bool v :: !args) post_chomp;
86     may (fun v ->
87            args := sv_of_string "TRIM" :: sv_of_bool v :: !args) trim;
88     may (fun v ->
89            args := sv_of_string "INTERPOLATE" :: sv_of_bool v :: !args) interpolate;
90     may (fun v ->
91            args := sv_of_string "ANYCASE" :: sv_of_bool v :: !args) anycase;
92     may (fun v ->
93            args := sv_of_string "INCLUDE_PATH" :: arrayref (av_of_string_list v) :: !args) include_path;
94     may (fun v ->
95            args := sv_of_string "DELIMITER" :: sv_of_string v :: !args) delimiter;
96     may (fun v ->
97            args := sv_of_string "ABSOLUTE" :: sv_of_bool v :: !args) absolute;
98     may (fun v ->
99            args := sv_of_string "RELATIVE" :: sv_of_bool v :: !args) relative;
100     may (fun v ->
101            args := sv_of_string "DEFAULT" :: sv_of_string v :: !args) default;
102     may (fun v ->
103            args := sv_of_string "BLOCKS" :: hashref (hv_of_string_pair_list v) :: !args) blocks;
104     may (fun v ->
105            args := sv_of_string "AUTO_RESET" :: sv_of_bool v :: !args) auto_reset;
106     may (fun v ->
107            args := sv_of_string "RECURSION" :: sv_of_bool v :: !args) recursion;
108     may (fun v ->
109            args := sv_of_string "VARIABLES" :: sv_of_variant v :: !args) variables;
110     may (fun v ->
111            args := sv_of_string "CONSTANTS" :: sv_of_variant v :: !args) constants;
112     may (fun v ->
113            args := sv_of_string "CONSTANT_NAMESPACE" :: sv_of_string v :: !args) constant_namespace;
114     may (fun v ->
115            args := sv_of_string "NAMESPACE" :: sv_of_variant v :: !args) namespace;
116     may (fun v ->
117            args := sv_of_string "PRE_PROCESS" :: arrayref (av_of_string_list v) :: !args) pre_process;
118     may (fun v ->
119            args := sv_of_string "POST_PROCESS" :: arrayref (av_of_string_list v) :: !args) post_process;
120     may (fun v ->
121            args := sv_of_string "PROCESS" :: arrayref (av_of_string_list v) :: !args) process;
122     may (fun v ->
123            args := sv_of_string "WRAPPER" :: arrayref (av_of_string_list v) :: !args) wrapper;
124     may (fun v ->
125            args := sv_of_string "ERROR" :: sv_of_string v :: !args) error;
126     may (fun v ->
127            args := sv_of_string "ERRORS" :: hashref (hv_of_string_pair_list v) :: !args) errors;
128     may (fun v ->
129            args := sv_of_string "EVAL_PERL" :: sv_of_bool v :: !args) eval_perl;
130     may (fun v ->
131            raise Not_implemented) output;
132     may (fun v ->
133            raise Not_implemented) output_path;
134     may (fun v ->
135            args := sv_of_string "DEBUG" :: sv_of_string v :: !args) debug;
136     may (fun v ->
137            args := sv_of_string "DEBUG_FORMAT" :: sv_of_string v :: !args) debug_format;
138     may (fun v ->
139            args := sv_of_string "CACHE_SIZE" :: sv_of_int v :: !args) cache_size;
140     may (fun v ->
141            args := sv_of_string "COMPILE_EXT" :: sv_of_string v :: !args) compile_ext;
142     may (fun v ->
143            args := sv_of_string "COMPILE_DIR" :: sv_of_string v :: !args) compile_dir;
144     may (fun v ->
145            args := sv_of_string "PLUGINS" :: hashref (hv_of_string_pair_list v) :: !args) plugins;
146     may (fun v ->
147            args := sv_of_string "PLUGIN_BASE" :: arrayref (av_of_string_list v) :: !args) plugin_base;
148     may (fun v ->
149            args := sv_of_string "LOAD_PERL" :: sv_of_bool v :: !args) load_perl;
150     may (fun v ->
151            raise Not_implemented) filters;
152     may (fun v ->
153            args := sv_of_string "V1DOLLAR" :: sv_of_bool v :: !args) v1dollar;
154     may (fun v ->
155            raise Not_implemented) load_templates;
156     may (fun v ->
157            raise Not_implemented) load_plugins;
158     may (fun v ->
159            raise Not_implemented) load_filters;
160     may (fun v ->
161            args := sv_of_string "TOLERANT" :: sv_of_bool v :: !args) tolerant;
162     may (fun v ->
163            raise Not_implemented) service;
164     may (fun v ->
165            raise Not_implemented) context;
166     may (fun v ->
167            raise Not_implemented) stash;
168     may (fun v ->
169            raise Not_implemented) parser;
170     may (fun v ->
171            raise Not_implemented) grammar;
172     let sv = call_class_method "Template" "new" !args in
173       new template sv