]> git.cameronkatri.com Git - cgit.git/blob - ui-shared.c
Merge branch 'lh/atom'
[cgit.git] / ui-shared.c
1 /* ui-shared.c: common web output functions
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9 #include "cgit.h"
10 #include "cmd.h"
11 #include "html.h"
12
13 const char cgit_doctype[] =
14 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
15 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
16
17 static char *http_date(time_t t)
18 {
19 static char day[][4] =
20 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
21 static char month[][4] =
22 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
23 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
24 struct tm *tm = gmtime(&t);
25 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
26 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
27 tm->tm_hour, tm->tm_min, tm->tm_sec);
28 }
29
30 void cgit_print_error(char *msg)
31 {
32 html("<div class='error'>");
33 html_txt(msg);
34 html("</div>\n");
35 }
36
37 char *cgit_hosturl()
38 {
39 char *host, *port;
40
41 host = getenv("SERVER_NAME");
42 if (!host)
43 return NULL;
44 port = getenv("SERVER_PORT");
45 if (port && atoi(port) != 80)
46 host = xstrdup(fmt("%s:%d", host, atoi(port)));
47 else
48 host = xstrdup(host);
49 return host;
50 }
51
52 char *cgit_rooturl()
53 {
54 if (ctx.cfg.virtual_root)
55 return fmt("%s/", ctx.cfg.virtual_root);
56 else
57 return ctx.cfg.script_name;
58 }
59
60 char *cgit_repourl(const char *reponame)
61 {
62 if (ctx.cfg.virtual_root) {
63 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame);
64 } else {
65 return fmt("?r=%s", reponame);
66 }
67 }
68
69 char *cgit_fileurl(const char *reponame, const char *pagename,
70 const char *filename, const char *query)
71 {
72 char *tmp;
73 char *delim;
74
75 if (ctx.cfg.virtual_root) {
76 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame,
77 pagename, (filename ? filename:""));
78 delim = "?";
79 } else {
80 tmp = fmt("?url=%s/%s/%s", reponame, pagename,
81 (filename ? filename : ""));
82 delim = "&";
83 }
84 if (query)
85 tmp = fmt("%s%s%s", tmp, delim, query);
86 return tmp;
87 }
88
89 char *cgit_pageurl(const char *reponame, const char *pagename,
90 const char *query)
91 {
92 return cgit_fileurl(reponame,pagename,0,query);
93 }
94
95 const char *cgit_repobasename(const char *reponame)
96 {
97 /* I assume we don't need to store more than one repo basename */
98 static char rvbuf[1024];
99 int p;
100 const char *rv;
101 strncpy(rvbuf,reponame,sizeof(rvbuf));
102 if(rvbuf[sizeof(rvbuf)-1])
103 die("cgit_repobasename: truncated repository name '%s'", reponame);
104 p = strlen(rvbuf)-1;
105 /* strip trailing slashes */
106 while(p && rvbuf[p]=='/') rvbuf[p--]=0;
107 /* strip trailing .git */
108 if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) {
109 p -= 3; rvbuf[p--] = 0;
110 }
111 /* strip more trailing slashes if any */
112 while( p && rvbuf[p]=='/') rvbuf[p--]=0;
113 /* find last slash in the remaining string */
114 rv = strrchr(rvbuf,'/');
115 if(rv)
116 return ++rv;
117 return rvbuf;
118 }
119
120 char *cgit_currurl()
121 {
122 if (!ctx.cfg.virtual_root)
123 return ctx.cfg.script_name;
124 else if (ctx.qry.page)
125 return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page);
126 else if (ctx.qry.repo)
127 return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo);
128 else
129 return fmt("%s/", ctx.cfg.virtual_root);
130 }
131
132 static void site_url(char *page, char *search, int ofs)
133 {
134 char *delim = "?";
135
136 if (ctx.cfg.virtual_root) {
137 html_attr(ctx.cfg.virtual_root);
138 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
139 html("/");
140 } else
141 html(ctx.cfg.script_name);
142
143 if (page) {
144 htmlf("?p=%s", page);
145 delim = "&";
146 }
147 if (search) {
148 html(delim);
149 html("q=");
150 html_attr(search);
151 delim = "&";
152 }
153 if (ofs) {
154 html(delim);
155 htmlf("ofs=%d", ofs);
156 }
157 }
158
159 static void site_link(char *page, char *name, char *title, char *class,
160 char *search, int ofs)
161 {
162 html("<a");
163 if (title) {
164 html(" title='");
165 html_attr(title);
166 html("'");
167 }
168 if (class) {
169 html(" class='");
170 html_attr(class);
171 html("'");
172 }
173 html(" href='");
174 site_url(page, search, ofs);
175 html("'>");
176 html_txt(name);
177 html("</a>");
178 }
179
180 void cgit_index_link(char *name, char *title, char *class, char *pattern,
181 int ofs)
182 {
183 site_link(NULL, name, title, class, pattern, ofs);
184 }
185
186 static char *repolink(char *title, char *class, char *page, char *head,
187 char *path)
188 {
189 char *delim = "?";
190
191 html("<a");
192 if (title) {
193 html(" title='");
194 html_attr(title);
195 html("'");
196 }
197 if (class) {
198 html(" class='");
199 html_attr(class);
200 html("'");
201 }
202 html(" href='");
203 if (ctx.cfg.virtual_root) {
204 html_attr(ctx.cfg.virtual_root);
205 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
206 html("/");
207 html_attr(ctx.repo->url);
208 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
209 html("/");
210 if (page) {
211 html(page);
212 html("/");
213 if (path)
214 html_attr(path);
215 }
216 } else {
217 html(ctx.cfg.script_name);
218 html("?url=");
219 html_attr(ctx.repo->url);
220 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
221 html("/");
222 if (page) {
223 html(page);
224 html("/");
225 if (path)
226 html_attr(path);
227 }
228 delim = "&amp;";
229 }
230 if (head && strcmp(head, ctx.repo->defbranch)) {
231 html(delim);
232 html("h=");
233 html_attr(head);
234 delim = "&amp;";
235 }
236 return fmt("%s", delim);
237 }
238
239 static void reporevlink(char *page, char *name, char *title, char *class,
240 char *head, char *rev, char *path)
241 {
242 char *delim;
243
244 delim = repolink(title, class, page, head, path);
245 if (rev && strcmp(rev, ctx.qry.head)) {
246 html(delim);
247 html("id=");
248 html_attr(rev);
249 }
250 html("'>");
251 html_txt(name);
252 html("</a>");
253 }
254
255 void cgit_tree_link(char *name, char *title, char *class, char *head,
256 char *rev, char *path)
257 {
258 reporevlink("tree", name, title, class, head, rev, path);
259 }
260
261 void cgit_log_link(char *name, char *title, char *class, char *head,
262 char *rev, char *path, int ofs, char *grep, char *pattern)
263 {
264 char *delim;
265
266 delim = repolink(title, class, "log", head, path);
267 if (rev && strcmp(rev, ctx.qry.head)) {
268 html(delim);
269 html("id=");
270 html_attr(rev);
271 delim = "&";
272 }
273 if (grep && pattern) {
274 html(delim);
275 html("qt=");
276 html_attr(grep);
277 delim = "&";
278 html(delim);
279 html("q=");
280 html_attr(pattern);
281 }
282 if (ofs > 0) {
283 html(delim);
284 html("ofs=");
285 htmlf("%d", ofs);
286 }
287 html("'>");
288 html_txt(name);
289 html("</a>");
290 }
291
292 void cgit_commit_link(char *name, char *title, char *class, char *head,
293 char *rev)
294 {
295 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
296 name[ctx.cfg.max_msg_len] = '\0';
297 name[ctx.cfg.max_msg_len - 1] = '.';
298 name[ctx.cfg.max_msg_len - 2] = '.';
299 name[ctx.cfg.max_msg_len - 3] = '.';
300 }
301 reporevlink("commit", name, title, class, head, rev, NULL);
302 }
303
304 void cgit_refs_link(char *name, char *title, char *class, char *head,
305 char *rev, char *path)
306 {
307 reporevlink("refs", name, title, class, head, rev, path);
308 }
309
310 void cgit_snapshot_link(char *name, char *title, char *class, char *head,
311 char *rev, char *archivename)
312 {
313 reporevlink("snapshot", name, title, class, head, rev, archivename);
314 }
315
316 void cgit_diff_link(char *name, char *title, char *class, char *head,
317 char *new_rev, char *old_rev, char *path)
318 {
319 char *delim;
320
321 delim = repolink(title, class, "diff", head, path);
322 if (new_rev && strcmp(new_rev, ctx.qry.head)) {
323 html(delim);
324 html("id=");
325 html_attr(new_rev);
326 delim = "&amp;";
327 }
328 if (old_rev) {
329 html(delim);
330 html("id2=");
331 html_attr(old_rev);
332 }
333 html("'>");
334 html_txt(name);
335 html("</a>");
336 }
337
338 void cgit_patch_link(char *name, char *title, char *class, char *head,
339 char *rev)
340 {
341 reporevlink("patch", name, title, class, head, rev, NULL);
342 }
343
344 void cgit_object_link(struct object *obj)
345 {
346 char *page, *arg, *url;
347
348 if (obj->type == OBJ_COMMIT) {
349 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL,
350 ctx.qry.head, sha1_to_hex(obj->sha1));
351 return;
352 } else if (obj->type == OBJ_TREE) {
353 page = "tree";
354 arg = "id";
355 } else if (obj->type == OBJ_TAG) {
356 page = "tag";
357 arg = "id";
358 } else {
359 page = "blob";
360 arg = "id";
361 }
362
363 url = cgit_pageurl(ctx.qry.repo, page,
364 fmt("%s=%s", arg, sha1_to_hex(obj->sha1)));
365 html_link_open(url, NULL, NULL);
366 htmlf("%s %s", typename(obj->type),
367 sha1_to_hex(obj->sha1));
368 html_link_close();
369 }
370
371 void cgit_print_date(time_t secs, char *format, int local_time)
372 {
373 char buf[64];
374 struct tm *time;
375
376 if (!secs)
377 return;
378 if(local_time)
379 time = localtime(&secs);
380 else
381 time = gmtime(&secs);
382 strftime(buf, sizeof(buf)-1, format, time);
383 html_txt(buf);
384 }
385
386 void cgit_print_age(time_t t, time_t max_relative, char *format)
387 {
388 time_t now, secs;
389
390 if (!t)
391 return;
392 time(&now);
393 secs = now - t;
394
395 if (secs > max_relative && max_relative >= 0) {
396 cgit_print_date(t, format, ctx.cfg.local_time);
397 return;
398 }
399
400 if (secs < TM_HOUR * 2) {
401 htmlf("<span class='age-mins'>%.0f min.</span>",
402 secs * 1.0 / TM_MIN);
403 return;
404 }
405 if (secs < TM_DAY * 2) {
406 htmlf("<span class='age-hours'>%.0f hours</span>",
407 secs * 1.0 / TM_HOUR);
408 return;
409 }
410 if (secs < TM_WEEK * 2) {
411 htmlf("<span class='age-days'>%.0f days</span>",
412 secs * 1.0 / TM_DAY);
413 return;
414 }
415 if (secs < TM_MONTH * 2) {
416 htmlf("<span class='age-weeks'>%.0f weeks</span>",
417 secs * 1.0 / TM_WEEK);
418 return;
419 }
420 if (secs < TM_YEAR * 2) {
421 htmlf("<span class='age-months'>%.0f months</span>",
422 secs * 1.0 / TM_MONTH);
423 return;
424 }
425 htmlf("<span class='age-years'>%.0f years</span>",
426 secs * 1.0 / TM_YEAR);
427 }
428
429 void cgit_print_http_headers(struct cgit_context *ctx)
430 {
431 if (ctx->page.mimetype && ctx->page.charset)
432 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype,
433 ctx->page.charset);
434 else if (ctx->page.mimetype)
435 htmlf("Content-Type: %s\n", ctx->page.mimetype);
436 if (ctx->page.filename)
437 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
438 ctx->page.filename);
439 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified));
440 htmlf("Expires: %s\n", http_date(ctx->page.expires));
441 html("\n");
442 }
443
444 void cgit_print_docstart(struct cgit_context *ctx)
445 {
446 char *host = cgit_hosturl();
447 html(cgit_doctype);
448 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
449 html("<head>\n");
450 html("<title>");
451 html_txt(ctx->page.title);
452 html("</title>\n");
453 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
454 if (ctx->cfg.robots && *ctx->cfg.robots)
455 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots);
456 html("<link rel='stylesheet' type='text/css' href='");
457 html_attr(ctx->cfg.css);
458 html("'/>\n");
459 if (ctx->cfg.favicon) {
460 html("<link rel='shortcut icon' href='");
461 html_attr(ctx->cfg.favicon);
462 html("'/>\n");
463 }
464 if (host && ctx->repo) {
465 html("<link rel='alternate' title='Atom feed' href='http://");
466 html_attr(cgit_hosturl());
467 html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.path,
468 fmt("h=%s", ctx->qry.head)));
469 html("' type='application/atom+xml'/>");
470 }
471 html("</head>\n");
472 html("<body>\n");
473 }
474
475 void cgit_print_docend()
476 {
477 html("</div>");
478 if (ctx.cfg.footer)
479 html_include(ctx.cfg.footer);
480 else {
481 html("<div class='footer'>generated ");
482 cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time);
483 htmlf(" by cgit %s", cgit_version);
484 html("</div>\n");
485 }
486 html("</body>\n</html>\n");
487 }
488
489 int print_branch_option(const char *refname, const unsigned char *sha1,
490 int flags, void *cb_data)
491 {
492 char *name = (char *)refname;
493 html_option(name, name, ctx.qry.head);
494 return 0;
495 }
496
497 int print_archive_ref(const char *refname, const unsigned char *sha1,
498 int flags, void *cb_data)
499 {
500 struct tag *tag;
501 struct taginfo *info;
502 struct object *obj;
503 char buf[256], *url;
504 unsigned char fileid[20];
505 int *header = (int *)cb_data;
506
507 if (prefixcmp(refname, "refs/archives"))
508 return 0;
509 strncpy(buf, refname+14, sizeof(buf));
510 obj = parse_object(sha1);
511 if (!obj)
512 return 1;
513 if (obj->type == OBJ_TAG) {
514 tag = lookup_tag(sha1);
515 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag)))
516 return 0;
517 hashcpy(fileid, tag->tagged->sha1);
518 } else if (obj->type != OBJ_BLOB) {
519 return 0;
520 } else {
521 hashcpy(fileid, sha1);
522 }
523 if (!*header) {
524 html("<h1>download</h1>\n");
525 *header = 1;
526 }
527 url = cgit_pageurl(ctx.qry.repo, "blob",
528 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid),
529 buf));
530 html_link_open(url, NULL, "menu");
531 html_txt(strlpart(buf, 20));
532 html_link_close();
533 return 0;
534 }
535
536 void add_hidden_formfields(int incl_head, int incl_search, char *page)
537 {
538 char *url;
539
540 if (!ctx.cfg.virtual_root) {
541 url = fmt("%s/%s", ctx.qry.repo, page);
542 if (ctx.qry.path)
543 url = fmt("%s/%s", url, ctx.qry.path);
544 html_hidden("url", url);
545 }
546
547 if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
548 strcmp(ctx.qry.head, ctx.repo->defbranch))
549 html_hidden("h", ctx.qry.head);
550
551 if (ctx.qry.sha1)
552 html_hidden("id", ctx.qry.sha1);
553 if (ctx.qry.sha2)
554 html_hidden("id2", ctx.qry.sha2);
555
556 if (incl_search) {
557 if (ctx.qry.grep)
558 html_hidden("qt", ctx.qry.grep);
559 if (ctx.qry.search)
560 html_hidden("q", ctx.qry.search);
561 }
562 }
563
564 char *hc(struct cgit_cmd *cmd, const char *page)
565 {
566 return (strcmp(cmd->name, page) ? NULL : "active");
567 }
568
569 void cgit_print_pageheader(struct cgit_context *ctx)
570 {
571 struct cgit_cmd *cmd = cgit_get_cmd(ctx);
572
573 html("<table id='header'>\n");
574 html("<tr>\n");
575 html("<td class='logo' rowspan='2'><a href='");
576 if (ctx->cfg.logo_link)
577 html_attr(ctx->cfg.logo_link);
578 else
579 html_attr(cgit_rooturl());
580 html("'><img src='");
581 html_attr(ctx->cfg.logo);
582 html("' alt='cgit logo'/></a></td>\n");
583
584 html("<td class='main'>");
585 if (ctx->repo) {
586 cgit_index_link("index", NULL, NULL, NULL, 0);
587 html(" : ");
588 reporevlink(NULL, ctx->repo->name, NULL, hc(cmd, "summary"),
589 ctx->qry.head, NULL, NULL);
590 html("</td><td class='form'>");
591 html("<form method='get' action=''>\n");
592 add_hidden_formfields(0, 1, ctx->qry.page);
593 html("<select name='h' onchange='this.form.submit();'>\n");
594 for_each_branch_ref(print_branch_option, ctx->qry.head);
595 html("</select> ");
596 html("<input type='submit' name='' value='switch'/>");
597 html("</form>");
598 } else
599 html_txt(ctx->cfg.root_title);
600 html("</td></tr>\n");
601
602 html("<tr><td class='sub'>");
603 if (ctx->repo) {
604 html_txt(ctx->repo->desc);
605 html("</td><td class='sub right'>");
606 html_txt(ctx->repo->owner);
607 } else {
608 if (ctx->cfg.root_desc)
609 html_txt(ctx->cfg.root_desc);
610 else if (ctx->cfg.index_info)
611 html_include(ctx->cfg.index_info);
612 }
613 html("</td></tr></table>\n");
614
615 html("<table class='tabs'><tr><td>\n");
616 if (ctx->repo) {
617 reporevlink(NULL, "summary", NULL, hc(cmd, "summary"),
618 ctx->qry.head, NULL, NULL);
619 cgit_refs_link("refs", NULL, hc(cmd, "refs"), ctx->qry.head,
620 ctx->qry.sha1, NULL);
621 cgit_log_link("log", NULL, hc(cmd, "log"), ctx->qry.head,
622 NULL, NULL, 0, NULL, NULL);
623 cgit_tree_link("tree", NULL, hc(cmd, "tree"), ctx->qry.head,
624 ctx->qry.sha1, NULL);
625 cgit_commit_link("commit", NULL, hc(cmd, "commit"),
626 ctx->qry.head, ctx->qry.sha1);
627 cgit_diff_link("diff", NULL, hc(cmd, "diff"), ctx->qry.head,
628 ctx->qry.sha1, ctx->qry.sha2, NULL);
629 if (ctx->repo->readme)
630 reporevlink("about", "about", NULL,
631 hc(cmd, "about"), ctx->qry.head, NULL,
632 NULL);
633 html("</td><td class='form'>");
634 html("<form class='right' method='get' action='");
635 if (ctx->cfg.virtual_root)
636 html_attr(cgit_fileurl(ctx->qry.repo, "log",
637 ctx->qry.path, NULL));
638 html("'>\n");
639 add_hidden_formfields(1, 0, "log");
640 html("<select name='qt'>\n");
641 html_option("grep", "log msg", ctx->qry.grep);
642 html_option("author", "author", ctx->qry.grep);
643 html_option("committer", "committer", ctx->qry.grep);
644 html("</select>\n");
645 html("<input class='txt' type='text' size='10' name='q' value='");
646 html_attr(ctx->qry.search);
647 html("'/>\n");
648 html("<input type='submit' value='search'/>\n");
649 html("</form>\n");
650 } else {
651 site_link(NULL, "index", NULL, hc(cmd, "repolist"), NULL, 0);
652 if (ctx->cfg.root_readme)
653 site_link("about", "about", NULL, hc(cmd, "about"),
654 NULL, 0);
655 html("</td><td class='form'>");
656 html("<form method='get' action='");
657 html_attr(cgit_rooturl());
658 html("'>\n");
659 html("<input type='text' name='q' size='10' value='");
660 html_attr(ctx->qry.search);
661 html("'/>\n");
662 html("<input type='submit' value='search'/>\n");
663 html("</form>");
664 }
665 html("</td></tr></table>\n");
666 html("<div class='content'>");
667 }
668
669 void cgit_print_filemode(unsigned short mode)
670 {
671 if (S_ISDIR(mode))
672 html("d");
673 else if (S_ISLNK(mode))
674 html("l");
675 else if (S_ISGITLINK(mode))
676 html("m");
677 else
678 html("-");
679 html_fileperm(mode >> 6);
680 html_fileperm(mode >> 3);
681 html_fileperm(mode);
682 }
683
684 void cgit_print_snapshot_links(const char *repo, const char *head,
685 const char *hex, int snapshots)
686 {
687 const struct cgit_snapshot_format* f;
688 char *filename;
689
690 for (f = cgit_snapshot_formats; f->suffix; f++) {
691 if (!(snapshots & f->bit))
692 continue;
693 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex,
694 f->suffix);
695 cgit_snapshot_link(filename, NULL, NULL, (char *)head,
696 (char *)hex, filename);
697 html("<br/>");
698 }
699 }