summaryrefslogtreecommitdiff
path: root/src/cmark.c
blob: 106abbf1af75b0e52f92ac7861bfe9b0b2a63906 (plain)
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
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include "references.h"
#include "html/houdini.h"
#include "cmark.h"
#include "buffer.h"
#include "ast.h"

// AST traversal and manipulation functions

cmark_node_block *cmark_block_next(cmark_node_block *current)
{
	return current->next;
}

cmark_node_block *cmark_block_previous(cmark_node_block *current)
{
	return current->prev;
}

cmark_node_block *cmark_block_parent(cmark_node_block *current)
{
	return current->parent;
}

cmark_node_block *cmark_block_children(cmark_node_block *current)
{
	return current->children;
}

void cmark_block_delete(cmark_node_block *current)
{
	if (current->prev) {
		current->prev->next = current->next;
	}
	if (current->next) {
		current->next->prev = current->prev;
	}
	current->next = NULL;
	cmark_free_blocks(current);
}

void cmark_block_insert_before(cmark_node_block *new, cmark_node_block *current)
{
	// Find last node in new:
	cmark_node_block *new_last = new;
	while (new_last->next) {
		new_last = new_last->next;
	}
	new_last->next = current;
	current->prev = new_last;
	if (current->prev) {
		current->prev->next = new;
		new->prev = current->prev;
	}
}

void cmark_block_insert_after(cmark_node_block *current, cmark_node_block *new)
{
	// Find last node in new:
	cmark_node_block *new_last = new;
	while (new_last->next) {
		new_last = new_last->next;
	}
	if (current->next) {
		new_last->next = current->next;
		current->next->prev = new_last;
	}
	current->next = new;
	new->prev = current;
}

/* * */

unsigned char *cmark_markdown_to_html(unsigned char *text, int len)
{
	node_block *blocks;
	unsigned char *result;

	blocks = cmark_parse_document(text, len);

	result = cmark_render_html(blocks);
	cmark_free_blocks(blocks);

	return result;
}

// Utility function used by free_inlines
static void splice_into_list(cmark_node_inl* e, node_inl* children) {
	cmark_node_inl * tmp;
	if (children) {
		tmp = children;
		// Find last child
		while (tmp->next) {
			tmp = tmp->next;
		}
		// Splice children into list
		tmp->next = e->next;
		e->next = children;
	}
	return ;
}

// Free an inline list.  Avoid recursion to prevent stack overflows
// on deeply nested structures.
void cmark_free_inlines(cmark_node_inl* e)
{
	node_inl * next;

	while (e != NULL) {
		switch (e->tag){
		case CMARK_INL_STRING:
		case CMARK_INL_RAW_HTML:
		case CMARK_INL_CODE:
			cmark_chunk_free(&e->content.literal);
			break;
		case CMARK_INL_LINEBREAK:
		case CMARK_INL_SOFTBREAK:
			break;
		case CMARK_INL_LINK:
		case CMARK_INL_IMAGE:
			free(e->content.linkable.url);
			free(e->content.linkable.title);
			splice_into_list(e, e->content.linkable.label);
			break;
		case CMARK_INL_EMPH:
		case CMARK_INL_STRONG:
		        splice_into_list(e, e->content.inlines);
			break;
		default:
		        fprintf(stderr, "[WARN] (%s:%d) Unknown inline tag %d",
				__FILE__, __LINE__, e->tag);
			break;
		}
		next = e->next;
		free(e);
		e = next;
	}
}

unsigned char *cmark_clean_autolink(chunk *url, int is_email)
{
	strbuf buf = GH_BUF_INIT;

	chunk_trim(url);

	if (url->len == 0)
		return NULL;

	if (is_email)
		strbuf_puts(&buf, "mailto:");

	houdini_unescape_html_f(&buf, url->data, url->len);
	return strbuf_detach(&buf);
}

// Free a node_block list and any children.
void cmark_free_blocks(cmark_node_block *e)
{
	cmark_node_block * next;
	while (e != NULL) {
		cmark_free_inlines(e->inline_content);
		strbuf_free(&e->string_content);
		if (e->tag == CMARK_BLOCK_FENCED_CODE) {
			strbuf_free(&e->as.code.info);
		}
		if (e->last_child) {
			// Splice children into list
			e->last_child->next = e->next;
			e->next = e->children;
		}
		next = e->next;
		free(e);
		e = next;
	}
}