2 75a8a1ec 2022-02-08 op * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
4 75a8a1ec 2022-02-08 op * Permission to use, copy, modify, and distribute this software for any
5 75a8a1ec 2022-02-08 op * purpose with or without fee is hereby granted, provided that the above
6 75a8a1ec 2022-02-08 op * copyright notice and this permission notice appear in all copies.
8 75a8a1ec 2022-02-08 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 75a8a1ec 2022-02-08 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 75a8a1ec 2022-02-08 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 75a8a1ec 2022-02-08 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 75a8a1ec 2022-02-08 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 75a8a1ec 2022-02-08 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 75a8a1ec 2022-02-08 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 75a8a1ec 2022-02-08 op * A streaming gemtext parser.
21 75a8a1ec 2022-02-08 op * - handle NULs
25 75a8a1ec 2022-02-08 op #include "compat.h"
27 75a8a1ec 2022-02-08 op #include <ctype.h>
28 75a8a1ec 2022-02-08 op #include <string.h>
29 75a8a1ec 2022-02-08 op #include <stdlib.h>
31 75a8a1ec 2022-02-08 op #include "defaults.h"
32 75a8a1ec 2022-02-08 op #include "parser.h"
33 75a8a1ec 2022-02-08 op #include "utf8.h"
35 75a8a1ec 2022-02-08 op static int gemtext_parse(struct parser *, const char *, size_t);
36 75a8a1ec 2022-02-08 op static int gemtext_foreach_line(struct parser *, const char *, size_t);
37 75a8a1ec 2022-02-08 op static int gemtext_free(struct parser *);
38 75a8a1ec 2022-02-08 op static int gemtext_serialize(struct parser *, struct evbuffer *);
40 75a8a1ec 2022-02-08 op static int parse_text(struct parser*, enum line_type, const char*, size_t);
41 75a8a1ec 2022-02-08 op static int parse_link(struct parser*, enum line_type, const char*, size_t);
42 75a8a1ec 2022-02-08 op static int parse_title(struct parser*, enum line_type, const char*, size_t);
43 75a8a1ec 2022-02-08 op static int parse_item(struct parser*, enum line_type, const char*, size_t);
44 75a8a1ec 2022-02-08 op static int parse_quote(struct parser*, enum line_type, const char*, size_t);
45 75a8a1ec 2022-02-08 op static int parse_pre_start(struct parser*, enum line_type, const char*, size_t);
46 75a8a1ec 2022-02-08 op static int parse_pre_cnt(struct parser*, enum line_type, const char*, size_t);
47 75a8a1ec 2022-02-08 op static int parse_pre_end(struct parser*, enum line_type, const char*, size_t);
48 75a8a1ec 2022-02-08 op static void search_title(struct parser*, enum line_type);
50 75a8a1ec 2022-02-08 op typedef int (parselinefn)(struct parser*, enum line_type, const char*, size_t);
52 75a8a1ec 2022-02-08 op static parselinefn *parsers[] = {
53 75a8a1ec 2022-02-08 op [LINE_TEXT] = parse_text,
54 75a8a1ec 2022-02-08 op [LINE_LINK] = parse_link,
55 75a8a1ec 2022-02-08 op [LINE_TITLE_1] = parse_title,
56 75a8a1ec 2022-02-08 op [LINE_TITLE_2] = parse_title,
57 75a8a1ec 2022-02-08 op [LINE_TITLE_3] = parse_title,
58 75a8a1ec 2022-02-08 op [LINE_ITEM] = parse_item,
59 75a8a1ec 2022-02-08 op [LINE_QUOTE] = parse_quote,
60 75a8a1ec 2022-02-08 op [LINE_PRE_START] = parse_pre_start,
61 75a8a1ec 2022-02-08 op [LINE_PRE_CONTENT] = parse_pre_cnt,
62 75a8a1ec 2022-02-08 op [LINE_PRE_END] = parse_pre_end,
66 75a8a1ec 2022-02-08 op gemtext_initparser(struct parser *p)
68 75a8a1ec 2022-02-08 op memset(p, 0, sizeof(*p));
70 75a8a1ec 2022-02-08 op p->name = "text/gemini";
71 75a8a1ec 2022-02-08 op p->parse = &gemtext_parse;
72 75a8a1ec 2022-02-08 op p->free = &gemtext_free;
73 75a8a1ec 2022-02-08 op p->serialize = &gemtext_serialize;
75 75a8a1ec 2022-02-08 op TAILQ_INIT(&p->head);
78 75a8a1ec 2022-02-08 op static inline int
79 75a8a1ec 2022-02-08 op emit_line(struct parser *p, enum line_type type, char *line, char *alt)
81 75a8a1ec 2022-02-08 op struct line *l;
83 75a8a1ec 2022-02-08 op if ((l = calloc(1, sizeof(*l))) == NULL)
86 75a8a1ec 2022-02-08 op l->type = type;
87 75a8a1ec 2022-02-08 op l->line = line;
90 75a8a1ec 2022-02-08 op switch (l->type) {
91 75a8a1ec 2022-02-08 op case LINE_PRE_START:
92 75a8a1ec 2022-02-08 op case LINE_PRE_END:
93 75a8a1ec 2022-02-08 op if (hide_pre_context)
94 75a8a1ec 2022-02-08 op l->flags = L_HIDDEN;
95 75a8a1ec 2022-02-08 op if (l->type == LINE_PRE_END &&
96 75a8a1ec 2022-02-08 op hide_pre_closing_line)
97 75a8a1ec 2022-02-08 op l->flags = L_HIDDEN;
99 75a8a1ec 2022-02-08 op case LINE_PRE_CONTENT:
100 75a8a1ec 2022-02-08 op if (hide_pre_blocks)
101 75a8a1ec 2022-02-08 op l->flags = L_HIDDEN;
103 75a8a1ec 2022-02-08 op case LINE_LINK:
104 75a8a1ec 2022-02-08 op if (emojify_link &&
105 75a8a1ec 2022-02-08 op !emojied_line(line, (const char **)&l->data))
106 75a8a1ec 2022-02-08 op l->data = NULL;
112 75a8a1ec 2022-02-08 op TAILQ_INSERT_TAIL(&p->head, l, lines);
118 75a8a1ec 2022-02-08 op parse_text(struct parser *p, enum line_type t, const char *buf, size_t len)
122 75a8a1ec 2022-02-08 op if ((l = calloc(1, len+1)) == NULL)
124 75a8a1ec 2022-02-08 op memcpy(l, buf, len);
125 75a8a1ec 2022-02-08 op return emit_line(p, t, l, NULL);
129 75a8a1ec 2022-02-08 op parse_link(struct parser *p, enum line_type t, const char *buf, size_t len)
132 75a8a1ec 2022-02-08 op const char *url_start;
134 75a8a1ec 2022-02-08 op if (len <= 2)
135 75a8a1ec 2022-02-08 op return emit_line(p, LINE_TEXT, NULL, NULL);
139 75a8a1ec 2022-02-08 op while (len > 0 && isspace(buf[0])) {
144 75a8a1ec 2022-02-08 op if (len == 0)
145 75a8a1ec 2022-02-08 op return emit_line(p, LINE_TEXT, NULL, NULL);
147 75a8a1ec 2022-02-08 op url_start = buf;
148 75a8a1ec 2022-02-08 op while (len > 0 && !isspace(buf[0])) {
153 75a8a1ec 2022-02-08 op if ((u = calloc(1, buf - url_start + 1)) == NULL)
155 75a8a1ec 2022-02-08 op memcpy(u, url_start, buf - url_start);
157 75a8a1ec 2022-02-08 op if (len == 0)
158 75a8a1ec 2022-02-08 op goto nolabel;
160 75a8a1ec 2022-02-08 op while (len > 0 && isspace(buf[0])) {
165 75a8a1ec 2022-02-08 op if (len == 0)
166 75a8a1ec 2022-02-08 op goto nolabel;
168 75a8a1ec 2022-02-08 op if ((l = calloc(1, len + 1)) == NULL)
171 75a8a1ec 2022-02-08 op memcpy(l, buf, len);
172 75a8a1ec 2022-02-08 op return emit_line(p, t, l, u);
175 75a8a1ec 2022-02-08 op if ((l = strdup(u)) == NULL)
177 75a8a1ec 2022-02-08 op return emit_line(p, t, l, u);
181 75a8a1ec 2022-02-08 op parse_title(struct parser *p, enum line_type t, const char *buf, size_t len)
186 75a8a1ec 2022-02-08 op case LINE_TITLE_1:
187 75a8a1ec 2022-02-08 op if (len <= 1)
188 75a8a1ec 2022-02-08 op return emit_line(p, t, NULL, NULL);
192 75a8a1ec 2022-02-08 op case LINE_TITLE_2:
193 75a8a1ec 2022-02-08 op if (len <= 2)
194 75a8a1ec 2022-02-08 op return emit_line(p, t, NULL, NULL);
198 75a8a1ec 2022-02-08 op case LINE_TITLE_3:
199 75a8a1ec 2022-02-08 op if (len <= 3)
200 75a8a1ec 2022-02-08 op return emit_line(p, t, NULL, NULL);
205 75a8a1ec 2022-02-08 op /* unreachable */
209 75a8a1ec 2022-02-08 op while (len > 0 && isspace(buf[0])) {
214 75a8a1ec 2022-02-08 op if (len == 0)
215 75a8a1ec 2022-02-08 op return emit_line(p, t, NULL, NULL);
217 75a8a1ec 2022-02-08 op if (t == LINE_TITLE_1 && *p->title == '\0')
218 75a8a1ec 2022-02-08 op strncpy(p->title, buf, MIN(sizeof(p->title)-1, len));
220 75a8a1ec 2022-02-08 op if ((l = calloc(1, len+1)) == NULL)
222 75a8a1ec 2022-02-08 op memcpy(l, buf, len);
223 75a8a1ec 2022-02-08 op return emit_line(p, t, l, NULL);
227 75a8a1ec 2022-02-08 op parse_item(struct parser *p, enum line_type t, const char *buf, size_t len)
231 75a8a1ec 2022-02-08 op if (len == 1)
232 75a8a1ec 2022-02-08 op return emit_line(p, t, NULL, NULL);
237 75a8a1ec 2022-02-08 op while (len > 0 && isspace(buf[0])) {
242 75a8a1ec 2022-02-08 op if (len == 0)
243 75a8a1ec 2022-02-08 op return emit_line(p, t, NULL, NULL);
245 75a8a1ec 2022-02-08 op if ((l = calloc(1, len+1)) == NULL)
247 75a8a1ec 2022-02-08 op memcpy(l, buf, len);
248 75a8a1ec 2022-02-08 op return emit_line(p, t, l, NULL);
252 75a8a1ec 2022-02-08 op parse_quote(struct parser *p, enum line_type t, const char *buf, size_t len)
256 75a8a1ec 2022-02-08 op if (len == 1)
257 75a8a1ec 2022-02-08 op return emit_line(p, t, NULL, NULL);
262 75a8a1ec 2022-02-08 op while (len > 0 && isspace(buf[0])) {
267 75a8a1ec 2022-02-08 op if (len == 0)
268 75a8a1ec 2022-02-08 op return emit_line(p, t, NULL, NULL);
270 75a8a1ec 2022-02-08 op if ((l = calloc(1, len+1)) == NULL)
272 75a8a1ec 2022-02-08 op memcpy(l, buf, len);
273 75a8a1ec 2022-02-08 op return emit_line(p, t, l, NULL);
277 75a8a1ec 2022-02-08 op parse_pre_start(struct parser *p, enum line_type t, const char *buf, size_t len)
281 75a8a1ec 2022-02-08 op if (len <= 3)
282 75a8a1ec 2022-02-08 op return emit_line(p, t, NULL, NULL);
287 75a8a1ec 2022-02-08 op while (len > 0 && isspace(buf[0])) {
292 75a8a1ec 2022-02-08 op if (len == 0)
293 75a8a1ec 2022-02-08 op return emit_line(p, t, NULL, NULL);
295 75a8a1ec 2022-02-08 op if ((l = calloc(1, len+1)) == NULL)
298 75a8a1ec 2022-02-08 op memcpy(l, buf, len);
299 75a8a1ec 2022-02-08 op return emit_line(p, t, l, NULL);
303 75a8a1ec 2022-02-08 op parse_pre_cnt(struct parser *p, enum line_type t, const char *buf, size_t len)
307 75a8a1ec 2022-02-08 op if (len == 0)
308 75a8a1ec 2022-02-08 op return emit_line(p, t, NULL, NULL);
310 75a8a1ec 2022-02-08 op if ((l = calloc(1, len+1)) == NULL)
312 75a8a1ec 2022-02-08 op memcpy(l, buf, len);
313 75a8a1ec 2022-02-08 op return emit_line(p, t, l, NULL);
317 75a8a1ec 2022-02-08 op parse_pre_end(struct parser *p, enum line_type t, const char *buf, size_t len)
319 75a8a1ec 2022-02-08 op return emit_line(p, t, NULL, NULL);
322 75a8a1ec 2022-02-08 op static inline enum line_type
323 75a8a1ec 2022-02-08 op detect_line_type(const char *buf, size_t len, int in_pre)
325 75a8a1ec 2022-02-08 op if (in_pre) {
326 75a8a1ec 2022-02-08 op if (len >= 3 &&
327 75a8a1ec 2022-02-08 op buf[0] == '`' && buf[1] == '`' && buf[2] == '`')
328 75a8a1ec 2022-02-08 op return LINE_PRE_END;
330 75a8a1ec 2022-02-08 op return LINE_PRE_CONTENT;
333 75a8a1ec 2022-02-08 op if (len == 0)
334 75a8a1ec 2022-02-08 op return LINE_TEXT;
336 75a8a1ec 2022-02-08 op switch (*buf) {
338 75a8a1ec 2022-02-08 op if (len > 1 && buf[1] == ' ')
339 75a8a1ec 2022-02-08 op return LINE_ITEM;
341 75a8a1ec 2022-02-08 op case '>': return LINE_QUOTE;
343 75a8a1ec 2022-02-08 op if (len >= 1 && buf[1] == '>')
344 75a8a1ec 2022-02-08 op return LINE_LINK;
347 75a8a1ec 2022-02-08 op if (len == 1)
348 75a8a1ec 2022-02-08 op return LINE_TEXT;
349 75a8a1ec 2022-02-08 op if (buf[1] != '#')
350 75a8a1ec 2022-02-08 op return LINE_TITLE_1;
351 75a8a1ec 2022-02-08 op if (len == 2)
352 75a8a1ec 2022-02-08 op return LINE_TEXT;
353 75a8a1ec 2022-02-08 op if (buf[2] != '#')
354 75a8a1ec 2022-02-08 op return LINE_TITLE_2;
355 75a8a1ec 2022-02-08 op if (len == 3)
356 75a8a1ec 2022-02-08 op return LINE_TEXT;
357 75a8a1ec 2022-02-08 op return LINE_TITLE_3;
360 75a8a1ec 2022-02-08 op return LINE_TEXT;
361 75a8a1ec 2022-02-08 op if (buf[0] == '`' && buf[1] == '`' && buf[2] == '`')
362 75a8a1ec 2022-02-08 op return LINE_PRE_START;
366 75a8a1ec 2022-02-08 op return LINE_TEXT;
370 75a8a1ec 2022-02-08 op gemtext_parse(struct parser *p, const char *buf, size_t size)
372 75a8a1ec 2022-02-08 op return parser_foreach_line(p, buf, size, gemtext_foreach_line);
376 75a8a1ec 2022-02-08 op gemtext_foreach_line(struct parser *p, const char *line, size_t linelen)
378 75a8a1ec 2022-02-08 op enum line_type t;
380 75a8a1ec 2022-02-08 op t = detect_line_type(line, linelen, p->flags & PARSER_IN_PRE);
381 75a8a1ec 2022-02-08 op if (t == LINE_PRE_START)
382 75a8a1ec 2022-02-08 op p->flags ^= PARSER_IN_PRE;
383 75a8a1ec 2022-02-08 op if (t == LINE_PRE_END)
384 75a8a1ec 2022-02-08 op p->flags ^= PARSER_IN_PRE;
385 75a8a1ec 2022-02-08 op return parsers[t](p, t, line, linelen);
389 75a8a1ec 2022-02-08 op gemtext_free(struct parser *p)
391 75a8a1ec 2022-02-08 op enum line_type t;
393 75a8a1ec 2022-02-08 op /* flush the buffer */
394 75a8a1ec 2022-02-08 op if (p->len != 0) {
395 75a8a1ec 2022-02-08 op t = detect_line_type(p->buf, p->len, p->flags & PARSER_IN_PRE);
396 75a8a1ec 2022-02-08 op if (!parsers[t](p, t, p->buf, p->len))
398 75a8a1ec 2022-02-08 op if ((p->flags & PARSER_IN_PRE) &&
399 75a8a1ec 2022-02-08 op !emit_line(p, LINE_PRE_END, NULL, NULL))
403 75a8a1ec 2022-02-08 op free(p->buf);
406 75a8a1ec 2022-02-08 op * use the first level 2 or 3 header as page title if none
409 75a8a1ec 2022-02-08 op if (*p->title == '\0')
410 75a8a1ec 2022-02-08 op search_title(p, LINE_TITLE_2);
411 75a8a1ec 2022-02-08 op if (*p->title == '\0')
412 75a8a1ec 2022-02-08 op search_title(p, LINE_TITLE_3);
418 75a8a1ec 2022-02-08 op search_title(struct parser *p, enum line_type level)
420 75a8a1ec 2022-02-08 op struct line *l;
422 75a8a1ec 2022-02-08 op TAILQ_FOREACH(l, &p->head, lines) {
423 75a8a1ec 2022-02-08 op if (l->type == level) {
424 75a8a1ec 2022-02-08 op if (l->line == NULL)
426 75a8a1ec 2022-02-08 op strlcpy(p->title, l->line, sizeof(p->title));
432 75a8a1ec 2022-02-08 op static const char *gemtext_prefixes[] = {
433 75a8a1ec 2022-02-08 op [LINE_TEXT] = "",
434 75a8a1ec 2022-02-08 op [LINE_TITLE_1] = "# ",
435 75a8a1ec 2022-02-08 op [LINE_TITLE_2] = "## ",
436 75a8a1ec 2022-02-08 op [LINE_TITLE_3] = "### ",
437 75a8a1ec 2022-02-08 op [LINE_ITEM] = "* ",
438 75a8a1ec 2022-02-08 op [LINE_QUOTE] = "> ",
439 75a8a1ec 2022-02-08 op [LINE_PRE_START] = "``` ",
440 75a8a1ec 2022-02-08 op [LINE_PRE_CONTENT] = "",
441 75a8a1ec 2022-02-08 op [LINE_PRE_END] = "```",
445 75a8a1ec 2022-02-08 op gemtext_serialize(struct parser *p, struct evbuffer *evb)
447 75a8a1ec 2022-02-08 op struct line *line;
448 75a8a1ec 2022-02-08 op const char *text;
449 75a8a1ec 2022-02-08 op const char *alt;
452 75a8a1ec 2022-02-08 op TAILQ_FOREACH(line, &p->head, lines) {
453 75a8a1ec 2022-02-08 op if ((text = line->line) == NULL)
456 75a8a1ec 2022-02-08 op if ((alt = line->alt) == NULL)
459 75a8a1ec 2022-02-08 op switch (line->type) {
460 75a8a1ec 2022-02-08 op case LINE_TEXT:
461 75a8a1ec 2022-02-08 op case LINE_TITLE_1:
462 75a8a1ec 2022-02-08 op case LINE_TITLE_2:
463 75a8a1ec 2022-02-08 op case LINE_TITLE_3:
464 75a8a1ec 2022-02-08 op case LINE_ITEM:
465 75a8a1ec 2022-02-08 op case LINE_QUOTE:
466 75a8a1ec 2022-02-08 op case LINE_PRE_START:
467 75a8a1ec 2022-02-08 op case LINE_PRE_CONTENT:
468 75a8a1ec 2022-02-08 op case LINE_PRE_END:
469 75a8a1ec 2022-02-08 op r = evbuffer_add_printf(evb, "%s%s\n",
470 75a8a1ec 2022-02-08 op gemtext_prefixes[line->type], text);
473 75a8a1ec 2022-02-08 op case LINE_LINK:
474 75a8a1ec 2022-02-08 op r = evbuffer_add_printf(evb, "=> %s %s\n",
479 75a8a1ec 2022-02-08 op /* not reached */