-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.ml
More file actions
73 lines (57 loc) · 1.28 KB
/
scan.ml
File metadata and controls
73 lines (57 loc) · 1.28 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
(* Scan *)
open Nethtml
(** Combinators for extracting data from Xml documents *)
let rec has f e =
f e ||
match e with
| Data _ -> false
| Element(_, _, xl) ->
List.exists (has f) xl
let rec visit f e =
f e;
match e with
| Data _ -> ()
| Element(_, _, xl) ->
List.iter (visit f) xl
(** [element name] is a predicate that returns [true] iff the element is of
kind name *)
let element u = function
| Element(v, _, _) -> u = v
| _ -> false
let with_attribute name f q = function
| Data _ -> q
| Element(_, attrs, _) ->
try
f (List.assoc name attrs)
with
| Not_found -> q
let on_matching f g e =
visit
(fun e ->
if f e then g e
)
e
let alt f1 g1 f2 g2 e =
visit
(fun e ->
if f1 e then
(g1 e; true)
else
if f2 e then
(g2 e; true)
else
false)
let data_matches f = function
| Data u -> f u
| _ -> false
let on_data f = function
| Data u -> f u
| _ -> ()
let show_data e =
visit
(fun e -> on_data (fun u -> Printf.printf "%S\n%!" u) e; false)
e
let get_classes e = with_attribute "class" (fun cl -> Util.split_at ' ' cl) [] e
let has_class cl e = List.mem cl (get_classes e)
let ( &&& ) f1 f2 e = f1 e && f2 e
let ( !!! ) f e = not (f e)