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 786e6deb 2021-07-21 op #include "compat.h"
27 5e11c00c 2021-03-02 op #include <ctype.h>
28 5e11c00c 2021-03-02 op #include <string.h>
29 5e11c00c 2021-03-02 op #include <stdlib.h>
31 786e6deb 2021-07-21 op #include "defaults.h"
32 786e6deb 2021-07-21 op #include "parser.h"
33 786e6deb 2021-07-21 op #include "utf8.h"
35 5e11c00c 2021-03-02 op static int gemtext_parse(struct parser*, const char*, size_t);
36 a5845bb5 2021-03-20 op static int gemtext_foreach_line(struct parser*, const char*, size_t);
37 a5c3e03d 2021-03-02 op static int gemtext_free(struct parser*);
39 5e11c00c 2021-03-02 op static int parse_text(struct parser*, enum line_type, const char*, size_t);
40 5e11c00c 2021-03-02 op static int parse_link(struct parser*, enum line_type, const char*, size_t);
41 5e11c00c 2021-03-02 op static int parse_title(struct parser*, enum line_type, const char*, size_t);
42 5e11c00c 2021-03-02 op static int parse_item(struct parser*, enum line_type, const char*, size_t);
43 5e11c00c 2021-03-02 op static int parse_quote(struct parser*, enum line_type, const char*, size_t);
44 5e11c00c 2021-03-02 op static int parse_pre_start(struct parser*, enum line_type, const char*, size_t);
45 5e11c00c 2021-03-02 op static int parse_pre_cnt(struct parser*, enum line_type, const char*, size_t);
46 5e11c00c 2021-03-02 op static int parse_pre_end(struct parser*, enum line_type, const char*, size_t);
47 48e3f26d 2021-05-13 op static void search_title(struct parser*, enum line_type);
49 5e11c00c 2021-03-02 op typedef int (parselinefn)(struct parser*, enum line_type, const char*, size_t);
51 5e11c00c 2021-03-02 op static parselinefn *parsers[] = {
52 3902f7b9 2021-07-14 op [LINE_TEXT] = parse_text,
53 3902f7b9 2021-07-14 op [LINE_LINK] = parse_link,
54 3902f7b9 2021-07-14 op [LINE_TITLE_1] = parse_title,
55 6b80b8fe 2021-07-14 op [LINE_TITLE_2] = parse_title,
56 3902f7b9 2021-07-14 op [LINE_TITLE_3] = parse_title,
57 6b80b8fe 2021-07-14 op [LINE_ITEM] = parse_item,
58 3902f7b9 2021-07-14 op [LINE_QUOTE] = parse_quote,
59 3902f7b9 2021-07-14 op [LINE_PRE_START] = parse_pre_start,
60 6b80b8fe 2021-07-14 op [LINE_PRE_CONTENT] = parse_pre_cnt,
61 3902f7b9 2021-07-14 op [LINE_PRE_END] = parse_pre_end,
65 5e11c00c 2021-03-02 op gemtext_initparser(struct parser *p)
67 5e11c00c 2021-03-02 op memset(p, 0, sizeof(*p));
69 fc43eadd 2021-03-12 op p->name = "text/gemini";
70 5e11c00c 2021-03-02 op p->parse = &gemtext_parse;
71 5e11c00c 2021-03-02 op p->free = &gemtext_free;
74 5e11c00c 2021-03-02 op static inline int
75 5e11c00c 2021-03-02 op emit_line(struct parser *p, enum line_type type, char *line, char *alt)
77 5e11c00c 2021-03-02 op struct line *l;
79 5e11c00c 2021-03-02 op if ((l = calloc(1, sizeof(*l))) == NULL)
82 5e11c00c 2021-03-02 op l->type = type;
83 5e11c00c 2021-03-02 op l->line = line;
86 b1379f34 2021-07-05 op switch (l->type) {
87 b1379f34 2021-07-05 op case LINE_PRE_START:
88 b1379f34 2021-07-05 op case LINE_PRE_END:
89 b1379f34 2021-07-05 op if (hide_pre_context)
90 b1379f34 2021-07-05 op l->flags = L_HIDDEN;
91 144733a7 2021-07-15 op if (l->type == LINE_PRE_END &&
92 144733a7 2021-07-15 op hide_pre_closing_line)
93 144733a7 2021-07-15 op l->flags = L_HIDDEN;
95 b1379f34 2021-07-05 op case LINE_PRE_CONTENT:
96 b1379f34 2021-07-05 op if (hide_pre_blocks)
97 b1379f34 2021-07-05 op l->flags = L_HIDDEN;
99 fbadd395 2021-07-16 op case LINE_LINK:
100 fbadd395 2021-07-16 op if (emojify_link &&
101 fbadd395 2021-07-16 op !emojied_line(line, (const char **)&l->data))
102 fbadd395 2021-07-16 op l->data = NULL;
108 5e11c00c 2021-03-02 op if (TAILQ_EMPTY(&p->head))
109 5e11c00c 2021-03-02 op TAILQ_INSERT_HEAD(&p->head, l, lines);
111 5e11c00c 2021-03-02 op TAILQ_INSERT_TAIL(&p->head, l, lines);
117 5e11c00c 2021-03-02 op parse_text(struct parser *p, enum line_type t, const char *buf, size_t len)
121 5e11c00c 2021-03-02 op if ((l = calloc(1, len+1)) == NULL)
123 5e11c00c 2021-03-02 op memcpy(l, buf, len);
124 5e11c00c 2021-03-02 op return emit_line(p, t, l, NULL);
128 5e11c00c 2021-03-02 op parse_link(struct parser *p, enum line_type t, const char *buf, size_t len)
131 5e11c00c 2021-03-02 op const char *url_start;
133 5e11c00c 2021-03-02 op if (len <= 2)
134 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
138 5e11c00c 2021-03-02 op while (len > 0 && isspace(buf[0])) {
143 5e11c00c 2021-03-02 op if (len == 0)
144 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
146 5e11c00c 2021-03-02 op url_start = buf;
147 5e11c00c 2021-03-02 op while (len > 0 && !isspace(buf[0])) {
152 5e11c00c 2021-03-02 op if ((u = calloc(1, buf - url_start + 1)) == NULL)
154 5e11c00c 2021-03-02 op memcpy(u, url_start, buf - url_start);
156 5e11c00c 2021-03-02 op if (len == 0)
157 f250a75b 2021-03-06 op goto nolabel;
159 f250a75b 2021-03-06 op while (len > 0 && isspace(buf[0])) {
164 5e11c00c 2021-03-02 op if (len == 0)
165 f250a75b 2021-03-06 op goto nolabel;
167 5e11c00c 2021-03-02 op if ((l = calloc(1, len + 1)) == NULL)
170 5e11c00c 2021-03-02 op memcpy(l, buf, len);
171 f250a75b 2021-03-06 op return emit_line(p, t, l, u);
174 f250a75b 2021-03-06 op if ((l = strdup(u)) == NULL)
176 f250a75b 2021-03-06 op return emit_line(p, t, l, u);
180 5e11c00c 2021-03-02 op parse_title(struct parser *p, enum line_type t, const char *buf, size_t len)
185 5e11c00c 2021-03-02 op case LINE_TITLE_1:
186 5e11c00c 2021-03-02 op if (len <= 1)
187 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
191 5e11c00c 2021-03-02 op case LINE_TITLE_2:
192 5e11c00c 2021-03-02 op if (len <= 2)
193 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
197 5e11c00c 2021-03-02 op case LINE_TITLE_3:
198 5e11c00c 2021-03-02 op if (len <= 3)
199 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
204 5e11c00c 2021-03-02 op /* unreachable */
208 5e11c00c 2021-03-02 op while (len > 0 && isspace(buf[0])) {
213 5e11c00c 2021-03-02 op if (len == 0)
214 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
216 dc5df781 2021-03-13 op if (t == LINE_TITLE_1 && *p->title == '\0')
217 dc5df781 2021-03-13 op strncpy(p->title, buf, MIN(sizeof(p->title)-1, len));
219 5e11c00c 2021-03-02 op if ((l = calloc(1, len+1)) == NULL)
221 5e11c00c 2021-03-02 op memcpy(l, buf, len);
222 5e11c00c 2021-03-02 op return emit_line(p, t, l, NULL);
226 5e11c00c 2021-03-02 op parse_item(struct parser *p, enum line_type t, const char *buf, size_t len)
230 5e11c00c 2021-03-02 op if (len == 1)
231 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
236 5e11c00c 2021-03-02 op while (len > 0 && isspace(buf[0])) {
241 5e11c00c 2021-03-02 op if (len == 0)
242 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
244 5e11c00c 2021-03-02 op if ((l = calloc(1, len+1)) == NULL)
246 5e11c00c 2021-03-02 op memcpy(l, buf, len);
247 5e11c00c 2021-03-02 op return emit_line(p, t, l, NULL);
251 5e11c00c 2021-03-02 op parse_quote(struct parser *p, enum line_type t, const char *buf, size_t len)
255 5e11c00c 2021-03-02 op if (len == 1)
256 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
261 5e11c00c 2021-03-02 op while (len > 0 && isspace(buf[0])) {
266 5e11c00c 2021-03-02 op if (len == 0)
267 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
269 5e11c00c 2021-03-02 op if ((l = calloc(1, len+1)) == NULL)
271 5e11c00c 2021-03-02 op memcpy(l, buf, len);
272 5e11c00c 2021-03-02 op return emit_line(p, t, l, NULL);
276 5e11c00c 2021-03-02 op parse_pre_start(struct parser *p, enum line_type t, const char *buf, size_t len)
280 5e11c00c 2021-03-02 op if (len <= 3)
281 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
286 5e11c00c 2021-03-02 op while (len > 0 && isspace(buf[0])) {
291 5e11c00c 2021-03-02 op if (len == 0)
292 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
294 5e11c00c 2021-03-02 op if ((l = calloc(1, len+1)) == NULL)
297 5e11c00c 2021-03-02 op memcpy(l, buf, len);
298 e2060f05 2021-03-17 op return emit_line(p, t, l, NULL);
302 5e11c00c 2021-03-02 op parse_pre_cnt(struct parser *p, enum line_type t, const char *buf, size_t len)
306 5e11c00c 2021-03-02 op if (len == 0)
307 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
309 5e11c00c 2021-03-02 op if ((l = calloc(1, len+1)) == NULL)
311 5e11c00c 2021-03-02 op memcpy(l, buf, len);
312 5e11c00c 2021-03-02 op return emit_line(p, t, l, NULL);
316 5e11c00c 2021-03-02 op parse_pre_end(struct parser *p, enum line_type t, const char *buf, size_t len)
318 5e11c00c 2021-03-02 op return emit_line(p, t, NULL, NULL);
321 5e11c00c 2021-03-02 op static inline enum line_type
322 5e11c00c 2021-03-02 op detect_line_type(const char *buf, size_t len, int in_pre)
324 5e11c00c 2021-03-02 op if (in_pre) {
325 5e11c00c 2021-03-02 op if (len >= 3 &&
326 5e11c00c 2021-03-02 op buf[0] == '`' && buf[1] == '`' && buf[2] == '`')
327 5e11c00c 2021-03-02 op return LINE_PRE_END;
329 5e11c00c 2021-03-02 op return LINE_PRE_CONTENT;
332 1575cb8a 2021-07-07 op if (len == 0)
333 1575cb8a 2021-07-07 op return LINE_TEXT;
335 5e11c00c 2021-03-02 op switch (*buf) {
336 5e11c00c 2021-03-02 op case '*': return LINE_ITEM;
337 5e11c00c 2021-03-02 op case '>': return LINE_QUOTE;
339 5e11c00c 2021-03-02 op if (len >= 1 && buf[1] == '>')
340 5e11c00c 2021-03-02 op return LINE_LINK;
343 5e11c00c 2021-03-02 op if (len == 1)
344 5e11c00c 2021-03-02 op return LINE_TEXT;
345 5e11c00c 2021-03-02 op if (buf[1] != '#')
346 5e11c00c 2021-03-02 op return LINE_TITLE_1;
347 5e11c00c 2021-03-02 op if (len == 2)
348 5e11c00c 2021-03-02 op return LINE_TEXT;
349 5e11c00c 2021-03-02 op if (buf[2] != '#')
350 5e11c00c 2021-03-02 op return LINE_TITLE_2;
351 5e11c00c 2021-03-02 op if (len == 3)
352 5e11c00c 2021-03-02 op return LINE_TEXT;
353 5e11c00c 2021-03-02 op return LINE_TITLE_3;
356 5e11c00c 2021-03-02 op return LINE_TEXT;
357 5e11c00c 2021-03-02 op if (buf[0] == '`' && buf[1] == '`' && buf[2] == '`')
358 5e11c00c 2021-03-02 op return LINE_PRE_START;
362 5e11c00c 2021-03-02 op return LINE_TEXT;
366 5e11c00c 2021-03-02 op gemtext_parse(struct parser *p, const char *buf, size_t size)
368 a5845bb5 2021-03-20 op return parser_foreach_line(p, buf, size, gemtext_foreach_line);
372 a5845bb5 2021-03-20 op gemtext_foreach_line(struct parser *p, const char *line, size_t linelen)
374 a5845bb5 2021-03-20 op enum line_type t;
376 27dce34f 2021-07-06 op t = detect_line_type(line, linelen, p->flags & PARSER_IN_PRE);
377 a5845bb5 2021-03-20 op if (t == LINE_PRE_START)
378 27dce34f 2021-07-06 op p->flags ^= PARSER_IN_PRE;
379 a5845bb5 2021-03-20 op if (t == LINE_PRE_END)
380 27dce34f 2021-07-06 op p->flags ^= PARSER_IN_PRE;
381 a5845bb5 2021-03-20 op return parsers[t](p, t, line, linelen);
385 5e11c00c 2021-03-02 op gemtext_free(struct parser *p)
387 a5c3e03d 2021-03-02 op enum line_type t;
389 a5c3e03d 2021-03-02 op /* flush the buffer */
390 a5c3e03d 2021-03-02 op if (p->len != 0) {
391 27dce34f 2021-07-06 op t = detect_line_type(p->buf, p->len, p->flags & PARSER_IN_PRE);
392 a5c3e03d 2021-03-02 op if (!parsers[t](p, t, p->buf, p->len))
394 27dce34f 2021-07-06 op if ((p->flags & PARSER_IN_PRE) &&
395 27dce34f 2021-07-06 op !emit_line(p, LINE_PRE_END, NULL, NULL))
399 5e11c00c 2021-03-02 op free(p->buf);
402 48e3f26d 2021-05-13 op * use the first level 2 or 3 header as page title if none
405 48e3f26d 2021-05-13 op if (*p->title == '\0')
406 48e3f26d 2021-05-13 op search_title(p, LINE_TITLE_2);
407 48e3f26d 2021-05-13 op if (*p->title == '\0')
408 48e3f26d 2021-05-13 op search_title(p, LINE_TITLE_3);
414 48e3f26d 2021-05-13 op search_title(struct parser *p, enum line_type level)
416 48e3f26d 2021-05-13 op struct line *l;
418 48e3f26d 2021-05-13 op TAILQ_FOREACH(l, &p->head, lines) {
419 48e3f26d 2021-05-13 op if (l->type == level) {
420 48e3f26d 2021-05-13 op if (l->line == NULL)
422 48e3f26d 2021-05-13 op strlcpy(p->title, l->line, sizeof(p->title));