forked from fletcher/peg-multimarkdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility_functions.c
558 lines (505 loc) · 17.9 KB
/
utility_functions.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/* utility_functions.c - List manipulation functions, element
* constructors, and macro definitions for leg markdown parser. */
#include "utility_functions.h"
#include "markdown_peg.h"
#include <string.h>
#include <assert.h>
const char *current_input_position(markdown_parser_state *state, int bufPos, int bufLimit)
{
return state->charbuf - bufLimit + bufPos;
}
const size_t offset_to_current_position(const char *offset, markdown_parser_state *state, int bufPos, int bufLimit)
{
return current_input_position(state, bufPos, bufLimit) - offset;
}
/**********************************************************************
List manipulation functions
***********************************************************************/
/* cons - cons an element onto a list, returning pointer to new head */
element * cons(element *new, element *list) {
assert(new != NULL);
new->next = list;
return new;
}
/* reverse - reverse a list, returning pointer to new list */
element *reverse(element *list) {
element *new = NULL;
element *next = NULL;
while (list != NULL) {
next = list->next;
new = cons(list, new);
list = next;
}
return new;
}
/* append_list - add element to end of list */
void append_list(element *new, element *list) {
assert(new != NULL);
element *step = list;
while (step->next != NULL) {
step = step->next;
}
new->next = NULL;
step->next = new;
}
/* concat_string_list - concatenates string contents of list of STR elements.
* Frees STR elements as they are added to the concatenation. */
GString *concat_string_list(element *list) {
GString *result;
element *next;
result = g_string_new("");
while (list != NULL) {
assert(list->key == STR || list->key == SPACE);
assert(list->contents.str != NULL);
g_string_append(result, list->contents.str);
next = list->next;
free_element(list);
list = next;
}
return result;
}
/**********************************************************************
Auxiliary functions for parsing actions.
These make it easier to build up data structures (including lists)
in the parsing actions.
***********************************************************************/
/* mk_element - generic constructor for element */
element * mk_element(int key) {
element *result = malloc(sizeof(element));
result->key = key;
result->children = NULL;
result->next = NULL;
result->contents.str = NULL;
return result;
}
/* mk_str - constructor for STR element */
element * mk_str(char *string) {
element *result;
assert(string != NULL);
result = mk_element(STR);
result->contents.str = strdup(string);
return result;
}
/* mk_str_from_list - makes STR element by concatenating a
* reversed list of strings, adding optional extra newline */
element * mk_str_from_list(element *list, bool extra_newline) {
element *result;
GString *c = concat_string_list(reverse(list));
if (extra_newline)
g_string_append(c, "\n");
result = mk_element(STR);
result->contents.str = c->str;
g_string_free(c, false);
return result;
}
/* mk_list - makes new list with key 'key' and children the reverse of 'lst'.
* This is designed to be used with cons to build lists in a parser action.
* The reversing is necessary because cons adds to the head of a list. */
element * mk_list(int key, element *lst) {
element *result;
result = mk_element(key);
result->children = reverse(lst);
return result;
}
/* mk_link - constructor for LINK element */
element * mk_link(element *label, char *url, char *title, element *attr, char *id) {
element *result;
result = mk_element(LINK);
result->contents.link = malloc(sizeof(link));
result->contents.link->label = label;
result->contents.link->url = strdup(url);
result->contents.link->title = strdup(title);
result->contents.link->attr = attr;
result->contents.link->identifier = strdup(id);
return result;
}
/* mk_codeblock - constructor for CODEBLOCK element */
element *mk_codeblock(char *code, char *language, size_t preceedingIndentation) {
element *result = mk_element(CODEBLOCK);
result->contents.codeblock = malloc(sizeof(codeblock));
result->contents.codeblock->code = strdup(code);
result->contents.codeblock->language = strdup(language);
result->contents.codeblock->preceedingIndentation = preceedingIndentation;
return result;
}
/* extension = returns true if extension is selected */
bool extension(markdown_parser_state *state, int ext) {
return (state->syntax_extensions & ext);
}
/* match_inlines - returns true if inline lists match (case-insensitive...) */
bool match_inlines(element *l1, element *l2) {
while (l1 != NULL && l2 != NULL) {
if (l1->key != l2->key)
return false;
switch (l1->key) {
case SPACE:
case LINEBREAK:
case ELLIPSIS:
case EMDASH:
case ENDASH:
case APOSTROPHE:
break;
case CODE:
case STR:
case HTML:
if (strcasecmp(l1->contents.str, l2->contents.str) == 0)
break;
else
return false;
case EMPH:
case STRONG:
case DELETE:
case COMMENT:
case LIST:
case SINGLEQUOTED:
case DOUBLEQUOTED:
if (match_inlines(l1->children, l2->children))
break;
else
return false;
case LINK:
case IMAGE:
return false; /* No links or images within links */
default:
fprintf(stderr, "match_inlines encountered unknown key = %d\n", l1->key);
exit(EXIT_FAILURE);
break;
}
l1 = l1->next;
l2 = l2->next;
}
return (l1 == NULL && l2 == NULL); /* return true if both lists exhausted */
}
/* find_reference - return true if link found in references matching label.
* 'link' is modified with the matching url and title. */
bool find_reference(markdown_parser_state *state, link *result, element *label) {
element *cur = state->references; /* pointer to walk up list of references */
link *curitem;
while (cur != NULL) {
curitem = cur->contents.link;
if (match_inlines(label, curitem->label)) {
*result = *curitem;
return true;
}
else
cur = cur->next;
}
return false;
}
/* find_note - return true if note found in notes matching label.
if found, 'result' is set to point to matched note. */
bool find_note(markdown_parser_state *state, element **result, char *label) {
element *cur = state->notes; /* pointer to walk up list of notes */
while (cur != NULL) {
if (strcmp(label, cur->contents.str) == 0) {
*result = cur;
return true;
}
else
cur = cur->next;
}
return false;
}
/* peg-multimarkdown additions */
/* print_raw_element - print an element as original text */
void print_raw_element(GString *out, element *elt) {
if (elt->key == LINK) {
print_raw_element_list(out,elt->contents.link->label);
} else {
if (elt->contents.str != NULL) {
g_string_append_printf(out, "%s", elt->contents.str);
} else {
print_raw_element_list(out, elt->children);
}
}
}
/* print_raw_element_list - print a list of elements as original text */
void print_raw_element_list(GString *out, element *list) {
while (list != NULL) {
print_raw_element(out, list);
list = list->next;
}
}
/* string_from_element_list */
/* Creates a flat string from an element list */
char *string_from_element_list(element *list, bool freeList) {
GString *raw = g_string_new("");
print_raw_element(raw, list);
if (freeList)
free(list);
return g_string_free(raw, false);
}
/* label_from_element_list */
/* Returns a null-terminated string, which must be freed after use. */
char *label_from_element_list(element *list, bool obfuscate) {
char *label;
char *label2;
GString *raw = g_string_new("");
print_raw_element_list(raw, list);
label = label_from_string(raw->str,obfuscate);
label2 = strdup(label);
free(label);
g_string_free(raw,true);
return label2;
}
/* label_from_string - strip spaces and illegal characters to generate valid
HTML id */
/* Returns a null-terminated string, which must be freed after use. */
char *label_from_string(char *str, bool obfuscate) {
bool valid = FALSE;
GString *out = g_string_new("");
char *label;
while (*str != '\0') {
if (valid) {
/* can relax on following characters */
if ((*str >= '0' && *str <= '9') || (*str >= 'A' && *str <= 'Z')
|| (*str >= 'a' && *str <= 'z') || (*str == '.') || (*str== '_')
|| (*str== '-') || (*str== ':'))
{
g_string_append_c(out, tolower(*str));
}
} else {
/* need alpha as first character */
if ((*str >= 'A' && *str <= 'Z') || (*str >= 'a' && *str <= 'z'))
{
g_string_append_c(out, tolower(*str));
valid = TRUE;
}
}
str++;
}
label = out->str;
g_string_free(out, false);
return label;
}
/* find_label - return true if header, table, etc is found matching label.
* 'link' is modified with the matching url and title. */
bool find_label(markdown_parser_state *state, link *result, element *label) {
char *lab;
element *cur = state->labels; /* pointer to walk up list of references */
GString *text = g_string_new("");
print_raw_element_list(text, label);
lab = label_from_string(text->str,0);
GString *query = g_string_new(lab);
free(lab);
g_string_free(text, true);
while (cur != NULL) {
if (strcmp(query->str,cur->contents.str) == 0) {
g_string_free(query, true);
return true;
}
else
cur = cur->next;
}
g_string_free(query, true);
return false;
}
/* localize_typography - return the proper string, based on language chosen */
/* Default action is English */
void localize_typography(GString *out, int character, int lang, int output) {
switch (output) {
case HTMLOUT:
switch (character) {
case LSQUOTE:
switch (lang) {
case SWEDISH:
g_string_append_printf(out, "’");
break;
case FRENCH:
g_string_append_printf(out,"'");
break;
case GERMAN:
g_string_append_printf(out,"‚");
break;
case GERMANGUILL:
g_string_append_printf(out,"›");
break;
default:
g_string_append_printf(out,"‘");
}
break;
case RSQUOTE:
switch (lang) {
case GERMAN:
g_string_append_printf(out,"‘");
break;
case GERMANGUILL:
g_string_append_printf(out,"‹");
break;
default:
g_string_append_printf(out,"’");
}
break;
case APOS:
g_string_append_printf(out,"’");
break;
case LDQUOTE:
switch (lang) {
case DUTCH:
case GERMAN:
g_string_append_printf(out,"„");
break;
case GERMANGUILL:
g_string_append_printf(out,"»");
break;
case FRENCH:
g_string_append_printf(out,"«");
break;
case SWEDISH:
g_string_append_printf(out, "”");
break;
default:
g_string_append_printf(out,"“");
}
break;
case RDQUOTE:
switch (lang) {
case SWEDISH:
case DUTCH:
g_string_append_printf(out,"”");
break;
case GERMAN:
g_string_append_printf(out,"“");
break;
case GERMANGUILL:
g_string_append_printf(out,"«");
break;
case FRENCH:
g_string_append_printf(out,"»");
break;
default:
g_string_append_printf(out,"”");
}
break;
case NDASH:
g_string_append_printf(out,"–");
break;
case MDASH:
g_string_append_printf(out,"—");
break;
case ELLIP:
g_string_append_printf(out,"…");
break;
default:;
}
break;
case LATEXOUT:
switch (character) {
case LSQUOTE:
switch (lang) {
case SWEDISH:
g_string_append_printf(out,"'");
break;
case FRENCH:
g_string_append_printf(out,"'");
break;
case GERMAN:
g_string_append_printf(out,"‚");
break;
case GERMANGUILL:
g_string_append_printf(out,"›");
break;
default:
g_string_append_printf(out,"`");
}
break;
case RSQUOTE:
switch (lang) {
case GERMAN:
g_string_append_printf(out,"`");
break;
case GERMANGUILL:
g_string_append_printf(out,"‹");
break;
default:
g_string_append_printf(out,"'");
}
break;
case APOS:
g_string_append_printf(out,"'");
break;
case LDQUOTE:
switch (lang) {
case DUTCH:
case GERMAN:
g_string_append_printf(out,"„");
break;
case GERMANGUILL:
g_string_append_printf(out,"»");
break;
case FRENCH:
g_string_append_printf(out,"«");
break;
case SWEDISH:
g_string_append_printf(out,"''");
break;
default:
g_string_append_printf(out,"``");
}
break;
case RDQUOTE:
switch (lang) {
case SWEDISH:
case DUTCH:
g_string_append_printf(out,"''");
break;
case GERMAN:
g_string_append_printf(out,"``");
break;
case GERMANGUILL:
g_string_append_printf(out,"«");
break;
case FRENCH:
g_string_append_printf(out,"»");
break;
default:
g_string_append_printf(out,"''");
}
break;
case NDASH:
g_string_append_printf(out,"--");
break;
case MDASH:
g_string_append_printf(out,"---");
break;
case ELLIP:
g_string_append_printf(out,"{\\ldots}");
break;
default:;
}
break;
default:;
}
}
/* Trim spaces at end of string */
void trim_trailing_whitespace(char *str) {
while ( ( str[strlen(str)-1] == ' ' ) ||
( str[strlen(str)-1] == '\n' ) ||
( str[strlen(str)-1] == '\r' ) ||
( str[strlen(str)-1] == '\t' ) ) {
str[strlen(str)-1] = '\0';
}
}
/* Don't let us get caught in "infinite" loop */
bool check_timeout(markdown_parser_state *state) {
/* Once we abort, keep aborting */
if (state->parse_aborted)
return 0;
/* We're not timing this run */
if (state->start_time == 0)
return 1;
clock_t end = clock();
double elapsed = ((double) (end - state->start_time)) / CLOCKS_PER_SEC;
/* fprintf(stderr,"%2.2f elapsed; (%4.2f CLOCKS_PER_SEC)\n",elapsed,CLOCKS_PER_SEC); */
/* fprintf(stderr,"%2.2f elapsed\n",elapsed); */
/* If > 30 clock seconds, then abort */
float max = 30;
if (elapsed > max) {
state->parse_aborted = 1;
return 0;
}
return 1;
}
void debug(void) {
__builtin_trap();
}