Mercurial > paster
comparison page-index.c @ 73:6792975da9a0
pasterd: start removing themes
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 21 Feb 2023 22:22:02 +0100 |
parents | 1a98bc0daa49 |
children | 67b3d13a5035 |
comparison
equal
deleted
inserted
replaced
72:1a98bc0daa49 | 73:6792975da9a0 |
---|---|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 */ | 17 */ |
18 | 18 |
19 #include <sys/types.h> | |
20 #include <assert.h> | 19 #include <assert.h> |
21 #include <stdarg.h> | |
22 #include <stdint.h> | |
23 | |
24 #include <kcgi.h> | |
25 | 20 |
26 #include "database.h" | 21 #include "database.h" |
22 #include "fmt-paste.h" | |
23 #include "fmt.h" | |
27 #include "fragment-paste.h" | 24 #include "fragment-paste.h" |
28 #include "page-index.h" | 25 #include "page-index.h" |
29 #include "page.h" | 26 #include "page.h" |
30 #include "paste.h" | 27 #include "paste.h" |
31 #include "util.h" | 28 #include "util.h" |
32 | 29 |
33 struct template { | 30 #include "html/index.h" |
34 struct kreq *req; | |
35 const struct paste *pastes; | |
36 size_t pastesz; | |
37 }; | |
38 | 31 |
39 static const char *keywords[] = { | 32 static void |
40 "pastes" | 33 print_paste_table(struct kreq *req, struct khtmlreq *html, const void *data) |
41 }; | 34 { |
35 (void)html; | |
42 | 36 |
43 static int | 37 fmt_paste_table(req, data); |
44 template(size_t keyword, void *arg) | |
45 { | |
46 struct template *tp = arg; | |
47 | |
48 switch (keyword) { | |
49 case 0: | |
50 for (size_t i = 0; i < tp->pastesz; ++i) | |
51 fragment_paste(tp->req, &tp->pastes[i]); | |
52 break; | |
53 default: | |
54 break; | |
55 } | |
56 | |
57 return 1; | |
58 } | 38 } |
59 | 39 |
60 static void | 40 static void |
61 get(struct kreq *r) | 41 get(struct kreq *r) |
62 { | 42 { |
63 struct paste pastes[10] = {0}; | 43 struct paste pastes[10] = {0}; |
64 size_t pastesz = NELEM(pastes); | 44 size_t pastesz = NELEM(pastes); |
65 | 45 |
66 if (!database_recents(pastes, &pastesz)) | 46 if (!database_recents(pastes, &pastesz)) |
67 page(r, NULL, KHTTP_500, "pages/500.html", "500"); | 47 page(r, NULL, KHTTP_500, "pages/500.html", "500"); |
68 else | 48 else { |
69 page_index_render(r, pastes, pastesz); | 49 page_index_render(r, pastes, pastesz); |
70 | 50 |
71 for (size_t i = 0; i < pastesz; ++i) | 51 for (size_t i = 0; i < pastesz; ++i) |
72 paste_finish(&pastes[i]); | 52 paste_finish(&pastes[i]); |
53 } | |
73 } | 54 } |
74 | 55 |
75 void | 56 void |
76 page_index_render(struct kreq *r, const struct paste *pastes, size_t pastesz) | 57 page_index_render(struct kreq *r, const struct paste *pastes, size_t pastesz) |
77 { | 58 { |
78 struct template data = { | 59 assert(r); |
79 .req = r, | 60 assert(pastes); |
61 | |
62 struct fmt_paste_vec vec = { | |
80 .pastes = pastes, | 63 .pastes = pastes, |
81 .pastesz = pastesz | 64 .pastesz = pastesz |
82 }; | 65 }; |
83 | 66 |
84 struct ktemplate kt = { | 67 page2(r, KHTTP_200, "recent pastes", html_index, &vec, (const struct fmt_printer []) { |
85 .key = keywords, | 68 { "paste-table", print_paste_table }, |
86 .keysz = NELEM(keywords), | 69 { NULL, NULL } |
87 .arg = &data, | 70 }); |
88 .cb = template | |
89 }; | |
90 | |
91 page(r, &kt, KHTTP_200, "pages/index.html", "Recent pastes"); | |
92 } | 71 } |
93 | 72 |
94 void | 73 void |
95 page_index(struct kreq *r) | 74 page_index(struct kreq *r) |
96 { | 75 { |