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>
31 Lisp_Object
make_dom (xmlNode
*node
)
33 if (node
->type
== XML_ELEMENT_NODE
)
35 Lisp_Object result
= Fcons (intern ((char *) node
->name
), Qnil
);
38 Lisp_Object plist
= Qnil
;
40 /* First add the attributes. */
41 property
= node
->properties
;
42 while (property
!= NULL
)
44 if (property
->children
&&
45 property
->children
->content
)
47 char *content
= (char *) property
->children
->content
;
48 plist
= Fcons (Fcons (intern ((char *) property
->name
),
49 build_string (content
)),
52 property
= property
->next
;
54 result
= Fcons (Fnreverse (plist
), result
);
56 /* Then add the children of the node. */
57 child
= node
->children
;
60 result
= Fcons (make_dom (child
), result
);
64 return Fnreverse (result
);
66 else if (node
->type
== XML_TEXT_NODE
|| node
->type
== XML_CDATA_SECTION_NODE
)
69 return build_string ((char *) node
->content
);
78 parse_region (Lisp_Object start
, Lisp_Object end
, Lisp_Object base_url
, int htmlp
)
82 Lisp_Object result
= Qnil
;
83 const char *burl
= "";
85 EMACS_INT istart
, iend
;
89 validate_region (&start
, &end
);
91 istart
= XINT (start
);
94 if (istart
< GPT
&& GPT
< iend
)
97 if (! NILP (base_url
))
99 CHECK_STRING (base_url
);
100 burl
= SSDATA (base_url
);
103 bytes
= CHAR_TO_BYTE (iend
) - CHAR_TO_BYTE (istart
);
106 doc
= htmlReadMemory ((char *) BYTE_POS_ADDR (CHAR_TO_BYTE (istart
)),
107 bytes
, burl
, "utf-8",
108 HTML_PARSE_RECOVER
|HTML_PARSE_NONET
|
109 HTML_PARSE_NOWARNING
|HTML_PARSE_NOERROR
|
110 HTML_PARSE_NOBLANKS
);
112 doc
= xmlReadMemory ((char *) BYTE_POS_ADDR (CHAR_TO_BYTE (istart
)),
113 bytes
, burl
, "utf-8",
114 XML_PARSE_NONET
|XML_PARSE_NOWARNING
|
119 node
= xmlDocGetRootElement (doc
);
121 result
= make_dom (node
);
129 DEFUN ("libxml-parse-html-region", Flibxml_parse_html_region
,
130 Slibxml_parse_html_region
,
132 doc
: /* Parse the region as an HTML document and return the parse tree.
133 If BASE-URL is non-nil, it is used to expand relative URLs. */)
134 (Lisp_Object start
, Lisp_Object end
, Lisp_Object base_url
)
136 return parse_region (start
, end
, base_url
, 1);
139 DEFUN ("libxml-parse-xml-region", Flibxml_parse_xml_region
,
140 Slibxml_parse_xml_region
,
142 doc
: /* Parse the region as an XML document and return the parse tree.
143 If BASE-URL is non-nil, it is used to expand relative URLs. */)
144 (Lisp_Object start
, Lisp_Object end
, Lisp_Object base_url
)
146 return parse_region (start
, end
, base_url
, 0);
150 /***********************************************************************
152 ***********************************************************************/
156 defsubr (&Slibxml_parse_html_region
);
157 defsubr (&Slibxml_parse_xml_region
);
160 #endif /* HAVE_LIBXML2 */