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;
53 mcache_free_entry(const char *url)
55 struct mcache_entry *e;
58 slot = ohash_qlookup(&mcache.h, url);
59 if ((e = ohash_remove(&mcache.h, slot)) == NULL)
62 evbuffer_free(e->evb);
69 struct ohash_info info = {
70 .key_offset = offsetof(struct mcache_entry, url),
71 .calloc = hash_calloc,
76 ohash_init(&mcache.h, 5, &info);
80 mcache_tab(struct tab *tab)
82 struct mcache_entry *e;
88 url = tab->hist_cur->h;
90 len = sizeof(*e) + l + 1;
92 if ((e = calloc(1, len)) == NULL)
94 e->parser_name = tab->buffer.page.name;
95 e->trust = tab->trust;
96 memcpy(e->url, url, l);
98 if ((e->evb = evbuffer_new()) == NULL)
101 TAILQ_FOREACH(line, &tab->buffer.page.head, lines) {
102 const char *text, *alt;
105 if ((text = line->line) == NULL)
108 if ((alt = line->alt) == NULL)
111 switch (line->type) {
119 case LINE_PRE_CONTENT:
121 r = evbuffer_add_printf(e->evb, "%s%s\n",
122 gemtext_prefixes[line->type], text);
126 r = evbuffer_add_printf(e->evb, "=> %s %s\n",
132 case LINE_PATCH_HUNK_HDR:
140 case LINE_COMPL_CURRENT:
143 case LINE_DOWNLOAD_DONE:
144 case LINE_DOWNLOAD_INFO:
154 /* free any previously cached copies of this page */
155 mcache_free_entry(url);
157 slot = ohash_qlookup(&mcache.h, url);
158 ohash_insert(&mcache.h, slot, e);
163 evbuffer_free(e->evb);
169 mcache_lookup(const char *url, struct tab *tab)
171 struct mcache_entry *e;
174 slot = ohash_qlookup(&mcache.h, url);
175 if ((e = ohash_find(&mcache.h, slot)) == NULL)
178 parser_init(tab, gemtext_initparser);
179 if (!parser_parse(tab, EVBUFFER_DATA(e->evb), EVBUFFER_LENGTH(e->evb)))
181 if (!parser_free(tab))
184 tab->buffer.page.name = e->parser_name;
185 tab->trust = e->trust;
190 erase_buffer(&tab->buffer);