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