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
51
52
53
54
55
56
57
58
59
60
61
type html = html_node list
and html_node =
| Node of nodename * attributes * html
| Data of string
| Rawdata of string
and nodename = string
and attributes = attribute list
and attribute = string * string option
let to_string html =
let b = Buffer.create 1024 in
let pp f = Printf.bprintf b f in
let rec loop = function
| Node(nodename, attributes, html) ->
pp "<%s" nodename;
ppa attributes;
pp ">";
List.iter loop html;
pp "</%s>" nodename
| Data s -> pp "%s" s
| Rawdata s -> pp "%s" s
| Comment c -> pp "<!-- %s -->" c
and ppa attrs =
List.iter
(function
| (a, Some v) ->
if not (String.contains v '\'') then
pp " %s='%s'" a v
else if not (String.contains v '"') then
pp " %s=\"%s\"" a v
else
(
pp " %s=\"" a;
for i = 0 to String.length v - 1 do
match v.[i] with
| '"' -> pp """
| c -> pp "%c" c
done;
pp "\""
)
| a, None ->
Printf.bprintf b " %s=''" a
)
attrs
in
List.iter loop html;
Buffer.contents b