diff options
Diffstat (limited to 'src/xml.c')
| -rw-r--r-- | src/xml.c | 145 |
1 files changed, 75 insertions, 70 deletions
| @@ -30,50 +30,55 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ | |||
| 30 | 30 | ||
| 31 | Lisp_Object make_dom (xmlNode *node) | 31 | Lisp_Object make_dom (xmlNode *node) |
| 32 | { | 32 | { |
| 33 | if (node->type == XML_ELEMENT_NODE) { | 33 | if (node->type == XML_ELEMENT_NODE) |
| 34 | Lisp_Object result = Fcons (intern (node->name), Qnil); | 34 | { |
| 35 | xmlNode *child; | 35 | Lisp_Object result = Fcons (intern (node->name), Qnil); |
| 36 | xmlAttr *property; | 36 | xmlNode *child; |
| 37 | 37 | xmlAttr *property; | |
| 38 | /* First add the attributes. */ | 38 | Lisp_Object plist = Qnil; |
| 39 | property = node->properties; | 39 | |
| 40 | while (property != NULL) { | 40 | /* First add the attributes. */ |
| 41 | if (property->children && | 41 | property = node->properties; |
| 42 | property->children->content) { | 42 | while (property != NULL) |
| 43 | char *pname = xmalloc (strlen (property->name) + 2); | 43 | { |
| 44 | *pname = ':'; | 44 | if (property->children && |
| 45 | strcpy(pname + 1, property->name); | 45 | property->children->content) |
| 46 | result = Fcons (Fcons (intern (pname), | 46 | { |
| 47 | build_string(property->children->content)), | 47 | plist = Fcons (Fcons (intern (property->name), |
| 48 | result); | 48 | build_string (property->children->content)), |
| 49 | xfree (pname); | 49 | plist); |
| 50 | } | 50 | } |
| 51 | property = property->next; | 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); | ||
| 52 | } | 64 | } |
| 53 | /* Then add the children of the node. */ | 65 | else if (node->type == XML_TEXT_NODE) |
| 54 | child = node->children; | 66 | { |
| 55 | while (child != NULL) { | 67 | if (node->content) |
| 56 | result = Fcons (make_dom (child), result); | 68 | return build_string (node->content); |
| 57 | child = child->next; | 69 | else |
| 70 | return Qnil; | ||
| 58 | } | 71 | } |
| 59 | return Fnreverse (result); | 72 | else |
| 60 | } else if (node->type == XML_TEXT_NODE) { | ||
| 61 | Lisp_Object content = Qnil; | ||
| 62 | |||
| 63 | if (node->content) | ||
| 64 | content = build_string (node->content); | ||
| 65 | |||
| 66 | return Fcons (intern (node->name), content); | ||
| 67 | } else | ||
| 68 | return Qnil; | 73 | return Qnil; |
| 69 | } | 74 | } |
| 70 | 75 | ||
| 71 | static Lisp_Object | 76 | static Lisp_Object |
| 72 | parse_buffer (Lisp_Object string, Lisp_Object base_url, int htmlp) | 77 | parse_string (Lisp_Object string, Lisp_Object base_url, int htmlp) |
| 73 | { | 78 | { |
| 74 | xmlDoc *doc; | 79 | xmlDoc *doc; |
| 75 | xmlNode *node; | 80 | xmlNode *node; |
| 76 | Lisp_Object result; | 81 | Lisp_Object result = Qnil; |
| 77 | int ibeg, iend; | 82 | int ibeg, iend; |
| 78 | char *burl = ""; | 83 | char *burl = ""; |
| 79 | 84 | ||
| @@ -81,50 +86,50 @@ parse_buffer (Lisp_Object string, Lisp_Object base_url, int htmlp) | |||
| 81 | 86 | ||
| 82 | CHECK_STRING (string); | 87 | CHECK_STRING (string); |
| 83 | 88 | ||
| 84 | if (! NILP (base_url)) { | 89 | if (! NILP (base_url)) |
| 85 | CHECK_STRING (base_url); | 90 | { |
| 86 | burl = SDATA (base_url); | 91 | CHECK_STRING (base_url); |
| 87 | } | 92 | burl = SDATA (base_url); |
| 88 | 93 | } | |
| 89 | if (htmlp) | ||
| 90 | doc = htmlReadMemory (SDATA (string), SBYTES (string), burl, "utf-8", | ||
| 91 | HTML_PARSE_RECOVER|HTML_PARSE_NONET| | ||
| 92 | HTML_PARSE_NOWARNING|HTML_PARSE_NOERROR); | ||
| 93 | else | ||
| 94 | doc = xmlReadMemory (SDATA (string), SBYTES (string), burl, "utf-8", | ||
| 95 | XML_PARSE_NONET|XML_PARSE_NOWARNING| | ||
| 96 | XML_PARSE_NOERROR); | ||
| 97 | |||
| 98 | if (doc != NULL) { | ||
| 99 | node = xmlDocGetRootElement (doc); | ||
| 100 | if (node != NULL) | ||
| 101 | result = make_dom (node); | ||
| 102 | 94 | ||
| 103 | xmlFreeDoc (doc); | 95 | doc = htmlp |
| 104 | xmlCleanupParser (); | 96 | ? htmlReadMemory (SDATA (string), SBYTES (string), burl, "utf-8", |
| 105 | } | 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 | } | ||
| 106 | 111 | ||
| 107 | return result; | 112 | return result; |
| 108 | } | 113 | } |
| 109 | 114 | ||
| 110 | DEFUN ("html-parse-string", Fhtml_parse_string, Shtml_parse_string, | 115 | DEFUN ("xml-parse-html-string-internal", Fxml_parse_html_string_internal, |
| 111 | 0, 2, 0, | 116 | Sxml_parse_html_string_internal, |
| 112 | doc: /* Parse the string as an HTML document and return the parse tree. | 117 | 1, 2, 0, |
| 113 | If BASE-URL is non-nil, it will be used to expand relative URLs in | 118 | doc: /* Parse STRING as an HTML document and return the parse tree. |
| 114 | the HTML document.*/) | 119 | If BASE-URL is non-nil, it is used to expand relative URLs. */) |
| 115 | (Lisp_Object string, Lisp_Object base_url) | 120 | (Lisp_Object string, Lisp_Object base_url) |
| 116 | { | 121 | { |
| 117 | return parse_buffer (string, base_url, 1); | 122 | return parse_string (string, base_url, 1); |
| 118 | } | 123 | } |
| 119 | 124 | ||
| 120 | DEFUN ("xml-parse-string", Fxml_parse_string, Sxml_parse_string, | 125 | DEFUN ("xml-parse-string-internal", Fxml_parse_string_internal, |
| 121 | 0, 2, 0, | 126 | Sxml_parse_string_internal, |
| 122 | doc: /* Parse the string as an XML document and return the parse tree. | 127 | 1, 2, 0, |
| 123 | If BASE-URL is non-nil, it will be used to expand relative URLs in | 128 | doc: /* Parse STRING as an XML document and return the parse tree. |
| 124 | the XML document.*/) | 129 | If BASE-URL is non-nil, it is used to expand relative URLs. */) |
| 125 | (Lisp_Object string, Lisp_Object base_url) | 130 | (Lisp_Object string, Lisp_Object base_url) |
| 126 | { | 131 | { |
| 127 | return parse_buffer (string, base_url, 0); | 132 | return parse_string (string, base_url, 0); |
| 128 | } | 133 | } |
| 129 | 134 | ||
| 130 | 135 | ||
| @@ -134,8 +139,8 @@ the XML document.*/) | |||
| 134 | void | 139 | void |
| 135 | syms_of_xml (void) | 140 | syms_of_xml (void) |
| 136 | { | 141 | { |
| 137 | defsubr (&Shtml_parse_string); | 142 | defsubr (&Sxml_parse_html_string_internal); |
| 138 | defsubr (&Sxml_parse_string); | 143 | defsubr (&Sxml_parse_string_internal); |
| 139 | } | 144 | } |
| 140 | 145 | ||
| 141 | #endif /* HAVE_LIBXML2 */ | 146 | #endif /* HAVE_LIBXML2 */ |