-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown.c
314 lines (267 loc) · 7.53 KB
/
markdown.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
/**
* markdown.c -- markdown queue implementation
*
* author: Pat Gaffney <pat@hypepat.com>
* created: 2016-06-15
* modified: 2016-12-22
* project: patdown
*
************************************************************************/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "errors.h"
#include "patdown.h"
#include "strings.h"
/************************************************************************
* # Markdown Blocks
*
* Markdown nodes are created and inserted into a queue. They are
* distinguished by their `mdblock_t` and their position in the queue.
* The queue forms a linear structure of nodes parsed from the file.
*
************************************************************************/
/** A container node for a parsed Markdown block. */
typedef struct Markdown
{
String *string; /* String value of parsed block. */
mdblock_t type; /* Type (element) of parsed block. */
void *addtinfo; /* (Optional) additional block data. */
struct Markdown *next; /* Pointer to next node in the queue. */
} Markdown;
/** Markdown Queue Pointers */
static Markdown *head = NULL; /* Head of the queue. */
static Markdown *tail = NULL; /* Tail of the queue. */
/** Allow the parser to set the current block before inserting it. */
static mdblock_t currentblk = UNKNOWN;
/** Private Markdown queue functions. **/
static Markdown *md_alloc_node(void);
static bool md_insert_queue(Markdown **, Markdown **, Markdown *);
static void free_markdown_node(Markdown *);
/** Private Markdown extension functions. **/
static CodeBlk *alloc_code_blk(void);
/**
* Allocate memory for a new Markdown block.
*
* - throws fatal_memory_error: Memory could not be allocated.
*
* - returns: A pointer to the new Markdown node.
*/
static Markdown *md_alloc_node(void)
{
Markdown *node = NULL;
node = malloc(sizeof(Markdown));
if (!node) throw_fatal_memory_error();
return node;
}
/**
* Add a Markdown node to the queue with a given set of data.
*
* - parameter s: The actual string of parsed markdown.
* - parameter type: The block type, or, HTML element.
* - parameter addtinfo: Any additional information -- optional.
*
* - returns: `true` if node is inserted, `false` if node is `NULL`.
*/
bool add_markdown(String *s, const mdblock_t type, void *addtinfo)
{
Markdown *node = md_alloc_node();
/* Create an empty String node if *s was NULL. */
if (!s) node->string = init_string(0);
else node->string = s;
node->type = type;
node->addtinfo = addtinfo;
node->next = NULL;
currentblk = UNKNOWN;
if (md_insert_queue(&head, &tail, node)) return true;
else return false;
}
/**
* Insert a Markdown block into the queue at the tail.
*
* - parameter head: The first node in the queue.
* - parameter tail: The last node in the queue.
* - parameter node: The node to be inserted at the tail.
*
* - returns: `true` if node is inserted, `false` if node is `NULL`.
*/
static bool md_insert_queue(Markdown **head, Markdown **tail, Markdown *node)
{
if (node) {
if (!*head) *head = node;
else (*tail)->next = node;
*tail = node;
return true;
}
return false;
}
/**
* Get the number of parsed Markdown blocks.
*
* - returns: The number of nodes in the queue.
*/
size_t get_queue_length(void)
{
size_t len = 0;
Markdown *node = head;
if (!node) return 0;
while (node) {
len++;
node = node->next;
}
return len;
}
/**
* Set the current block being parsed.
*
* This is useful when parsing multi-line blocks (i.e. paragraphs).
* This value will be returned from `get_last_block()` instead of
* `tail->type` when available. The value of `currentblk` is reset to
* `UNKNOWN` everytime `add_markdown()` is called.
*
* - parameter blk: The new value of `currentblk`.
*/
void set_current_block(const mdblock_t blk)
{
currentblk = blk;
}
/**
* Get the type of the last block added to the queue.
*
* - returns: The `mdblock_t` of tail, or `currentblk`, if it isn't `UNKNOWN`.
*/
mdblock_t get_last_block(void)
{
if (currentblk != UNKNOWN) {
return currentblk;
}
else if (tail) {
return tail->type;
}
return UNKNOWN;
}
/**
* Dequeue the last block added to the queue.
*
* - returns: The `String` of that tail node, or `NULL` if no tail.
*/
String *dequeue_last_block(void)
{
String *lastBlock = NULL; /* String to return. */
Markdown *tmp = head; /* Temp node to traverse queue. */
if (!tail) return NULL;
lastBlock = tail->string;
/* Find the new tail. */
while (tmp->next != tail) {
tmp = tmp->next;
}
/* Free the current tail. */
if (tail->addtinfo) free(tail->addtinfo);
free(tail);
tail = tmp;
return lastBlock;
}
/**
* Debug-print the entire Markdown queue.
*
* This function is used for debugging purposes only.
*/
void debug_print_queue(void)
{
Markdown *tmp = head;
static char *blocknames[25] = {
"UNKNOWN",
"BLANK_LINE",
"ATX_HEADER_1",
"ATX_HEADER_2",
"ATX_HEADER_3",
"ATX_HEADER_4",
"ATX_HEADER_5",
"ATX_HEADER_6",
"HORIZONTAL_RULE",
"PARAGRAPH",
"SETEXT_HEADER_1",
"SETEXT_HEADER_2",
"INDENTED_CODE_BLOCK",
"FENCED_CODE_BLOCK",
"HTML_BLOCK",
"HTML_COMMENT",
"LINK_REFERENCE_DEF",
"BLOCKQUOTE_START",
"BLOCKQUOTE_END",
"UNORDERED_LIST_START",
"UNORDERED_LIST_ITEM",
"UNORDERED_LIST_END",
"ORDERED_LIST_START",
"ORDERED_LIST_ITEM",
"ORDERED_LIST_END"
};
while (tmp) {
if (tmp->type == LINK_REFERENCE_DEF) {
printf("%s: [%s]: %s \'%s\'\n",
blocknames[tmp->type],
((LinkRef *)tmp->addtinfo)->label,
((LinkRef *)tmp->addtinfo)->dest,
((LinkRef *)tmp->addtinfo)->title);
}
else {
printf("%s: \'%s\'\n",
blocknames[tmp->type],
tmp->string->data);
}
tmp = tmp->next;
}
}
/**
* Free all the Markdown nodes in the queue.
*
* This is the external interface for freeing the internal
* Markdown queue created by parsing the input file.
*/
void free_markdown(void)
{
free_markdown_node(head);
}
/**
* Free the memory allocated for a Markdown node.
*
* This is the internal interface for freeing a particular
* Markdown node. All nodes below the node pointer to by the
* argument will also be free'd.
*
* - parameter node: The node at which to begin freeing.
*/
static void free_markdown_node(Markdown *node)
{
if (node) {
if (node->addtinfo) free(node->addtinfo);
free_string(node->string);
free_markdown_node(node->next);
free(node);
}
}
/************************************************************************
* ## Markdown Block Extensions
************************************************************************/
/**
* Allocate memory for a CodeBlk structure.
*
* - throws fatal_memory_error: Memory could not be allocated.
*
* - returns: A pointer to the new structure.
*/
static CodeBlk *alloc_code_blk(void)
{
CodeBlk *cb = malloc(sizeof(CodeBlk));
if (!cb) throw_fatal_memory_error();
return cb;
}
/**
* Initialize a CodeBlk structure.
*
* - returns: A pointer to the new structure.
*/
CodeBlk *init_code_blk(void)
{
return alloc_code_blk();
}