1 /* Interface to libxml2.
2 Copyright (C) 2010 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
24 #include <libxml/tree.h>
25 #include <libxml/parser.h>
26 #include <libxml/HTMLparser.h>
31 Lisp_Object
make_dom (xmlNode
*node
)
33 if (node
->type
== XML_ELEMENT_NODE
) {
34 Lisp_Object result
= Fcons (intern (node
->name
), Qnil
);
38 /* First add the attributes. */
39 property
= node
->properties
;
40 while (property
!= NULL
) {
41 if (property
->children
&&
42 property
->children
->content
) {
43 char *pname
= xmalloc (strlen (property
->name
) + 2);
45 strcpy(pname
+ 1, property
->name
);
46 result
= Fcons (Fcons (intern (pname
),
47 build_string(property
->children
->content
)),
51 property
= property
->next
;
53 /* Then add the children of the node. */
54 child
= node
->children
;
55 while (child
!= NULL
) {
56 result
= Fcons (make_dom (child
), result
);
59 return Fnreverse (result
);
60 } else if (node
->type
== XML_TEXT_NODE
) {
61 Lisp_Object content
= Qnil
;
64 content
= build_string (node
->content
);
66 return Fcons (intern (node
->name
), content
);
72 parse_buffer (Lisp_Object string
, Lisp_Object base_url
, int htmlp
)
82 CHECK_STRING (string
);
84 if (! NILP (base_url
)) {
85 CHECK_STRING (base_url
);
86 burl
= SDATA (base_url
);
90 doc
= htmlReadMemory (SDATA (string
), SBYTES (string
), burl
, "utf-8",
91 HTML_PARSE_RECOVER
|HTML_PARSE_NONET
|
92 HTML_PARSE_NOWARNING
|HTML_PARSE_NOERROR
);
94 doc
= xmlReadMemory (SDATA (string
), SBYTES (string
), burl
, "utf-8",
95 XML_PARSE_NONET
|XML_PARSE_NOWARNING
|
99 node
= xmlDocGetRootElement (doc
);
101 result
= make_dom (node
);
110 DEFUN ("html-parse-string", Fhtml_parse_string
, Shtml_parse_string
,
112 doc
: /* Parse the string as an HTML document and return the parse tree.
113 If BASE-URL is non-nil, it will be used to expand relative URLs in
114 the HTML document.*/)
116 Lisp_Object string
, base_url
;
118 return parse_buffer (string
, base_url
, 1);
121 DEFUN ("xml-parse-string", Fxml_parse_string
, Sxml_parse_string
,
123 doc
: /* Parse the string as an XML document and return the parse tree.
124 If BASE-URL is non-nil, it will be used to expand relative URLs in
127 Lisp_Object string
, base_url
;
129 return parse_buffer (string
, base_url
, 0);
133 /***********************************************************************
135 ***********************************************************************/
139 defsubr (&Shtml_parse_string
);
140 defsubr (&Sxml_parse_string
);
143 #endif /* HAVE_LIBXML2 */