int main(int argc, char *argv[]) { CURL *curl; int counter = 0; if (argc < 2) return 1; curl = curl_easy_init(); TidyDoc tdoc = tidyCreate(); TidyBuffer output = {0}; tidyOptSetBool(tdoc, TidyXmlOut, yes); tidyOptSetBool(tdoc, TidyShowWarnings, no); tidyOptSetInt(tdoc, TidyWrapLen, 0); for(int i=0; i < 20; i++) { // tidyBufFree(&output); tidyBufClear(&output); // tidyParseFile(tdoc, argv[1]); tidyParseString(tdoc, getpage(curl,i).c_str()); tidySaveBuffer(tdoc, &output); // tidySaveFile(tdoc, "tidy_test.xml"); // doc.LoadFile(argv[1]); // doc.LoadFile("tidy_test.xml"); parseTidyBuf(output, counter); } curl_easy_cleanup(curl); return 0; }
static void tidy_doc_update_properties(PHPTidyObj *obj) { TidyBuffer output; zval temp; tidyBufInit(&output); tidySaveBuffer (obj->ptdoc->doc, &output); if (output.size) { if (!obj->std.properties) { rebuild_object_properties(&obj->std); } ZVAL_STRINGL(&temp, (char*)output.bp, output.size-1); zend_hash_str_update(obj->std.properties, "value", sizeof("value") - 1, &temp); } tidyBufFree(&output); if (obj->ptdoc->errbuf->size) { if (!obj->std.properties) { rebuild_object_properties(&obj->std); } ZVAL_STRINGL(&temp, (char*)obj->ptdoc->errbuf->bp, obj->ptdoc->errbuf->size-1); zend_hash_str_update(obj->std.properties, "errorBuffer", sizeof("errorBuffer") - 1, &temp); } }
static int tidy_doc_cast_handler(zval *in, zval *out, int type) { TidyBuffer output; PHPTidyObj *obj; switch (type) { case IS_LONG: ZVAL_LONG(out, 0); break; case IS_DOUBLE: ZVAL_DOUBLE(out, 0); break; case _IS_BOOL: ZVAL_TRUE(out); break; case IS_STRING: obj = Z_TIDY_P(in); tidyBufInit(&output); tidySaveBuffer (obj->ptdoc->doc, &output); ZVAL_STRINGL(out, (char *) output.bp, output.size ? output.size-1 : 0); tidyBufFree(&output); break; default: return FAILURE; } return SUCCESS; }
QString tidyHtml(QString str, bool& ok) { #ifdef NO_TIDY ok = true; return str; #else QString res = str; ok = false; static bool isTidyWithIntBodyOnly = isTidyWithIntBodyOnlyCheck(); TidyDoc tdoc = tidyCreate(); TidyBuffer output; TidyBuffer errbuf; tidyBufInit(&output); tidyBufInit(&errbuf); bool configOk = tidyOptSetBool(tdoc, TidyXhtmlOut, yes) && tidyOptSetBool(tdoc, TidyForceOutput, yes) && tidyOptSetBool(tdoc, TidyMark, no) && (isTidyWithIntBodyOnly ? tidyOptSetInt(tdoc, TidyBodyOnly, 1) : tidyOptSetBool(tdoc, TidyBodyOnly, yes)) && tidyOptSetInt(tdoc, TidyWrapLen, 0) && tidyOptSetInt(tdoc, TidyDoctypeMode, TidyDoctypeOmit); if (configOk && (tidySetCharEncoding(tdoc, "utf8") >= 0) && (tidySetErrorBuffer(tdoc, &errbuf) >= 0) && (tidyParseString(tdoc, str.toUtf8().data()) >= 0) && (tidyCleanAndRepair(tdoc) >= 0) && (tidyRunDiagnostics(tdoc) >= 0) && (tidySaveBuffer(tdoc, &output) >= 0) && (output.bp != 0 && output.size > 0)) { res = QString::fromUtf8((char*)output.bp, output.size); ok = true; } #ifdef DEBUG_MARKUP if (errbuf.size > 0) { QString errStr = QString::fromUtf8((char*)errbuf.bp, errbuf.size); qDebug() << "\n[DEBUG] MARKUP, libtidy errors and warnings:\n" << errStr; } #endif if (output.bp != 0) tidyBufFree(&output); if (errbuf.bp != 0) tidyBufFree(&errbuf); tidyRelease(tdoc); return res.trimmed(); #endif }
// formats the 'dom' tree into HTML output. int lua_tidy_saveString(lua_State *L) { pTidy t = toTidy(L,1); TidyBuffer output; tidyBufInit(&output); lua_pushnumber(L, tidySaveBuffer(t->tdoc, &output)); lua_pushlstring(L, (char*)output.bp, output.size); tidyBufFree( &output ); return 1; }
/* {{{ proto string tidy_get_output() Return a string representing the parsed tidy markup */ static PHP_FUNCTION(tidy_get_output) { TidyBuffer output; TIDY_FETCH_OBJECT; tidyBufInit(&output); tidySaveBuffer(obj->ptdoc->doc, &output); FIX_BUFFER(&output); RETVAL_STRINGL((char *) output.bp, output.size ? output.size-1 : 0); tidyBufFree(&output); }
static html_valid_status_t html_valid_run (html_valid_t * htv, SV * html, SV ** output_ptr, SV ** errors_ptr) { const char * html_string; STRLEN html_length; SV * output; SV * errors; TidyBuffer tidy_output = {0}; TidyBuffer tidy_errbuf = {0}; /* First set these up sanely in case the stuff hits the fan. */ * output_ptr = & PL_sv_undef; * errors_ptr = & PL_sv_undef; /* Work around bug where allocator sometimes does not get set. */ CopyAllocator (htv->tdoc, & tidy_output); CopyAllocator (htv->tdoc, & tidy_errbuf); html_string = SvPV (html, html_length); CALL_TIDY (tidySetErrorBuffer (htv->tdoc, & tidy_errbuf)); htv->n_mallocs++; CALL_TIDY (tidyParseString (htv->tdoc, html_string)); CALL_TIDY (tidyCleanAndRepair (htv->tdoc)); CALL_TIDY (tidyRunDiagnostics (htv->tdoc)); CALL_TIDY (tidySaveBuffer (htv->tdoc, & tidy_output)); htv->n_mallocs++; /* Copy the contents of the buffers into the Perl scalars. */ output = newSVpv ((char *) tidy_output.bp, tidy_output.size); errors = newSVpv ((char *) tidy_errbuf.bp, tidy_errbuf.size); /* HTML Tidy randomly segfaults here due to "allocator" not being set in some cases, hence the above CopyAllocator fix. */ tidyBufFree (& tidy_output); htv->n_mallocs--; tidyBufFree (& tidy_errbuf); htv->n_mallocs--; /* These are not our mallocs, they are Perl's mallocs, so we don't increase htv->n_mallocs for these. After we return them, we no longer take care of these. */ * output_ptr = output; * errors_ptr = errors; return html_valid_ok; }
std::string cleanHTML (std::string html) { TidyDoc tidyDoc = tidyCreate(); TidyBuffer tidyOutputBuffer = {0}; // Configure Tidy // The flags tell Tidy to output XML and disable showing warnings bool configSuccess = tidyOptSetBool(tidyDoc, TidyXmlOut, yes) && tidyOptSetBool(tidyDoc, TidyQuiet, yes) && tidyOptSetBool(tidyDoc, TidyNumEntities, yes) && tidyOptSetBool(tidyDoc, TidyShowWarnings, no); tidyOptSetValue(tidyDoc,TidyForceOutput,"true"); int tidyResponseCode = -1; // Parse input if (configSuccess) tidyResponseCode = tidyParseString(tidyDoc, html.c_str()); // Process HTML if (tidyResponseCode >= 0) tidyResponseCode = tidyCleanAndRepair(tidyDoc); // Output the HTML to our buffer if (tidyResponseCode >= 0) tidyResponseCode = tidySaveBuffer(tidyDoc, &tidyOutputBuffer); // Any errors from Tidy? if (tidyResponseCode < 0) throw ("Tidy encountered an error while parsing an HTML response. Tidy response code: " + tidyResponseCode); // Grab the result from the buffer and then free Tidy's memory std::string tidyResult = (char*)tidyOutputBuffer.bp; tidyBufFree(&tidyOutputBuffer); tidyRelease(tidyDoc); return tidyResult; }
void TidyNetworkReply::tidyUp() { QUrl redirect = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); if (redirect.isValid()) { redirect.setScheme("tidy"); setAttribute(QNetworkRequest::RedirectionTargetAttribute, QVariant(redirect)); emit finished(); reply->deleteLater(); return; } int rc = -1; Bool ok; ok = tidyOptSetBool( tdoc, TidyXmlOut, yes ); // Convert to XHTML if (ok) ok = tidyOptSetBool(tdoc, TidyQuoteNbsp, no); //if (ok) //ok = tidyOptSetValue(tdoc, TidyBlockTags, "header,nav,article,time,section,footer"); if ( ok ) rc = tidySetErrorBuffer( tdoc, &errbuf ); // Capture diagnostics if ( rc >= 0 ) rc = tidyParseString( tdoc, reply->readAll() ); // Parse the input if ( rc >= 0 ) rc = tidyCleanAndRepair( tdoc ); // Tidy it up! if ( rc >= 0 ) rc = tidyRunDiagnostics( tdoc ); // Kvetch if ( rc > 1 ) // If error, force output. rc = ( tidyOptSetBool(tdoc, TidyForceOutput, yes) ? rc : -1 ); if ( rc >= 0 ) rc = tidySaveBuffer( tdoc, &output ); // Pretty Print if ( rc >= 0 ) { if ( rc > 0 ) { ;//printf( "\nDiagnostics:\n\n%s", errbuf.bp ); } } else { ;//printf( "A severe error (%d) occurred.\n", rc ); } open(ReadOnly); emit readyRead(); emit finished(); reply->deleteLater(); //QTimer::singleShot(0, this, SIGNAL(readyRead())); //QTimer::singleShot(0, this, SIGNAL(finished())); }
void HTMLTidy::run() throw( std::runtime_error ) { TidyBuffer outputBuffer = { 0 }; TidyBuffer errorBuffer = { 0 }; // try to create valid XHTML document for XML parser: int tidyResult = -1; if( tidyOptSetBool( handle, TidyXhtmlOut, yes ) ) { tidyResult = tidySetErrorBuffer( handle, &errorBuffer ); } if( tidyResult >= 0 ) { tidyResult = tidyParseString( handle, document.c_str() ); } if( tidyResult >= 0 ) { tidyResult = tidyCleanAndRepair( handle ); } if( tidyResult >= 0 ) { tidyResult = tidyRunDiagnostics( handle ); } if( tidyResult > 1 ) { if( !tidyOptSetBool( handle, TidyForceOutput, yes ) ) { tidyResult = -1; } } if( tidyResult >= 0 ) { tidyResult = tidySaveBuffer( handle, &outputBuffer ); } if( tidyResult > 0 ) { std::clog << "*********************************" << std::endl; std::clog << "HTMLTidy: Diagnostics of libtidy:" << std::endl; std::clog << errorBuffer.bp; std::clog << "*********************************" << std::endl; } else if( tidyResult < 0 ) { std::stringstream sstrTidyResult; sstrTidyResult << tidyResult; throw std::runtime_error( "HTMLTidy: A severe error occured while tidying up the received document (" + sstrTidyResult.str() + ")." ); } resultDocument.reserve( outputBuffer.size ); // avoid frequent (re-)allocations for( unsigned int i = 0; i < outputBuffer.size; i++ ) { resultDocument.insert( resultDocument.end(), static_cast< char >( *(outputBuffer.bp + i) ) ); } tidyBufFree( &outputBuffer ); tidyBufFree( &errorBuffer ); }
bool CCFHtmlTidy::TidyMain(const char* pSourceIn, const char* pOptions, std::string &strOut, std::string &strErr) { TidyBuffer output; TidyBuffer errbuf; int rc = -1; Bool ok = yes; TidyDoc tdoc = tidyCreate(); // Initialize "document" tidyBufInit(&output); tidyBufInit(&errbuf); TidyOptionsSet(tidyDocToImpl(tdoc), pOptions); if (ok) rc = tidySetErrorBuffer(tdoc, &errbuf); // Capture diagnostics if (rc >= 0) rc = tidyParseString(tdoc, pSourceIn); // Parse the input if (rc >= 0) rc = tidyCleanAndRepair(tdoc); // Tidy it up! if (rc >= 0) rc = tidyRunDiagnostics(tdoc); // Kvetch //if ( rc > 1 ) // If error, force output. // rc = ( tidyOptSetBool(tdoc, TidyForceOutput, yes) ? rc : -1 ); if (rc >= 0) rc = tidySaveBuffer(tdoc, &output); // Pretty Print if (rc >= 0) { if (output.bp) { strOut = reinterpret_cast<char const*>(output.bp); } } strErr = reinterpret_cast<char const*>(errbuf.bp); std::string strEmpty = "No warnings or errors were found.\n\n"; if (0 == strEmpty.compare(strErr)) { strErr.clear(); } tidyBufFree(&output); tidyBufFree(&errbuf); tidyRelease(tdoc); return true; }
QString tidy(QString input) // take html code and return it converted to xhtml code { // the following code is (c) Charles Reitzel and Dave Raggett, see the package tidy TidyBuffer output = {0}; TidyBuffer errbuf = {0}; QString result; int rc = -1; Bool ok; TidyDoc tdoc = tidyCreate(); // Initialize "document" ok = tidyOptSetBool( tdoc, TidyXhtmlOut, yes ); // Convert to XHTML if ( ok ) rc = tidySetErrorBuffer( tdoc, &errbuf ); // Capture diagnostics tidySetCharEncoding( tdoc, "utf8" ); if ( rc >= 0 ) rc = tidyParseString( tdoc, input.toUtf8().constData() ); // Parse the input if ( rc >= 0 ) rc = tidyCleanAndRepair( tdoc ); // Tidy it up! if ( rc >= 0 ) rc = tidyRunDiagnostics( tdoc ); // Kvetch if ( rc > 1 ) // If error, force output. rc = ( tidyOptSetBool(tdoc, TidyForceOutput, yes) ? rc : -1 ); if ( rc >= 0 ) rc = tidySaveBuffer( tdoc, &output ); // Pretty Print if ( rc >= 0 ) { char* outputstring; // content of the outputfile // find out length of outputstring int length=0; // length of outputstring byte* string=output.bp; while (*string) { string++; length++; } outputstring=(char*)malloc(length); snprintf(outputstring,length,"%s",output.bp); result=QString::fromUtf8(outputstring,length); } else printf( "A severe error (\%d) occurred.\\n", rc ); tidyBufFree( &output ); tidyBufFree( &errbuf ); tidyRelease( tdoc ); result=result.replace("ö","ö"); return result; }
void tidy(std::string &input) { TidyBuffer output = {0}; TidyBuffer errbuf = {0}; TidyDoc tdoc = tidyCreate(); tidyOptSetBool(tdoc, TidyXhtmlOut, yes); tidySetErrorBuffer(tdoc, &errbuf); // Capture diagnostics tidyParseString(tdoc, input.c_str()); tidyCleanAndRepair(tdoc); tidySaveBuffer(tdoc, &output); input = std::string((const char*)output.bp); tidyBufFree(&output); tidyBufFree(&errbuf); tidyRelease(tdoc); }
bool HTidyInterface::formatSource( const char* textIn, CString &strTidy, CString &strMsg ) { TidyBuffer output; TidyBuffer errbuf; int rc = -1; Bool ok = yes; TidyDoc tdoc = tidyCreate(); // Initialize "document" tidyBufInit(&output); tidyBufInit(&errbuf); InitTidyDefault(tdoc); SetTidyConfig(tdoc); if ( ok ) rc = tidySetErrorBuffer(tdoc, &errbuf); // Capture diagnostics if ( rc >= 0 ) rc = tidyParseString(tdoc, textIn); // Parse the input if ( rc >= 0 ) rc = tidyCleanAndRepair(tdoc); // Tidy it up! if ( rc >= 0 ) rc = tidyRunDiagnostics(tdoc); // Kvetch //if ( rc > 1 ) // If error, force output. // rc = ( tidyOptSetBool(tdoc, TidyForceOutput, yes) ? rc : -1 ); if ( rc >= 0 ) rc = tidySaveBuffer(tdoc, &output); // Pretty Print if ( rc >= 0 ) { strTidy = reinterpret_cast< char const* >(output.bp); } strMsg = reinterpret_cast< char const* >(errbuf.bp); CString strEmpty = _T("No warnings or errors were found.\r\n\r\n"); if (0 == strEmpty.Compare(strMsg)) { strMsg.Empty(); } tidyBufFree(&output); tidyBufFree(&errbuf); tidyRelease(tdoc); return true; }
int main(int argc, char **argv ) { const char* input = "<title>Hello</title><p>World!"; TidyBuffer output = {0}; TidyBuffer errbuf = {0}; int rc = -1; Bool ok; // Initialize "document" TidyDoc tdoc = tidyCreate(); printf( "Tidying:\t%s\n", input ); // Convert to XHTML ok = tidyOptSetBool( tdoc, TidyXhtmlOut, yes ); if ( ok ) rc = tidySetErrorBuffer( tdoc, &errbuf ); // Capture diagnostics if ( rc >= 0 ) rc = tidyParseString( tdoc, input ); // Parse the input if ( rc >= 0 ) rc = tidyCleanAndRepair( tdoc ); // Tidy it up! if ( rc >= 0 ) rc = tidyRunDiagnostics( tdoc ); // Kvetch if ( rc > 1 ) // If error, force output. rc = ( tidyOptSetBool(tdoc, TidyForceOutput, yes) ? rc : -1 ); if ( rc >= 0 ) rc = tidySaveBuffer( tdoc, &output ); // Pretty Print if ( rc >= 0 ) { if ( rc > 0 ) printf( "\nDiagnostics:\n\n%s", errbuf.bp ); printf( "\nAnd here is the result:\n\n%s", output.bp ); } else printf( "A severe error (%d) occurred.\n", rc ); tidyBufFree( &output ); tidyBufFree( &errbuf ); tidyRelease( tdoc ); return rc; }
static int php_tidy_output_handler(void **nothing, php_output_context *output_context) { int status = FAILURE; TidyDoc doc; TidyBuffer inbuf, outbuf, errbuf; if (TG(clean_output) && (output_context->op & PHP_OUTPUT_HANDLER_START) && (output_context->op & PHP_OUTPUT_HANDLER_FINAL)) { doc = tidyCreate(); tidyBufInit(&errbuf); if (0 == tidySetErrorBuffer(doc, &errbuf)) { tidyOptSetBool(doc, TidyForceOutput, yes); tidyOptSetBool(doc, TidyMark, no); if (ZEND_SIZE_T_UINT_OVFL(output_context->in.used)) { php_error_docref(NULL, E_WARNING, "Input string is too long"); return status; } TIDY_SET_DEFAULT_CONFIG(doc); tidyBufInit(&inbuf); tidyBufAttach(&inbuf, (byte *) output_context->in.data, (uint)output_context->in.used); if (0 <= tidyParseBuffer(doc, &inbuf) && 0 <= tidyCleanAndRepair(doc)) { tidyBufInit(&outbuf); tidySaveBuffer(doc, &outbuf); FIX_BUFFER(&outbuf); output_context->out.data = (char *) outbuf.bp; output_context->out.used = outbuf.size ? outbuf.size-1 : 0; output_context->out.free = 1; status = SUCCESS; } } tidyRelease(doc); tidyBufFree(&errbuf); } return status; }
// quick and dirty shortcut function. int lua_tidy_easyClean ( lua_State *L ) { TidyBuffer output; TidyBuffer errbuf; int rc; pTidy t; const char * input; tidyBufInit(&output); tidyBufInit(&errbuf); rc = -1; t = toTidy(L,1); input = lua_tostring(L,2); rc = tidySetErrorBuffer( t->tdoc, &errbuf ); if ( rc >= 0 ) rc = tidyParseString( t->tdoc, input ); if ( rc >= 0 ) rc = tidyCleanAndRepair( t->tdoc ); if ( rc >= 0 ) rc = tidyRunDiagnostics( t->tdoc ); if ( rc >= 0 ) rc = tidySaveBuffer( t->tdoc, &output ); lua_pushlstring(L, (char*)output.bp,output.size); if ( rc != 0 ) lua_pushlstring(L, (char*)errbuf.bp,errbuf.size); else lua_pushnil(L); lua_pushnumber(L, rc); tidyBufFree( &output ); tidyBufFree( &errbuf ); return 3; }
void FetchTaskHandler(const FetchTaskMessage &message, const Theron::Address from) { std::cout<<"get data.................."<<std::endl; std::string url=message.fi->url; char curl_errbuf[CURL_ERROR_SIZE]; CURL *curl = curl_easy_init(); int err; fetch::FetchResult *result=new fetch::FetchResult(); fetch::FetchInfo fi=*(message.fi); delete message.fi; result->type=fetch::UNKNOWN; result->url=fi.url; result->pathList=fi.pathList; result->attMap=fi.attMap; int errCode=0; if(curl!=NULL) { curl_easy_setopt(curl, CURLOPT_URL,url.c_str()); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errbuf); // curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10); curl_easy_setopt(curl, CURLOPT_NOSIGNAL,1); curl_easy_setopt(curl, CURLOPT_TIMEOUT ,60); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fetch_write); std::stringstream iss; curl_easy_setopt(curl, CURLOPT_WRITEDATA, &iss); err=curl_easy_perform(curl); if ( !err ) { std::map<std::string,std::string>::iterator efit=fi.attMap.find("encode"); char *resStr=new char[iss.str().length()*3]; memset(resStr,0,iss.str().length()*3); if(efit!=fi.attMap.end()) { UErrorCode error = U_ZERO_ERROR; ucnv_convert("UTF-8",efit->second.c_str(),resStr, iss.str().length()*3, iss.str().c_str(), iss.str().length(), &error ); }else { strcpy(resStr,iss.str().c_str()); } TidyDoc tdoc; // TidyBuffer tidy_errbuf = {0}; // TidyBuffer docbuf = {0}; tdoc = tidyCreate(); tidyOptSetInt(tdoc, TidyWrapLen, 4096); // tidySetErrorBuffer( tdoc, &tidy_errbuf ); tidyOptSetBool( tdoc, TidyXmlOut, yes ); tidyOptSetBool(tdoc, TidyQuoteNbsp, no); tidyOptSetBool(tdoc, TidyForceOutput, yes); /* try harder */ // tidyOptSetBool( tdoc, TidyXmlDecl, yes ); tidyOptSetBool(tdoc, TidyQuiet, yes); tidyOptSetBool(tdoc, TidyShowWarnings, no); tidyOptSetValue(tdoc,TidyDoctype,"omit"); tidyOptSetBool(tdoc, TidyFixBackslash, yes); tidyOptSetBool(tdoc, TidyMark, no); tidySetCharEncoding(tdoc,"utf8"); // tidyBufInit(&docbuf); // err = tidyParseBuffer(tdoc, &docbuf); err = tidyParseString(tdoc, resStr); if ( err >= 0 ) { err = tidyCleanAndRepair(tdoc); /* fix any problems */ if ( err >= 0 ) { // err=tidyRunDiagnostics(tdoc); /* load tidy error buffer */ // if ( err >= 0 ) { // std::cout<<"tidy error:"<<tidy_errbuf.bp<<std::endl; /* show errors */ TidyBuffer outbuf = {0}; tidyBufInit(&outbuf); tidySaveBuffer( tdoc, &outbuf ); std::stringstream hss; hss<<(char*)outbuf.bp; tidyBufFree(&outbuf); result->result=hss.str(); // } // else // { // errCode=-5; // } }else { errCode=-4; } }else { errCode=-3; } // tidyBufFree(&tidy_errbuf); // tidyBufFree(&docbuf); tidyRelease(tdoc); delete [] resStr; }else { errCode=-2; } }else { errCode=-1; } if(errCode<0) { std::stringstream ess; ess<<errCode; result->type=fetch::ERROR; result->result=ess.str(); } curl_easy_cleanup(curl); Send(FetchResultMessage(result), from); }
static void php_tidy_quick_repair(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_file) { char *enc = NULL; size_t enc_len = 0; zend_bool use_include_path = 0; TidyDoc doc; TidyBuffer *errbuf; zend_string *data, *arg1; zval *config = NULL; if (is_file) { if (zend_parse_parameters(ZEND_NUM_ARGS(), "P|zsb", &arg1, &config, &enc, &enc_len, &use_include_path) == FAILURE) { RETURN_FALSE; } if (!(data = php_tidy_file_to_mem(ZSTR_VAL(arg1), use_include_path))) { RETURN_FALSE; } } else { if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|zsb", &arg1, &config, &enc, &enc_len, &use_include_path) == FAILURE) { RETURN_FALSE; } data = arg1; } if (ZEND_SIZE_T_UINT_OVFL(ZSTR_LEN(data))) { php_error_docref(NULL, E_WARNING, "Input string is too long"); RETURN_FALSE; } doc = tidyCreate(); errbuf = emalloc(sizeof(TidyBuffer)); tidyBufInit(errbuf); if (tidySetErrorBuffer(doc, errbuf) != 0) { tidyBufFree(errbuf); efree(errbuf); tidyRelease(doc); php_error_docref(NULL, E_ERROR, "Could not set Tidy error buffer"); } tidyOptSetBool(doc, TidyForceOutput, yes); tidyOptSetBool(doc, TidyMark, no); TIDY_SET_DEFAULT_CONFIG(doc); if (config) { TIDY_APPLY_CONFIG_ZVAL(doc, config); } if(enc_len) { if (tidySetCharEncoding(doc, enc) < 0) { php_error_docref(NULL, E_WARNING, "Could not set encoding '%s'", enc); RETVAL_FALSE; } } if (data) { TidyBuffer buf; tidyBufInit(&buf); tidyBufAttach(&buf, (byte *) ZSTR_VAL(data), (uint)ZSTR_LEN(data)); if (tidyParseBuffer(doc, &buf) < 0) { php_error_docref(NULL, E_WARNING, "%s", errbuf->bp); RETVAL_FALSE; } else { if (tidyCleanAndRepair(doc) >= 0) { TidyBuffer output; tidyBufInit(&output); tidySaveBuffer (doc, &output); FIX_BUFFER(&output); RETVAL_STRINGL((char *) output.bp, output.size ? output.size-1 : 0); tidyBufFree(&output); } else { RETVAL_FALSE; } } } if (is_file) { zend_string_release(data); } tidyBufFree(errbuf); efree(errbuf); tidyRelease(doc); }
bool TidyReader::openFile (const char * szFilename) { UT_DEBUGMSG(("using libtidy to parse HTML...\n")); m_tidy = tidyCreate (); if (m_tidy == 0) return false; if (tidyOptSetBool (m_tidy, TidyXhtmlOut, yes) == 0) { UT_DEBUGMSG(("tidyOptSetBool failed!\n")); closeFile (); return false; } #ifndef DEBUG tidySetErrorBuffer (m_tidy, &m_errbuf); #endif int parse_status; if (m_buffer && m_length) { UT_DEBUGMSG(("parse HTML in buffer...\n")); UT_Byte * buffer = const_cast<UT_Byte *>(m_buffer); // grr. TidyBuffer inbuf; tidyBufInit (&inbuf); tidyBufAttach (&inbuf, buffer, static_cast<unsigned int>(m_length)); parse_status = tidyParseBuffer (m_tidy, &inbuf); tidyBufDetach (&inbuf); } else { UT_DEBUGMSG(("parse HTML in file: %s\n",szFilename)); parse_status = tidyParseFile (m_tidy, szFilename); } if (parse_status < 0) { UT_DEBUGMSG(("tidyParseBuffer/File failed!\n")); closeFile (); return false; } parse_status = tidyCleanAndRepair (m_tidy); if (parse_status < 0) { UT_DEBUGMSG(("tidyCleanAndRepair failed!\n")); closeFile (); return false; } parse_status = tidyRunDiagnostics (m_tidy); if (parse_status < 0) { UT_DEBUGMSG(("tidyRunDiagnostics failed!\n")); closeFile (); return false; } if (parse_status > 1) { parse_status = (tidyOptSetBool (m_tidy, TidyForceOutput, yes) ? parse_status : -1); } if (parse_status < 0) { UT_DEBUGMSG(("tidyOptSetBool failed!\n")); closeFile (); return false; } parse_status = tidySaveBuffer (m_tidy, &m_outbuf); if (parse_status < 0) { UT_DEBUGMSG(("tidySaveBuffer failed!\n")); closeFile (); return false; } UT_DEBUGMSG(("tidy succeeded!\n")); #ifdef DEBUG fputs ("================================================================\n", stderr); fputs ((const char *) m_outbuf.bp, stderr); fputs ("================================================================\n", stderr); #endif m_outbuf.next = 0; return true; }
int CProxyParse::RunFromMem( wxString content ) { char *pBuffer; //http://www.51proxied.com/http_non_anonymous.html //wxString path = wxT("f:/work/windows/wxUrlRefresh/data/最新透明HTTP代理服务器.htm"); //wxString path1 = wxT("f:/work/windows/wxUrlRefresh/data/result.xml"); wxString data_path = wxGetCwd() + "/data/"; wxString path1 = data_path + "_tmp.xml"; if (!wxDirExists(data_path)) wxMkdir(data_path); pBuffer = (char*)calloc(content.Length()+1, 1); wxStrncpy(pBuffer, content, content.Len()+1); wxLogMessage("Run Tidy!"); TidyBuffer output; TidyBuffer errbuf; int rc = -1; Bool ok; TidyDoc tdoc = tidyCreate(); // Initialize "document" tidyBufInit( &output ); tidyBufInit( &errbuf ); //printf( "Tidying:\t\%s\\n", input ); tidySetCharEncoding(tdoc, "utf8"); ok = tidyOptSetBool( tdoc, TidyXhtmlOut, yes ); // Convert to XHTML if ( ok ) rc = tidySetErrorBuffer( tdoc, &errbuf ); // Capture diagnostics if ( rc >= 0 ) rc = tidyParseString( tdoc, pBuffer ); // Parse the input if ( rc >= 0 ) rc = tidyCleanAndRepair( tdoc ); // Tidy it up! if ( rc >= 0 ) rc = tidyRunDiagnostics( tdoc ); // Kvetch if ( rc > 1 ) // If error, force output. rc = ( tidyOptSetBool(tdoc, TidyForceOutput, yes) ? rc : -1 ); if ( rc >= 0 ) rc = tidySaveBuffer( tdoc, &output ); // Pretty Print if ( rc >= 0 ) { #ifdef _DEBUG //if ( rc > 0 ) // WriteAllToFile("f:/work/windows/wxUrlRefresh/data/error.xml", (char*)errbuf.bp, errbuf.size); WriteAllToFile(path1, (char*)output.bp, output.size); #endif } else wxLogError("tidyFail"); tidyBufFree( &output ); tidyBufFree( &errbuf ); tidyRelease( tdoc ); if (pBuffer) free(pBuffer); wxLogMessage("Fetch data!"); // 解析数据 TiXmlDocument doc(path1); if (doc.LoadFile()) { // root CTiXmlProxyVistor vistor(&m_array); TiXmlElement *pRoot = doc.RootElement(); pRoot->Accept(&vistor); } else { wxLogMessage("shit"); return -2; } return 0; }
static PyObject *parseString(PyObject *self, PyObject *args) { char *cp; int i, len, list_size; TidyDoc tdoc; TidyOption option = TidyUnknownOption; PyObject *res = NULL, *arglist = NULL; PyObject *key_list = NULL, *item = NULL, *value = NULL; TidyBuffer output = {0}; TidyBuffer errbuf = {0}; if (!PyArg_ParseTuple(args, "s#|O", &cp, &len, &arglist)) return NULL; if (arglist && !PyDict_Check(arglist)) { PyErr_SetString(PyExc_TypeError, "Second argument must be a dictionary!"); return NULL; } tdoc = tidyCreate(); tidySetErrorBuffer(tdoc, &errbuf); if (!arglist) goto im_so_lazy; /* no args provided */ key_list = PyDict_Keys(arglist); list_size = PyList_Size(key_list); for (i = 0; i < list_size; i++) { item = PyList_GetItem(key_list, i); value = PyDict_GetItem(arglist, item); Py_INCREF(item); Py_INCREF(value); option = tidyGetOptionByName(tdoc, PyString_AsString(item)); if (option == TidyUnknownOption) { PyErr_Format(PyExc_KeyError, "Unknown tidy option '%s'", PyString_AsString(item)); TDOC_RETURN(); } switch (tidyOptGetType(option)) { case TidyString: PY_TO_TIDY(String_Check, Value, String_AsString, "a String"); break; case TidyInteger: PY_TO_TIDY(Int_Check, Int, Int_AsLong, "an Integer"); break; case TidyBoolean: PY_TO_TIDY(Int_Check, Bool, Int_AsLong, "a Boolean or an Integer"); break; default: { PyErr_Format(PyExc_RuntimeError, "Something strange happened, there is no option type %d", tidyOptGetType(option)); TDOC_RETURN(); } } Py_DECREF(item); Py_DECREF(value); } im_so_lazy: tidyParseString(tdoc, cp); tidyCleanAndRepair(tdoc); tidySaveBuffer(tdoc, &output); res = Py_BuildValue("s#", output.bp, output.size); tidyBufFree(&output); tidyBufFree(&errbuf); tidyRelease(tdoc); return res; }
static PyObject* elementtidy_fixup(PyObject* self, PyObject* args) { int rc; TidyDoc doc; TidyBuffer out = {0}; TidyBuffer err = {0}; PyObject* pyout; PyObject* pyerr; char* text; char* encoding = NULL; if (!PyArg_ParseTuple(args, "s|s:fixup", &text, &encoding)) return NULL; doc = tidyCreate(); /* options for nice XHTML output */ if (encoding) /* if an encoding is given, use it for both input and output */ tidyOptSetValue(doc, TidyCharEncoding, encoding); else /* if no encoding is given, use default input and utf-8 output */ tidyOptSetValue(doc, TidyOutCharEncoding, "utf8"); tidyOptSetBool(doc, TidyForceOutput, yes); tidyOptSetInt(doc, TidyWrapLen, 0); tidyOptSetBool(doc, TidyQuiet, yes); tidyOptSetBool(doc, TidyXhtmlOut, yes); tidyOptSetBool(doc, TidyXmlDecl, yes); tidyOptSetInt(doc, TidyIndentContent, 0); tidyOptSetBool(doc, TidyNumEntities, yes); rc = tidySetErrorBuffer(doc, &err); if (rc < 0) { PyErr_SetString(PyExc_IOError, "tidySetErrorBuffer failed"); goto error; } rc = tidyParseString(doc, text); if (rc < 0) { PyErr_SetString(PyExc_IOError, "tidyParseString failed"); goto error; } rc = tidyCleanAndRepair(doc); if (rc < 0) { PyErr_SetString(PyExc_IOError, "tidyCleanAndRepair failed"); goto error; } rc = tidyRunDiagnostics(doc); if (rc < 0) { PyErr_SetString(PyExc_IOError, "tidyRunDiagnostics failed"); goto error; } rc = tidySaveBuffer(doc, &out); if (rc < 0) { PyErr_SetString(PyExc_IOError, "tidyRunDiagnostics failed"); goto error; } pyout = PyString_FromString(out.bp ? out.bp : ""); if (!pyout) goto error; pyerr = PyString_FromString(err.bp ? err.bp : ""); if (!pyerr) { Py_DECREF(pyout); goto error; } tidyBufFree(&out); tidyBufFree(&err); tidyRelease(doc); return Py_BuildValue("NN", pyout, pyerr); error: tidyBufFree(&out); tidyBufFree(&err); tidyRelease(doc); return NULL; }