#include "md2html.h" #include "md4c/src/md4c-html.h" #include "../../../DesktopEditor/common/File.h" namespace Md { #define MD_PARSER_FLAGS MD_DIALECT_GITHUB | MD_FLAG_NOINDENTEDCODEBLOCKS | MD_HTML_FLAG_SKIP_UTF8_BOM | MD_FLAG_HARD_SOFT_BREAKS | MD_HTML_FLAG_XHTML #define MD_RENDERER_FLAGS MD_HTML_FLAG_XHTML void ToHtml(const MD_CHAR* pValue, MD_SIZE uSize, void* pData) { if (NULL != pData) (*(std::string*)pData).append(pValue, uSize); } std::string ConvertMdStringToHtml(const std::string& sMdString) { std::string sData; md_html(sMdString.c_str(), sMdString.length(), ToHtml, &sData, MD_PARSER_FLAGS, MD_RENDERER_FLAGS); return sData; } void ToHtmlFile(const MD_CHAR* pValue, MD_SIZE uSize, void* pData) { if (NULL != pData) ((NSFile::CFileBinary*)pData)->WriteFile(pValue, uSize); } void WriteBaseHtmlStyles(NSFile::CFileBinary& oFile) { oFile.WriteStringUTF8(L""); } bool ConvertMdFileToHtml(const std::wstring& wsPathToMdFile, const std::wstring& wsPathToHtmlFile) { std::string sMdData; if (!NSFile::CFileBinary::ReadAllTextUtf8A(wsPathToMdFile, sMdData)) return false; NSFile::CFileBinary oFile; if (!oFile.CreateFile(wsPathToHtmlFile)) return false; oFile.WriteStringUTF8(L"
"); oFile.WriteStringUTF8(L""); //oFile.WriteStringUTF8(L""); oFile.WriteStringUTF8(L""); WriteBaseHtmlStyles(oFile); oFile.WriteStringUTF8(L""); bool bResult = true; if (0 != md_html(sMdData.c_str(), sMdData.length(), ToHtmlFile, &oFile, MD_PARSER_FLAGS, MD_RENDERER_FLAGS)) bResult = false; oFile.WriteStringUTF8(L""); oFile.CloseFile(); if (!bResult) NSFile::CFileBinary::Remove(wsPathToHtmlFile); return bResult; } }