49392 fb2

added getting parameters via asc_getCoreProps
This commit is contained in:
Kulikova Svetlana
2021-04-01 15:55:57 +03:00
parent 930c225129
commit e58e2599fb
5 changed files with 205 additions and 63 deletions

View File

@ -623,7 +623,7 @@ namespace NSDoctRenderer
return bIsBreak;
}
bool ExecuteScript(const std::string& strScript, const std::wstring& sCachePath, std::wstring& strError, std::wstring& strReturnParams)
bool ExecuteScript(const std::string& strScript, const std::wstring& sCachePath, std::wstring& strError, std::wstring& strReturnParams, bool bNeedCore)
{
LOGGER_SPEED_START
@ -900,6 +900,143 @@ namespace NSDoctRenderer
bIsBreak = Doct_renderer_SaveFile(&m_oParams, pNative, context, args, strError, js_objectApi);
}
// CORE PARAMS
if (!bIsBreak && m_oParams.m_eDstFormat == DoctRendererFormat::HTML && bNeedCore)
{
JSSmart<CJSObject> js_objectCore = js_objectApi->call_func("asc_getCoreProps", 1, args)->toObject();
if(try_catch->Check())
{
strError = L"code=\"core_props\"";
bIsBreak = true;
}
else
{
if (js_objectCore->isUndefined() || !js_objectCore->isObject())
{
strError = L"code=\"core_props\"";
bIsBreak = true;
return bIsBreak;
}
JSSmart<CJSValue> js_results = js_objectCore->call_func("asc_getTitle", 1, args);
if(try_catch->Check())
{
strError = L"code=\"get_title\"";
bIsBreak = true;
}
else if (!js_results->isNull())
{
strReturnParams += L"<dc:title>";
strReturnParams += js_results->toStringW();
strReturnParams += L"</dc:title>";
}
js_results = js_objectCore->call_func("asc_getCreator", 1, args);
if(try_catch->Check())
{
strError = L"code=\"get_creator\"";
bIsBreak = true;
}
else if (!js_results->isNull())
{
strReturnParams += L"<dc:creator>";
strReturnParams += js_results->toStringW();
strReturnParams += L"</dc:creator>";
}
js_results = js_objectCore->call_func("asc_getDescription", 1, args);
if(try_catch->Check())
{
strError = L"code=\"get_description\"";
bIsBreak = true;
}
else if (!js_results->isNull())
{
strReturnParams += L"<dc:description>";
strReturnParams += js_results->toStringW();
strReturnParams += L"</dc:description>";
}
js_results = js_objectCore->call_func("asc_getSubject", 1, args);
if(try_catch->Check())
{
strError = L"code=\"get_subject\"";
bIsBreak = true;
}
else if (!js_results->isNull())
{
strReturnParams += L"<dc:subject>";
strReturnParams += js_results->toStringW();
strReturnParams += L"</dc:subject>";
}
js_results = js_objectCore->call_func("asc_getIdentifier", 1, args);
if(try_catch->Check())
{
strError = L"code=\"get_identifier\"";
bIsBreak = true;
}
else if (!js_results->isNull())
{
strReturnParams += L"<dc:identifier>";
strReturnParams += js_results->toStringW();
strReturnParams += L"</dc:identifier>";
}
js_results = js_objectCore->call_func("asc_getLanguage", 1, args);
if(try_catch->Check())
{
strError = L"code=\"get_language\"";
bIsBreak = true;
}
else if (!js_results->isNull())
{
strReturnParams += L"<dc:language>";
strReturnParams += js_results->toStringW();
strReturnParams += L"</dc:language>";
}
js_results = js_objectCore->call_func("asc_getCreated", 1, args);
if(try_catch->Check())
{
strError = L"code=\"get_created\"";
bIsBreak = true;
}
else if (!js_results->isNull())
{
strReturnParams += L"<dcterms:created xsi:type=\"dcterms:W3CDTF\">";
strReturnParams += js_results->toStringW();
strReturnParams += L"</dcterms:created>";
}
js_results = js_objectCore->call_func("asc_getKeywords", 1, args);
if(try_catch->Check())
{
strError = L"code=\"get_keywords\"";
bIsBreak = true;
}
else if (!js_results->isNull())
{
strReturnParams += L"<cp:keywords>";
strReturnParams += js_results->toStringW();
strReturnParams += L"</cp:keywords>";
}
js_results = js_objectCore->call_func("asc_getVersion", 1, args);
if(try_catch->Check())
{
strError = L"code=\"get_version\"";
bIsBreak = true;
}
else if (!js_results->isNull())
{
strReturnParams += L"<cp:version>";
strReturnParams += js_results->toStringW();
strReturnParams += L"</cp:version>";
}
}
}
LOGGER_SPEED_LAP("save")
}
@ -951,7 +1088,7 @@ namespace NSDoctRenderer
RELEASEOBJECT(m_pInternal);
}
bool CDoctrenderer::Execute(const std::wstring& strXml, std::wstring& strError)
bool CDoctrenderer::Execute(const std::wstring& strXml, std::wstring& strError, bool bNeedCore)
{
strError = L"";
m_pInternal->m_oParams.FromXml(strXml);
@ -1069,7 +1206,7 @@ namespace NSDoctRenderer
strScript += "\n$.ready();";
std::wstring sReturnParams = L"";
bool bResult = m_pInternal->ExecuteScript(strScript, sCachePath, strError, sReturnParams);
bool bResult = m_pInternal->ExecuteScript(strScript, sCachePath, strError, sReturnParams, bNeedCore);
if (strError.length() != 0)
{

View File

@ -65,7 +65,7 @@ namespace NSDoctRenderer
~CDoctrenderer();
public:
bool Execute(const std::wstring& strXml, std::wstring& strError);
bool Execute(const std::wstring& strXml, std::wstring& strError, bool bNeedCore = false);
std::vector<std::wstring> GetImagesInChanges();
private:

View File

@ -242,7 +242,7 @@ HRESULT CEpubFile::FromHtml(const std::wstring& sHtmlFile, const std::wstring& s
}
// content.opf
NSFile::CFileBinary oContentOpf;
bool bWasLanguage = false;
bool bWasLanguage = false, bWasTitle = false;
std::wstring sTitle = NSFile::GetFileName(sDstFile);
std::wstring sUUID = GenerateUUID();
if (oContentOpf.CreateFileW(m_sTempDir + L"/OEBPS/content.opf"))
@ -268,6 +268,7 @@ HRESULT CEpubFile::FromHtml(const std::wstring& sHtmlFile, const std::wstring& s
bWasIdentifier = true;
else if (sName == L"dc:title")
{
bWasTitle = true;
size_t nBegin = sOut.find(L'>');
if (nBegin == std::wstring::npos)
continue;
@ -289,9 +290,12 @@ HRESULT CEpubFile::FromHtml(const std::wstring& sHtmlFile, const std::wstring& s
oContentOpf.WriteStringUTF8(sUUID);
oContentOpf.WriteStringUTF8(L"</dc:identifier>");
}
oContentOpf.WriteStringUTF8(L"<dc:title>");
oContentOpf.WriteStringUTF8(sTitle);
oContentOpf.WriteStringUTF8(L"</dc:title>");
if (!bWasTitle)
{
oContentOpf.WriteStringUTF8(L"<dc:title>");
oContentOpf.WriteStringUTF8(sTitle);
oContentOpf.WriteStringUTF8(L"</dc:title>");
}
if (!bWasLanguage)
oContentOpf.WriteStringUTF8(L"<dc:language>en-EN</dc:language>");
// manifest

View File

@ -1706,52 +1706,59 @@ HRESULT CFb2File::FromHtml(const std::wstring& sHtmlFile, const std::wstring& sC
NSStringUtils::CStringBuilder oDocument;
oDocument.WriteString(L"<?xml version=\"1.0\" encoding=\"UTF-8\"?><FictionBook xmlns=\"http://www.gribuser.ru/xml/fictionbook/2.0\" xmlns:l=\"http://www.w3.org/1999/xlink\"><description>");
// description
std::wstring sCoreXml;
if (NSFile::CFileBinary::ReadAllTextUtf8(sCoreFile, sCoreXml))
// title-info
oDocument.WriteString(L"<title-info>");
std::wstring sBookTitle = NSFile::GetFileName(sDst);
std::wstring sAuthor = sBookTitle;
std::wstring sAnnotation, sKeywords, sDate;
std::wstring sLanguage = L"en-EN", sVersion = L"1.0";
std::wstring sIdentifier = GenerateUUID();
XmlUtils::CXmlLiteReader oCoreReader;
oCoreReader.FromString(sCoreFile);
oCoreReader.ReadNextNode();
int nDeath = oCoreReader.GetDepth();
while (oCoreReader.ReadNextSiblingNode(nDeath))
{
// title-info
oDocument.WriteString(L"<title-info>");
std::wstring sBookTitle = NSFile::GetFileName(sDst);
std::wstring sAuthor = sBookTitle;
std::wstring sAnnotation;
std::wstring sKeywords;
XmlUtils::CXmlLiteReader oCoreReader;
oCoreReader.FromString(sCoreXml);
oCoreReader.ReadNextNode();
int nDeath = oCoreReader.GetDepth();
while (oCoreReader.ReadNextSiblingNode(nDeath))
{
if (oCoreReader.GetNamespacePrefix() == L"dc")
{
std::wstring sName = oCoreReader.GetName();
if (sName == L"dc:creator")
sAuthor = oCoreReader.GetText2();
else if (sName == L"dc:title")
sBookTitle = oCoreReader.GetText2();
else if (sName == L"dc:description")
sAnnotation = oCoreReader.GetText2();
else if (sName == L"dc:subject")
sKeywords += oCoreReader.GetText2() + L' ';
}
}
oDocument.WriteString(L"<genre>dramaturgy</genre><author><nickname>");
oDocument.WriteString(sAuthor);
oDocument.WriteString(L"</nickname></author><book-title>");
oDocument.WriteString(sBookTitle);
oDocument.WriteString(L"</book-title>");
if (!sAnnotation.empty())
oDocument.WriteString(L"<annotation><p>" + sAnnotation + L"</p></annotation>");
if (!sKeywords.empty())
oDocument.WriteString(L"<keywords>" + sKeywords + L"</keywords>");
oDocument.WriteString(L"<lang>en</lang></title-info><document-info><author><nickname>");
// document-info
oDocument.WriteString(sAuthor);
oDocument.WriteString(L"</nickname></author><date></date><id>");
oDocument.WriteString(GenerateUUID());
oDocument.WriteString(L"</id><version>1.0</version></document-info>");
std::wstring sName = oCoreReader.GetName();
if (sName == L"dc:creator")
sAuthor = oCoreReader.GetText2();
else if (sName == L"dc:title")
sBookTitle = oCoreReader.GetText2();
else if (sName == L"dc:description")
sAnnotation = oCoreReader.GetText2();
else if (sName == L"dc:subject" || sName == L"cp:keywords")
sKeywords += oCoreReader.GetText2() + L' ';
else if (sName == L"dc:identifier")
sIdentifier = oCoreReader.GetText2();
else if (sName == L"dc:language")
sLanguage = oCoreReader.GetText2();
else if (sName == L"dcterms:created")
sDate = oCoreReader.GetText2();
else if (sName == L"cp:version")
sVersion = oCoreReader.GetText2();
}
oDocument.WriteString(L"<genre>dramaturgy</genre><author><nickname>");
oDocument.WriteString(sAuthor);
oDocument.WriteString(L"</nickname></author><book-title>");
oDocument.WriteString(sBookTitle);
oDocument.WriteString(L"</book-title>");
if (!sAnnotation.empty())
oDocument.WriteString(L"<annotation><p>" + sAnnotation + L"</p></annotation>");
if (!sKeywords.empty())
oDocument.WriteString(L"<keywords>" + sKeywords + L"</keywords>");
oDocument.WriteString(L"<lang>");
oDocument.WriteString(sLanguage);
oDocument.WriteString(L"</lang></title-info><document-info><author><nickname>");
// document-info
oDocument.WriteString(sAuthor);
oDocument.WriteString(L"</nickname></author><date>");
oDocument.WriteString(sDate);
oDocument.WriteString(L"</date><id>");
oDocument.WriteString(sIdentifier);
oDocument.WriteString(L"</id><version>");
oDocument.WriteString(sVersion);
oDocument.WriteString(L"</version></document-info>");
// body
oDocument.WriteString(L"</description><body><section>");
std::string sContent;

View File

@ -1545,7 +1545,7 @@ namespace NExtractTools
NSDoctRenderer::CDoctrenderer oDoctRenderer(NULL != params.m_sAllFontsPath ? *params.m_sAllFontsPath : L"");
std::wstring sXml = getDoctXml(eFromType, eToType, sFileFromDir, sHtmlFile, sImagesDirectory, sThemeDir, -1, L"", params);
std::wstring sResult;
oDoctRenderer.Execute(sXml, sResult);
oDoctRenderer.Execute(sXml, sResult, true);
if (sResult.find(L"error") != std::wstring::npos)
{
std::wcerr << L"DoctRenderer:" << sResult << std::endl;
@ -1553,16 +1553,10 @@ namespace NExtractTools
}
else
{
std::wstring sDocxDir = sTemp + FILE_SEPARATOR_STR + _T("docx_unpacked");
NSDirectory::CreateDirectory(sDocxDir);
nRes = doct_bin2docx_dir(sFrom, sTo, sDocxDir, false, sThemeDir, params);
if (SUCCEEDED_X2T(nRes))
{
CFb2File fb2File;
fb2File.SetTmpDirectory(sTemp);
if (S_FALSE == fb2File.FromHtml(sHtmlFile, sDocxDir + FILE_SEPARATOR_STR + L"docProps" + FILE_SEPARATOR_STR + L"core.xml", sTo))
nRes = AVS_FILEUTILS_ERROR_CONVERT;
}
CFb2File fb2File;
fb2File.SetTmpDirectory(sTemp);
if (S_FALSE == fb2File.FromHtml(sHtmlFile, sResult, sTo))
nRes = AVS_FILEUTILS_ERROR_CONVERT;
}
return nRes;
}