-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.cpp
218 lines (198 loc) · 5.35 KB
/
converter.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "ast.h"
#include "converter.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int section_no = 0;
int subsection_no = 0;
string myString(int n){
stringstream ss;
ss<<n;
return ss.str();
}
string headTitle = "<head>\n<style>\ntable {\n\tborder-collapse: collapse;\n}\ntable, th, td {\n\tborder: 1px solid black;\n}\n</style>\n<title>Latex to HTML</title>\n</head>";
converter :: converter(){
myMapping[0] = "h1";
myMapping[1] = "h3";
myMapping[2] = "ul";
myMapping[3] = "ol";
myMapping[4] = "li";
myMapping[5] = "strong";
myMapping[6] = "em";
myMapping[7] = "u";
myMapping[8] = "br";
myMapping[9] = "a";
myMapping[10] = "a";
myMapping[11] = "table";
myMapping[12] = "figure";
myMapping[13] = "img";
myMapping[14] = "figcaption";
myMapping[15] = "";
myMapping[16] = "body";
myMapping[17] = "tr";
myMapping[18] = "td";
myMapping[19] = "";
myMapping[20] = "√";
myMapping[21] = "";
myMapping[22] = "∑";
myMapping[23] = "∫";
}
string converter :: traverseSection(ast_node * root, int type){
section_no++;
subsection_no = 0;
string s = "";
s+="<"+getMapping(type)+">";
s+=myString(section_no)+" "+root->data;
s+="</"+getMapping(type)+">";
s+=traverseChildren (root);
return s;
}
string converter :: traverseSubSection(ast_node * root, int type){
subsection_no++;
string s = "";
s+="<"+getMapping(type)+">";
s+=myString(section_no)+"."+myString(subsection_no)+" "+root->data;
s+="</"+getMapping(type)+"/>";
s+=traverseChildren(root);
return s;
}
string converter :: traverseList(ast_node * root, int type){
return traverseDefault(root,type);
}
string converter :: traverseFont(ast_node * root, int type){
return traverseDefault(root,type);
}
string converter :: traversePara(ast_node * root, int type){
string s = "";
s+="<"+getMapping(type)+">";
s+=" ";
s+=traverseChildren(root);
return s;
}
string converter :: traverseAnchor(ast_node * root, int type){
string s = "";
if(type == 9){
//label
s += "<a name=\"" + root->data + "\"/>";
}
else{
//ref
s += "<a href=\"#" + root->data + "\">" + root->data + "</a>";
}
s += traverseChildren(root);
return s;
}
string converter :: traverseTable(ast_node * root, int type){
return traverseDefault(root,type);
}
string converter :: traverseImage(ast_node * root, int type){
string s = "";
if(type == 13){
s+="<"+ getMapping(type)+" src = "+"\""+root->data+"\" alt = \"Image Here\" "+root->attributes+">";
s+=traverseChildren(root);
}
else{
s+=traverseDefault(root,type);
}
return s;
}
string converter :: traverseContent(ast_node * root, int type){
return root->data;
}
string converter :: traverseMath(ast_node *root, int type){
string s = "";
if(type == 22 || type == 23){
s+="<sub>" + root->data + "</sub>" + getMapping(type) + "<sup>" + root->attributes + "</sup>";
}
else if(type == 20){
s+=getMapping(type)+root->data;
}
else if(type == 21){
s+="("+root->data+")/("+root->attributes+")";
}
s+=traverseChildren(root);
return s;
}
string converter :: traverseDefault(ast_node * root, int type){
string s = "";
s+="<"+getMapping(type)+">";
s+=traverseChildren(root);
s+="</"+getMapping(type)+">";
return s;
}
string converter :: traverseChildren(ast_node * root){
string s = "";
for(int i = 0; i<root->children.size(); i++){
s+=traversal(root->children[i]);
}
return s;
}
string converter :: traversal(ast_node *root){
string s = "";
if(root){
int type = root->node_type;
string attributes = root->attributes;
string data = root->data;
switch (type) {
case 0:
s+= traverseSection(root,type);
break;
case 1:
s+=traverseSubSection(root,type);
break;
case 2:
case 3:
case 4:
s+=traverseList(root,type);
break;
case 5:
case 6:
case 7:
s+=traverseFont(root,type);
break;
case 8:
s+=traversePara(root,type);
break;
case 9:
case 10:
s+=traverseAnchor(root,type);
break;
case 11:
case 17:
case 18:
s+=traverseTable(root,type);
break;
case 12:
case 13:
case 14:
s+=traverseImage(root,type);
break;
case 15:
s+=traverseContent(root,type);
break;
case 19:
case 20:
case 21:
case 22:
case 23:
s+=traverseMath(root,type);
break;
default:
s+=traverseDefault(root,type);
}
}
return s;
}
string converter :: getMapping(int type){
return myMapping[type];
}
void converter :: printHTML(string s){
ofstream cout("output.html");
cout<<"<!DOCTYPE html>\n";
cout<<"<html>\n";
cout<<headTitle;
cout<<s;
cout<<"\n</html>";
}