Пример #1
0
/**
 * HTML整形
 */
const wxString ExtractBoardList::HtmlFormat(const wxString& html)
{
/**
### XML入出力のためのバッファを作成する ###

xmlOutputBufferPtr xmlOutputBufferCreateIO (xmlOutputWriteCallback iowrite, 
					    xmlOutputCloseCallback ioclose, 
					    void * ioctx, 
					    xmlCharEncodingHandlerPtr encoder)
*/

     htmlDocPtr docPtr = htmlReadMemory(html.mb_str(), html.Len(), "", "utf-8", HTML_PARSE_RECOVER);
     if (docPtr)
     {
	  // libxml2の***Ptrは何かの構造体のポインタ
	  wxString val;
	  xmlOutputBufferPtr buf = xmlOutputBufferCreateIO((xmlOutputWriteCallback)writeToWxString,
							   (xmlOutputCloseCallback)closeWxString,
							   &val, 0);
	  
	  htmlDocContentDumpOutput(buf,
				   docPtr, 
				   "utf-8");

	  return val;
     }
}
/**
 * HTML整形
 */
const wxString ExtractBoardList::HtmlFormat(const wxString& html)
{
     wxString val;
     const wxCharBuffer& cb = html.utf8_str();

     htmlDocPtr docPtr = htmlReadMemory(cb.data(), ::strlen(cb.data()), "", "utf-8", HTML_PARSE_RECOVER);
     if (docPtr)
     {
	  // libxml2の***Ptrは何かの構造体のポインタ
	  xmlOutputBufferPtr buf = xmlOutputBufferCreateIO((xmlOutputWriteCallback)writeToWxString,
							   (xmlOutputCloseCallback)closeWxString,
							   &val, 0);
	  
	  htmlDocContentDumpOutput(buf,
				   docPtr, 
				   "utf-8");

	  xmlOutputBufferClose(buf);
	  xmlFreeDoc(docPtr);
     }
     xmlCleanupParser();

     return val;
}
Пример #3
0
/**
 * htmlDocDumpMemory:
 * @cur:  the document
 * @mem:  OUT: the memory pointer
 * @size:  OUT: the memory length
 *
 * Dump an HTML document in memory and return the xmlChar * and it's size.
 * It's up to the caller to free the memory.
 */
void
htmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
    xmlOutputBufferPtr buf;
    xmlCharEncodingHandlerPtr handler = NULL;
    const char *encoding;

    xmlInitParser();

    if ((mem == NULL) || (size == NULL))
        return;
    if (cur == NULL) {
	*mem = NULL;
	*size = 0;
	return;
    }

    encoding = (const char *) htmlGetMetaEncoding(cur);

    if (encoding != NULL) {
	xmlCharEncoding enc;

	enc = xmlParseCharEncoding(encoding);
	if (enc != cur->charset) {
	    if (cur->charset != XML_CHAR_ENCODING_UTF8) {
		/*
		 * Not supported yet
		 */
		*mem = NULL;
		*size = 0;
		return;
	    }

	    handler = xmlFindCharEncodingHandler(encoding);
	    if (handler == NULL) {
		*mem = NULL;
		*size = 0;
		return;
	    }
	} else {
	    handler = xmlFindCharEncodingHandler(encoding);
	}
    }

    /*
     * Fallback to HTML or ASCII when the encoding is unspecified
     */
    if (handler == NULL)
	handler = xmlFindCharEncodingHandler("HTML");
    if (handler == NULL)
	handler = xmlFindCharEncodingHandler("ascii");

    buf = xmlAllocOutputBuffer(handler);
    if (buf == NULL) {
	*mem = NULL;
	*size = 0;
	return;
    }

    htmlDocContentDumpOutput(buf, cur, NULL);
    xmlOutputBufferFlush(buf);
    if (buf->conv != NULL) {
	*size = buf->conv->use;
	*mem = xmlStrndup(buf->conv->content, *size);
    } else {
	*size = buf->buffer->use;
	*mem = xmlStrndup(buf->buffer->content, *size);
    }
    (void)xmlOutputBufferClose(buf);
}