2 5e11c00c 2021-03-02 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 5e11c00c 2021-03-02 op * Permission to use, copy, modify, and distribute this software for any
5 5e11c00c 2021-03-02 op * purpose with or without fee is hereby granted, provided that the above
6 5e11c00c 2021-03-02 op * copyright notice and this permission notice appear in all copies.
8 5e11c00c 2021-03-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5e11c00c 2021-03-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5e11c00c 2021-03-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5e11c00c 2021-03-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5e11c00c 2021-03-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5e11c00c 2021-03-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5e11c00c 2021-03-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 5e11c00c 2021-03-02 op * A streaming gemtext parser.
21 5e11c00c 2021-03-02 op * - handle NULs
25 5f2dc0bd 2021-07-13 op #include "defaults.h"
26 395b9f4e 2021-07-12 op #include "parser.h"
27 a4ec04c5 2021-05-13 op #include "telescope.h"
29 5e11c00c 2021-03-02 op #include <ctype.h>
30 5e11c00c 2021-03-02 op #include <string.h>
31 5e11c00c 2021-03-02 op #include <stdlib.h>
33 5e11c00c 2021-03-02 op static int gemtext_parse(struct parser*, const char*, size_t);
34 a5845bb5 2021-03-20 op static int gemtext_foreach_line(struct parser*, const char*, size_t);
35 a5c3e03d 2021-03-02 op static int gemtext_free(struct parser*);
37 5e11c00c 2021-03-02 op static int parse_text(struct parser*, enum line_type, const char*, size_t);
38 5e11c00c 2021-03-02 op static int parse_link(struct parser*, enum line_type, const char*, size_t);
39 5e11c00c 2021-03-02 op static int parse_title(struct parser*, enum line_type, const char*, size_t);
40 5e11c00c 2021-03-02 op static int parse_item(struct parser*, enum line_type, const char*, size_t);
41 5e11c00c 2021-03-02 op static int parse_quote(struct parser*, enum line_type, const char*, size_t);
42 5e11c00c 2021-03-02 op static int parse_pre_start(struct parser*, enum line_type, const char*, size_t);
43 5e11c00c 2021-03-02 op static int parse_pre_cnt(struct parser*, enum line_type, const char*, size_t);
44 5e11c00c 2021-03-02 op static int parse_pre_end(struct parser*, enum line_type, const char*, size_t);
45 48e3f26d 2021-05-13 op static void search_title(struct parser*, enum line_type);
47 5e11c00c 2021-03-02 op typedef int (parselinefn)(struct parser*, enum line_type, const char*, size_t);
49 5e11c00c 2021-03-02 op static parselinefn *parsers[] = {
50 3902f7b9 2021-07-14 op [LINE_TEXT] = parse_text,
51 3902f7b9 2021-07-14 op [LINE_LINK] = parse_link,
52 3902f7b9 2021-07-14 op [LINE_TITLE_1] = parse_title,
53 6b80b8fe 2021-07-14 op [LINE_TITLE_2] = parse_title,
54 3902f7b9 2021-07-14 op [LINE_TITLE_3] = parse_title,
55 6b80b8fe 2021-07-14 op [LINE_ITEM] = parse_item,
56 3902f7b9 2021-07-14 op [LINE_QUOTE] = parse_quote,
57 3902f7b9 2021-07-14 op [LINE_PRE_START] = parse_pre_start,
58 6b80b8fe 2021-07-14 op [LINE_PRE_CONTENT] = parse_pre_cnt,
59 3902f7b9 2021-07-14 op [LINE_PRE_END] = parse_pre_end,
63 5e11c00c 2021-03-02 op gemtext_initparser(struct parser *p)
65 5e11c00c 2021-03-02 op memset(p, 0, sizeof(*p));
67 fc43eadd 2021-03-12 op p->name = "text/gemini";
68 5e11c00c 2021-03-02 op p->parse = &gemtext_parse;
69 5e11c00c 2021-03-02 op p->free = &gemtext_free;
72 5e11c00c 2021-03-02 op static inline int
73 5e11c00c 2021-03-02 op emit_line(struct parser *p, enum line_type type, char *line, char *alt)
75 5e11c00c 2021-03-02 op struct line *l;
77 5e11c00c 2021-03-02 op if ((l = calloc(1, sizeof(*l))) == NULL)
80 5e11c00c 2021-03-02 op l->type = type;
81 5e11c00c 2021-03-02 op l->line = line;
82 e9beee55 2021-07-14 op l->meta.alt = alt;
84 b1379f34 2021-07-05 op switch (l->type) {
85 b1379f34 2021-07-05 op case LINE_PRE_START:
86 b1379f34 2021-07-05 op case LINE_PRE_END:
87 b1379f34 2021-07-05 op if (hide_pre_context)
88 b1379f34 2021-07-05 op l->flags = L_HIDDEN;
89 144733a7 2021-07-15 op if (l->type == LINE_PRE_END &&
90 144733a7 2021-07-15 op hide_pre_closing_line)
91 144733a7 2021-07-15 op l->flags = L_HIDDEN;
93 b1379f34 2021-07-05 op case LINE_PRE_CONTENT:
94 b1379f34 2021-07-05 op if (hide_pre_blocks)
95 b1379f34 2021-07-05 op l->flags = L_HIDDEN;
102 5e11c00c 2021-03-02 op if (TAILQ_EMPTY(&p->head))
103 5e11c00c 2021-03-02 op TAILQ_INSERT_HEAD(&p->head, l, lines);
105 5e11c00c 2021-03-02 op TAILQ_INSERT_TAIL(&p->head, l, lines);
111 5e11c00c 2021-03-02 op parse_text(struct parser *p, enum line_type t, const char *buf, size_t len)
115 5e11c00c 2021-03-02 op if ((l = calloc(1, len+1)) == NULL)
117 5e11c00c 2021-03-02 op memcpy(l, buf, len);
118 5e11c00c 2021-03-02 op return emit_line(p, t, l, NULL);
122 5e11c00c 2021-03-02 op parse_link(struct parser *p, enum line_type t, const char *buf, size_t len)
125 5e11c00c 2021-03-02 op const char *url_start;
127 5e11c00c 2021-03-02 op if (len <= 2)
128 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
132 5e11c00c 2021-03-02 op while (len > 0 && isspace(buf[0])) {
137 5e11c00c 2021-03-02 op if (len == 0)
138 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
140 5e11c00c 2021-03-02 op url_start = buf;
141 5e11c00c 2021-03-02 op while (len > 0 && !isspace(buf[0])) {
146 5e11c00c 2021-03-02 op if ((u = calloc(1, buf - url_start + 1)) == NULL)
148 5e11c00c 2021-03-02 op memcpy(u, url_start, buf - url_start);
150 5e11c00c 2021-03-02 op if (len == 0)
151 f250a75b 2021-03-06 op goto nolabel;
153 f250a75b 2021-03-06 op while (len > 0 && isspace(buf[0])) {
158 5e11c00c 2021-03-02 op if (len == 0)
159 f250a75b 2021-03-06 op goto nolabel;
161 5e11c00c 2021-03-02 op if ((l = calloc(1, len + 1)) == NULL)
164 5e11c00c 2021-03-02 op memcpy(l, buf, len);
165 f250a75b 2021-03-06 op return emit_line(p, t, l, u);
168 f250a75b 2021-03-06 op if ((l = strdup(u)) == NULL)
170 f250a75b 2021-03-06 op return emit_line(p, t, l, u);
174 5e11c00c 2021-03-02 op parse_title(struct parser *p, enum line_type t, const char *buf, size_t len)
179 5e11c00c 2021-03-02 op case LINE_TITLE_1:
180 5e11c00c 2021-03-02 op if (len <= 1)
181 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
185 5e11c00c 2021-03-02 op case LINE_TITLE_2:
186 5e11c00c 2021-03-02 op if (len <= 2)
187 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
191 5e11c00c 2021-03-02 op case LINE_TITLE_3:
192 5e11c00c 2021-03-02 op if (len <= 3)
193 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
198 5e11c00c 2021-03-02 op /* unreachable */
202 5e11c00c 2021-03-02 op while (len > 0 && isspace(buf[0])) {
207 5e11c00c 2021-03-02 op if (len == 0)
208 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
210 dc5df781 2021-03-13 op if (t == LINE_TITLE_1 && *p->title == '\0')
211 dc5df781 2021-03-13 op strncpy(p->title, buf, MIN(sizeof(p->title)-1, len));
213 5e11c00c 2021-03-02 op if ((l = calloc(1, len+1)) == NULL)
215 5e11c00c 2021-03-02 op memcpy(l, buf, len);
216 5e11c00c 2021-03-02 op return emit_line(p, t, l, NULL);
220 5e11c00c 2021-03-02 op parse_item(struct parser *p, enum line_type t, const char *buf, size_t len)
224 5e11c00c 2021-03-02 op if (len == 1)
225 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
230 5e11c00c 2021-03-02 op while (len > 0 && isspace(buf[0])) {
235 5e11c00c 2021-03-02 op if (len == 0)
236 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
238 5e11c00c 2021-03-02 op if ((l = calloc(1, len+1)) == NULL)
240 5e11c00c 2021-03-02 op memcpy(l, buf, len);
241 5e11c00c 2021-03-02 op return emit_line(p, t, l, NULL);
245 5e11c00c 2021-03-02 op parse_quote(struct parser *p, enum line_type t, const char *buf, size_t len)
249 5e11c00c 2021-03-02 op if (len == 1)
250 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
255 5e11c00c 2021-03-02 op while (len > 0 && isspace(buf[0])) {
260 5e11c00c 2021-03-02 op if (len == 0)
261 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
263 5e11c00c 2021-03-02 op if ((l = calloc(1, len+1)) == NULL)
265 5e11c00c 2021-03-02 op memcpy(l, buf, len);
266 5e11c00c 2021-03-02 op return emit_line(p, t, l, NULL);
270 5e11c00c 2021-03-02 op parse_pre_start(struct parser *p, enum line_type t, const char *buf, size_t len)
274 5e11c00c 2021-03-02 op if (len <= 3)
275 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
280 5e11c00c 2021-03-02 op while (len > 0 && isspace(buf[0])) {
285 5e11c00c 2021-03-02 op if (len == 0)
286 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
288 5e11c00c 2021-03-02 op if ((l = calloc(1, len+1)) == NULL)
291 5e11c00c 2021-03-02 op memcpy(l, buf, len);
292 e2060f05 2021-03-17 op return emit_line(p, t, l, NULL);
296 5e11c00c 2021-03-02 op parse_pre_cnt(struct parser *p, enum line_type t, const char *buf, size_t len)
300 5e11c00c 2021-03-02 op if (len == 0)
301 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
303 5e11c00c 2021-03-02 op if ((l = calloc(1, len+1)) == NULL)
305 5e11c00c 2021-03-02 op memcpy(l, buf, len);
306 5e11c00c 2021-03-02 op return emit_line(p, t, l, NULL);
310 5e11c00c 2021-03-02 op parse_pre_end(struct parser *p, enum line_type t, const char *buf, size_t len)
312 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
315 5e11c00c 2021-03-02 op static inline enum line_type
316 5e11c00c 2021-03-02 op detect_line_type(const char *buf, size_t len, int in_pre)
318 5e11c00c 2021-03-02 op if (in_pre) {
319 5e11c00c 2021-03-02 op if (len >= 3 &&
320 5e11c00c 2021-03-02 op buf[0] == '`' && buf[1] == '`' && buf[2] == '`')
321 5e11c00c 2021-03-02 op return LINE_PRE_END;
323 5e11c00c 2021-03-02 op return LINE_PRE_CONTENT;
326 1575cb8a 2021-07-07 op if (len == 0)
327 1575cb8a 2021-07-07 op return LINE_TEXT;
329 5e11c00c 2021-03-02 op switch (*buf) {
330 5e11c00c 2021-03-02 op case '*': return LINE_ITEM;
331 5e11c00c 2021-03-02 op case '>': return LINE_QUOTE;
333 5e11c00c 2021-03-02 op if (len >= 1 && buf[1] == '>')
334 5e11c00c 2021-03-02 op return LINE_LINK;
337 5e11c00c 2021-03-02 op if (len == 1)
338 5e11c00c 2021-03-02 op return LINE_TEXT;
339 5e11c00c 2021-03-02 op if (buf[1] != '#')
340 5e11c00c 2021-03-02 op return LINE_TITLE_1;
341 5e11c00c 2021-03-02 op if (len == 2)
342 5e11c00c 2021-03-02 op return LINE_TEXT;
343 5e11c00c 2021-03-02 op if (buf[2] != '#')
344 5e11c00c 2021-03-02 op return LINE_TITLE_2;
345 5e11c00c 2021-03-02 op if (len == 3)
346 5e11c00c 2021-03-02 op return LINE_TEXT;
347 5e11c00c 2021-03-02 op return LINE_TITLE_3;
350 5e11c00c 2021-03-02 op return LINE_TEXT;
351 5e11c00c 2021-03-02 op if (buf[0] == '`' && buf[1] == '`' && buf[2] == '`')
352 5e11c00c 2021-03-02 op return LINE_PRE_START;
356 5e11c00c 2021-03-02 op return LINE_TEXT;
360 5e11c00c 2021-03-02 op gemtext_parse(struct parser *p, const char *buf, size_t size)
362 a5845bb5 2021-03-20 op return parser_foreach_line(p, buf, size, gemtext_foreach_line);
366 a5845bb5 2021-03-20 op gemtext_foreach_line(struct parser *p, const char *line, size_t linelen)
368 a5845bb5 2021-03-20 op enum line_type t;
370 27dce34f 2021-07-06 op t = detect_line_type(line, linelen, p->flags & PARSER_IN_PRE);
371 a5845bb5 2021-03-20 op if (t == LINE_PRE_START)
372 27dce34f 2021-07-06 op p->flags ^= PARSER_IN_PRE;
373 a5845bb5 2021-03-20 op if (t == LINE_PRE_END)
374 27dce34f 2021-07-06 op p->flags ^= PARSER_IN_PRE;
375 a5845bb5 2021-03-20 op return parsers[t](p, t, line, linelen);
379 5e11c00c 2021-03-02 op gemtext_free(struct parser *p)
381 a5c3e03d 2021-03-02 op enum line_type t;
383 a5c3e03d 2021-03-02 op /* flush the buffer */
384 a5c3e03d 2021-03-02 op if (p->len != 0) {
385 27dce34f 2021-07-06 op t = detect_line_type(p->buf, p->len, p->flags & PARSER_IN_PRE);
386 a5c3e03d 2021-03-02 op if (!parsers[t](p, t, p->buf, p->len))
388 27dce34f 2021-07-06 op if ((p->flags & PARSER_IN_PRE) &&
389 27dce34f 2021-07-06 op !emit_line(p, LINE_PRE_END, NULL, NULL))
393 5e11c00c 2021-03-02 op free(p->buf);
396 48e3f26d 2021-05-13 op * use the first level 2 or 3 header as page title if none
399 48e3f26d 2021-05-13 op if (*p->title == '\0')
400 48e3f26d 2021-05-13 op search_title(p, LINE_TITLE_2);
401 48e3f26d 2021-05-13 op if (*p->title == '\0')
402 48e3f26d 2021-05-13 op search_title(p, LINE_TITLE_3);
408 48e3f26d 2021-05-13 op search_title(struct parser *p, enum line_type level)
410 48e3f26d 2021-05-13 op struct line *l;
412 48e3f26d 2021-05-13 op TAILQ_FOREACH(l, &p->head, lines) {
413 48e3f26d 2021-05-13 op if (l->type == level) {
414 48e3f26d 2021-05-13 op if (l->line == NULL)
416 48e3f26d 2021-05-13 op strlcpy(p->title, l->line, sizeof(p->title));