@@ -48,16 +48,39 @@ let encode_unsafe_char_and_at s =
48
48
Buffer. contents b
49
49
50
50
let compose_decl ?(version = " 1.0" ) ?(encoding = " UTF-8" ) () =
51
- " <?xml version=\" " ^ version ^ " \" encoding=\" " ^ encoding ^ " \" ?>\n "
51
+ Format. sprintf
52
+ {|<? xml version= " %s" encoding= " %s" ?> \n| }
53
+ version encoding
52
54
53
55
let compose_doctype dt args =
54
- " <!DOCTYPE " ^ dt
55
- ^ (if args = []
56
- then " "
57
- else
58
- " PUBLIC " ^
59
- String. concat " " (List. map (fun a -> " \" " ^ a ^ " \" " ) args)) ^ " >"
60
-
56
+ let pp_args fmt = function
57
+ | [] -> ()
58
+ | l ->
59
+ Format. fprintf fmt " PUBLIC %a"
60
+ (Format. pp_print_list ~pp_sep: Format. pp_print_space
61
+ (fun fmt -> Format. fprintf fmt " %S" ))
62
+ l
63
+ in
64
+ Format. asprintf
65
+ " <!DOCTYPE %s%a>"
66
+ dt
67
+ pp_args args
68
+
69
+ let re_end_comment = Re. (compile @@ alt [
70
+ seq [ bos ; str " >" ] ;
71
+ seq [ bos ; str " ->" ] ;
72
+ str " -->" ;
73
+ str " --!>" ;
74
+ ])
75
+ let escape_comment s =
76
+ let f g = match Re.Group. get g 0 with
77
+ | ">" -> " >"
78
+ | "->" -> " ->"
79
+ | "-->" -> " -->"
80
+ | "--!>" -> " --!>"
81
+ | s -> s
82
+ in
83
+ Re. replace ~all: true re_end_comment ~f s
61
84
62
85
(* copied form js_of_ocaml: compiler/javascript.ml *)
63
86
let pp_number fmt v =
@@ -177,16 +200,6 @@ module type TagList = sig val emptytags : string list end
177
200
178
201
let pp_noop _fmt _ = ()
179
202
180
- (* Present only in ocaml >= 4.02 *)
181
- let rec pp_print_list ~pp_sep pp_v ppf = function
182
- | [] -> ()
183
- | [v] -> pp_v ppf v
184
- | v :: vs ->
185
- pp_v ppf v;
186
- pp_sep ppf () ;
187
- pp_print_list ~pp_sep pp_v ppf vs
188
-
189
-
190
203
module Make_fmt
191
204
(Xml : Xml_sigs.Iterable )
192
205
(I : TagList ) =
@@ -213,14 +226,14 @@ struct
213
226
| AStr s -> Format. fprintf fmt " %S" (encode s)
214
227
| AStrL (sep , slist ) ->
215
228
Format. fprintf fmt " \" %a\" "
216
- (pp_print_list ~pp_sep: (pp_sep sep) (pp_encode encode)) slist
229
+ (Format. pp_print_list ~pp_sep: (pp_sep sep) (pp_encode encode)) slist
217
230
218
231
let pp_attrib encode fmt a =
219
232
Format. fprintf fmt
220
233
" %s=%a" (aname a) (pp_attrib_value encode) a
221
234
222
235
let pp_attribs encode =
223
- pp_print_list ~pp_sep: pp_noop (pp_attrib encode)
236
+ Format. pp_print_list ~pp_sep: pp_noop (pp_attrib encode)
224
237
225
238
let pp_closedtag encode fmt tag attrs =
226
239
if is_emptytag tag then
@@ -240,13 +253,13 @@ struct
240
253
241
254
and pp_elt encode fmt elt = match content elt with
242
255
| Comment texte ->
243
- Format. fprintf fmt " <!--%a -->" (pp_encode encode) texte
256
+ Format. fprintf fmt " <!--%s -->" (escape_comment texte)
244
257
245
258
| Entity e ->
246
259
Format. fprintf fmt " &%s;" e
247
260
248
261
| PCDATA texte ->
249
- Format. pp_print_string fmt ( encode texte)
262
+ pp_encode encode fmt texte
250
263
251
264
| EncodedPCDATA texte ->
252
265
Format. pp_print_string fmt texte
@@ -260,7 +273,7 @@ struct
260
273
| Empty -> ()
261
274
262
275
and pp_elts encode =
263
- pp_print_list ~pp_sep: pp_noop (pp_elt encode)
276
+ Format. pp_print_list ~pp_sep: pp_noop (pp_elt encode)
264
277
265
278
let pp ?(encode =encode_unsafe_char) () =
266
279
pp_elt encode
0 commit comments