int tidyDocParseString( TidyDocImpl* doc, ctmbstr content ) { int status = -EINVAL; TidyBuffer inbuf = {0}; StreamIn* in = NULL; if ( content ) { tidyBufAttach( &inbuf, (byte*)content, TY_(tmbstrlen)(content)+1 ); in = TY_(BufferInput)( doc, &inbuf, cfg( doc, TidyInCharEncoding )); status = tidyDocParseStream( doc, in ); tidyBufDetach( &inbuf ); TY_(freeStreamIn)(in); } return status; }
/* returns false if unknown option, missing parameter, ** or option doesn't use parameter */ Bool ParseConfigValue( TidyDocImpl* doc, TidyOptionId optId, ctmbstr optval ) { const TidyOptionImpl* option = option_defs + optId; Bool status = ( optId < N_TIDY_OPTIONS && optval != NULL ); if ( !status ) ReportBadArgument( doc, option->name ); else { TidyBuffer inbuf = {0}; /* Set up input source */ tidyBufAttach( &inbuf, (byte*)optval, tmbstrlen(optval)+1 ); doc->config.cfgIn = BufferInput( doc, &inbuf, ASCII ); doc->config.c = GetC( &doc->config ); status = option->parser( doc, option ); freeStreamIn(doc->config.cfgIn); /* Release input source */ doc->config.cfgIn = NULL; tidyBufDetach( &inbuf ); } return status; }
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; }