2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #include "telescope.h"
28 const char *gemtext_prefixes[] = {
30 [LINE_TITLE_1] = "# ",
31 [LINE_TITLE_2] = "## ",
32 [LINE_TITLE_3] = "### ",
35 [LINE_PRE_START] = "``` ",
36 [LINE_PRE_CONTENT] = "",
37 [LINE_PRE_END] = "```",
46 const char *parser_name;
55 struct ohash_info info = {
56 .key_offset = offsetof(struct mcache_entry, url),
57 .calloc = hash_calloc,
62 ohash_init(&mcache.h, 5, &info);
66 mcache_tab(struct tab *tab)
68 struct mcache_entry *e;
74 url = tab->hist_cur->h;
76 len = sizeof(*e) + l + 1;
78 if ((e = calloc(1, len)) == NULL)
80 e->parser_name = tab->buffer.page.name;
81 e->trust = tab->trust;
82 memcpy(e->url, url, l);
84 if ((e->evb = evbuffer_new()) == NULL)
87 TAILQ_FOREACH(line, &tab->buffer.page.head, lines) {
88 const char *text, *alt;
91 if ((text = line->line) == NULL)
94 if ((alt = line->alt) == NULL)
105 case LINE_PRE_CONTENT:
107 r = evbuffer_add_printf(e->evb, "%s%s\n",
108 gemtext_prefixes[line->type], text);
112 r = evbuffer_add_printf(e->evb, "=> %s %s\n",
118 case LINE_PATCH_HUNK_HDR:
126 case LINE_COMPL_CURRENT:
129 case LINE_DOWNLOAD_DONE:
130 case LINE_DOWNLOAD_INFO:
140 slot = ohash_qlookup(&mcache.h, url);
141 ohash_insert(&mcache.h, slot, e);
146 evbuffer_free(e->evb);
152 mcache_lookup(const char *url, struct tab *tab)
154 struct mcache_entry *e;
157 slot = ohash_qlookup(&mcache.h, url);
158 if ((e = ohash_find(&mcache.h, slot)) == NULL)
161 parser_init(tab, gemtext_initparser);
162 if (!parser_parse(tab, EVBUFFER_DATA(e->evb), EVBUFFER_LENGTH(e->evb)))
164 if (!parser_free(tab))
167 tab->buffer.page.name = e->parser_name;
168 tab->trust = e->trust;
173 erase_buffer(&tab->buffer);