aboutsummaryrefslogtreecommitdiff
path: root/parser_gemtext.c
blob: 18b733769b1ae829dc232a0a9e2258a91cec3b2b (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
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
/*
 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * A streaming gemtext parser.
 *
 * TODO:
 *  - handle NULs
 *  - UTF8
 */

#include "compat.h"

#include <ctype.h>
#include <string.h>
#include <stdlib.h>

#include "defaults.h"
#include "parser.h"
#include "utf8.h"

static int	gemtext_parse(struct parser*, const char*, size_t);
static int	gemtext_foreach_line(struct parser*, const char*, size_t);
static int	gemtext_free(struct parser*);

static int	parse_text(struct parser*, enum line_type, const char*, size_t);
static int	parse_link(struct parser*, enum line_type, const char*, size_t);
static int	parse_title(struct parser*, enum line_type, const char*, size_t);
static int	parse_item(struct parser*, enum line_type, const char*, size_t);
static int	parse_quote(struct parser*, enum line_type, const char*, size_t);
static int	parse_pre_start(struct parser*, enum line_type, const char*, size_t);
static int	parse_pre_cnt(struct parser*, enum line_type, const char*, size_t);
static int	parse_pre_end(struct parser*, enum line_type, const char*, size_t);
static void	search_title(struct parser*, enum line_type);

typedef int (parselinefn)(struct parser*, enum line_type, const char*, size_t);

static parselinefn *parsers[] = {
	[LINE_TEXT]		= parse_text,
	[LINE_LINK]		= parse_link,
	[LINE_TITLE_1]		= parse_title,
	[LINE_TITLE_2]		= parse_title,
	[LINE_TITLE_3]		= parse_title,
	[LINE_ITEM]		= parse_item,
	[LINE_QUOTE]		= parse_quote,
	[LINE_PRE_START]	= parse_pre_start,
	[LINE_PRE_CONTENT]	= parse_pre_cnt,
	[LINE_PRE_END]		= parse_pre_end,
};

void
gemtext_initparser(struct parser *p)
{
	memset(p, 0, sizeof(*p));

	p->name = "text/gemini";
	p->parse = &gemtext_parse;
	p->free  = &gemtext_free;

	TAILQ_INIT(&p->head);
}

static inline int
emit_line(struct parser *p, enum line_type type, char *line, char *alt)
{
	struct line *l;

	if ((l = calloc(1, sizeof(*l))) == NULL)
		return 0;

	l->type = type;
	l->line = line;
	l->alt = alt;

	switch (l->type) {
	case LINE_PRE_START:
	case LINE_PRE_END:
		if (hide_pre_context)
			l->flags = L_HIDDEN;
		if (l->type == LINE_PRE_END &&
		    hide_pre_closing_line)
			l->flags = L_HIDDEN;
		break;
	case LINE_PRE_CONTENT:
		if (hide_pre_blocks)
			l->flags = L_HIDDEN;
		break;
	case LINE_LINK:
		if (emojify_link &&
		    !emojied_line(line, (const char **)&l->data))
			l->data = NULL;
		break;
	default:
		break;
	}

	TAILQ_INSERT_TAIL(&p->head, l, lines);

	return 1;
}

static int
parse_text(struct parser *p, enum line_type t, const char *buf, size_t len)
{
	char *l;

	if ((l = calloc(1, len+1)) == NULL)
		return 0;
	memcpy(l, buf, len);
	return emit_line(p, t, l, NULL);
}

static int
parse_link(struct parser *p, enum line_type t, const char *buf, size_t len)
{
	char *l, *u;
	const char *url_start;

	if (len <= 2)
		return emit_line(p, t, NULL, NULL);
	buf += 2;
	len -= 2;

	while (len > 0 && isspace(buf[0])) {
		buf++;
		len--;
	}

	if (len == 0)
		return emit_line(p, t, NULL, NULL);

	url_start = buf;
	while (len > 0 && !isspace(buf[0])) {
		buf++;
		len--;
	}

	if ((u = calloc(1, buf - url_start + 1)) == NULL)
		return 0;
	memcpy(u, url_start, buf - url_start);

	if (len == 0)
		goto nolabel;

	while (len > 0 && isspace(buf[0])) {
		buf++;
		len--;
	}

	if (len == 0)
		goto nolabel;

	if ((l = calloc(1, len + 1)) == NULL)
		return 0;

	memcpy(l, buf, len);
	return emit_line(p, t, l, u);

nolabel:
	if ((l = strdup(u)) == NULL)
		return 0;
	return emit_line(p, t, l, u);
}

static int
parse_title(struct parser *p, enum line_type t, const char *buf, size_t len)
{
	char *l;

	switch (t) {
	case LINE_TITLE_1:
		if (len <= 1)
			return emit_line(p, t, NULL, NULL);
		buf++;
		len--;
		break;
	case LINE_TITLE_2:
		if (len <= 2)
			return emit_line(p, t, NULL, NULL);
		buf += 2;
		len -= 2;
		break;
	case LINE_TITLE_3:
		if (len <= 3)
			return emit_line(p, t, NULL, NULL);
		buf += 3;
		len -= 3;
		break;
	default:
		/* unreachable */
		abort();
	}

	while (len > 0 && isspace(buf[0])) {
		buf++;
		len--;
	}

	if (len == 0)
		return emit_line(p, t, NULL, NULL);

	if (t == LINE_TITLE_1 && *p->title == '\0')
		strncpy(p->title, buf, MIN(sizeof(p->title)-1, len));

	if ((l = calloc(1, len+1)) == NULL)
		return 0;
	memcpy(l, buf, len);
	return emit_line(p, t, l, NULL);
}

static int
parse_item(struct parser *p, enum line_type t, const char *buf, size_t len)
{
	char *l;

	if (len == 1)
		return emit_line(p, t, NULL, NULL);

	buf++;
	len--;

	while (len > 0 && isspace(buf[0])) {
		buf++;
		len--;
	}

	if (len == 0)
		return emit_line(p, t, NULL, NULL);

	if ((l = calloc(1, len+1)) == NULL)
		return 0;
	memcpy(l, buf, len);
	return emit_line(p, t, l, NULL);
}

static int
parse_quote(struct parser *p, enum line_type t, const char *buf, size_t len)
{
	char *l;

	if (len == 1)
		return emit_line(p, t, NULL, NULL);

	buf++;
	len--;

	while (len > 0 && isspace(buf[0])) {
		buf++;
		len--;
	}

	if (len == 0)
		return emit_line(p, t, NULL, NULL);

	if ((l = calloc(1, len+1)) == NULL)
		return 0;
	memcpy(l, buf, len);
	return emit_line(p, t, l, NULL);
}

static int
parse_pre_start(struct parser *p, enum line_type t, const char *buf, size_t len)
{
	char *l;

	if (len <= 3)
		return emit_line(p, t, NULL, NULL);

	buf += 3;
	len -= 3;

	while (len > 0 && isspace(buf[0])) {
		buf++;
		len--;
	}

	if (len == 0)
		return emit_line(p, t, NULL, NULL);

	if ((l = calloc(1, len+1)) == NULL)
		return 0;

	memcpy(l, buf, len);
	return emit_line(p, t, l, NULL);
}

static int
parse_pre_cnt(struct parser *p, enum line_type t, const char *buf, size_t len)
{
	char *l;

	if (len == 0)
		return emit_line(p, t, NULL, NULL);

	if ((l = calloc(1, len+1)) == NULL)
		return 0;
	memcpy(l, buf, len);
	return emit_line(p, t, l, NULL);
}

static int
parse_pre_end(struct parser *p, enum line_type t, const char *buf, size_t len)
{
	return emit_line(p, t, NULL, NULL);
}

static inline enum line_type
detect_line_type(const char *buf, size_t len, int in_pre)
{
	if (in_pre) {
		if (len >= 3 &&
		    buf[0] == '`' && buf[1] == '`' && buf[2] == '`')
			return LINE_PRE_END;
		else
			return LINE_PRE_CONTENT;
	}

	if (len == 0)
		return LINE_TEXT;

	switch (*buf) {
	case '*': return LINE_ITEM;
	case '>': return LINE_QUOTE;
	case '=':
		if (len >= 1 && buf[1] == '>')
			return LINE_LINK;
		break;
	case '#':
		if (len == 1)
			return LINE_TEXT;
		if (buf[1] != '#')
			return LINE_TITLE_1;
		if (len == 2)
			return LINE_TEXT;
		if (buf[2] != '#')
			return LINE_TITLE_2;
		if (len == 3)
			return LINE_TEXT;
		return LINE_TITLE_3;
	case '`':
		if (len < 3)
			return LINE_TEXT;
		if (buf[0] == '`' && buf[1] == '`' && buf[2] == '`')
			return LINE_PRE_START;
		break;
	}

	return LINE_TEXT;
}

static int
gemtext_parse(struct parser *p, const char *buf, size_t size)
{
	return parser_foreach_line(p, buf, size, gemtext_foreach_line);
}

static int
gemtext_foreach_line(struct parser *p, const char *line, size_t linelen)
{
	enum line_type t;

	t = detect_line_type(line, linelen, p->flags & PARSER_IN_PRE);
	if (t == LINE_PRE_START)
		p->flags ^= PARSER_IN_PRE;
	if (t == LINE_PRE_END)
		p->flags ^= PARSER_IN_PRE;
	return parsers[t](p, t, line, linelen);
}

static int
gemtext_free(struct parser *p)
{
	enum line_type	t;

	/* flush the buffer */
	if (p->len != 0) {
		t = detect_line_type(p->buf, p->len, p->flags & PARSER_IN_PRE);
		if (!parsers[t](p, t, p->buf, p->len))
			return 0;
		if ((p->flags & PARSER_IN_PRE) &&
		    !emit_line(p, LINE_PRE_END, NULL, NULL))
			return 0;
	}

	free(p->buf);

	/*
	 * use the first level 2 or 3 header as page title if none
	 * found yet.
	 */
	if (*p->title == '\0')
		search_title(p, LINE_TITLE_2);
	if (*p->title == '\0')
		search_title(p, LINE_TITLE_3);

	return 1;
}

static void
search_title(struct parser *p, enum line_type level)
{
	struct line *l;

	TAILQ_FOREACH(l, &p->head, lines) {
		if (l->type == level) {
			if (l->line == NULL)
				continue;
			strlcpy(p->title, l->line, sizeof(p->title));
			break;
		}
	}
}