1 /* Interface to libxml2.
2 Copyright (C) 2010-2011 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>
32 make_dom (xmlNode
*node
)
34 if (node
->type
== XML_ELEMENT_NODE
)
36 Lisp_Object result
= Fcons (intern ((char *) node
->name
), Qnil
);
39 Lisp_Object plist
= Qnil
;
41 /* First add the attributes. */
42 property
= node
->properties
;
43 while (property
!= NULL
)
45 if (property
->children
&&
46 property
->children
->content
)
48 char *content
= (char *) property
->children
->content
;
49 plist
= Fcons (Fcons (intern ((char *) property
->name
),
50 build_string (content
)),
53 property
= property
->next
;
55 result
= Fcons (Fnreverse (plist
), result
);
57 /* Then add the children of the node. */
58 child
= node
->children
;
61 result
= Fcons (make_dom (child
), result
);
65 return Fnreverse (result
);
67 else if (node
->type
== XML_TEXT_NODE
|| node
->type
== XML_CDATA_SECTION_NODE
)
70 return build_string ((char *) node
->content
);
74 else if (node
->type
== XML_COMMENT_NODE
)
77 return list3 (intern ("comment"), Qnil
,
78 build_string ((char *) node
->content
));
87 parse_region (Lisp_Object start
, Lisp_Object end
, Lisp_Object base_url
, int htmlp
)
90 Lisp_Object result
= Qnil
;
91 const char *burl
= "";
93 EMACS_INT istart
, iend
;
97 validate_region (&start
, &end
);
99 istart
= XINT (start
);
102 if (istart
< GPT
&& GPT
< iend
)
105 if (! NILP (base_url
))
107 CHECK_STRING (base_url
);
108 burl
= SSDATA (base_url
);
111 bytes
= CHAR_TO_BYTE (iend
) - CHAR_TO_BYTE (istart
);
114 doc
= htmlReadMemory ((char *) BYTE_POS_ADDR (CHAR_TO_BYTE (istart
)),
115 bytes
, burl
, "utf-8",
116 HTML_PARSE_RECOVER
|HTML_PARSE_NONET
|
117 HTML_PARSE_NOWARNING
|HTML_PARSE_NOERROR
|
118 HTML_PARSE_NOBLANKS
);
120 doc
= xmlReadMemory ((char *) BYTE_POS_ADDR (CHAR_TO_BYTE (istart
)),
121 bytes
, burl
, "utf-8",
122 XML_PARSE_NONET
|XML_PARSE_NOWARNING
|
123 XML_PARSE_NOBLANKS
|XML_PARSE_NOERROR
);
127 xmlNode
*n
= doc
->children
->next
;
128 Lisp_Object r
= Qnil
;
132 result
= Fcons (r
, result
);
140 result
= Fcons (intern ("top"),
141 Fcons (Qnil
, Fnreverse (Fcons (r
, result
))));
150 DEFUN ("libxml-parse-html-region", Flibxml_parse_html_region
,
151 Slibxml_parse_html_region
,
153 doc
: /* Parse the region as an HTML document and return the parse tree.
154 If BASE-URL is non-nil, it is used to expand relative URLs. */)
155 (Lisp_Object start
, Lisp_Object end
, Lisp_Object base_url
)
157 return parse_region (start
, end
, base_url
, 1);
160 DEFUN ("libxml-parse-xml-region", Flibxml_parse_xml_region
,
161 Slibxml_parse_xml_region
,
163 doc
: /* Parse the region as an XML document and return the parse tree.
164 If BASE-URL is non-nil, it is used to expand relative URLs. */)
165 (Lisp_Object start
, Lisp_Object end
, Lisp_Object base_url
)
167 return parse_region (start
, end
, base_url
, 0);
171 /***********************************************************************
173 ***********************************************************************/
177 defsubr (&Slibxml_parse_html_region
);
178 defsubr (&Slibxml_parse_xml_region
);
181 #endif /* HAVE_LIBXML2 */