Blob


1 {!
2 /*
3 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
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
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <sys/types.h>
20 #include <sys/queue.h>
22 #include <ctype.h>
23 #include <event.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <imsg.h>
29 #include "proc.h"
31 #include "gotwebd.h"
32 #include "tmpl.h"
34 static inline int rss_tag_item(struct template *, struct repo_tag *);
35 static inline int rss_author(struct template *, char *);
37 static int
38 gotweb_render_age(struct template *tp, time_t time, int ref_tm)
39 {
40 const struct got_error *err;
41 char *age;
42 int r;
44 err = gotweb_get_time_str(&age, time, ref_tm);
45 if (err)
46 return 0;
47 r = tp->tp_puts(tp, age);
48 free(age);
49 return r;
50 }
52 !}
54 {{ define gotweb_render_header(struct template *tp) }}
55 {!
56 struct request *c = tp->tp_arg;
57 struct server *srv = c->srv;
58 struct querystring *qs = c->t->qs;
59 struct gotweb_url u_path;
60 const char *prfx = c->document_uri;
61 const char *css = srv->custom_css;
63 memset(&u_path, 0, sizeof(u_path));
64 u_path.index_page = -1;
65 u_path.page = -1;
66 u_path.action = SUMMARY;
67 !}
68 <!doctype html>
69 <html>
70 <head>
71 <meta charset="utf-8" />
72 <title>{{ srv->site_name }}</title>
73 <meta name="viewport" content="initial-scale=.75" />
74 <meta name="msapplication-TileColor" content="#da532c" />
75 <meta name="theme-color" content="#ffffff"/>
76 <link rel="apple-touch-icon" sizes="180x180" href="{{ prfx }}apple-touch-icon.png" />
77 <link rel="icon" type="image/png" sizes="32x32" href="{{ prfx }}favicon-32x32.png" />
78 <link rel="icon" type="image/png" sizes="16x16" href="{{ prfx }}favicon-16x16.png" />
79 <link rel="manifest" href="{{ prfx }}site.webmanifest"/>
80 <link rel="mask-icon" href="{{ prfx }}safari-pinned-tab.svg" />
81 <link rel="stylesheet" type="text/css" href="{{ prfx }}{{ css }}" />
82 </head>
83 <body>
84 <div id="gw_body">
85 <div id="header">
86 <div id="got_link">
87 <a href="{{ srv->logo_url }}" target="_blank">
88 <img src="{{ prfx }}{{ srv->logo }}" />
89 </a>
90 </div>
91 </div>
92 <div id="site_path">
93 <div id="site_link">
94 <a href="?index_page={{ printf "%d", qs->index_page }}">
95 {{ srv->site_link }}
96 </a>
97 {{ if qs->path }}
98 {! u_path.path = qs->path; !}
99 {{ " / " }}
100 <a href="{{ render gotweb_render_url(tp->tp_arg, &u_path)}}">
101 {{ qs->path }}
102 </a>
103 {{ end }}
104 {{ if qs->action != INDEX }}
105 {{ " / " }}{{ gotweb_action_name(qs->action) }}
106 {{ end }}
107 </div>
108 </div>
109 <div id="content">
110 {{ end }}
112 {{ define gotweb_render_footer(struct template *tp) }}
113 {!
114 struct request *c = tp->tp_arg;
115 struct server *srv = c->srv;
116 !}
117 <div id="site_owner_wrapper">
118 <div id="site_owner">
119 {{ if srv->show_site_owner }}
120 {{ srv->site_owner }}
121 {{ end }}
122 </div>
123 </div>
124 </div>
125 </div>
126 </body>
127 </html>
128 {{ end }}
130 {{ define gotweb_render_repo_table_hdr(struct template *tp) }}
131 {!
132 struct request *c = tp->tp_arg;
133 struct server *srv = c->srv;
134 !}
135 <div id="index_header">
136 <div id="index_header_project">
137 Project
138 </div>
139 {{ if srv->show_repo_description }}
140 <div id="index_header_description">
141 Description
142 </div>
143 {{ end }}
144 {{ if srv->show_repo_owner }}
145 <div id="index_header_owner">
146 Owner
147 </div>
148 {{ end }}
149 {{ if srv->show_repo_age }}
150 <div id="index_header_age">
151 Last Change
152 </div>
153 {{ end }}
154 </div>
155 {{ end }}
157 {{ define gotweb_render_repo_fragment(struct template *tp, struct repo_dir *repo_dir) }}
158 {!
159 struct request *c = tp->tp_arg;
160 struct server *srv = c->srv;
161 struct gotweb_url summary = {
162 .action = SUMMARY,
163 .index_page = -1,
164 .page = -1,
165 .path = repo_dir->name,
166 }, briefs = {
167 .action = BRIEFS,
168 .index_page = -1,
169 .page = -1,
170 .path = repo_dir->name,
171 }, commits = {
172 .action = COMMITS,
173 .index_page = -1,
174 .page = -1,
175 .path = repo_dir->name,
176 }, tags = {
177 .action = TAGS,
178 .index_page = -1,
179 .page = -1,
180 .path = repo_dir->name,
181 }, tree = {
182 .action = TREE,
183 .index_page = -1,
184 .page = -1,
185 .path = repo_dir->name,
186 }, rss = {
187 .action = RSS,
188 .index_page = -1,
189 .page = -1,
190 .path = repo_dir->name,
191 };
192 !}
193 <div class="index_wrapper">
194 <div class="index_project">
195 <a href="{{ render gotweb_render_url(tp->tp_arg, &summary) }}">{{ repo_dir->name }}</a>
196 </div>
197 {{ if srv->show_repo_description }}
198 <div class="index_project_description">
199 {{ repo_dir->description }}
200 </div>
201 {{ end }}
202 {{ if srv->show_repo_owner }}
203 <div class="index_project_owner">
204 {{ repo_dir->owner }}
205 </div>
206 {{ end }}
207 {{ if srv->show_repo_age }}
208 <div class="index_project_age">
209 {{ repo_dir->age }}
210 </div>
211 {{ end }}
212 <div class="navs_wrapper">
213 <div class="navs">
214 <a href="{{ render gotweb_render_url(tp->tp_arg, &summary) }}">summary</a>
215 {{ " | " }}
216 <a href="{{ render gotweb_render_url(tp->tp_arg, &briefs) }}">briefs</a>
217 {{ " | " }}
218 <a href="{{ render gotweb_render_url(tp->tp_arg, &commits) }}">commits</a>
219 {{ " | " }}
220 <a href="{{ render gotweb_render_url(tp->tp_arg, &tags) }}">tags</a>
221 {{ " | " }}
222 <a href="{{ render gotweb_render_url(tp->tp_arg, &tree) }}">tree</a>
223 {{ " | " }}
224 <a href="{{ render gotweb_render_url(tp->tp_arg, &rss) }}">rss</a>
225 </div>
226 <div class="dotted_line"></div>
227 </div>
228 </div>
229 {{ end }}
231 {{ define gotweb_render_briefs(struct template *tp) }}
232 {!
233 const struct got_error *error;
234 struct request *c = tp->tp_arg;
235 struct server *srv = c->srv;
236 struct transport *t = c->t;
237 struct querystring *qs = c->t->qs;
238 struct repo_commit *rc;
239 struct repo_dir *repo_dir = t->repo_dir;
240 struct gotweb_url diff_url, tree_url;
241 char *tmp;
243 diff_url = (struct gotweb_url){
244 .action = DIFF,
245 .index_page = -1,
246 .page = -1,
247 .path = repo_dir->name,
248 .headref = qs->headref,
249 };
250 tree_url = (struct gotweb_url){
251 .action = TREE,
252 .index_page = -1,
253 .page = -1,
254 .path = repo_dir->name,
255 .headref = qs->headref,
256 };
258 if (qs->action == SUMMARY) {
259 qs->action = BRIEFS;
260 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
261 } else
262 error = got_get_repo_commits(c, srv->max_commits_display);
263 if (error)
264 return -1;
265 !}
266 <div id="briefs_title_wrapper">
267 <div id="briefs_title">Commit Briefs</div>
268 </div>
269 <div id="briefs_content">
270 {{ tailq-foreach rc &t->repo_commits entry }}
271 {!
272 diff_url.commit = rc->commit_id;
273 tree_url.commit = rc->commit_id;
275 tmp = strchr(rc->author, '<');
276 if (tmp)
277 *tmp = '\0';
279 tmp = strchr(rc->commit_msg, '\n');
280 if (tmp)
281 *tmp = '\0';
282 !}
283 <div class="briefs_age">
284 {{ render gotweb_render_age(tp, rc->committer_time, TM_DIFF) }}
285 </div>
286 <div class="briefs_author">
287 {{ rc->author }}
288 </div>
289 <div class="briefs_log">
290 <a href="{{ render gotweb_render_url(tp->tp_arg, &diff_url) }}">
291 {{ rc->commit_msg }}
292 </a>
293 {{ if rc->refs_str }}
294 {{ " " }} <span class="refs_str">({{ rc->refs_str }})</span>
295 {{ end }}
296 </a>
297 </div>
298 <div class="navs_wrapper">
299 <div class="navs">
300 <a href="{{ render gotweb_render_url(tp->tp_arg, &diff_url) }}">diff</a>
301 {{ " | " }}
302 <a href="{{ render gotweb_render_url(tp->tp_arg, &tree_url) }}">tree</a>
303 </div>
304 </div>
305 <div class="dotted_line"></div>
306 {{ end }}
307 {{ if t->next_id || t->prev_id }}
308 {{ render gotweb_render_navs(tp) }}
309 {{ end }}
310 </div>
311 {{ end }}
313 {{ define gotweb_render_navs(struct template *tp) }}
314 {!
315 struct request *c = tp->tp_arg;
316 struct transport *t = c->t;
317 struct gotweb_url prev, next;
318 int have_prev, have_next;
320 gotweb_get_navs(c, &prev, &have_prev, &next, &have_next);
321 !}
322 <div id="np_wrapper">
323 <div id="nav_prev">
324 {{ if have_prev }}
325 <a href="{{ render gotweb_render_url(c, &prev) }}">
326 Previous
327 </a>
328 {{ end }}
329 </div>
330 <div id="nav_next">
331 {{ if have_next }}
332 <a href="{{ render gotweb_render_url(c, &next) }}">
333 Next
334 </a>
335 {{ end }}
336 </div>
337 </div>
338 {{ finally }}
339 {!
340 free(t->next_id);
341 t->next_id = NULL;
342 free(t->prev_id);
343 t->prev_id = NULL;
344 !}
345 {{ end }}
347 {{ define gotweb_render_commits(struct template *tp) }}
348 {!
349 struct request *c = tp->tp_arg;
350 struct transport *t = c->t;
351 struct repo_dir *repo_dir = t->repo_dir;
352 struct repo_commit *rc;
353 struct gotweb_url diff, tree;
355 diff = (struct gotweb_url){
356 .action = DIFF,
357 .index_page = -1,
358 .page = -1,
359 .path = repo_dir->name,
360 };
361 tree = (struct gotweb_url){
362 .action = TREE,
363 .index_page = -1,
364 .page = -1,
365 .path = repo_dir->name,
366 };
367 !}
368 <div class="commits_title_wrapper">
369 <div class="commits_title">Commits</div>
370 </div>
371 <div class="commits_content">
372 {{ tailq-foreach rc &t->repo_commits entry }}
373 {!
374 diff.commit = rc->commit_id;
375 tree.commit = rc->commit_id;
376 !}
377 <div class="commits_header_wrapper">
378 <div class="commits_header">
379 <div class="header_commit_title">Commit:</div>
380 <div class="header_commit">{{ rc->commit_id }}</div>
381 <div class="header_author_title">Author:</div>
382 <div class="header_author">{{ rc->author }}</div>
383 <div class="header_age_title">Date:</div>
384 <div class="header_age">
385 {{ render gotweb_render_age(tp, rc->committer_time, TM_LONG) }}
386 </div>
387 </div>
388 </div>
389 <div class="navs_wrapper">
390 <div class="navs">
391 <a href="{{ render gotweb_render_url(c, &diff) }}">diff</a>
392 {{ " | " }}
393 <a href="{{ render gotweb_render_url(c, &tree) }}">tree</a>
394 </div>
395 </div>
396 <div class="dotted_line"></div>
397 {{ end }}
398 {{ if t->next_id || t->prev_id }}
399 {{ render gotweb_render_navs(tp) }}
400 {{ end }}
401 </div>
402 {{ end }}
404 {{ define gotweb_render_rss(struct template *tp) }}
405 {!
406 struct request *c = tp->tp_arg;
407 struct server *srv = c->srv;
408 struct transport *t = c->t;
409 struct repo_dir *repo_dir = t->repo_dir;
410 struct repo_tag *rt;
411 struct gotweb_url summary = {
412 .action = SUMMARY,
413 .index_page = -1,
414 .page = -1,
415 .path = repo_dir->name,
416 };
417 !}
418 <?xml version="1.0" encoding="UTF-8"?>
419 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
420 <channel>
421 <title>Tags of {{ repo_dir->name }}</title>
422 <link>
423 <![CDATA[
424 {{ render gotweb_render_absolute_url(c, &summary) }}
425 ]]>
426 </link>
427 {{ if srv->show_repo_description }}
428 <description>{{ repo_dir->description }}</description>
429 {{ end }}
430 {{ tailq-foreach rt &t->repo_tags entry }}
431 {{ render rss_tag_item(tp, rt) }}
432 {{ end }}
433 </channel>
434 </rss>
435 {{ end }}
437 {{ define rss_tag_item(struct template *tp, struct repo_tag *rt) }}
438 {!
439 struct request *c = tp->tp_arg;
440 struct transport *t = c->t;
441 struct repo_dir *repo_dir = t->repo_dir;
442 char *tag_name = rt->tag_name;
443 struct gotweb_url tag = {
444 .action = TAG,
445 .index_page = -1,
446 .page = -1,
447 .path = repo_dir->name,
448 .commit = rt->commit_id,
449 };
451 if (strncmp(tag_name, "refs/tags/", 10) == 0)
452 tag_name += 10;
453 !}
454 <item>
455 <title>{{ repo_dir->name }} {{" "}} {{ tag_name }}</title>
456 <link>
457 <![CDATA[
458 {{ render gotweb_render_absolute_url(c, &tag) }}
459 ]]>
460 </link>
461 <description>
462 <![CDATA[<pre>{{ rt->tag_commit }}</pre>]]>
463 </description>
464 {{ render rss_author(tp, rt->tagger) }}
465 <guid isPermaLink="false">{{ rt->commit_id }}</guid>
466 <pubDate>
467 {{ render gotweb_render_age(tp, rt->tagger_time, TM_RFC822) }}
468 </pubDate>
469 </item>
470 {{ end }}
472 {{ define rss_author(struct template *tp, char *author) }}
473 {!
474 char *t, *mail;
476 /* what to do if the author name contains a paren? */
477 if (strchr(author, '(') != NULL || strchr(author, ')') != NULL)
478 return 0;
480 t = strchr(author, '<');
481 if (t == NULL)
482 return 0;
483 *t = '\0';
484 mail = t+1;
486 while (isspace((unsigned char)*--t))
487 *t = '\0';
489 t = strchr(mail, '>');
490 if (t == NULL)
491 return 0;
492 *t = '\0';
493 !}
494 <author>
495 {{ mail }} {{" "}} ({{ author }})
496 </author>
497 {{ end }}