-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
292 lines (263 loc) · 7.05 KB
/
parser.c
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
#include <stdio.h>
#include <stdlib.h>
#define true 1
#define false 0
enum TokenKind {
TOKEN_BOLD,
TOKEN_HASH,
TOKEN_ITALIC,
TOKEN_LIST,
TOKEN_STRIKE
};
typedef struct {
char mod;
int idx;
int count; // used only in the case of a #
} Modifier;
typedef struct {
int count;
Modifier* mod[100];
int idx;
int peek_idx;
} Stack;
void preprocess(FILE*, Stack*);
int modifier_in_stack(Stack*, char, int);
const char* get_tag(char, int, int);
void write_tag(FILE* file, const char* tag);
void htmlize(char*, char*, Stack*);
void push(Stack* stack, Modifier* item) {
Modifier* item_ptr = (Modifier*)malloc(sizeof(Modifier));
item_ptr->count = item->count;
item_ptr->idx = item->idx;
item_ptr->mod = item->mod;
stack->mod[stack->idx] = item_ptr;
stack->idx++;
stack->peek_idx++;
}
Modifier peek(Stack* stack) {
Modifier result;
result.count = stack->mod[stack->idx - 1]->count;
result.mod = stack->mod[stack->idx -1]->mod;
result.idx = stack->mod[stack->idx - 1]->idx;
return result;
}
Modifier pop(Stack* stack) {
stack->idx--;
Modifier result;
result.count = stack->mod[stack->idx]->count;
result.mod = stack->mod[stack->idx]->mod;
result.idx = stack->mod[stack->idx]->idx;
free(stack->mod[stack->idx]);
return result;
}
void preprocess(FILE * file, Stack* stack) {
int idx = 0;
char c = getc(file);
// this variable is used to check whether the previous
// character was actually a newline
// most useful for the # and -
// since we only want to render a heading or list on a new line
char prev_char;
int render_block = true;
while (c != EOF) {
if (c == '#') {
if (prev_char != '\n') render_block = false;
Modifier mod = {.idx = idx, .mod = '#', .count = 1};
if (stack->idx > 0) {
Modifier item = pop(stack);
if (item.mod == '#') item.count++;
push(stack, &item);
} else push(stack, &mod);
} else if (c == '-') {
Modifier mod = {.idx = idx, .mod = '-'};
push(stack, &mod);
} else if (c == '\n') {
if (stack->idx > 0) {
Modifier mod = peek(stack);
if (mod.mod == '#' || mod.mod == '-') {
Modifier a = peek(stack);
int is_space = prev_char == ' ';
if (render_block || a.idx == 0) {
render_block = true;
pop(stack);
}
}
}
} else if (c == '*' || c == '_' || c == '~') {
if (stack->idx > 0) {
Modifier prev = peek(stack);
if (prev.mod == c) {
pop(stack);
} else {
Modifier mod = {.idx = idx, .mod = c};
push(stack, &mod);
}
} else {
Modifier mod = {.idx = idx, .mod = c};
push(stack, &mod);
}
}
c = getc(file);
if (c != '#' && c != '-') prev_char = c;
idx++;
}
}
void htmlize(char* input, char* output, Stack* stack) {
FILE* in = fopen(input, "r");
FILE* out = fopen(output, "w");
enum TokenKind tokens[10];
int idx = 0;
int char_idx = 0;
int count = 1; // used for the hashes
char c = getc(in);
while (c != EOF) {
// printf("Next char = %c \n", c);
if (modifier_in_stack(stack, c, char_idx)) {
putc(c, out);
c = getc(in);
char_idx++;
continue;
}
// printf("Char = %c \n", c);
switch (c) {
case '#': {
tokens[idx] = TOKEN_HASH;
count = 1;
while (1) {
c = getc(in);
char_idx++;
if (c != '#') break;
count++;
}
const char* result = get_tag('#', true, count);
write_tag(out, result);
idx++;
// printf("Stopped at char = %c ", c);
continue;
}
case '-': {
tokens[idx] = TOKEN_LIST;
const char* result = get_tag('-', true, 0);
// printf("Tag = %s \n", result);
write_tag(out, result);
idx++;
break;
}
case '*': {
tokens[idx] = TOKEN_BOLD;
int opener = !(tokens[idx-1] == TOKEN_BOLD);
if (opener) {
idx++;
} else idx--;
const char* result = get_tag('*', opener, 0);
write_tag(out, result);
break;
}
case '_': {
tokens[idx] = TOKEN_ITALIC;
int opener = !(tokens[idx-1] == TOKEN_ITALIC);
if (opener) {
idx++;
} else idx--;
const char* result = get_tag('_', opener, 0);
write_tag(out, result);
break;
}
case '~': {
tokens[idx] = TOKEN_STRIKE;
int opener = !(tokens[idx-1] == TOKEN_STRIKE);
if (opener) {
idx++;
} else idx--;
const char* result = get_tag('~', opener, 0);
write_tag(out, result);
}
case '\n': {
// puts("GOT HERE");
enum TokenKind prev = tokens[idx-1];
char t;
if (prev != TOKEN_LIST && prev != TOKEN_HASH) {
// putc(c, out);
write_tag(out, "<br />");
} else {
if (prev == TOKEN_HASH) t = '#';
if (prev == TOKEN_LIST) t = '-';
const char* result = get_tag(t, false, count);
// printf("Tag = %s \n", result);
write_tag(out, result);
putc(c, out);
idx--;
}
break;
}
default:
putc(c, out);
break;
}
// printf("Idx = %d\n", idx);
c = getc(in);
char_idx++;
}
fclose(in);
fclose(out);
}
int modifier_in_stack(Stack* stack, char c, int char_idx) {
for (int i = 0; i < stack->idx; i++) {
Modifier* item = stack->mod[i];
if (item->mod == c && item->idx == char_idx) return true;
}
return false;
}
void write_tag(FILE* file, const char* tag) {
int idx = 0;
// printf("Writing tag %s \n", tag);
while (true) {
putc(tag[idx], file);
if (tag[idx+1] == '>') {
putc('>', file);
break;
}
idx++;
}
}
const char* get_tag(char c, int opener, int count) {
switch (c) {
char result[6];
// char* result;
case '#': {
if (opener) {
sprintf(result, "<h%d>", count);
} else sprintf(result, "</h%d>", count);
return result;
// return "<h1>";
}
case '*': {
if (opener) return "<b>";
return "</b>";
}
case '_': {
if (opener) return "<i>";
return "</i>";
}
case '-': {
if (opener) return "<li>";
return "</li>";
}
case '~': {
if (opener) return "<strike>";
return "</strike>";
}
default:
return (char[2]){c, '\0'};
}
}
int main(int argc, char* argv[]) {
if (argc != 3) puts("Not enough or too many arguments");
char* input_file = argv[1];
char* output_file = argv[2];
Stack stack = {.idx = 0, .peek_idx = 0};
FILE* file = fopen(input_file, "r");
preprocess(file, &stack);
fclose(file);
htmlize(input_file, output_file, &stack);
}