Skip to content

Commit

Permalink
fix: images parsing and copying
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaZotov committed Dec 3, 2023
1 parent bc37da7 commit d94e038
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 36 deletions.
1 change: 0 additions & 1 deletion src/translator/commands/format/sc_scn_tex_cite_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ ScScnTexCommandResult ScSCnTexCiteCommand::Complete(
ScSCnPrefixTree & tree,
ScScnTexCommandParams const & params)
{
std::cout << params.at(1);
return params.at(1);
}
31 changes: 18 additions & 13 deletions src/translator/filesystem/sc_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 12 additions & 7 deletions src/translator/filesystem/sc_file_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<std::string> const & extensions) const
{
return extensions.find(m_path.substr(m_path.rfind('.'))) != extensions.cend();
return extensions.find(GetExtension()) != extensions.cend();
}

private:
Expand All @@ -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;

Expand Down
18 changes: 16 additions & 2 deletions src/translator/helper/scs_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions src/translator/helper/scs_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
};


27 changes: 17 additions & 10 deletions src/translator/sc_scn_tex2scs_translator.cpp
Original file line number Diff line number Diff line change
@@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/translator/sc_scn_tex2scs_translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit d94e038

Please sign in to comment.