From 430f85d1417910da3b575fd6237490cee34c72d8 Mon Sep 17 00:00:00 2001 From: Kulikova Svetlana Date: Wed, 12 Aug 2020 12:22:04 +0300 Subject: [PATCH] Cross references --- HtmlFile2/htmlfile2.cpp | 160 +++++++++++++++++++--------------------- 1 file changed, 75 insertions(+), 85 deletions(-) diff --git a/HtmlFile2/htmlfile2.cpp b/HtmlFile2/htmlfile2.cpp index 2215e80710..eb15d91336 100644 --- a/HtmlFile2/htmlfile2.cpp +++ b/HtmlFile2/htmlfile2.cpp @@ -42,6 +42,7 @@ public: std::wstring m_sDst; // Директория назначения std::wstring m_sBase; // Полный базовый адрес + std::map> m_sSrcs; // Имена обрабатываемых файлов (имя файла, имя перекрестной ссылки) std::map m_mStyles; // Стили в document.xml. Хранятся как (имя тэга, его стиль) private: @@ -49,6 +50,7 @@ private: int m_nFootnoteId; // ID сноски int m_nHyperlinkId; // ID ссылки int m_nStyleId; // ID стиля + int m_nCrossId; // ID перекрестной ссылки NSStringUtils::CStringBuilder m_oStylesXml; // styles.xml NSStringUtils::CStringBuilder m_oDocXmlRels; // document.xml.rels @@ -62,6 +64,7 @@ public: m_nFootnoteId = 1; m_nHyperlinkId = 1; m_nStyleId = 1; + m_nCrossId = 1; m_sBase = L""; } @@ -281,19 +284,19 @@ public: // Стили по умолчанию // Нормальный стиль, от которого базируются - m_oStylesXml += L""; + // m_oStylesXml += L""; // Маркированный список - m_oStylesXml += L""; + // m_oStylesXml += L""; // Заголовки - m_oStylesXml += L""; + // m_oStylesXml += L""; // Текст: p, div - m_oStylesXml += L""; - m_oStylesXml += L""; + // m_oStylesXml += L""; + // m_oStylesXml += L""; // Ссылки - m_oStylesXml += L""; + // m_oStylesXml += L""; } - bool readSrc() + bool readSrc(const std::wstring& sFileName) { // Читаем html if(!isHtml()) @@ -308,7 +311,7 @@ public: if(sName == L"head") readHead(); else if(sName == L"body") - readBody(); + readBody(sFileName); } return true; } @@ -445,47 +448,6 @@ public: } private: - /* - std::vector getStyle(std::vector sSelectors) - { - std::string sClass = ""; - std::string sStyle = ""; - std::string sId = ""; - - while(m_oLightReader.MoveToNextAttribute()) - { - std::wstring sAName = m_oLightReader.GetName(); - if(sAName == L"style") - sStyle = m_oLightReader.GetTextA(); - else if(sAName == L"class") - sClass = m_oLightReader.GetTextA(); - else if(sAName == L"id") - sId = m_oLightReader.GetTextA(); - } - m_oLightReader.MoveToElement(); - - std::vector sSubClass(sSelectors); - if(!sStyle.empty()) - { - std::string sSelector = ""; - if(!sId.empty()) - sSelector += "#" + sId + " "; - if(!sClass.empty()) - sSelector += "." + sClass + " "; - sSelector += m_oLightReader.GetNameA(); - sSubClass.push_back(sSelector); - - NSCSS::CCompiledStyle oStyle = m_oStylesCalculator.GetCompiledStyle(sSubClass); - m_oStylesXml += L""; - m_oStylesXml += oStyle.GetStyleW(); - m_oStylesXml += L""; - } - - return sSubClass; - } - */ std::vector GetSubClass(std::vector& sSelectors) { @@ -540,18 +502,11 @@ private: m_sBase = m_oLightReader.GetText(); m_oLightReader.MoveToElement(); } - /* - // Заголовок документа - else if(sName == L"title") - readTitle(); - */ } } - void readBody() + void readBody(const std::wstring& sFileName) { - // sSelectors = getStyle(sSelectors); - bool bWasP = true; std::vector sSelectors; sSelectors = GetSubClass(sSelectors); @@ -559,6 +514,23 @@ private: oLi.bNeedLi = false; oLi.nLevelLi = -1; oLi.bType = true; + + std::map>::iterator it = m_sSrcs.find(sFileName); + if(it != m_sSrcs.end()) + { + for(const std::wstring& sId : it->second) + { + std::wstring sCrossId = std::to_wstring(m_nCrossId++); + m_oDocXml += L""; + } + } + m_oDocXml += L""; readStream(sSelectors, L"", false, oLi, bWasP); m_oDocXml += L""; @@ -578,9 +550,12 @@ private: std::wstring sName = m_oLightReader.GetName(); if(sName == L"#text") { - m_oDocXml += L""; + if(bWasP) + { + m_oDocXml += L""; + } std::wstring sText = m_oLightReader.GetText(); if(bBdo) std::reverse(sText.begin(), sText.end()); @@ -693,9 +668,6 @@ private: m_oDocXml += L""; bWasP = true; } - //m_oDocXml += L""; readStream(sSubClass, sRStyle, bBdo, oLi, bWasP); if(!bWasP) { @@ -868,22 +840,29 @@ private: { std::wstring sRef = L""; std::wstring sTitle = L""; + bool bCross = false; while(m_oLightReader.MoveToNextAttribute()) { std::wstring sName = m_oLightReader.GetName(); if(sName == L"href") { sRef = m_oLightReader.GetText(); - size_t nLen = (sRef.length() > 4 ? 4 : 0); - // Ссылка на сайт - if(sRef.substr(0, nLen) == L"http") - { - - } - // Ссылка на документ, который нужно обработать + size_t nSrc = sRef.rfind(L"/"); + if(nSrc == std::wstring::npos) + nSrc = 0; else + nSrc++; + size_t nLen = sRef.rfind(L"html"); + if(nLen == std::wstring::npos) + continue; + else + nLen += 4; + std::wstring sFileName = sRef.substr(nSrc, nLen - nSrc); + std::map>::iterator it = m_sSrcs.find(sFileName); + if(it != m_sSrcs.end()) { - + bCross = true; + it->second.push_back(L"cHyp" + std::to_wstring(m_nHyperlinkId)); } } else if(sName == L"title") @@ -896,20 +875,28 @@ private: if(sTitle.empty()) sTitle = sRef; - // Пишем рельсы - m_oDocXmlRels += L""; + // Перекрестная ссылка внутри файла + if(bCross) + m_oDocXml += L""; - // Пишем в document.xml - m_oDocXml += L""; - readStream(sSelectors, sRStyle += L"", bBdo, oLi, bWasP); + bWasP = false; + readStream(sSelectors, sRStyle += L"", bBdo, oLi, bWasP); m_oDocXml += L""; } @@ -1113,7 +1100,7 @@ HRESULT CHtmlFile2::Open(const std::wstring& sSrc, const std::wstring& sDst, CHt if(!m_internal->m_oLightReader.MoveToStart()) return S_FALSE; - if(!m_internal->readSrc()) + if(!m_internal->readSrc(NSFile::GetFileName(sSrc))) return S_FALSE; m_internal->write(); NSFile::CFileBinary::Remove(m_internal->m_sTmp + L"/res.xhtml"); @@ -1125,7 +1112,10 @@ HRESULT CHtmlFile2::OpenBatch(const std::vector& sSrc, const std:: m_internal->m_sDst = sDst; m_internal->CreateDocxEmpty(oParams); - for(std::wstring sS : sSrc) + for(const std::wstring& sS : sSrc) + m_internal->m_sSrcs.insert(std::make_pair(NSFile::GetFileName(sS), std::vector())); + + for(const std::wstring& sS : sSrc) { #ifdef _DEBUG std::wcout << NSFile::GetFileName(sS) << std::endl; @@ -1142,7 +1132,7 @@ HRESULT CHtmlFile2::OpenBatch(const std::vector& sSrc, const std:: if(!m_internal->m_oLightReader.MoveToStart()) return S_FALSE; - if(!m_internal->readSrc()) + if(!m_internal->readSrc(NSFile::GetFileName(sS))) return S_FALSE; NSFile::CFileBinary::Remove(m_internal->m_sTmp + L"/res.xhtml");