Skip to content

Commit 3279122

Browse files
committed
Add Input stream functions.
1 parent f3f86a8 commit 3279122

9 files changed

+136
-2
lines changed

implem/tyxml_xml.ml

+1
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,5 @@ include Xml_print.Make_simple(M)(struct let emptytags = [] end)
115115
include Xml_iter.Make(M)
116116
include Xml_print.Make_fmt(M)(struct let emptytags = [] end)
117117

118+
include Xml_stream.Import(M)
118119
let print fmt x = print_list ~output:(Format.pp_print_string fmt) [x]

implem/tyxml_xml.mli

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ include Xml_sigs.Iterable
3131
include Xml_sigs.Pp
3232
with type elt := elt
3333

34+
(** {2 Import/Export} *)
35+
36+
val of_seq : Xml_stream.signal Seq.t -> elt list
37+
3438

3539
(** {2 Iterators} *)
3640

lib/html_f.ml

+3
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,9 @@ struct
811811
type doc = [ `Html ] elt
812812
let doc_toelt x = x
813813

814+
module I = Xml_stream.Import(Xml)
815+
let of_seq s = totl @@ I.of_seq s
816+
814817
module Unsafe = struct
815818

816819
let data s = Xml.encodedpcdata s

lib/html_sigs.mli

+11-1
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,17 @@ module type T = sig
10531053

10541054
val ruby : ([< | ruby_attrib], [< | ruby_content_fun], [> | ruby]) star
10551055

1056-
(** {2 Conversion with untyped representation} *)
1056+
(** {2 Conversion with untyped representation}
1057+
1058+
WARNING: These functions do not ensure HTML or SVG validity! You should
1059+
always explicitly given an appropriate type to the output.
1060+
*)
1061+
1062+
(** [import signal] converts the given XML signal into Tyxml elements.
1063+
It can be used with HTML and SVG parsing libraries, such as Markup.
1064+
@raise malformed_stream if the stream is malformed.
1065+
*)
1066+
val of_seq : Xml_stream.signal Seq.t -> 'a elt list_wrap
10571067

10581068
val tot : Xml.elt -> 'a elt
10591069
val totl : Xml.elt list_wrap -> 'a elt list_wrap

lib/svg_f.ml

+3
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,9 @@ struct
894894
type doc = [ `Svg ] elt
895895
let doc_toelt x = x
896896

897+
module I = Xml_stream.Import(Xml)
898+
let of_seq s = totl @@ I.of_seq s
899+
897900
module Unsafe = struct
898901

899902
let data s = Xml.encodedpcdata s

lib/svg_sigs.mli

+11-1
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,17 @@ module type T = sig
955955
?a: ((foreignobject_attr attrib) list) ->
956956
Xml.elt list_wrap -> [> | foreignobject] elt
957957

958-
(** {2 Conversion with untyped representation} *)
958+
(** {2 Conversion with untyped representation}
959+
960+
WARNING: These functions do not ensure HTML or SVG validity! You should
961+
always explicitly given an appropriate type to the output.
962+
*)
963+
964+
(** [import signal] converts the given XML signal into Tyxml elements.
965+
It can be used with HTML and SVG parsing libraries, such as Markup.
966+
@raise malformed_stream if the stream is malformed.
967+
*)
968+
val of_seq : Xml_stream.signal Seq.t -> 'a elt list_wrap
959969

960970
val tot : Xml.elt -> 'a elt
961971
val totl : Xml.elt list_wrap -> ('a elt) list_wrap

lib/xml_stream.ml

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
(* TyXML
2+
* http://www.ocsigen.org/tyxml
3+
* Copyright (C) 2018 Gabriel Radanne
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, with linking exception;
8+
* either version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02111-1307, USA.
18+
*)
19+
20+
type name = string * string
21+
22+
(** Input *)
23+
24+
type signal = [
25+
| `Comment of string
26+
| `End_element
27+
| `Start_element of name * (name * string) list
28+
| `Text of string list
29+
]
30+
31+
exception Malformed_stream
32+
33+
module Import
34+
(Xml : Xml_sigs.T)
35+
= struct
36+
37+
let of_list l =
38+
List.fold_right
39+
(fun a b -> Xml.W.(cons (return a) b))
40+
l (Xml.W.nil ())
41+
42+
let mk_attribs attrs =
43+
(* TODO: This is not very structured *)
44+
let f ((_,name), v) = Xml.string_attrib name (Xml.W.return v) in
45+
List.map f attrs
46+
47+
let rec mk children (seq : signal Seq.t) = match seq () with
48+
| Cons (`Comment s, q) ->
49+
mk (Xml.comment s :: children) q
50+
| Cons (`Text s, q) ->
51+
mk (List.map (fun x -> Xml.pcdata @@ Xml.W.return x) s @ children) q
52+
| Cons (`Start_element ((_, name), attrs), q) ->
53+
let a = mk_attribs attrs in
54+
let sub_children, rest = mk [] q in
55+
mk (Xml.node ~a name sub_children :: children) rest
56+
| Cons (`End_element, rest) ->
57+
of_list (List.rev children), rest
58+
| Nil ->
59+
of_list (List.rev children), Seq.empty
60+
61+
let of_seq seq =
62+
let l, rest = mk [] seq in
63+
match rest () with
64+
| Seq.Nil -> l
65+
| _ -> raise Malformed_stream
66+
67+
end

lib/xml_stream.mli

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(* TyXML
2+
* http://www.ocsigen.org/tyxml
3+
* Copyright (C) 2018 Gabriel Radanne
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, with linking exception;
8+
* either version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02111-1307, USA.
18+
*)
19+
20+
type name = string * string
21+
22+
(** {2 Input} *)
23+
24+
type signal = [
25+
| `Comment of string
26+
| `End_element
27+
| `Start_element of name * (name * string) list
28+
| `Text of string list
29+
]
30+
31+
exception Malformed_stream
32+
33+
module Import (Xml : Xml_sigs.T) : sig
34+
val of_seq : signal Seq.t -> Xml.elt Xml.list_wrap
35+
end

tyxml.opam

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ build: [
1919

2020
depends: [
2121
"dune" {build}
22+
"seq"
2223
"uutf" {>= "1.0.0"}
2324
"re" {>= "1.5.0"}
2425
"alcotest" {test}

0 commit comments

Comments
 (0)