-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnofear2tex.cc
428 lines (397 loc) · 10.4 KB
/
nofear2tex.cc
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Convert NoFear HTMLs to LaTeX long tables
#include <cctype>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>
#include <unordered_set>
#include <libxml/HTMLparser.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
using namespace std;
typedef unordered_set<string> sets_t;
sets_t tags_seen;
typedef vector<string> vs_t;
typedef unordered_set<string> sets_t;
class Property
{
public:
string key;
vs_t values;
};
typedef vector<Property> vproperty_t;
class Classes
{
public:
Classes(const vs_t& _n=vs_t(), const sets_t& _c=sets_t()) :
node(_n), context(_c) {}
vs_t node;
sets_t context;
};
typedef vector<Classes> vclasses_t;
void props_print(const vproperty_t& props)
{
for (const Property& p: props)
{
cerr << " " << p.key << ":";
for (const string& v: p.values) { cerr << ' ' << v; }
cerr << '\n';
}
}
vs_t& string_split(vs_t& result, const string& s)
{
result.clear();
istringstream iss(s);
copy(istream_iterator<string>(iss), istream_iterator<string>(),
back_inserter(result));
return result;
}
void props_get_classes(const vproperty_t& props, vs_t& classes)
{
static const string s_class("class");
classes.clear();
for (const Property& prop: props)
{
if (prop.key == s_class)
{
for (const string &v: prop.values)
{
vs_t vclasses;
string_split(vclasses, v);
classes.insert(classes.end(), vclasses.begin(), vclasses.end());
}
}
}
}
void sets_add(sets_t& sets, const vs_t &vs)
{
for (const string& s: vs)
{
sets.insert(sets.end(), s);
}
}
string xmlc2str(const xmlChar* x)
{
string s((const char*)(x));
return s;
}
string strip_old(const xmlChar* x)
{
const char *p = (const char*)(x);
char c;
while (((c = *p) != '\0') && isspace(c))
{
++p;
}
int sz = strlen(p);
while ((sz > 0) && isspace(p[sz - 1]))
{
--sz;
}
string s(p, sz);
return s;
}
string strip(const xmlChar* x)
{
string s((const char*)(x));
int sz = s.size();
int ltrim = 0;
while ((ltrim < sz) && (isspace(s[ltrim])))
{
++ltrim;
}
while ((sz > ltrim) && isspace(s[sz - 1]))
{
--sz;
}
string sws = s.substr(ltrim, sz - ltrim);
string ret;
for (char c: sws)
{
if ((c == L'’') || ((unsigned char)(c) == 0xe2))
{
ret.push_back('\'');
}
else if ((0 < c) && (c < 0x7f))
{
ret.push_back(c);
}
}
return ret;
}
class GState
{
public:
GState() : html(false) {}
bool html;
string act_x_scene_y;
};
class State
{
public:
State(GState& _gstate) :
gstate(_gstate),
in_table(false),
in_div(false),
line_number(false),
td(-1)
{
}
GState& gstate;
bool in_table;
bool in_div;
bool line_number;
int td;
string td_text;
vclasses_t classes;
};
void node_get_properties(vproperty_t& props, const xmlNodePtr p)
{
props.clear();
for (xmlAttrPtr a = p->properties; a; a = a->next)
{
Property property;
property.key = xmlc2str(a->name);
for (xmlNodePtr v = a->children; v; v = v->next)
{
property.values.push_back(xmlc2str(v->content));
}
props.push_back(property);
}
}
void node_traverse(State& state, const xmlNodePtr p, size_t depth)
{
static const string s_class("class");
static const string s_div("div");
static const string s_i("i");
static const string s_meta("meta");
static const string s_p("p");
static const string s_scan("scan");
static const string s_stage("noFear__stage");
static const string s_table("table");
static const string s_td("td");
static const string s_text("text");
static const string s_tooltip_head("noFear__tooltip__popup__heading");
static const string s_tooltip_cooy("noFear__tooltip__popup__copy");
static const string s_tr("tr");
static const string
interior_header("interior-header__title__text__pagetitle");
const string indent = string(2 * depth, ' ');
const string tag((const char*)(p->name));
if (false) { cerr << indent << tag << ", type=" << p->type << '\n'; }
vproperty_t props;
node_get_properties(props, p);
vs_t classes;
props_get_classes(props, classes);
sets_t context = (depth == 0 ? sets_t() : state.classes.back().context);
sets_add(context, classes);
state.classes.push_back(Classes(classes, context));
const bool is_stage = (tag == s_div) &&
(context.find(s_stage) != context.end());
const bool is_tooltip =
(context.find(s_tooltip_head) != context.end()) ||
(context.find(s_tooltip_cooy) != context.end());
const bool italize = (tag == s_i);
const bool emphasize = state.in_table && (is_stage || italize);
if (tag == s_meta)
{
;
// cerr << "meta\n";
// props_print(props);
}
if (tag == s_table)
{
state.in_table = true;
// props_print(props);
cout << "\\begin{longtable}{"
"p{.50\\textwidth} p{0.04\\textwidth} p{.40\\textwidth} }\n";
}
if (tag == s_td)
{
state.td = -1;
// props_print(props);
state.td_text.clear();
const Property& prop = props[0];
if (prop.key == "class")
{
static const string orig("noFear__cell noFear__cell--original");
static const string modern("noFear__cell noFear__cell--modern");
const string v = prop.values[0];
if (v == orig)
{
state.td = 0;
}
else if (v == modern)
{
state.td = 1;
}
}
}
if (emphasize)
{
state.td_text += "\\emph{";
}
if ((tag == s_text) && !is_tooltip)
{
if (state.td >= 0)
{
static const string s_number("noFear__number");
static const string s_speaker("noFear__speaker");
const string content = strip(p->content);
if (context.find(s_number) != context.end())
{
// '\\hspace*{-%s}\hbox to %s{\\texttt{%s}}'
static const string width("20pt");
static const string hsapce_b =
string("\\hspace*{-") +
width +
string("}\\hbox to ") +
width +
string("{\\texttt{");
static const string hspace_e("}}");
state.td_text += hsapce_b + content + hspace_e;
}
else if (!content.empty())
{
if ((!state.td_text.empty()) && (!isspace(state.td_text.back())))
{
state.td_text += ' ';
}
if (context.find(s_speaker) != context.end())
{
state.td_text +=
string("\\textbf{") + content + string("}");
}
else
{
state.td_text += content;
}
}
}
}
if ((tag[0] == 'h') && (props.size() == 1))
{
const Property& prop = props[0];
if ((prop.key == s_class) && (prop.values[0] == interior_header))
{
// cerr << "interior_header: " << prop.values[0] << '\n';
string header = strip(p->children->content);
if (state.gstate.act_x_scene_y != header)
{
state.gstate.act_x_scene_y = header;
cout << "\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
"\\subsection{" << header << "}\n\n";
}
}
}
for (xmlNodePtr c = p->children; c; c = c->next)
{
node_traverse(state, c, depth + 1);
}
static const string newline_nl("\\newline\n");
static const string newline_nl_rb = newline_nl + string("}");
static const size_t newline_nl_sz = newline_nl.size();
if ((tag == s_div) || (tag == s_p))
{
if (state.in_table && !is_tooltip)
{
state.td_text += newline_nl;
}
}
if (emphasize)
{
state.td_text += "}";
}
if ((tag == s_td) && (state.td >= 0))
{
if (state.td > 0) { cout << "& &\n"; }
size_t sz = state.td_text.size();
if ((newline_nl_sz < sz) &&
(state.td_text.substr(sz - newline_nl_sz) == newline_nl))
{
state.td_text.erase(sz - newline_nl_sz);
}
else if ((newline_nl_sz + 1 < sz) &&
(state.td_text.substr(sz - (newline_nl_sz + 1)) == newline_nl_rb))
{
state.td_text.erase(sz - (newline_nl_sz + 1));
state.td_text.push_back('}');
}
cout << state.td_text;
state.td = -1;
}
if (tag == s_tr)
{
cout << "\n\\\\[6pt]\n";
}
if (tag == s_table)
{
cout << "\n\\end{longtable}\n";
state.in_table = false;
}
state.classes.pop_back();
}
void traverse(GState& gstate, const htmlDocPtr p)
{
State state(gstate);
for (xmlNodePtr c = p->children; c; c = c->next)
{
node_traverse(state, c, 0);
}
}
int nofear_to_latex(GState &gstate, const char *fn)
{
int rc = 0;
int options =
XML_PARSE_RECOVER |
XML_PARSE_NOERROR |
XML_PARSE_NOWARNING;
xmlDoc *p = xmlReadFile(fn, nullptr, options);
if (p)
{
traverse(gstate, p);
xmlFreeDoc(p);
}
else
{
cerr << "Failed to parse " << fn << '\n';
rc = 1;
}
return rc;
}
int html_nofear_to_latex(GState &gstate, const char *fn)
{
int rc = 0;
htmlDocPtr p = htmlParseFile(fn, "utf-8");
if (p)
{
traverse(gstate, p);
// htmlFreeParserCtxt(p);
}
else
{
cerr << "Failed to parse " << fn << '\n';
rc = 1;
}
return rc;
}
int main(int argc, char **argv)
{
int rc = 0;
GState gstate;
int ai = 1;
if ((argc > 1) && (string(argv[1]) == string("-html")))
{
gstate.html = true;
ai = 2;
}
for (; (rc == 0) && (ai < argc); ++ai)
{
rc = (gstate.html
? html_nofear_to_latex(gstate, argv[ai])
: nofear_to_latex(gstate, argv[ai]));
}
return rc;
}