comparison page-index.c @ 79:52029a52a385

pasterd: revert using ktemplate
author David Demelier <markand@malikania.fr>
date Fri, 17 Mar 2023 07:43:20 +0100
parents 9bfe5ce3cc45
children 1ffe2f5a8500
comparison
equal deleted inserted replaced
78:9bfe5ce3cc45 79:52029a52a385
17 */ 17 */
18 18
19 #include <assert.h> 19 #include <assert.h>
20 20
21 #include "database.h" 21 #include "database.h"
22 #include "json-util.h"
23 #include "page-index.h" 22 #include "page-index.h"
23 #include "page-status.h"
24 #include "page.h" 24 #include "page.h"
25 #include "paste.h"
25 #include "util.h" 26 #include "util.h"
26 27
27 #include "html/index.h" 28 #include "html/index.h"
28 29
29 #define LIMIT 16 30 #define LIMIT 16
30 #define TITLE "paster -- recent pastes" 31 #define TITLE "paster -- recent pastes"
31 32
33 struct page {
34 struct kreq *req;
35 struct ktemplate template;
36 const struct paste *pastes;
37 const size_t pastesz;
38 };
39
40 enum {
41 KEYWORD_PASTES
42 };
43
44 static const char * const keywords[] = {
45 [KEYWORD_PASTES] = "pastes"
46 };
47
48 static int
49 format(size_t keyword, void *data)
50 {
51 struct page *page = data;
52 struct khtmlreq html;
53 const struct paste *paste;
54
55 khtml_open(&html, page->req, KHTML_PRETTY);
56
57 switch (keyword) {
58 case KEYWORD_PASTES:
59 for (size_t i = 0; i < page->pastesz; ++i) {
60 paste = &page->pastes[i];
61
62 khtml_elem(&html, KELEM_TR);
63
64 /* link */
65 khtml_elem(&html, KELEM_TD);
66 khtml_attr(&html, KELEM_A,
67 KATTR_HREF, bprintf("/paste/%s", paste->id), KATTR__MAX);
68 khtml_printf(&html, paste->title);
69 khtml_closeelem(&html, 1);
70
71 /* author */
72 khtml_elem(&html, KELEM_TD);
73 khtml_puts(&html, paste->author);
74 khtml_closeelem(&html, 1);
75
76 /* language */
77 khtml_elem(&html, KELEM_TD);
78 khtml_puts(&html, paste->language);
79 khtml_closeelem(&html, 1);
80
81 /* date */
82 khtml_elem(&html, KELEM_TD);
83 khtml_puts(&html, bstrftime("%F %T", localtime(&paste->timestamp)));
84 khtml_closeelem(&html, 1);
85
86 /* expiration */
87 khtml_elem(&html, KELEM_TD);
88 khtml_puts(&html, ttl(paste->timestamp, paste->duration));
89 khtml_closeelem(&html, 1);
90
91 khtml_closeelem(&html, 1);
92
93 }
94 break;
95 default:
96 break;
97 }
98
99 khtml_close(&html);
100
101 return 1;
102 }
103
32 static void 104 static void
33 get(struct kreq *req) 105 get(struct kreq *req)
34 { 106 {
35 json_t *pastes; 107 struct paste pastes[LIMIT];
108 size_t pastesz = NELEM(pastes);
36 109
37 if (!(pastes = database_recents(LIMIT))) 110 if (database_recents(pastes, &pastesz) < 0)
38 page_status(req, KHTTP_500); 111 page_status(req, KHTTP_500);
39 else 112 else {
40 page_index_render(req, pastes); 113 page_index_render(req, pastes, pastesz);
114
115 for (size_t i = 0; i < pastesz; ++i)
116 paste_finish(&pastes[i]);
117 }
41 } 118 }
42 119
43 void 120 void
44 page_index_render(struct kreq *req, json_t *pastes) 121 page_index_render(struct kreq *req, const struct paste *pastes, size_t pastesz)
45 { 122 {
46 assert(req); 123 assert(req);
47 assert(pastes); 124 assert(pastes);
48 125
49 page(req, KHTTP_200, html_index, json_pack("{ss so}", 126 struct page self = {
50 "title", TITLE, 127 .req = req,
51 "pastes", pastes 128 .template = {
52 )); 129 .cb = format,
130 .arg = &self,
131 .key = keywords,
132 .keysz = NELEM(keywords)
133 },
134 .pastes = pastes,
135 .pastesz = pastesz
136 };
137
138 page(req, KHTTP_200, TITLE, html_index, &self.template);
53 } 139 }
54 140
55 void 141 void
56 page_index(struct kreq *req) 142 page_index(struct kreq *req)
57 { 143 {