Merge changes made in Gnus trunk.
[emacs.git] / src / xml.c
bloba686e55f0b0d9ce8d4b0784af0c171564eb5afe5
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/>. */
19 #include <config.h>
21 #ifdef HAVE_LIBXML2
23 #include <setjmp.h>
24 #include <libxml/tree.h>
25 #include <libxml/parser.h>
26 #include <libxml/HTMLparser.h>
28 #include "lisp.h"
29 #include "buffer.h"
31 Lisp_Object make_dom (xmlNode *node)
33 if (node->type == XML_ELEMENT_NODE)
35 Lisp_Object result = Fcons (intern (node->name), Qnil);
36 xmlNode *child;
37 xmlAttr *property;
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 plist = Fcons (Fcons (intern (property->name),
48 build_string (property->children->content)),
49 plist);
51 property = property->next;
53 result = Fcons (Fnreverse (plist), result);
55 /* Then add the children of the node. */
56 child = node->children;
57 while (child != NULL)
59 result = Fcons (make_dom (child), result);
60 child = child->next;
63 return Fnreverse (result);
65 else if (node->type == XML_TEXT_NODE)
67 if (node->content)
68 return build_string (node->content);
69 else
70 return Qnil;
72 else
73 return Qnil;
76 static Lisp_Object
77 parse_region (Lisp_Object start, Lisp_Object end, Lisp_Object base_url, int htmlp)
79 xmlDoc *doc;
80 xmlNode *node;
81 Lisp_Object result = Qnil;
82 const char *burl = "";
83 EMACS_INT bytes;
84 EMACS_INT istart, iend;
86 LIBXML_TEST_VERSION;
88 validate_region (&start, &end);
90 istart = XINT (start);
91 iend = XINT (end);
93 if (istart < GPT && GPT < iend)
94 move_gap (iend);
96 if (! NILP (base_url))
98 CHECK_STRING (base_url);
99 burl = SDATA (base_url);
102 bytes = CHAR_TO_BYTE (iend) - CHAR_TO_BYTE (istart);
104 if (htmlp)
105 doc = htmlReadMemory (BYTE_POS_ADDR (CHAR_TO_BYTE (istart)),
106 bytes, burl, "utf-8",
107 HTML_PARSE_RECOVER|HTML_PARSE_NONET|
108 HTML_PARSE_NOWARNING|HTML_PARSE_NOERROR);
109 else
110 doc = xmlReadMemory (BYTE_POS_ADDR (CHAR_TO_BYTE (istart)),
111 bytes, burl, "utf-8",
112 XML_PARSE_NONET|XML_PARSE_NOWARNING|
113 XML_PARSE_NOERROR);
115 if (doc != NULL)
117 node = xmlDocGetRootElement (doc);
118 if (node != NULL)
119 result = make_dom (node);
120 xmlFreeDoc (doc);
121 xmlCleanupParser ();
124 return result;
127 DEFUN ("libxml-parse-html-region", Flibxml_parse_html_region,
128 Slibxml_parse_html_region,
129 2, 3, 0,
130 doc: /* Parse the region as an HTML document and return the parse tree.
131 If BASE-URL is non-nil, it is used to expand relative URLs. */)
132 (Lisp_Object start, Lisp_Object end, Lisp_Object base_url)
134 return parse_region (start, end, base_url, 1);
137 DEFUN ("libxml-parse-xml-region", Flibxml_parse_xml_region,
138 Slibxml_parse_xml_region,
139 2, 3, 0,
140 doc: /* Parse the region as an XML document and return the parse tree.
141 If BASE-URL is non-nil, it is used to expand relative URLs. */)
142 (Lisp_Object start, Lisp_Object end, Lisp_Object base_url)
144 return parse_region (start, end, base_url, 0);
148 /***********************************************************************
149 Initialization
150 ***********************************************************************/
151 void
152 syms_of_xml (void)
154 defsubr (&Slibxml_parse_html_region);
155 defsubr (&Slibxml_parse_xml_region);
158 #endif /* HAVE_LIBXML2 */