-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_wasm.c
372 lines (329 loc) · 8.83 KB
/
parser_wasm.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
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// emscripten magic
#include <emscripten.h>
struct Node
{
struct Node* prev;
struct Node* next;
char value[20];
};
typedef struct
{
int count; // represents the number of nodes in the linked list
struct Node* head;
struct Node* tail;
} LL;
LL ll = { .head = NULL, .tail = NULL, .count = 0 };
struct Node* create_node(const char* value)
{
struct Node* node = malloc(sizeof(struct Node));
strcpy(node->value, value);
return node;
}
int append(const char* value)
{
ll.count++;
struct Node* new_node = create_node(value);
new_node->next = NULL;
if (ll.head == NULL)
{
new_node->prev = NULL;
ll.head = new_node;
ll.tail = new_node;
return 0;
}
// ll.head = new_node;
new_node->prev = ll.tail;
ll.tail->next = new_node;
ll.tail = new_node;
return 0;
}
struct Node* node_at(int index)
{
struct Node* curr_node;
if (ll.head != NULL && index > 0 && index <= ll.count) { curr_node = ll.head; } else { return NULL; }
int count = 0;
while (count < index-1 && curr_node != NULL)
{
curr_node = curr_node->next;
count++;
}
return curr_node;
}
// insert the value at the specified index
// if there is a node at the position, it will push that node forward
// if there is no node at that index, it will return -1
int insert_before(int index, char* value) {
struct Node* node = create_node(value);
struct Node* node_at_index = node_at(index);
if (node_at_index != NULL)
{
struct Node* prev = node_at_index->prev;
node_at_index->prev = node;
if (prev != NULL) prev->next = node;
node->next = node_at_index;
node->prev = prev != NULL ? prev : NULL;
if (prev == NULL) ll.head = node;
} else return -1;
ll.count++;
return 0;
}
int insert_after(int index, char* value)
{
struct Node* node = create_node(value);
struct Node* node_at_index = node_at(index);
if (node_at_index != NULL)
{
struct Node* next = node_at_index->next;
node_at_index->next = node;
if (next != NULL) next->prev = node;
node->prev = node_at_index;
node->next = next != NULL ? next : NULL;
if (next == NULL) ll.tail = node;
} else return -1;
ll.count++;
return 0;
}
// thou shall silently crash
int delete(int index)
{
struct Node* node = node_at(index);
if (node != NULL)
{
if (node->prev == NULL)
{
if (node->next != NULL)
{
node->next->prev = NULL;
ll.head = node->next;
}
}
else
{
if (node->next != NULL) node->next->prev = node->prev;
node->prev->next = node->next;
}
free(node); // PURGE!!!!!!!!!!!!!
ll.count--;
}
return 0;
}
// this will free all memory allocated
void clear_ll()
{
for (int i = 0; i < ll.count; i++)
{
delete(i+1);
}
}
int naive_traverse()
{
struct Node* curr_node = ll.head;
for (int i = 0; i < ll.count; i++)
{
printf("%s", curr_node->value);
curr_node = curr_node->next;
}
return 0;
}
char content_in[10000];
char content_out[10000];
// reads text and writes each word into a Linked List
void text_as_ll()
{
char buffer[20];
int idx = 0;
int cid = 0;
char c = content_in[cid];
while (c != '\0')
{
// printf("Value of C = %c \n", c);
if (
c != ' '
&& c != '\n'
&& c != '*'
&& c != '_'
&& c != '~'
&& c != '`')
{
buffer[idx] = c;
idx++;
}
else
{
// printf("Buffer = %s \n", buffer);
append(buffer);
idx = 0;
for (int i = 0; i < 20; i++)
{
buffer[i] = 0;
}
if (c == '\n') append("\n");
if (c == ' ') append(" ");
if (c == '*') append("*");
if (c == '_') append("_");
if (c == '~') append("~");
if (c == '`') append("`");
}
cid++;
c = content_in[cid];
}
// printf("%s \n", buffer);
append(buffer);
append("\n");
}
// converts headings to corresponding html tags
void match_headings(int idx)
{
int h_count = 0;
// count the number of headings
struct Node* node = node_at(idx);
for (int i = 0; i < 4; i++)
{
if (node->value[i] == '#')
{
h_count++;
} else break;
}
struct Node* next_node = node->next;
int last_word_idx = idx+1; // index of last word before the next \n
while (strcmp(next_node->value, "\n") != 0)
{
next_node = next_node->next;
last_word_idx++;
}
char opening[6];
char closing[6];
sprintf(opening, "<h%d>", h_count);
sprintf(closing, "</h%d>", h_count);
insert_before(idx+1, opening);
insert_after(last_word_idx, closing);
delete(idx); // delete the hashtags
delete(idx+1); // delete the whitespace after it
}
// handle blockquotes
void match_block(int idx)
{
struct Node* node = node_at(idx);
struct Node* next = node->next;
int last_word_idx = idx+1;
while (strcmp(next->value, "\n") != 0)
{
next = next->next;
last_word_idx++;
}
strcpy(node->value, "<blockquote>");
insert_after(last_word_idx, "</blockquote>");
}
// converts list into unordered list
void match_list(int idx)
{
struct Node* node = node_at(idx);
struct Node* next = node->next;
int last_node_idx = idx+1;
while (strcmp(next->value, "\n") != 0)
{
next = next->next;
last_node_idx++;
}
strcpy(node->value, "<li>");
insert_after(last_node_idx, "</li>");
insert_after(last_node_idx+1, "\n");
}
// matches _,*,~,`,´´´
void match_duals(int idx)
{
// find next special character
struct Node* node = node_at(idx);
struct Node* next = node->next;
while (strcmp(next->value, "\n") != 0)
{
if (strcmp(next->value, "*") == 0)
{
strcpy(node->value, "<b>");
strcpy(next->value, "</b>");
break;
} else if (strcmp(next->value, "_") == 0)
{
strcpy(node->value, "<i>");
strcpy(next->value, "</i>");
break;
}
else if (strcmp(next->value, "~") == 0)
{
strcpy(node->value, "<strike>");
strcpy(next->value, "</strike>");
break;
}
else if (strcmp(next->value, "`") == 0)
{
strcpy(node->value, "<em>");
strcpy(next->value, "</em>");
break;
}
next = next->next;
} return;
}
void parse()
{
struct Node* node = ll.head;
int idx = 1;
while (node->next != NULL)
{
if (
strcmp(node->value, "*") == 0
|| strcmp(node->value, "_") == 0
|| strcmp(node->value, "~") == 0
|| strcmp(node->value, "`") == 0
)
{
match_duals(idx);
}
if (strcmp(node->value, "-") == 0)
{
if (idx == 1 || strcmp(node_at(idx-1)->value, "\n") == 0) match_list(idx);
}
if ((idx == 1 || strcmp(node_at(idx-1)->value, "\n") == 0)
&& strcmp(node->value, ">") == 0)
{
match_block(idx);
}
if ( (idx == 1 || strcmp(node_at(idx-1)->value, "\n") == 0)
&& node->value[0] == '#'
&& (strcmp(node->next->value, " ") == 0)
)
{
match_headings(idx);
} else idx++;
node = node_at(idx);
// idx++;
}
}
void join_and_out()
{
int out_at = 0; // keeps track of which position at which we are in the content_out string
struct Node* node = ll.head;
for (int i = 0; i < ll.count && node != NULL; i++)
{
int length = strlen(node->value);
for (int j = 0; j < length; j++)
{
content_out[out_at] = node->value[j];
out_at++;
}
node = node->next;
}
}
EMSCRIPTEN_KEEPALIVE
char* eat_and_poop(char* text_in)
{
strcpy(content_in, text_in); // eat text
text_as_ll();
parse(); // digest
join_and_out(); // poop
// clear_ll();
clear_ll(); ll.count = 0; ll.head = NULL; ll.tail = NULL; // just making sure
return content_out;
}