void TDzsHKwBs::SaveHtml(const PXmlTok& TopicTok, const TStr& FPath){ TStr NrFPath=TStr::GetNrFPath(FPath); TStr TitleStr=TopicTok->GetTagTok("title")->GetTokStr(false); TStr FPathSeg=GetFPathSegStr(TitleStr); printf("%s\n", TitleStr.CStr()); TDir::GenDir(NrFPath+FPathSeg); PSOut SOut=TFOut::New(NrFPath+FPathSeg+"/"+"default.htm"); SOut->PutStr("<html>"); SOut->PutDosLn(); SOut->PutDosLn(); // head SOut->PutStr("<head>"); SOut->PutDosLn(); SOut->PutStr("<title>"); SOut->PutStr(TitleStr); SOut->PutStr("</title>"); SOut->PutDosLn(); SOut->PutStr("</head>"); SOut->PutDosLn(); SOut->PutDosLn(); // body-start SOut->PutStr("<body>"); SOut->PutDosLn(); SOut->PutStr("<center><h3>"); SOut->PutStr(TitleStr); SOut->PutStr("</h3></center"); SOut->PutDosLn(); // subtrees SOut->PutStr("<ul>"); SOut->PutDosLn(); for (int SubTokN=0; SubTokN<TopicTok->GetSubToks(); SubTokN++){ PXmlTok CurTok=TopicTok->GetSubTok(SubTokN); if (CurTok->IsTag("topic")){ TStr SubTitleStr=CurTok->GetTagTok("title")->GetTokStr(false); TStr SubFPathSeg=GetFPathSegStr(SubTitleStr); SOut->PutStr("<li>"); SOut->PutStr(TStr("<a href=\"")+SubFPathSeg+"/\">"); SOut->PutStr("<b>"); SOut->PutStr(SubTitleStr); SOut->PutStr("</b>"); SOut->PutStr("</a>"); SOut->PutStr("</li>"); SOut->PutDosLn(); SaveHtml(CurTok, NrFPath+FPathSeg); } } SOut->PutStr("</ul>"); SOut->PutDosLn(); // references if (TopicTok->IsSubTag("refs")){ SOut->PutDosLn(); SOut->PutStr("<hr>"); SOut->PutDosLn(); SOut->PutDosLn(); PXmlTok RefsTok=TopicTok->GetTagTok("refs"); SOut->PutStr("<ul>"); SOut->PutDosLn(); for (int RefTokN=0; RefTokN<RefsTok->GetSubToks(); RefTokN++){ // get <ref> data PXmlTok RefTok=RefsTok->GetSubTok(RefTokN); TStr RefTitleStr=RefTok->GetTagTok("title")->GetTokStr(false); TStr RefUrlStr=RefTok->GetTagTok("url")->GetTokStr(false); // generate <li> SOut->PutStr("<li>"); SOut->PutStr(TStr("<a href=\"")+RefUrlStr+"\">"); SOut->PutStr(RefTitleStr); SOut->PutStr("</a>"); SOut->PutStr("</li>"); SOut->PutDosLn(); } SOut->PutStr("</ul>"); SOut->PutDosLn(); } // footer SOut->PutStr("<center>"); SOut->PutDosLn(); SOut->PutStr("<small>"); SOut->PutStr("Copyright DZS"); SOut->PutStr("</small>"); SOut->PutDosLn(); SOut->PutStr("</center>"); SOut->PutDosLn(); // body-end SOut->PutStr("</body>"); SOut->PutDosLn(); SOut->PutStr("</html>"); SOut->PutDosLn(); }
/** * It Downloads all the resources, main function of concern, all the required members variables must * be populated for it to work correctly */ void COfflineBrowser::BrowseOffline() { USES_CONVERSION; //Delete if this directory exists, it is a disputed call, you can safely comment it out DeleteDirectory(m_sDir); //Create the directory again CreateDirectory(m_sDir.c_str(), NULL); //SaveHtml(m_sHtml); //Create HTML Document which will be hold the html that was have MSHTML::IHTMLDocument2Ptr pDoc; HRESULT hr = CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, (void**)&pDoc); if(pDoc == NULL) return; //Load HTML to Html Document SAFEARRAY* psa = SafeArrayCreateVector(VT_VARIANT, 0, 1); VARIANT *param; bstr_t bsData = (LPCTSTR)m_sHtml.c_str(); hr = SafeArrayAccessData(psa, (LPVOID*)¶m); param->vt = VT_BSTR; param->bstrVal = (BSTR)bsData; //write your buffer hr = pDoc->write(psa); //closes the document, "applying" your code hr = pDoc->close(); //Don't forget to free the SAFEARRAY! SafeArrayDestroy(psa); //Iterate through all the elements in the document MSHTML::IHTMLElementCollectionPtr pCollection = pDoc->all; for(long a=0;a<pCollection->length;a++) { std::string sValue; IHTMLElementPtr pElem = pCollection->item( a ); //If src attribute is found that means we've a resource to download if(GetAttribute(pElem, L"src", sValue)) { //If resource URL is relative if(!IsAbsolute(sValue)) { if(sValue[0] == '/') sValue = sValue.substr(1, sValue.length()-1); //Create directories needed to hold this resource //CreateDirectories(sValue, m_sDir); //Download the resource if(1)//!DownloadResource(sValue, sValue)) { std::string sTemp = m_sScheme + m_sHost; sTemp += sValue; //Update src to the new src and put the original src attribute as //srcdump just for future references if(sTemp[0] == '/') sTemp = sTemp.substr(1, sTemp.length()-1); SetAttribute(pElem, L"src", sTemp); SetAttribute(pElem, L"srcdump", sValue); } //Unable to download the resource else { //Put srcdump same as src, It if for no use, I just put it to make //HTML DOM consistent SetAttribute(pElem, L"srcdump", sValue); } } //If resource URL is absolute else { std::string sTemp; //Make URL relative sTemp = TrimHostName(sValue); //Create directories needed to hold this resource //CreateDirectories(sTemp, m_sDir); //Dowload the resource if(1)//DownloadResource(sTemp, sTemp)) { //Update src to the new src and put the original src attribute as //srcdump just for future references if(sTemp[0] == '/') sTemp = sTemp.substr(1, sTemp.length()-1); SetAttribute(pElem, L"src", sTemp); SetAttribute(pElem, L"srcdump", sValue); } } } } //Get upated HTML out of amendments we made and save it to the described directory MSHTML::IHTMLDocument3Ptr pDoc3 = pDoc; MSHTML::IHTMLElementPtr pDocElem; pDoc3->get_documentElement(&pDocElem); BSTR bstrHtml; pDocElem->get_outerHTML(&bstrHtml); std::string sNewHtml((const wchar_t*)OLE2T(bstrHtml)); SaveHtml(sNewHtml); }