为了搞定HTML,这几天在学着用
Tidylib,终于搞明白了,喜滋滋地合并到代码中一测试,傻眼了--字符集问题。。。
Tidylib的输入流似乎只支持const char*, 因此不的不将std::wstring从‘宽字节’转换为‘多字节。转换了几次,时好时坏,搞到半夜才发现自己所用的几个测试HTML页面都各种包含着不同的字符集,于是字符集问题就出来了,也搞死我了~最后一咬牙,一跺脚,老子我不转了,都用‘RAW’数据好了,‘宽’到‘多’直接用UTF8了。。。于是就有了下面的代码。
int CHtmlTidyObject::Tidy(const std::wstring &input, std::wstring &output)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int codepage = CP_UTF8;//54936;//CP_UTF8;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int ret = -1;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
TidyDoc tdoc = tidyCreate();
if(tidyOptSetBool(tdoc, TidyMark, no) != yes)
return -1;
if(tidyOptSetInt(tdoc, TidyDoctypeMode, TidyDoctypeOmit) != yes)
return -1;
if(tidyOptSetBool(tdoc, TidyHideComments, yes) != yes)
return -1;
if(tidyOptSetInt(tdoc, TidyWrapLen, 0) != yes)
return -1;
//if(tidyOptSetBool(tdoc, TidyMakeClean, yes) != yes)//css
// return -1;
if(tidyOptSetBool(tdoc, TidyUpperCaseTags, yes) != yes)
return -1;
if(tidyOptSetBool(tdoc, TidyHtmlOut, yes) != yes)
return -1;
if(tidySetCharEncoding(tdoc, "raw") != 0)
return -1;
if(tidyOptSetBool(tdoc, TidyShowWarnings, no) != yes)
return -1;
if(tidyOptSetInt(tdoc, TidyShowErrors, 0) != yes)
return -1;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(tidyOptSetBool(tdoc, TidyForceOutput, yes) != yes)
return -1;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int sz = WideCharToMultiByte(codepage, 0, input.c_str(), input.size(), NULL, 0, NULL, NULL);
if(sz == -1)
return -1;
char* buf = new char[sz + 1];
sz = WideCharToMultiByte(codepage, 0, input.c_str(), input.size(), buf, sz, NULL, NULL);
if(tidyParseString(tdoc, buf) >= 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//TidyBuffer errbuf = {0};
//tidySetErrorBuffer( tdoc, &errbuf );
if(tidyCleanAndRepair(tdoc) >= 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//tidyRunDiagnostics( tdoc );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
TidyBuffer outbuf =
{ 0 };
if(tidySaveBuffer(tdoc, &outbuf) >= 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//std::cout << "OUTPUT->\n" << outbuf.bp << std::endl;
int wsz = MultiByteToWideChar(codepage, 0, (const char*)outbuf.bp, outbuf.size, NULL, 0);
wchar_t* wbuf = new wchar_t[wsz + 1];
wsz = MultiByteToWideChar(codepage, 0, (const char*)outbuf.bp, outbuf.size, wbuf, wsz);
output = wbuf;
delete [] wbuf;
ret = 0;
}
tidyBufFree(&outbuf);
}
//std::cout << "ERROR->\n" << errbuf.bp << std::endl;
//tidyBufFree(&errbuf);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
delete [] buf;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
tidyRelease(tdoc);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return ret;
}
感觉还有问题,但经过Tidy处理,TinyHtmlParser确实能解析原来解不开的HTML数据了,就先放着吧,测试看看先~唉,HTML从头到尾都是最影响LingosHook的部分,早知道应该多好好找找稳定的Parser,自己造的轮子对路面要求太高了。。。