-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparagraph2itemize.cpp
121 lines (103 loc) · 3.98 KB
/
paragraph2itemize.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
// This is paragraph2itemize.cpp of https://github.com/Willie169/LaTeX-ToolKit, licensed under either GPL-3.0-or-later or CC BY-SA 4.0-or-later.
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <sstream>
bool startsWith(const std::string& str, const std::string& prefix) {
return str.substr(0, prefix.length()) == prefix;
}
int countLeadingWhitespace(const std::string& str) {
int count = 0;
while (count < str.length() && (str[count] == ' ' || str[count] == '\t')) {
count++;
}
return count;
}
std::string getIndent(int level) {
return std::string(level * 2, ' ');
}
class LatexTransformer {
private:
int currentLevel = 0;
std::stack<int> itemizeLevels;
bool isEndingCommand(const std::string& line) {
return startsWith(line, "\\part") ||
startsWith(line, "\\chapter") ||
startsWith(line, "\\section") ||
startsWith(line, "\\subsection") ||
startsWith(line, "\\subsubsection");
}
void closeItemizeLevels(std::stringstream& output, int targetLevel = 0) {
while (!itemizeLevels.empty() && itemizeLevels.top() > targetLevel) {
output << getIndent(std::max(itemizeLevels.top() - 2, 0)) << "\\end{itemize}\n";
itemizeLevels.pop();
}
}
std::string extractContent(const std::string& line) {
size_t start = line.find('{');
size_t end = line.rfind('}');
if (start != std::string::npos && end != std::string::npos) {
return line.substr(start + 1, end - start - 1);
}
return "";
}
public:
std::string transform(const std::string& input) {
std::stringstream output;
std::istringstream iss(input);
std::string line;
while (std::getline(iss, line)) {
int indent = countLeadingWhitespace(line);
line = line.substr(indent);
if (isEndingCommand(line)) {
closeItemizeLevels(output);
output << std::string(indent, ' ') << line << '\n';
continue;
}
if (startsWith(line, "\\paragraph{")) {
closeItemizeLevels(output, 1);
if (itemizeLevels.empty()) {
output << std::string(indent, ' ') << "\\begin{itemize}\n";
itemizeLevels.push(1);
}
output << std::string(indent, ' ') << getIndent(1) << "\\item "
<< extractContent(line) << '\n';
}
else if (startsWith(line, "\\subparagraph{")) {
closeItemizeLevels(output, 2);
if (itemizeLevels.empty() || itemizeLevels.top() < 2) {
output << std::string(indent, ' ') << getIndent(1) << "\\begin{itemize}\n";
itemizeLevels.push(2);
}
output << std::string(indent, ' ') << getIndent(2) << "\\item "
<< extractContent(line) << '\n';
}
else if (startsWith(line, "\\subsubparagraph{")) {
closeItemizeLevels(output, 3);
if (itemizeLevels.empty() || itemizeLevels.top() < 3) {
output << std::string(indent, ' ') << getIndent(2) << "\\begin{itemize}\n";
itemizeLevels.push(3);
}
output << std::string(indent, ' ') << getIndent(3) << "\\item "
<< extractContent(line) << '\n';
}
else {
output << std::string(indent, ' ') << getIndent(itemizeLevels.empty() ? 0 : itemizeLevels.top())
<< line << '\n';
}
}
closeItemizeLevels(output);
return output.str();
}
};
int main() {
std::string input;
std::string line;
while (std::getline(std::cin, line)) {
input += line + '\n';
}
LatexTransformer transformer;
std::cout << transformer.transform(input);
return 0;
}