This repository was archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhsc.cpp
More file actions
429 lines (418 loc) · 15.9 KB
/
hsc.cpp
File metadata and controls
429 lines (418 loc) · 15.9 KB
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
429
/**
* @file hsc.cpp Homework Showing Compiler
* @version 0.4.0 (Beta)
* @author Jason M. Li
* @date 2022.10.15
*/
#include <cstdio>
#include <iostream>
#include <fstream>
#include <stack>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <ctime>
#define LINE_BUF 4096
using std::string;
enum class SP_CH : short {
META_DATE = 0, // 日期声明开始
META_CREATED, // 创建声明开始
META_MODIFIED, // 更正声明开始
META_MODIFIED_COLOR, // 更正声明[参数]颜色开始
START_SUBJECT, // 学科声明开始
START_NOTE, // 注释部分开始
START_ITEM, // 创建项目开始
START_HYPERLINK, // 超链接开始
START_HYPERLINK_TEXT, // 超链接文本开始(若指定文本)
START_EMPHASIZE, // 突出开始
START_EMPHASIZE_TEXT, // 突出文本开始(若指定版本)
START_NOTE_SYMBOL, // 注释提示开始
START_NOTE_SYMBOL_TEXT, // 注释文本开始(若指定提前或延后)
START_ESCAPE, // 转义开始
ROOT // 根元素/占位符
};
struct Tag {
SP_CH sp_ch = SP_CH::ROOT;
string temp_str = "";
int temp_int = 0;
Tag(SP_CH _sp_ch) {
this->sp_ch = _sp_ch;
}
};
struct Modification {
string bg_color = "yellow";
string modified_time = "--:--";
Modification() {}
Modification(string _bg_color, string _modified_time) {
this->bg_color = _bg_color;
this->modified_time = _modified_time;
}
};
struct Item {
string text;
int number;
Item() {}
Item(string _text) {
this->text = _text;
}
Item(string _text, int _number) {
this->text = _text;
this->number = _number;
}
};
struct Subject {
string name;
std::vector<Item> items;
Subject() {}
Subject(string _name) {
this->name = _name;
}
};
struct OutputFile {
string date = "--------";
string created_time = "--:--";
string published_time = "--:--:--";
std::vector<Modification> modified_times;
std::vector<Subject> subjects;
OutputFile() {}
};
string add_label(string text, string tagName) {
return '<' + tagName + '>' + text + "</" + tagName + '>';
}
string add_label(string text, string tagName, std::map<string, string> attrs) {
string result = '<' + tagName;
auto attr = attrs.begin();
for (; attr != attrs.end(); attr++) {
string temp_attr = ' ' + attr->first + "=\"" + attr->second + '"';
result += temp_attr;
}
result += ">";
result += text;
result += "</" + tagName + '>';
return result;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("Error 0x0001: No File Input.\n");
return 0x0001;
}
string filename(argv[1]);
std::ifstream reader;
std::ofstream writer;
reader.open(filename);
if (!reader.is_open()) {
printf("Error 0x0010: Read File Failed.\n");
return 0x0010;
}
std::stack<Tag> tags;
tags.push(Tag(SP_CH::ROOT));
tags.push(Tag(SP_CH::ROOT)); // 根元素,防溢出
OutputFile outfile;
int note_counter = 0,
number_counter = 0,
line_counter = 0;
char line_buffer[LINE_BUF];
string str_buffer;
int line_len;
while (reader.getline(line_buffer, LINE_BUF)) {
line_len = strlen(line_buffer);
str_buffer = line_buffer;
// 监测是否为行级元素
{
char first_ch = str_buffer[0];
if (first_ch == '+') {
string next_five_ch = str_buffer.substr(1, 5);
if (next_five_ch.compare("date ") == 0) {
tags.push(Tag(SP_CH::META_DATE));
}
else if (next_five_ch.compare("crea ") == 0) {
tags.push(Tag(SP_CH::META_CREATED));
}
else if (next_five_ch.compare("modi ") == 0) {
tags.push(Tag(SP_CH::META_MODIFIED));
}
str_buffer = str_buffer.substr(6);
line_len -= 6;
}
else if (first_ch == '-') {
if (str_buffer[1] == ' ') {
tags.push(Tag(SP_CH::START_ITEM));
str_buffer = str_buffer.substr(2);
line_len -= 2;
}
else if (str_buffer.substr(1, 2).compare("--") == 0) {
tags.push(Tag(SP_CH::START_NOTE));
str_buffer = "";
line_len = 0;
}
}
else if (first_ch == '#') {
tags.push(Tag(SP_CH::START_SUBJECT));
int start = 1;
if (str_buffer[1] == ' ') start = 2;
str_buffer = str_buffer.substr(start);
line_len -= start;
}
}
// 行内元素监测
for (int i = 0; i < line_len; i++) {
unsigned char ch = str_buffer[i];
int tag_size = tags.size();
SP_CH top_sp_ch = tags.top().sp_ch;
if (tag_size == 0) {
printf("ERROR 0x0020: no tags left.\n");
return 0x0020;
}
if (tag_size == 1) {
printf("WARNING: The last tag is ROOT tag.\n");
}
// 转义
if (top_sp_ch == SP_CH::START_ESCAPE) {
tags.pop();
if (ch == 'n') tags.top().temp_str += '\n';
else if (ch == 't') tags.top().temp_str += '\t';
else tags.top().temp_str += ch;
}
else if (ch == '\\' && top_sp_ch != SP_CH::START_EMPHASIZE) {
tags.push(Tag(SP_CH::START_ESCAPE));
}
// 注释
else if (ch == '<' && i < line_len - 1 && str_buffer[i + 1] == '?') {
tags.push(Tag(SP_CH::START_NOTE_SYMBOL));
++i;
}
else if ((ch == '+' || ch == '-') && top_sp_ch == SP_CH::START_NOTE_SYMBOL) {
tags.top().temp_int = 44 - ch;
tags.push(Tag(SP_CH::START_NOTE_SYMBOL_TEXT));
}
else if (ch >= '0' && ch <= '9' && top_sp_ch == SP_CH::START_NOTE_SYMBOL_TEXT) {
tags.top().temp_str += ch;
}
else if (ch == '>' && top_sp_ch == SP_CH::START_NOTE_SYMBOL) {
note_counter += 1;
tags.pop();
tags.top().temp_str += add_label("[注" + std::to_string(note_counter) + ']', "span", { {"class", "note"} });
}
else if (ch == '>' && top_sp_ch == SP_CH::START_NOTE_SYMBOL_TEXT) {
note_counter += 1;
int note_number = atoi(tags.top().temp_str.c_str());
tags.pop();
note_number *= tags.top().temp_int;
note_number += note_counter;
tags.pop();
tags.top().temp_str += add_label("[注" + std::to_string(note_number) + ']', "span", { {"class", "note"} });
note_counter -= 1;
}
// 强调
else if (ch == '*' && top_sp_ch != SP_CH::START_EMPHASIZE_TEXT) {
if (i < line_len - 1 && str_buffer[i + 1] == ':') {
tags.push(Tag(SP_CH::START_EMPHASIZE));
++i;
}
else {
tags.push(Tag(SP_CH::START_EMPHASIZE));
tags.push(Tag(SP_CH::START_EMPHASIZE_TEXT));
}
}
else if (ch >= '0' && ch <= '9' && top_sp_ch == SP_CH::START_EMPHASIZE) {
tags.top().temp_str += ch;
}
else if (ch == ':' && top_sp_ch == SP_CH::START_EMPHASIZE) {
int version = atoi(tags.top().temp_str.c_str());
tags.top().temp_int = version;
tags.top().temp_str.clear();
tags.push(Tag(SP_CH::START_EMPHASIZE_TEXT));
}
else if (ch == '*' && top_sp_ch == SP_CH::START_EMPHASIZE_TEXT) {
string text = tags.top().temp_str;
tags.pop();
int version = tags.top().temp_int;
tags.pop();
if (version == 0 || outfile.modified_times.size() < version) {
tags.top().temp_str += add_label(text, "strong");
}
else {
tags.top().temp_str += add_label(text, "strong", { {
"style", "--bgc: " + outfile.modified_times[version - 1].bg_color
} });
}
}
// 超链接
else if (ch == '[') {
tags.push(Tag(SP_CH::START_HYPERLINK));
}
else if (ch == ']' && top_sp_ch == SP_CH::START_HYPERLINK) {
tags.top().temp_int = 1;
}
else if (ch == '(' && top_sp_ch == SP_CH::START_HYPERLINK && tags.top().temp_int == 1) {
tags.push(Tag(SP_CH::START_HYPERLINK_TEXT));
}
else if (ch == ')' && top_sp_ch == SP_CH::START_HYPERLINK_TEXT) {
string text = tags.top().temp_str;
tags.pop();
string url = tags.top().temp_str;
tags.pop();
if (text.size() == 0) {
text = url;
}
tags.top().temp_str += add_label(text, "a", { {"href", url} });
}
// 空格分割的数据声明
else if (ch == ' ' && top_sp_ch == SP_CH::META_MODIFIED) {
tags.push(Tag(SP_CH::META_MODIFIED_COLOR));
}
else {
tags.top().temp_str += ch;
}
}
while (tags.top().sp_ch >= SP_CH::START_HYPERLINK && tags.top().sp_ch <= SP_CH::START_ESCAPE) {
// 过滤堆栈未使用内容
string temp_str = tags.top().temp_str;
int temp_int = tags.top().temp_int;
SP_CH top_sp_ch = tags.top().sp_ch;
tags.pop();
switch (top_sp_ch) {
case (SP_CH::START_EMPHASIZE):
tags.top().temp_str += "*:";
break;
case (SP_CH::START_EMPHASIZE_TEXT):
if (tags.top().temp_int != 0) {
int temp_ver = tags.top().temp_int;
tags.pop();
tags.top().temp_str += "*:";
tags.top().temp_str += std::to_string(temp_ver);
tags.top().temp_str += ':';
}
else {
tags.pop();
tags.top().temp_str += '*';
}
break;
case (SP_CH::START_ESCAPE):
tags.top().temp_str += '\\';
break;
case (SP_CH::START_HYPERLINK):
tags.top().temp_str += '[';
if (temp_int == 1) {
tags.top().temp_str += temp_str;
tags.top().temp_str += ']';
temp_str.clear();
}
break;
case (SP_CH::START_HYPERLINK_TEXT):
{
Tag temp_tag = tags.top();
tags.pop();
tags.top().temp_str += '[';
tags.top().temp_str += temp_tag.temp_str;
tags.top().temp_str += "](";
temp_str.clear();
}
break;
case (SP_CH::START_NOTE_SYMBOL):
tags.top().temp_str += "<?";
break;
case (SP_CH::START_NOTE_SYMBOL_TEXT):
tags.pop();
tags.top().temp_str += "<?";
tags.top().temp_str += (char)(44 + temp_int);
break;
}
tags.top().temp_str += temp_str;
}
{
// 监测堆栈/最近的行级项
SP_CH top_sp_ch = tags.top().sp_ch;
if (top_sp_ch == SP_CH::META_DATE) {
outfile.date = tags.top().temp_str;
tags.pop();
}
else if (top_sp_ch == SP_CH::META_CREATED) {
outfile.created_time = tags.top().temp_str;
tags.pop();
}
else if (top_sp_ch == SP_CH::META_MODIFIED) {
outfile.modified_times.push_back(Modification("yellow", tags.top().temp_str));
tags.pop();
}
else if (top_sp_ch == SP_CH::META_MODIFIED_COLOR) {
string color = tags.top().temp_str;
tags.pop();
outfile.modified_times.push_back(Modification(color, tags.top().temp_str));
tags.pop();
}
else if (top_sp_ch == SP_CH::START_NOTE) {
number_counter = 0; // 复位
outfile.subjects.push_back(Subject(add_label("备注", "span", { {"class", "note-part"} })));
}
else if (top_sp_ch == SP_CH::START_SUBJECT) {
outfile.subjects.push_back(Subject(tags.top().temp_str));
tags.pop();
}
else if (top_sp_ch == SP_CH::START_ITEM) {
number_counter += 1;
outfile.subjects.back().items.push_back(Item(tags.top().temp_str, number_counter));
tags.pop();
}
}
line_counter += 1;
}
reader.close();
writer.open(filename.substr(0, filename.size() - 4) + ".html");
if (!writer.is_open()) {
printf("Error 0x0011: Write File Failed.\n");
return 0x0011;
}
{
// 确定发布时间
time_t t = time(NULL);
struct tm* local = localtime(&t);
char temp_str[64] = { "\0" };
strftime(temp_str, sizeof(temp_str), "%m-%d %H:%M:%S", local);
outfile.published_time = temp_str;
}
{
// 写文件
writer << "<!DOCTYPE html>" << std::endl << "<html lang='zh-CN'><head><meta charset='UTF-8'><meta name='generator' content='HSC'><meta name='robots' content='noindex'><title>作业</title><link rel='shortcut icon' href='favicon.ico' type='image/x-icon'><link rel='stylesheet' href='style.css'></head>" << std::endl << "<body>";
string published_note = add_label("编辑于 " + outfile.published_time, "span");
writer << published_note << std::endl;
string table = "<thead><tr><td colspan='3'>" + outfile.date + "作业</td></tr><tr><td>科目</td><td>序号</td><td>项目</td></tr></thead>";
string tbody = "";
int subject_length = outfile.subjects.size();
for (int i = 0; i < subject_length; i++) {
Subject subject = outfile.subjects[i];
int item_length = subject.items.size();
Item item;
if (item_length != 0) {
item = subject.items[0];
string td1 = add_label(subject.name, "td", { {"rowspan", std::to_string(item_length)} }),
td2 = add_label(std::to_string(item.number), "td"),
td3 = add_label(item.text, "td");
tbody += add_label(td1 + td2 + td3, "tr", { {"class", "ll"} });
// ll: Long Line
}
for (int j = 1; j < item_length; j++) {
item = subject.items[j];
string td1 = add_label(std::to_string(item.number), "td");
string td2 = add_label(item.text, "td");
tbody += add_label(td1 + td2, "tr", { {"class", "sl"} });
// sl: Short Line
}
}
table += add_label(tbody, "tbody");
table = add_label(table, "table", { {"border", "1"} });
writer << table << std::endl;
string create_modi_note = outfile.created_time + " 创建";
int modi_length = outfile.modified_times.size();
for (int i = 0; i < modi_length; i++) {
Modification modi = outfile.modified_times[i];
create_modi_note += " / " + add_label(modi.modified_time + " 修改", "strong", { {"style", "--bgc:" + modi.bg_color} });
}
create_modi_note = add_label(create_modi_note, "span");
writer << create_modi_note << std::endl;
writer << "</body></html>" << std::flush;
}
writer.close();
return 0;
}