-
Notifications
You must be signed in to change notification settings - Fork 0
/
dollar2math.cpp
126 lines (113 loc) · 3.14 KB
/
dollar2math.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
static std::string::iterator find_next_real_doubledollar(
std::string::iterator it, std::string &filecontents) {
std::string::iterator ending_it = it;
while (ending_it != filecontents.end()) {
ending_it = std::find(ending_it, filecontents.end(), '$');
if (ending_it == filecontents.end()) {
break;
}
if (*(ending_it - 1) == '\\') {
++ending_it;
continue;
}
if ((ending_it + 1 == filecontents.end()) or (*(ending_it + 1) != '$')) {
++ending_it;
continue;
}
if (ending_it != filecontents.end()) {
break;
}
}
return ending_it;
}
static std::string::iterator
find_next_real_dollar(std::string::iterator it, std::string &filecontents) {
auto ending_it = it;
while (ending_it != filecontents.end()) {
ending_it = std::find(ending_it, filecontents.end(), '$');
if (ending_it == filecontents.begin()) {
break;
}
if (*(ending_it - 1) == '\\') {
++ending_it;
continue;
}
if (ending_it != filecontents.end()) {
break;
}
}
return ending_it;
}
namespace fs = std::filesystem;
int main(int argc, char **argv) {
if (argc != 2) {
std::cout << "You must provide a latex file" << std::endl;
return 1;
}
fs::path file = argv[1];
std::string filecontents;
{
std::ifstream t(file);
std::stringstream buffer;
buffer << t.rdbuf();
filecontents = buffer.str();
}
{
auto it = filecontents.begin();
while (it != filecontents.end()) {
it = find_next_real_doubledollar(it, filecontents);
if (it == filecontents.end()) {
break;
}
auto ending_it = find_next_real_doubledollar(it + 2, filecontents);
if (ending_it == filecontents.end()) {
std::cout << "There was an unmatched $$ in the file." << std::endl;
return 1;
}
*it = '\\';
*(it + 1) = '[';
*ending_it = '\\';
*(ending_it + 1) = ']';
it = ending_it + 1;
}
}
{
auto it = filecontents.begin();
while (it != filecontents.end()) {
it = find_next_real_dollar(it, filecontents);
if (it == filecontents.end()) {
break;
}
auto ending_it = find_next_real_dollar(it + 1, filecontents);
if (ending_it == filecontents.end()) {
std::cout << "There was an unmatched $ in the file." << std::endl;
return 1;
}
*ending_it = '\\';
auto ending_pos
= static_cast<size_t>(std::distance(filecontents.begin(), ending_it));
*it = '\\';
auto starting_pos
= static_cast<size_t>(std::distance(filecontents.begin(), it));
assert(*ending_it == filecontents[ending_pos]);
assert(*it == filecontents[starting_pos]);
filecontents.insert(ending_pos + 1, ")");
filecontents.insert(starting_pos + 1, "(");
it = filecontents.begin();
std::advance(it, ending_pos);
it = it + 3;
assert(*(it - 1) == *")");
assert(*(it - 2) == '\\');
}
}
std::cout << filecontents << std::endl;
}