EquationTags: misc implementation improvements

master
Sébastien Villemot 2023-01-04 17:15:51 +01:00
parent 9658d82cc6
commit 3927862d23
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
2 changed files with 28 additions and 31 deletions

View File

@ -21,6 +21,7 @@
#include <regex>
#include <ostream>
#include <utility>
set<int>
EquationTags::getEqnsByKey(const string &key) const
@ -93,51 +94,46 @@ EquationTags::writeLatexOutput(ostream &output, int eqn) const
if (!exists(eqn))
return;
auto escape_special_latex_symbols
= [](string str)
{
const regex special_latex_chars (R"([&%$#_{}])");
const regex backslash (R"(\\)");
const regex tilde (R"(~)");
const regex carrot (R"(\^)");
const regex textbackslash (R"(\\textbackslash)");
str = regex_replace(str, backslash, R"(\textbackslash)");
str = regex_replace(str, special_latex_chars, R"(\$&)");
str = regex_replace(str, carrot, R"(\^{})");
str = regex_replace(str, tilde, R"(\textasciitilde{})");
return regex_replace(str, textbackslash, R"(\textbackslash{})");
};
auto escape_special_latex_symbols = [](string str)
{
const regex special_latex_chars (R"([&%$#_{}])");
const regex backslash (R"(\\)");
const regex tilde (R"(~)");
const regex carrot (R"(\^)");
const regex textbackslash (R"(\\textbackslash)");
str = regex_replace(str, backslash, R"(\textbackslash)");
str = regex_replace(str, special_latex_chars, R"(\$&)");
str = regex_replace(str, carrot, R"(\^{})");
str = regex_replace(str, tilde, R"(\textasciitilde{})");
return regex_replace(str, textbackslash, R"(\textbackslash{})");
};
bool wrote_eq_tag = false;
output << R"(\noindent[)";
for (const auto & [key, value] : eqn_tags.at(eqn))
for (bool wrote_eq_tag {false};
const auto & [key, value] : eqn_tags.at(eqn))
{
if (wrote_eq_tag)
if (exchange(wrote_eq_tag, true))
output << ", ";
output << escape_special_latex_symbols(key);
if (!value.empty())
output << "= `" << escape_special_latex_symbols(value) << "'";
wrote_eq_tag = true;
}
output << "]" << endl;
}
void
EquationTags::writeJsonAST(ostream &output, const int eqn) const
EquationTags::writeJsonAST(ostream &output, int eqn) const
{
if (!exists(eqn))
return;
output << R"(, "tags": {)";
bool wroteFirst = false;
for (const auto &[key, value] : eqn_tags.at(eqn))
for (bool wroteFirst {false};
const auto &[key, value] : eqn_tags.at(eqn))
{
if (wroteFirst)
if (exchange(wroteFirst, true))
output << ", ";
else
wroteFirst = true;
output << R"(")" << key << R"(": ")" << value << R"(")";
}
output << "}";

View File

@ -63,11 +63,11 @@ public:
//! Various functions to get info from equation tags
//! Get equation tags for a given equation
map<string, string>
getTagsByEqn(const int eqn) const
getTagsByEqn(int eqn) const
{
if (auto it = eqn_tags.find(eqn); it != eqn_tags.end())
return it->second;
return map<string, string>{};
return {};
}
//! Get equations that have the given key
@ -104,23 +104,24 @@ public:
}
bool
exists(const int eqn) const
exists(int eqn) const
{
return eqn_tags.contains(eqn);
}
//! Returns true if equation tag with key exists for a given equation
bool
exists(const int eqn, const string &key) const
exists(int eqn, const string &key) const
{
return exists(eqn) && eqn_tags.at(eqn).contains(key);
auto it = eqn_tags.find(eqn);
return it != eqn_tags.end() && it->second.contains(key);
}
//! Various functions to write equation tags
void writeCheckSumInfo(ostream &output) const;
void writeOutput(ostream &output) const;
void writeLatexOutput(ostream &output, int eqn) const;
void writeJsonAST(ostream &output, const int eq) const;
void writeJsonAST(ostream &output, int eq) const;
};
#endif