Add Bug#.
[bpt/emacs.git] / src / xml.c
CommitLineData
381408e2 1/* Interface to libxml2.
73b0cd50 2 Copyright (C) 2010-2011 Free Software Foundation, Inc.
381408e2
LMI
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
18
19#include <config.h>
20
21#ifdef HAVE_LIBXML2
22
23#include <setjmp.h>
24#include <libxml/tree.h>
25#include <libxml/parser.h>
26#include <libxml/HTMLparser.h>
27
28#include "lisp.h"
29#include "buffer.h"
30
31Lisp_Object make_dom (xmlNode *node)
32{
4b9832a6
CY
33 if (node->type == XML_ELEMENT_NODE)
34 {
c45e5276 35 Lisp_Object result = Fcons (intern ((char *) node->name), Qnil);
4b9832a6
CY
36 xmlNode *child;
37 xmlAttr *property;
38 Lisp_Object plist = Qnil;
39
40 /* First add the attributes. */
41 property = node->properties;
42 while (property != NULL)
43 {
44 if (property->children &&
45 property->children->content)
46 {
c45e5276
PE
47 char *content = (char *) property->children->content;
48 plist = Fcons (Fcons (intern ((char *) property->name),
49 build_string (content)),
4b9832a6
CY
50 plist);
51 }
52 property = property->next;
53 }
54 result = Fcons (Fnreverse (plist), result);
55
56 /* Then add the children of the node. */
57 child = node->children;
58 while (child != NULL)
59 {
60 result = Fcons (make_dom (child), result);
61 child = child->next;
62 }
63
64 return Fnreverse (result);
381408e2 65 }
3c2317e8 66 else if (node->type == XML_TEXT_NODE || node->type == XML_CDATA_SECTION_NODE)
4b9832a6
CY
67 {
68 if (node->content)
c45e5276 69 return build_string ((char *) node->content);
4b9832a6
CY
70 else
71 return Qnil;
381408e2 72 }
4b9832a6 73 else
381408e2
LMI
74 return Qnil;
75}
76
77static Lisp_Object
1b217849 78parse_region (Lisp_Object start, Lisp_Object end, Lisp_Object base_url, int htmlp)
381408e2
LMI
79{
80 xmlDoc *doc;
81 xmlNode *node;
c97c655f 82 Lisp_Object result = Qnil;
55e572ef 83 const char *burl = "";
1b217849
LMI
84 EMACS_INT bytes;
85 EMACS_INT istart, iend;
8b620f11 86
381408e2 87 LIBXML_TEST_VERSION;
8b620f11 88
1b217849 89 validate_region (&start, &end);
20a5e996 90
1b217849
LMI
91 istart = XINT (start);
92 iend = XINT (end);
93
94 if (istart < GPT && GPT < iend)
95 move_gap (iend);
381408e2 96
4b9832a6
CY
97 if (! NILP (base_url))
98 {
99 CHECK_STRING (base_url);
42a5b22f 100 burl = SSDATA (base_url);
4b9832a6 101 }
8b620f11 102
1b217849 103 bytes = CHAR_TO_BYTE (iend) - CHAR_TO_BYTE (istart);
20a5e996 104
1b217849 105 if (htmlp)
c45e5276 106 doc = htmlReadMemory ((char *) BYTE_POS_ADDR (CHAR_TO_BYTE (istart)),
1b217849
LMI
107 bytes, burl, "utf-8",
108 HTML_PARSE_RECOVER|HTML_PARSE_NONET|
3c2317e8
LMI
109 HTML_PARSE_NOWARNING|HTML_PARSE_NOERROR|
110 HTML_PARSE_NOBLANKS);
1b217849 111 else
c45e5276 112 doc = xmlReadMemory ((char *) BYTE_POS_ADDR (CHAR_TO_BYTE (istart)),
1b217849
LMI
113 bytes, burl, "utf-8",
114 XML_PARSE_NONET|XML_PARSE_NOWARNING|
115 XML_PARSE_NOERROR);
4b9832a6
CY
116
117 if (doc != NULL)
118 {
119 node = xmlDocGetRootElement (doc);
120 if (node != NULL)
121 result = make_dom (node);
122 xmlFreeDoc (doc);
123 xmlCleanupParser ();
124 }
8b620f11 125
381408e2
LMI
126 return result;
127}
128
1b217849
LMI
129DEFUN ("libxml-parse-html-region", Flibxml_parse_html_region,
130 Slibxml_parse_html_region,
131 2, 3, 0,
132 doc: /* Parse the region as an HTML document and return the parse tree.
4b9832a6 133If BASE-URL is non-nil, it is used to expand relative URLs. */)
1b217849 134 (Lisp_Object start, Lisp_Object end, Lisp_Object base_url)
381408e2 135{
1b217849 136 return parse_region (start, end, base_url, 1);
381408e2
LMI
137}
138
1b217849
LMI
139DEFUN ("libxml-parse-xml-region", Flibxml_parse_xml_region,
140 Slibxml_parse_xml_region,
141 2, 3, 0,
142 doc: /* Parse the region as an XML document and return the parse tree.
4b9832a6 143If BASE-URL is non-nil, it is used to expand relative URLs. */)
1b217849 144 (Lisp_Object start, Lisp_Object end, Lisp_Object base_url)
381408e2 145{
1b217849 146 return parse_region (start, end, base_url, 0);
381408e2
LMI
147}
148
149\f
150/***********************************************************************
151 Initialization
152 ***********************************************************************/
153void
154syms_of_xml (void)
155{
1b217849
LMI
156 defsubr (&Slibxml_parse_html_region);
157 defsubr (&Slibxml_parse_xml_region);
381408e2
LMI
158}
159
160#endif /* HAVE_LIBXML2 */