forked from dreamwieber/AttributedMarkdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing_functions.m
114 lines (92 loc) · 2.79 KB
/
parsing_functions.m
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
/* parsing_functions.c - Functions for parsing markdown and
* freeing element lists. */
void parse_from(yyrule yystart)
{
GREG g;
memset(&g, 0, sizeof(g));
yyparse_from(&g, yystart); /* first pass, just to collect references */
yydeinit(&g);
}
static void free_element_contents(element elt);
/* free_element_list - free list of elements recursively */
void free_element_list(element * elt) {
element * next = NULL;
while (elt != NULL) {
next = elt->next;
free_element_contents(*elt);
if (elt->children != NULL) {
free_element_list(elt->children);
elt->children = NULL;
}
free(elt);
elt = next;
}
}
/* free_element_contents - free element contents depending on type */
static void free_element_contents(element elt) {
switch (elt.key) {
case STRING:
case SPACE:
case RAW:
case HTMLBLOCK:
case HTML:
case VERBATIM:
case CODE:
case NOTE:
[elt.contents.str release];
elt.contents.str = nil;
break;
case LINK:
case IMAGE:
case REFERENCE:
[elt.contents.link->url release];
elt.contents.link->url = nil;
[elt.contents.link->title release];
elt.contents.link->title = nil;
free_element_list(elt.contents.link->label);
free(elt.contents.link);
elt.contents.link = NULL;
break;
default:
;
}
}
/* free_element - free element and contents */
void free_element(element *elt) {
free_element_contents(*elt);
free(elt);
}
element * parse_references(NSString *string, int extensions) {
md.syntax_extensions = extensions;
struct Input saved = md.input;
md.input.charbuf = (char *)[string UTF8String];
md.input.position = 0;
parse_from(yy_References); /* first pass, just to collect references */
md.input = saved;
return md.references;
}
element * parse_notes(NSString *string, int extensions, element *reference_list) {
md.notes = NULL;
md.syntax_extensions = extensions;
if (extension(EXT_NOTES)) {
md.references = reference_list;
struct Input saved = md.input;
md.input.charbuf = (char *)[string UTF8String];
md.input.position = 0;
parse_from(yy_Notes); /* second pass for notes */
md.input = saved;
}
return md.notes;
}
element * parse_markdown(NSString *string, int extensions, element *reference_list, element *note_list) {
md.syntax_extensions = extensions;
md.references = reference_list;
md.notes = note_list;
struct Input saved = md.input;
md.input.charbuf = (char *)[string UTF8String];
md.input.position = 0;
parse_from(yy_Doc);
md.input = saved;
return md.parse_result;
}
/* vim:set ts=4 sw=4: */