Beispiel #1
0
static Lisp_Object
parse_region (Lisp_Object start, Lisp_Object end, Lisp_Object base_url,
              Lisp_Object discard_comments, bool htmlp)
{
    xmlDoc *doc;
    Lisp_Object result = Qnil;
    const char *burl = "";
    ptrdiff_t istart, iend, istart_byte, iend_byte;
    unsigned char *buftext;

    xmlCheckVersion (LIBXML_VERSION);

    validate_region (&start, &end);

    istart = XINT (start);
    iend = XINT (end);
    istart_byte = CHAR_TO_BYTE (istart);
    iend_byte = CHAR_TO_BYTE (iend);

    if (istart < GPT && GPT < iend)
        move_gap_both (iend, iend_byte);

    if (! NILP (base_url))
    {
        CHECK_STRING (base_url);
        burl = SSDATA (base_url);
    }

    buftext = BYTE_POS_ADDR (istart_byte);
#ifdef REL_ALLOC
    /* Prevent ralloc.c from relocating the current buffer while libxml2
       functions below read its text.  */
    r_alloc_inhibit_buffer_relocation (1);
#endif
    if (htmlp)
        doc = htmlReadMemory ((char *)buftext,
                              iend_byte - istart_byte, burl, "utf-8",
                              HTML_PARSE_RECOVER|HTML_PARSE_NONET|
                              HTML_PARSE_NOWARNING|HTML_PARSE_NOERROR|
                              HTML_PARSE_NOBLANKS);
    else
        doc = xmlReadMemory ((char *)buftext,
                             iend_byte - istart_byte, burl, "utf-8",
                             XML_PARSE_NONET|XML_PARSE_NOWARNING|
                             XML_PARSE_NOBLANKS |XML_PARSE_NOERROR);

#ifdef REL_ALLOC
    r_alloc_inhibit_buffer_relocation (0);
#endif
    /* If the assertion below fails, malloc was called inside the above
       libxml2 functions, and ralloc.c caused relocation of buffer text,
       so we could have read from unrelated memory.  */
    eassert (buftext == BYTE_POS_ADDR (istart_byte));

    if (doc != NULL)
    {
        Lisp_Object r = Qnil;
        if (NILP(discard_comments))
        {
            /* If the document has toplevel comments, then this should
               get us the nodes and the comments. */
            xmlNode *n = doc->children;

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

        if (NILP (result)) {
            /* The document doesn't have toplevel comments or we discarded
               them.  Get the tree the proper way. */
            xmlNode *node = xmlDocGetRootElement (doc);
            if (node != NULL)
                result = make_dom (node);
        } else
            result = Fcons (Qtop, Fcons (Qnil, Fnreverse (Fcons (r, result))));

        xmlFreeDoc (doc);
    }

    return result;
}
Beispiel #2
0
static Lisp_Object
parse_region (Lisp_Object start, Lisp_Object end, Lisp_Object base_url, int htmlp)
{
  xmlDoc *doc;
  Lisp_Object result = Qnil;
  const char *burl = "";
  ptrdiff_t istart, iend, istart_byte, iend_byte;

  fn_xmlCheckVersion (LIBXML_VERSION);

  validate_region (&start, &end);

  istart = XINT (start);
  iend = XINT (end);
  istart_byte = CHAR_TO_BYTE (istart);
  iend_byte = CHAR_TO_BYTE (iend);

  if (istart < GPT && GPT < iend)
    move_gap_both (iend, iend_byte);

  if (! NILP (base_url))
    {
      CHECK_STRING (base_url);
      burl = SSDATA (base_url);
    }

  if (htmlp)
    doc = fn_htmlReadMemory ((char *) BYTE_POS_ADDR (istart_byte),
			     iend_byte - istart_byte, burl, "utf-8",
			     HTML_PARSE_RECOVER|HTML_PARSE_NONET|
			     HTML_PARSE_NOWARNING|HTML_PARSE_NOERROR|
			     HTML_PARSE_NOBLANKS);
  else
    doc = fn_xmlReadMemory ((char *) BYTE_POS_ADDR (istart_byte),
			    iend_byte - istart_byte, burl, "utf-8",
			    XML_PARSE_NONET|XML_PARSE_NOWARNING|
			    XML_PARSE_NOBLANKS |XML_PARSE_NOERROR);

  if (doc != NULL)
    {
      /* If the document is just comments, then this should get us the
	 nodes anyway. */
      xmlNode *n = doc->children->next;
      Lisp_Object r = Qnil;

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

      if (NILP (result)) {
	/* The document isn't just comments, so get the tree the
	   proper way. */
	xmlNode *node = fn_xmlDocGetRootElement (doc);
	if (node != NULL)
	  result = make_dom (node);
      } else
	result = Fcons (intern ("top"),
			Fcons (Qnil, Fnreverse (Fcons (r, result))));

      fn_xmlFreeDoc (doc);
    }

  return result;
}
Beispiel #3
0
static Lisp_Object
parse_region (Lisp_Object start, Lisp_Object end, Lisp_Object base_url, int htmlp)
{
  xmlDoc *doc;
  Lisp_Object result = Qnil;
  const char *burl = "";
  EMACS_INT bytes;
  EMACS_INT istart, iend;

  LIBXML_TEST_VERSION;

  validate_region (&start, &end);

  istart = XINT (start);
  iend = XINT (end);

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

  if (! NILP (base_url))
    {
      CHECK_STRING (base_url);
      burl = SSDATA (base_url);
    }

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

  if (htmlp)
    doc = htmlReadMemory ((char *) BYTE_POS_ADDR (CHAR_TO_BYTE (istart)),
			  bytes, burl, "utf-8",
			  HTML_PARSE_RECOVER|HTML_PARSE_NONET|
			  HTML_PARSE_NOWARNING|HTML_PARSE_NOERROR|
			  HTML_PARSE_NOBLANKS);
  else
    doc = xmlReadMemory ((char *) BYTE_POS_ADDR (CHAR_TO_BYTE (istart)),
			 bytes, burl, "utf-8",
			 XML_PARSE_NONET|XML_PARSE_NOWARNING|
			 XML_PARSE_NOBLANKS |XML_PARSE_NOERROR);

  if (doc != NULL)
    {
      xmlNode *n = doc->children->next;
      Lisp_Object r = Qnil;

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

      if (NILP (result))
	result = r;
      else
	result = Fcons (intern ("top"),
			Fcons (Qnil, Fnreverse (Fcons (r, result))));

      xmlFreeDoc (doc);
      xmlCleanupParser ();
    }

  return result;
}