-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode2tex.cpp
137 lines (104 loc) · 3.44 KB
/
code2tex.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
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <vector>
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <regex>
using namespace std;
string getextension(string);
string extractquestion(string *);
int main(int argc, char** argv) {
cout << "target folder: " << argv[1] << endl;
cout << "assignment title: " << argv[2] << endl;
cout << "author: " << argv[3] << endl;
cout << "author detail #1: " << argv[4] << endl;
cout << "author detail #2: " << argv[5] << endl;
cout << "organization: " << argv[6] << endl;
vector<string> fileorder;
ifstream fin;
ofstream fout;
string line, tmp("");
string targetfolder = argv[1];
fin.open(targetfolder + "order.txt");
if (fin.fail()) {
cerr << "\033[31m" << "Unable to open " << targetfolder << "order.txt" << "\033[0m" << endl;
exit(11);
}
while(getline(fin, line)) {
fileorder.push_back(line);
}
fin.close();
cout << "read order processed from " << targetfolder << "order.txt" << endl;
fout.open(targetfolder + "generated.tex");
cout << "writing to " << targetfolder << "generated.tex" << endl;
fout << "\\documentclass[12pt]{exam}" << endl;
fout << "\\usepackage{geometry}" << endl;
fout << "\\geometry{a4paper, margin=1in}" << endl;
fout << "\\usepackage[utf8]{inputenc}" << endl << endl;
fout << "\\usepackage{minted}" << endl;
fout << "\\pagestyle{headandfoot}" << endl;
fout << "\\runningheader{" << argv[3] << "}{" << argv[2] << "}{Page \\thepage}" << endl;
fout << "\\firstpagefooter{}{}{}" << endl;
fout << "\\runningfooter{}{}{}" << endl;
fout << "\\runningheadrule" << endl;
fout << "\\title{" << argv[2] << "}" << endl;
fout << "\\author{" << argv[3] << "\\\\" << endl
<< argv[4] << "\\\\" << endl
<< argv[5] << "\\\\" << endl
<< "\\textsc{" << argv[6] << "}}" << endl;
fout << "\\date{\\today}" << endl;
fout << "\\begin{document}" << endl;
fout << "\\maketitle" << endl;
fout << "\\pagenumbering{arabic}" << endl;
fout << "\\begin{questions}" << endl;
for (auto filename = fileorder.begin(); filename != fileorder.end(); filename++) {
ifstream qin;
qin.open(targetfolder + *filename);
if (qin.fail()) {
cerr << "\033[31m" << "unable to open filename:" << targetfolder
<< *filename << "\033[31m" <<endl;
exit(10);
}
while (getline(qin,line)) {
tmp.append(line).append("\n");
}
fout << "\\question" << endl << extractquestion(&tmp) << endl;
fout << "\\begin{minted}[frame=lines, linenos]{" << getextension(*filename) << "}" << endl;
fout << tmp << endl;
qin.close();
cout << "adding code snippet from " << targetfolder + *filename << endl;
fout << "\\end{minted}" << endl;
fout << "\\newpage" << endl;
tmp = "";
}
fout << "\\end{questions}" << endl;
fout << "\\thanks" << endl;
fout << "\\end{document}";
fout.close();
cout << "TeX file created!" << endl;
return 0;
}
string getextension(string filename) {
vector<string> tokens;
boost::split(tokens, filename, boost::is_any_of("."));
return tokens.back();
}
string extractquestion(string *content) {
smatch match;
// thank you stack overflow
regex re("^/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/");
if(regex_search(*content,match,re)) {
string str = match.str(0);
content->erase(0, str.size());
size_t start = str.find("/*");
if (start != string::npos) {
str.replace(start, 2, "");
}
start = str.find("*/");
if (start != string::npos) {
str.replace(start, 2, "");
}
return str;
}
else
return "Question couldn't be parsed. Edit the tex file manually.";
}