Skip to content

Commit

Permalink
Merge pull request #355 from OnionGrief/dot2tex-fix-
Browse files Browse the repository at this point in the history
Dot2tex fix
  • Loading branch information
xendalm authored Jun 17, 2024
2 parents 99410d5 + 06f33f7 commit 42cb212
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 30 deletions.
43 changes: 31 additions & 12 deletions libs/AutomatonToImage/src/AutomatonToImage.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
#include <fstream>
#include <iostream>
#include <regex>
#include <sstream>
#include <vector>

#include "AutomatonToImage/AutomatonToImage.h"

using std::cout;
using std::ifstream;
using std::ofstream;
using std::string;
using std::stringstream;
using std::vector;

AutomatonToImage::AutomatonToImage() {}

AutomatonToImage::~AutomatonToImage() {}

string replace_before_dot2tex(const string& s) {
vector<std::pair<string, string>> substrs_to_replace = {{"\\^", "#^ "}, {"&", "#&"}};

string result = s;
for (const auto& [old_substr, new_substr] : substrs_to_replace) {
std::regex re(old_substr);
result = std::regex_replace(result, re, new_substr);
}

return result;
}

void write_to_file(const string& file_name, const string& content) {
ofstream file;
file.open(file_name, ofstream::trunc);
if (file.is_open())
file << replace_before_dot2tex(content);
file.close();
}

void remove_file(string dir, string file, bool guarded = false) {
stringstream command;
command << "cd " << dir;
Expand All @@ -32,13 +57,13 @@ void remove_file(string dir, string file, bool guarded = false) {
string AutomatonToImage::to_image(string automaton) {
remove_file("refal", "Meta_log.raux", true);
remove_file("refal", "Aux_input.raux", true);
FILE* fo;
fo = fopen("./refal/input.dot", "wt");
fprintf(fo, "%s", automaton.c_str());
fclose(fo);
ofstream fo;
write_to_file("./refal/input.dot", replace_before_dot2tex(automaton));
system("cd refal && refgo Preprocess+MathMode+FrameFormatter input.dot > "
"error_Preprocess.raux");

system("cd refal && dot2tex -ftikz -tmath \"Mod_input.dot\" > input.tex");

system("cd refal && refgo Postprocess+MathMode+FrameFormatter input.tex > "
"error_Postprocess.raux "
"2>&1");
Expand Down Expand Up @@ -67,16 +92,10 @@ string AutomatonToImage::to_image(string automaton) {

string AutomatonToImage::colorize(string automaton, string metadata) {

FILE* fo;
FILE* md;
ifstream infile_for_Final;
fo = fopen("./refal/Col_input.tex", "wt");
fprintf(fo, "%s", automaton.c_str());
fclose(fo);
write_to_file("./refal/Col_input.tex", automaton);
if (metadata != "") {
md = fopen("./refal/Meta_input.raux", "wt");
fprintf(md, "%s", metadata.c_str());
fclose(md);
write_to_file("./refal/Meta_input.raux", metadata);
system("cd refal && refgo Colorize+MathMode Col_input.tex > "
"error_Colorize.raux");
infile_for_Final.open("./refal/Final_input.tex");
Expand Down
12 changes: 3 additions & 9 deletions libs/Logger/src/LogTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ string LogTemplate::render() const {
automaton = std::get<FiniteAutomaton>(param.value).to_txt();
else
automaton = std::get<MemoryFiniteAutomaton>(param.value).to_txt();
automaton = replace_for_rendering(automaton);
size_t hash = hasher(automaton);
if (cache_automatons.count(hash) != 0) {
c_graph = cache_automatons[hash];
Expand Down Expand Up @@ -245,16 +244,11 @@ string LogTemplate::log_table(Table t) {
table += "$\\begin{array}{" + format + "}\\rowcolor{HeaderColor}\n";
table += cols + "\\hline\n";
for (int i = 0; i < t.rows.size(); i++) {
string r = t.rows[i] == " " ? "eps" : t.rows[i];
row = r + " & ";
row = t.rows[i] == " " ? "eps" : t.rows[i];
for (int j = 0; j < t.columns.size(); j++) {
if (j != t.columns.size() - 1) {
row = row + t.data[i * t.columns.size() + j] + " &";
} else {
row = row + t.data[i * t.columns.size() + j] + "\\\\";
}
row += " & " + replace_for_rendering(t.data[i * t.columns.size() + j]);
}
table += row + "\n";
table += row + "\\\\\n";
}
table += "\\end{array}$\n";
return table;
Expand Down
11 changes: 9 additions & 2 deletions refal/FrameFormatter.ref
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,14 @@ FindSeparator {
TakePrefix {
0 e.Rest = (e.Rest);
s.Size = ();
s.Length s.Sym e.Rest = s.Sym <TakePrefix <SafeSub s.Length <Weight (e.Rest) s.Sym>> e.Rest>;
s.Length s.Sym e.Rest
, <ASCIIStandard> :
{ e.A1 s.Sym e.A2 = s.Sym <TakePrefix <SafeSub s.Length <Weight (e.Rest) s.Sym>> e.Rest>;
e.A
, e.Rest : s.S1 e.Rest1
= s.Sym s.S1 <TakePrefix <SafeSub s.Length <Mul 2 <Weight (e.Rest) s.Sym>>> e.Rest1>;
e.A = s.Sym ();
};
}

Weight {
Expand Down Expand Up @@ -772,4 +779,4 @@ ChooseContent {
= <Putout 2 '\\vfill'><PutByLines Raw e.Preamble>
<Putout 2 '\\vspace*{-1ex}\n\\begin{center}\\adjustbox{max width=\\textwidth, min width=0.3\\textwidth, max height='<ToVal <Div <Mul s.DefHeight 100><FrameData MaxHeight>>>'\\textheight}{'>
<PutByLines Raw e.Content><Putout 2'}\\end{center}\n\\vspace*{-1ex}\\vfill'>;
}
}
24 changes: 17 additions & 7 deletions refal/MathMode.ref
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ $ENTRY Trim {
/* Hacked derivatives. TODO: parameterize by indices activation */
SpecialSymbolList {
= (NOREGEX ('eps')('\\empt'))(NOREGEX ('DERIV')('\\delta_'))(OK ('FIRST')('\\First'))(OK ('FOLLOW')('\\Follow'))(OK ('LAST')('\\Last'))
(OK ('UNION')('\\cup'))(OK ('\\{')('\\{'))(OK ('\\}')('\\}'))(OK ('{')('\\{'))(OK ('}')('\\}'))(OK ('[')('['))(OK (']')(']'))
(NOREGEX ('LANG')('\\Lang'))(OK ('\\&')('\\memref'))(NOREGEX ('\\textasciicircum')('\\mathbf{\\textasciicircum}\\hspace{-0.2ex}'))
(OK ('UNION')('\\cup'))(OK ('\\{')('\\{'))(OK ('\\}')('\\}'))(OK ('{')('\\{'))(OK ('}')('\\}'))(OK ('[[')('{'))(OK (']]')('}'))
(NOREGEX ('LANG')('\\Lang'))
(OK ('#&')('\\memref'))(OK ('\\&')('\\memref'))
(NOREGEX ('#^')('\\mathbf{\\textasciicircum}\\hspace{-0.2ex}'))
(NOREGEX ('\\textasciicircum')('\\mathbf{\\textasciicircum}\\hspace{-0.2ex}'))
(OK ('>->>')('\\transit'))(MEM ('MEMLOPEN')
('\\langle o:\\,'))(MEM ('MEMR')('\\rangle'))(MEM ('MEMLCLOSE')('\\langle c:\\,'));
}
Expand Down Expand Up @@ -43,10 +46,10 @@ SeparateModes {
, <InSet (<AnnoteSym><LinearizeSym>)(s.1)><InSet (<Numbers>)s.2> :
{True True
= <SeparateModes (e.SymbList) Normal (e.Prefix 'E') e.Rest>;
e.OtherValues = '\\regexpstr{'<MathMode e.Nempt>' }' e.Image<SeparateModes (e.SymbList) Normal () e.Rest>;
e.OtherValues = <WrapInRegexpstr <MathMode e.Nempt>> e.Image<SeparateModes (e.SymbList) Normal () e.Rest>;
};
= e.Image<SeparateModes (e.SymbList) Normal () e.Rest>;
e.Nempt = '\\regexpstr{'<MathMode e.Nempt>' }' e.Image<SeparateModes (e.SymbList) Normal () e.Rest>;
e.Nempt = <WrapInRegexpstr <MathMode e.Nempt>> e.Image<SeparateModes (e.SymbList) Normal () e.Rest>;
};
MEM = <SeparateModes (e.SymbList) Memory (e.Prefix e.Image) e.Rest>;
OK = <SeparateModes (e.SymbList) Normal (e.Prefix e.Image) e.Rest>;
Expand All @@ -57,7 +60,7 @@ SeparateModes {
False
, <Trim e.Prefix> :
{ = <SeparateModes (e.Spec) Text (s.Sym) e.Rest>;
e.Nempty = '\\regexpstr{'<MathMode e.Nempty>' }'<SeparateModes (e.Spec) Text (s.Sym) e.Rest>;
e.Nempty = <WrapInRegexpstr <MathMode e.Nempty>><SeparateModes (e.Spec) Text (s.Sym) e.Rest>;
};
};
(e.Spec) Text (e.Prefix) s.Sym e.Rest
Expand All @@ -75,7 +78,14 @@ SeparateModes {
};
(e.Spec) Text (e.Prefix) = '\\text{'e.Prefix' }';
(e.Spec) Normal ( ) = ;
(e.Spec) Normal (e.Prefix) = '\\regexpstr{'<MathMode e.Prefix>' }';
(e.Spec) Normal (e.Prefix) = <WrapInRegexpstr <MathMode e.Prefix>>;
}

WrapInRegexpstr {
e.x' & 'e.y = <WrapInRegexpstr e.x>' & '<WrapInRegexpstr e.y>;
e.x'\\\\'e.y = '\\regexpstr{'e.x' }\\\\'<WrapInRegexpstr e.y>;
= ;
e.y = '\\regexpstr{'e.y' }';
}

AdjustSpaceBeforeStar {
Expand Down Expand Up @@ -250,7 +260,7 @@ $ENTRY NonASCII {
}

$ENTRY ASCIIStandard {
= <Numbers><Letters>'$%&*()-_=+`~!@#;:\'\"<>,./?[]{}\\|^ \t\n';
= <Numbers><Letters>'$%&*()-_=+`~!@#;:\'\"<>,./?[]{}\\|^ \t\n\r';
}

$ENTRY Nempty {
Expand Down

0 comments on commit 42cb212

Please sign in to comment.