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"
27 const char *gemtext_prefixes[] = {
29 [LINE_TITLE_1] = "# ",
30 [LINE_TITLE_2] = "## ",
31 [LINE_TITLE_3] = "### ",
34 [LINE_PRE_START] = "``` ",
35 [LINE_PRE_CONTENT] = "",
36 [LINE_PRE_END] = "```",
45 const char *parser_name;
54 struct ohash_info info = {
55 .key_offset = offsetof(struct mcache_entry, url),
56 .calloc = hash_calloc,
61 ohash_init(&mcache.h, 5, &info);
65 mcache_tab(struct tab *tab)
67 struct mcache_entry *e;
73 url = tab->hist_cur->h;
75 len = sizeof(*e) + l + 1;
77 if ((e = calloc(1, len)) == NULL)
79 e->parser_name = tab->buffer.page.name;
80 e->trust = tab->trust;
81 memcpy(e->url, url, l);
83 if ((e->evb = evbuffer_new()) == NULL)
86 TAILQ_FOREACH(line, &tab->buffer.page.head, lines) {
87 const char *text, *alt;
90 if ((text = line->line) == NULL)
93 if ((alt = line->alt) == NULL)
104 case LINE_PRE_CONTENT:
106 r = evbuffer_add_printf(e->evb, "%s%s\n",
107 gemtext_prefixes[line->type], text);
111 r = evbuffer_add_printf(e->evb, "=> %s %s\n",
117 case LINE_PATCH_HUNK_HDR:
125 case LINE_COMPL_CURRENT:
128 case LINE_DOWNLOAD_DONE:
129 case LINE_DOWNLOAD_INFO:
139 slot = ohash_qlookup(&mcache.h, url);
140 ohash_insert(&mcache.h, slot, e);
145 evbuffer_free(e->evb);
151 mcache_lookup(const char *url, struct tab *tab)
153 struct mcache_entry *e;
156 slot = ohash_qlookup(&mcache.h, url);
157 if ((e = ohash_find(&mcache.h, slot)) == NULL)
160 parser_init(tab, gemtext_initparser);
161 if (!parser_parse(tab, EVBUFFER_DATA(e->evb), EVBUFFER_LENGTH(e->evb)))
163 if (!parser_free(tab))
166 tab->buffer.page.name = e->parser_name;
167 tab->trust = e->trust;
172 erase_buffer(&tab->buffer);