diff --git a/src/translator/commands/format/sc_scn_tex_cite_command.cpp b/src/translator/commands/format/sc_scn_tex_cite_command.cpp index 774ed38..ba74462 100644 --- a/src/translator/commands/format/sc_scn_tex_cite_command.cpp +++ b/src/translator/commands/format/sc_scn_tex_cite_command.cpp @@ -5,6 +5,5 @@ ScScnTexCommandResult ScSCnTexCiteCommand::Complete( ScSCnPrefixTree & tree, ScScnTexCommandParams const & params) { - std::cout << params.at(1); return params.at(1); } diff --git a/src/translator/filesystem/sc_file_system.cpp b/src/translator/filesystem/sc_file_system.cpp index 36a791b..469d0c1 100644 --- a/src/translator/filesystem/sc_file_system.cpp +++ b/src/translator/filesystem/sc_file_system.cpp @@ -23,23 +23,28 @@ void ScDirectory::RemoveDirectory() std::filesystem::remove_all(m_path); } -ScFile ScDirectory::CopyFile(std::string const & filePath, std::string const & newExtension) const +ScFile ScDirectory::CopyFile(ScFile const & file, std::string newExtension, bool copyContent) const { - std::string const & fileNameWithExt = filePath.substr(filePath.rfind('/') + 1, filePath.size()); - std::string const & fileName = fileNameWithExt.substr(0, fileNameWithExt.rfind('.')); + std::string const & fileName = file.GetName(); - ScStringStream stream; - (m_path.at(m_path.size() - 1) == '/' - ? stream << m_path - : stream << m_path << "/") - << fileName << (newExtension.at(0) == '.' ? newExtension : "." + newExtension); + if (newExtension.empty()) + newExtension = file.GetExtension(); - return ScFile{stream}; -} + ScStringStream pathStream; + pathStream << m_path; + if (m_path.back() != '/') + pathStream << "/"; + pathStream << fileName << (newExtension.front() == '.' ? newExtension : "." + newExtension); -ScFile ScDirectory::CopyFile(ScFile const & file, std::string const & newExtension) const -{ - return CopyFile(file.GetPath(), newExtension); + ScFile copiedFile{pathStream}; + if (copyContent) + { + std::stringstream contentStream; + file.Read(contentStream); + copiedFile.Write(contentStream.str()); + } + + return copiedFile; } ScDirectory ScDirectory::CopyDirectory(std::string nestedDirectoryPath, std::string const & targetDirectoryPath) const diff --git a/src/translator/filesystem/sc_file_system.h b/src/translator/filesystem/sc_file_system.h index b265c0d..26c2e37 100644 --- a/src/translator/filesystem/sc_file_system.h +++ b/src/translator/filesystem/sc_file_system.h @@ -26,14 +26,14 @@ class ScFile void Write(std::string const & text) { std::ofstream file; - file.open(m_path, std::ios::app); + file.open(m_path, std::ios::trunc); file << text; file.close(); } - void Read(std::stringstream & text) + void Read(std::stringstream & text) const { std::ifstream file; file.open(m_path); @@ -47,14 +47,21 @@ class ScFile [[nodiscard]] std::string GetName() const { - std::string const & fileNameWithExt = m_path.substr(m_path.rfind('/') + 1, m_path.size()); + std::string const & fileNameWithExt = m_path.substr(m_path.rfind('/') + 1); std::string const & fileName = fileNameWithExt.substr(0, fileNameWithExt.rfind('.')); return fileName; } + [[nodiscard]] std::string GetExtension() const + { + std::string const & fileNameWithExt = m_path.substr(m_path.rfind('/') + 1); + std::string const & fileExt = fileNameWithExt.substr(fileNameWithExt.rfind('.')); + return fileExt; + } + [[nodiscard]] bool HasExtension(std::unordered_set const & extensions) const { - return extensions.find(m_path.substr(m_path.rfind('.'))) != extensions.cend(); + return extensions.find(GetExtension()) != extensions.cend(); } private: @@ -72,9 +79,7 @@ class ScDirectory void RemoveDirectory(); - [[nodiscard]] ScFile CopyFile(std::string const & filePath, std::string const & newExtension) const; - - [[nodiscard]] ScFile CopyFile(ScFile const & file, std::string const & newExtension) const; + [[nodiscard]] ScFile CopyFile(ScFile const & file, std::string newExtension = "", bool copyContent = false) const; [[nodiscard]] ScDirectory CopyDirectory(std::string nestedDirectoryPath, std::string const & targetDirectoryPath) const; diff --git a/src/translator/helper/scs_helper.cpp b/src/translator/helper/scs_helper.cpp index 054f144..2e3e0b8 100644 --- a/src/translator/helper/scs_helper.cpp +++ b/src/translator/helper/scs_helper.cpp @@ -6,6 +6,9 @@ std::string SCsHelper::scNodeRoleRelation{"sc_node_role_relation"}; std::string SCsHelper::scNodeClass{"sc_node_class"}; std::string SCsHelper::scNodeStruct{"sc_node_struct"}; +std::string SCsHelper::m_currentFilePath; +std::string SCsHelper::m_currentFileDirectoryName; + std::string SCsHelper::GetNodeTypeByIdtf(std::string const & idtf) { char lastSymb = idtf.at(idtf.size() - 1); @@ -43,9 +46,20 @@ bool SCsHelper::IsFile(std::string const & string) return (string.find('[') != std::string::npos) || (string.rfind("![") != std::string::npos); } -std::string SCsHelper::Url(std::string const & idtf) +void SCsHelper::SetCurrentFile(std::string const & filePath) { - return "\"file://" + idtf + "\""; + m_currentFilePath = filePath; + std::string const & directoryPath = m_currentFilePath.substr(0, m_currentFilePath.rfind('/')); + m_currentFileDirectoryName = directoryPath.substr(directoryPath.rfind('/') + 1); +} + +std::string SCsHelper::Url(std::string const & path) +{ + std::string const & relativePath = path.find(m_currentFileDirectoryName) == std::string::npos + ? path + : path.substr(path.find(m_currentFileDirectoryName) + m_currentFileDirectoryName.size() + 1); + + return "\"file://" + relativePath + "\""; } std::string SCsHelper::FileClass(std::string const & idtf) diff --git a/src/translator/helper/scs_helper.h b/src/translator/helper/scs_helper.h index 5c0079e..e82b0d5 100644 --- a/src/translator/helper/scs_helper.h +++ b/src/translator/helper/scs_helper.h @@ -26,7 +26,9 @@ class SCsHelper static bool IsFile(std::string const & string); - static std::string Url(std::string const & idtf); + static void SetCurrentFile(std::string const & filePath); + + static std::string Url(std::string const & path); static std::string FileClass(std::string const & idtf); @@ -35,6 +37,8 @@ class SCsHelper static bool IsNoNamed(std::string const & string); static std::string GetFormat(std::string const & string); +private: + static std::string m_currentFilePath; + static std::string m_currentFileDirectoryName; }; - diff --git a/src/translator/sc_scn_tex2scs_translator.cpp b/src/translator/sc_scn_tex2scs_translator.cpp index 6729722..ed7a921 100644 --- a/src/translator/sc_scn_tex2scs_translator.cpp +++ b/src/translator/sc_scn_tex2scs_translator.cpp @@ -1,5 +1,6 @@ #include "sc_scn_tex2scs_translator.h" #include "log/sc_log.hpp" +#include "helper/scs_helper.h" ScSCnTex2SCsTranslator::ScSCnTex2SCsTranslator(bool debugMode, bool clearMode) { @@ -69,26 +70,31 @@ void ScSCnTex2SCsTranslator::TranslateFiles( TranslateFiles(directory.GetPath(), startDirectory, startTargetDirectory); }, [this, &targetDirectory](ScFile const & file) { - if (!file.HasExtension(m_extensions)) - return; - - SCsStream::Clear(); - bool result = TranslateFile(file.GetPath(), targetDirectory); - SC_LOG_INFO("[" << ++m_fileNumber << "/" << m_filesCount << "]: " << file.GetPath() - << " - " << (result ? "OK" : "ERROR")); + if (file.HasExtension(m_extensions)) + { + SCsStream::Clear(); + bool result = TranslateFile(file, targetDirectory); + SC_LOG_INFO("[" << ++m_fileNumber << "/" << m_filesCount << "]: " << file.GetPath() + << " - " << (result ? "OK" : "ERROR")); + } + else + { + ScFile copiedFile = targetDirectory.CopyFile(file, "", true); + SC_LOG_INFO("Generated file: " << copiedFile.GetPath()); + } }); } bool ScSCnTex2SCsTranslator::TranslateFile( - std::string const & filePath, ScDirectory const & targetDirectory) + ScFile const & file, ScDirectory const & targetDirectory) { - ScFile targetFile = targetDirectory.CopyFile(filePath, ".scs"); + ScFile targetFile = targetDirectory.CopyFile(file, ".scs"); std::string scsText; try { - scsText = TranslateText(filePath); + scsText = TranslateText(file.GetPath()); } catch (std::exception const & e) { @@ -110,6 +116,7 @@ std::string ScSCnTex2SCsTranslator::TranslateText(std::string const & filePath) antlr4::CommonTokenStream tokens(&lexer); SCnTexParser parser(&tokens); + SCsHelper::SetCurrentFile(filePath); SCnTexParser::ScnTexTextContext * ctx = parser.scnTexText(); file.close(); return ctx->resultText; diff --git a/src/translator/sc_scn_tex2scs_translator.h b/src/translator/sc_scn_tex2scs_translator.h index 8a340aa..e0ff144 100644 --- a/src/translator/sc_scn_tex2scs_translator.h +++ b/src/translator/sc_scn_tex2scs_translator.h @@ -35,7 +35,7 @@ class ScSCnTex2SCsTranslator ScDirectory startDirectory, ScDirectory const & startTargetDirectory); - bool TranslateFile(std::string const & filePath, ScDirectory const & targetDirectory); + bool TranslateFile(ScFile const & file, ScDirectory const & targetDirectory); std::string TranslateText(std::string const & filePath);