forked from fletcher/peg-multimarkdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown_lib.c
265 lines (226 loc) · 9.44 KB
/
markdown_lib.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
/**********************************************************************
markdown_lib.c - markdown in C using a PEG grammar.
(c) 2008 John MacFarlane (jgm at berkeley dot edu).
portions Copyright (c) 2010-2013 Fletcher T. Penney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License or the MIT
license. See LICENSE for details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
***********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "markdown_peg.h"
#define TABSTOP 4
#define VERSION "3.7"
/* preformat_text - allocate and copy text buffer while
* performing tab expansion. */
static GString *preformat_text(char *text) {
GString *buf;
char next_char;
int charstotab;
int len = 0;
buf = g_string_new("");
charstotab = TABSTOP;
while ((next_char = *text++) != '\0') {
switch (next_char) {
case '\t':
while (charstotab > 0)
g_string_append_c(buf, ' '), len++, charstotab--;
break;
case '\n':
g_string_append_c(buf, '\n'), len++, charstotab = TABSTOP;
break;
default:
g_string_append_c(buf, next_char), len++, charstotab--;
}
if (charstotab == 0)
charstotab = TABSTOP;
}
g_string_append(buf, "\n\n");
return(buf);
}
/* print_tree - print tree of elements, for debugging only. */
static void print_tree(element * elt, int indent) {
int i;
char * key;
while (elt != NULL) {
for (i = 0; i < indent; i++)
fputc(' ', stderr);
switch (elt->key) {
case LIST: key = "LIST"; break;
case RAW: key = "RAW"; break;
case SPACE: key = "SPACE"; break;
case LINEBREAK: key = "LINEBREAK"; break;
case ELLIPSIS: key = "ELLIPSIS"; break;
case EMDASH: key = "EMDASH"; break;
case ENDASH: key = "ENDASH"; break;
case APOSTROPHE: key = "APOSTROPHE"; break;
case SINGLEQUOTED: key = "SINGLEQUOTED"; break;
case DOUBLEQUOTED: key = "DOUBLEQUOTED"; break;
case STR: key = "STR"; break;
case LINK: key = "LINK"; break;
case IMAGE: key = "IMAGE"; break;
case CODE: key = "CODE"; break;
case HTML: key = "HTML"; break;
case EMPH: key = "EMPH"; break;
case STRONG: key = "STRONG"; break;
case PLAIN: key = "PLAIN"; break;
case PARA: key = "PARA"; break;
case LISTITEM: key = "LISTITEM"; break;
case BULLETLIST: key = "BULLETLIST"; break;
case ORDEREDLIST: key = "ORDEREDLIST"; break;
case H1: key = "H1"; break;
case H2: key = "H2"; break;
case H3: key = "H3"; break;
case H4: key = "H4"; break;
case H5: key = "H5"; break;
case H6: key = "H6"; break;
case BLOCKQUOTE: key = "BLOCKQUOTE"; break;
case VERBATIM: key = "VERBATIM"; break;
case HTMLBLOCK: key = "HTMLBLOCK"; break;
case HRULE: key = "HRULE"; break;
case REFERENCE: key = "REFERENCE"; break;
case NOTE: key = "NOTE"; break;
default: key = "?";
}
if ( elt->key == STR ) {
fprintf(stderr, "0x%p: %s '%s'\n", (void *)elt, key, elt->contents.str);
} else {
fprintf(stderr, "0x%p: %s\n", (void *)elt, key);
}
if (elt->children)
print_tree(elt->children, indent + 4);
elt = elt->next;
}
}
/* process_raw_blocks - traverses an element list, replacing any RAW elements with
* the result of parsing them as markdown text, and recursing into the children
* of parent elements. The result should be a tree of elements without any RAWs. */
static element * process_raw_blocks(element *input, int extensions, element *references, element *notes, element *labels) {
element *current = NULL;
element *last_child = NULL;
char *contents;
current = input;
while (current != NULL) {
if (current->key == RAW) {
/* \001 is used to indicate boundaries between nested lists when there
* is no blank line. We split the string by \001 and parse
* each chunk separately. */
contents = strtok(current->contents.str, "\001");
current->key = LIST;
current->children = parse_markdown(contents, extensions, references, notes, labels);
last_child = current->children;
while ((contents = strtok(NULL, "\001"))) {
while (last_child->next != NULL)
last_child = last_child->next;
last_child->next = parse_markdown(contents, extensions, references, notes, labels);
}
free(current->contents.str);
current->contents.str = NULL;
}
if (current->children != NULL)
current->children = process_raw_blocks(current->children, extensions, references, notes, labels);
current = current->next;
}
return input;
}
/* markdown_to_gstring - convert markdown text to the output format specified.
* Returns a GString, which must be freed after use using g_string_free(). */
GString * markdown_to_g_string(char *text, int extensions, int output_format) {
element *result;
element *references;
element *notes;
element *labels;
GString *formatted_text;
GString *out;
out = g_string_new("");
formatted_text = preformat_text(text);
references = parse_references(formatted_text->str, extensions);
notes = parse_notes(formatted_text->str, extensions, references);
labels = parse_labels(formatted_text->str, extensions, references, notes);
if (output_format == OPML_FORMAT) {
result = parse_markdown_for_opml(formatted_text->str, extensions);
} else {
result = parse_markdown_with_metadata(formatted_text->str, extensions, references, notes, labels);
result = process_raw_blocks(result, extensions, references, notes, labels);
}
g_string_free(formatted_text, TRUE);
if (result == NULL) {
/* The parsing was aborted */
g_string_append(out,"MultiMarkdown was unable to parse this file.");
} else {
print_element_list(out, result, output_format, extensions);
}
free_element_list(result);
free_element_list(references);
free_element_list(labels);
return out;
}
/* markdown_to_string - convert markdown text to the output format specified.
* Returns a null-terminated string, which must be freed after use. */
char * markdown_to_string(char *text, int extensions, int output_format) {
GString *out;
char *char_out;
out = markdown_to_g_string(text, extensions, output_format);
char_out = out->str;
g_string_free(out, FALSE);
return char_out;
}
/* vim:set ts=4 sw=4: */
/* extract_metadata_value - parse document and return value of specified
metadata key (e.g. "LateX Mode")/
Returns a null-terminated string, which must be freed after use. */
char * extract_metadata_value(char *text, int extensions, char *key) {
char *value;
element *result;
element *references;
element *notes;
element *labels;
GString *formatted_text;
formatted_text = preformat_text(text);
references = parse_references(formatted_text->str, extensions);
notes = parse_notes(formatted_text->str, extensions, references);
labels = parse_labels(formatted_text->str, extensions, references, notes);
result = parse_metadata_only(formatted_text->str, extensions);
value = metavalue_for_key(key, result);
free_element_list(result);
free_element_list(references);
free_element_list(labels);
return value;
}
/* has_metadata - parse document and report whether metadata is present */
gboolean has_metadata(char *text, int extensions) {
gboolean hasMeta;
element *result;
element *references;
element *notes;
element *labels;
GString *formatted_text;
formatted_text = preformat_text(text);
references = parse_references(formatted_text->str, extensions);
notes = parse_notes(formatted_text->str, extensions, references);
labels = parse_labels(formatted_text->str, extensions, references, notes);
result = parse_metadata_only(formatted_text->str, extensions);
hasMeta = FALSE;
if (result != NULL) {
if (result->children != NULL) {
hasMeta = TRUE;
free_element_list(result);
} else {
free_element(result);
}
}
free_element_list(references);
free_element_list(labels);
return hasMeta;
}
/* version - return the MultiMarkdown library version */
char * mmd_version() {
char* result = (char*)malloc(8);
sprintf(result, "%s",VERSION);
return result;
}