Commit | Line | Data |
---|---|---|
381408e2 LMI |
1 | /* Interface to libxml2. |
2 | Copyright (C) 2010 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GNU Emacs. | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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/>. */ | |
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 | ||
31 | Lisp_Object make_dom (xmlNode *node) | |
32 | { | |
4b9832a6 CY |
33 | if (node->type == XML_ELEMENT_NODE) |
34 | { | |
35 | Lisp_Object result = Fcons (intern (node->name), Qnil); | |
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 | { | |
47 | plist = Fcons (Fcons (intern (property->name), | |
48 | build_string (property->children->content)), | |
49 | plist); | |
50 | } | |
51 | property = property->next; | |
52 | } | |
53 | result = Fcons (Fnreverse (plist), result); | |
54 | ||
55 | /* Then add the children of the node. */ | |
56 | child = node->children; | |
57 | while (child != NULL) | |
58 | { | |
59 | result = Fcons (make_dom (child), result); | |
60 | child = child->next; | |
61 | } | |
62 | ||
63 | return Fnreverse (result); | |
381408e2 | 64 | } |
4b9832a6 CY |
65 | else if (node->type == XML_TEXT_NODE) |
66 | { | |
67 | if (node->content) | |
68 | return build_string (node->content); | |
69 | else | |
70 | return Qnil; | |
381408e2 | 71 | } |
4b9832a6 | 72 | else |
381408e2 LMI |
73 | return Qnil; |
74 | } | |
75 | ||
76 | static Lisp_Object | |
c97c655f | 77 | parse_string (Lisp_Object string, Lisp_Object base_url, int htmlp) |
381408e2 LMI |
78 | { |
79 | xmlDoc *doc; | |
80 | xmlNode *node; | |
c97c655f | 81 | Lisp_Object result = Qnil; |
381408e2 | 82 | int ibeg, iend; |
55e572ef | 83 | const char *burl = ""; |
8b620f11 | 84 | |
381408e2 | 85 | LIBXML_TEST_VERSION; |
8b620f11 | 86 | |
381408e2 LMI |
87 | CHECK_STRING (string); |
88 | ||
4b9832a6 CY |
89 | if (! NILP (base_url)) |
90 | { | |
91 | CHECK_STRING (base_url); | |
92 | burl = SDATA (base_url); | |
93 | } | |
8b620f11 | 94 | |
4b9832a6 CY |
95 | doc = htmlp |
96 | ? htmlReadMemory (SDATA (string), SBYTES (string), burl, "utf-8", | |
97 | HTML_PARSE_RECOVER|HTML_PARSE_NONET| | |
98 | HTML_PARSE_NOWARNING|HTML_PARSE_NOERROR) | |
99 | : xmlReadMemory (SDATA (string), SBYTES (string), burl, "utf-8", | |
100 | XML_PARSE_NONET|XML_PARSE_NOWARNING| | |
101 | XML_PARSE_NOERROR); | |
102 | ||
103 | if (doc != NULL) | |
104 | { | |
105 | node = xmlDocGetRootElement (doc); | |
106 | if (node != NULL) | |
107 | result = make_dom (node); | |
108 | xmlFreeDoc (doc); | |
109 | xmlCleanupParser (); | |
110 | } | |
8b620f11 | 111 | |
381408e2 LMI |
112 | return result; |
113 | } | |
114 | ||
4b9832a6 CY |
115 | DEFUN ("xml-parse-html-string-internal", Fxml_parse_html_string_internal, |
116 | Sxml_parse_html_string_internal, | |
1da70e99 AS |
117 | 1, 2, 0, |
118 | doc: /* Parse STRING as an HTML document and return the parse tree. | |
4b9832a6 | 119 | If BASE-URL is non-nil, it is used to expand relative URLs. */) |
1da70e99 | 120 | (Lisp_Object string, Lisp_Object base_url) |
381408e2 | 121 | { |
c97c655f | 122 | return parse_string (string, base_url, 1); |
381408e2 LMI |
123 | } |
124 | ||
4b9832a6 CY |
125 | DEFUN ("xml-parse-string-internal", Fxml_parse_string_internal, |
126 | Sxml_parse_string_internal, | |
1da70e99 AS |
127 | 1, 2, 0, |
128 | doc: /* Parse STRING as an XML document and return the parse tree. | |
4b9832a6 | 129 | If BASE-URL is non-nil, it is used to expand relative URLs. */) |
1da70e99 | 130 | (Lisp_Object string, Lisp_Object base_url) |
381408e2 | 131 | { |
c97c655f | 132 | return parse_string (string, base_url, 0); |
381408e2 LMI |
133 | } |
134 | ||
135 | \f | |
136 | /*********************************************************************** | |
137 | Initialization | |
138 | ***********************************************************************/ | |
139 | void | |
140 | syms_of_xml (void) | |
141 | { | |
4b9832a6 CY |
142 | defsubr (&Sxml_parse_html_string_internal); |
143 | defsubr (&Sxml_parse_string_internal); | |
381408e2 LMI |
144 | } |
145 | ||
146 | #endif /* HAVE_LIBXML2 */ |