xml.c 4.55 KB
Newer Older
1
/* Interface to libxml2.
2
   Copyright (C) 2010-2011 Free Software Foundation, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */

#include <config.h>

#ifdef HAVE_LIBXML2

#include <setjmp.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/HTMLparser.h>

#include "lisp.h"
#include "buffer.h"

Paul Eggert's avatar
Paul Eggert committed
31 32
static Lisp_Object
make_dom (xmlNode *node)
33
{
34 35
  if (node->type == XML_ELEMENT_NODE)
    {
36
      Lisp_Object result = Fcons (intern ((char *) node->name), Qnil);
37 38 39 40 41 42 43 44 45 46 47
      xmlNode *child;
      xmlAttr *property;
      Lisp_Object plist = Qnil;

      /* First add the attributes. */
      property = node->properties;
      while (property != NULL)
	{
	  if (property->children &&
	      property->children->content)
	    {
48 49 50
	      char *content = (char *) property->children->content;
	      plist = Fcons (Fcons (intern ((char *) property->name),
				    build_string (content)),
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
			     plist);
	    }
	  property = property->next;
	}
      result = Fcons (Fnreverse (plist), result);

      /* Then add the children of the node. */
      child = node->children;
      while (child != NULL)
	{
	  result = Fcons (make_dom (child), result);
	  child = child->next;
	}

      return Fnreverse (result);
66
    }
67
  else if (node->type == XML_TEXT_NODE || node->type == XML_CDATA_SECTION_NODE)
68 69
    {
      if (node->content)
70
	return build_string ((char *) node->content);
71 72
      else
	return Qnil;
73
    }
74 75 76 77 78 79 80 81
  else if (node->type == XML_COMMENT_NODE)
    {
      if (node->content)
	return list3 (intern ("comment"), Qnil,
		      build_string ((char *) node->content));
      else
	return Qnil;
    }
82
  else
83 84 85 86
    return Qnil;
}

static Lisp_Object
87
parse_region (Lisp_Object start, Lisp_Object end, Lisp_Object base_url, int htmlp)
88 89 90
{
  xmlDoc *doc;
  xmlNode *node;
91
  Lisp_Object result = Qnil;
92
  const char *burl = "";
93 94
  EMACS_INT bytes;
  EMACS_INT istart, iend;
95

96
  LIBXML_TEST_VERSION;
97

98
  validate_region (&start, &end);
99

100 101 102 103 104
  istart = XINT (start);
  iend = XINT (end);

  if (istart < GPT && GPT < iend)
    move_gap (iend);
105

106 107 108
  if (! NILP (base_url))
    {
      CHECK_STRING (base_url);
109
      burl = SSDATA (base_url);
110
    }
111

112
  bytes = CHAR_TO_BYTE (iend) - CHAR_TO_BYTE (istart);
113

114
  if (htmlp)
115
    doc = htmlReadMemory ((char *) BYTE_POS_ADDR (CHAR_TO_BYTE (istart)),
116 117
			  bytes, burl, "utf-8",
			  HTML_PARSE_RECOVER|HTML_PARSE_NONET|
118 119
			  HTML_PARSE_NOWARNING|HTML_PARSE_NOERROR|
			  HTML_PARSE_NOBLANKS);
120
  else
121
    doc = xmlReadMemory ((char *) BYTE_POS_ADDR (CHAR_TO_BYTE (istart)),
122 123
			 bytes, burl, "utf-8",
			 XML_PARSE_NONET|XML_PARSE_NOWARNING|
124
			 XML_PARSE_NOBLANKS |XML_PARSE_NOERROR);
125 126 127

  if (doc != NULL)
    {
128 129 130 131 132 133 134 135 136 137 138 139 140
      xmlNode *n = doc->children->next;
      Lisp_Object r = Qnil;

      while (n) {
	if (r != Qnil)
	  result = Fcons (r, result);
	r = make_dom (n);
	n = n->next;
      }

      if (result == Qnil)
	result = r;
      else
141 142
	result = Fcons (intern ("top"),
			Fcons (Qnil, Fnreverse (Fcons (r, result))));
143

144 145 146
      xmlFreeDoc (doc);
      xmlCleanupParser ();
    }
147

148 149 150
  return result;
}

151 152 153 154
DEFUN ("libxml-parse-html-region", Flibxml_parse_html_region,
       Slibxml_parse_html_region,
       2, 3, 0,
       doc: /* Parse the region as an HTML document and return the parse tree.
155
If BASE-URL is non-nil, it is used to expand relative URLs.  */)
156
  (Lisp_Object start, Lisp_Object end, Lisp_Object base_url)
157
{
158
  return parse_region (start, end, base_url, 1);
159 160
}

161 162 163 164
DEFUN ("libxml-parse-xml-region", Flibxml_parse_xml_region,
       Slibxml_parse_xml_region,
       2, 3, 0,
       doc: /* Parse the region as an XML document and return the parse tree.
165
If BASE-URL is non-nil, it is used to expand relative URLs.  */)
166
  (Lisp_Object start, Lisp_Object end, Lisp_Object base_url)
167
{
168
  return parse_region (start, end, base_url, 0);
169 170 171 172 173 174 175 176 177
}


/***********************************************************************
			    Initialization
 ***********************************************************************/
void
syms_of_xml (void)
{
178 179
  defsubr (&Slibxml_parse_html_region);
  defsubr (&Slibxml_parse_xml_region);
180 181 182
}

#endif /* HAVE_LIBXML2 */