summaryrefslogtreecommitdiffabout
path: root/ui-shared.c
authorLars Hjemli <hjemli@gmail.com>2008-04-12 13:53:31 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2008-04-12 18:00:27 (UTC)
commitf135569b2be3fb1816f802f9a162b3743b735d1c (patch) (unidiff)
tree240917f3d213ea255c34db77af4e1cdfec950264 /ui-shared.c
parent4a842288260a0b0c4a3d4032d441f7fd2afee699 (diff)
downloadcgit-f135569b2be3fb1816f802f9a162b3743b735d1c.zip
cgit-f135569b2be3fb1816f802f9a162b3743b735d1c.tar.gz
cgit-f135569b2be3fb1816f802f9a162b3743b735d1c.tar.bz2
Replace sidebar/logo
This replaces the sidebar with a more 'common' header layout and also updates the logo. Not quite finished yet, though. Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (limited to 'ui-shared.c') (more/less context) (ignore whitespace changes)
-rw-r--r--ui-shared.c125
1 files changed, 44 insertions, 81 deletions
diff --git a/ui-shared.c b/ui-shared.c
index aa65988..7287956 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -1,105 +1,106 @@
1/* ui-shared.c: common web output functions 1/* ui-shared.c: common web output functions
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10#include "cmd.h"
10#include "html.h" 11#include "html.h"
11 12
12const char cgit_doctype[] = 13const char cgit_doctype[] =
13"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" 14"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
14" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; 15" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
15 16
16static char *http_date(time_t t) 17static char *http_date(time_t t)
17{ 18{
18 static char day[][4] = 19 static char day[][4] =
19 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; 20 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
20 static char month[][4] = 21 static char month[][4] =
21 {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 22 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
22 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; 23 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
23 struct tm *tm = gmtime(&t); 24 struct tm *tm = gmtime(&t);
24 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], 25 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
25 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, 26 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
26 tm->tm_hour, tm->tm_min, tm->tm_sec); 27 tm->tm_hour, tm->tm_min, tm->tm_sec);
27} 28}
28 29
29void cgit_print_error(char *msg) 30void cgit_print_error(char *msg)
30{ 31{
31 html("<div class='error'>"); 32 html("<div class='error'>");
32 html_txt(msg); 33 html_txt(msg);
33 html("</div>\n"); 34 html("</div>\n");
34} 35}
35 36
36char *cgit_rooturl() 37char *cgit_rooturl()
37{ 38{
38 if (ctx.cfg.virtual_root) 39 if (ctx.cfg.virtual_root)
39 return fmt("%s/", ctx.cfg.virtual_root); 40 return fmt("%s/", ctx.cfg.virtual_root);
40 else 41 else
41 return ctx.cfg.script_name; 42 return ctx.cfg.script_name;
42} 43}
43 44
44char *cgit_repourl(const char *reponame) 45char *cgit_repourl(const char *reponame)
45{ 46{
46 if (ctx.cfg.virtual_root) { 47 if (ctx.cfg.virtual_root) {
47 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame); 48 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame);
48 } else { 49 } else {
49 return fmt("?r=%s", reponame); 50 return fmt("?r=%s", reponame);
50 } 51 }
51} 52}
52 53
53char *cgit_fileurl(const char *reponame, const char *pagename, 54char *cgit_fileurl(const char *reponame, const char *pagename,
54 const char *filename, const char *query) 55 const char *filename, const char *query)
55{ 56{
56 char *tmp; 57 char *tmp;
57 char *delim; 58 char *delim;
58 59
59 if (ctx.cfg.virtual_root) { 60 if (ctx.cfg.virtual_root) {
60 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame, 61 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame,
61 pagename, (filename ? filename:"")); 62 pagename, (filename ? filename:""));
62 delim = "?"; 63 delim = "?";
63 } else { 64 } else {
64 tmp = fmt("?url=%s/%s/%s", reponame, pagename, 65 tmp = fmt("?url=%s/%s/%s", reponame, pagename,
65 (filename ? filename : "")); 66 (filename ? filename : ""));
66 delim = "&"; 67 delim = "&";
67 } 68 }
68 if (query) 69 if (query)
69 tmp = fmt("%s%s%s", tmp, delim, query); 70 tmp = fmt("%s%s%s", tmp, delim, query);
70 return tmp; 71 return tmp;
71} 72}
72 73
73char *cgit_pageurl(const char *reponame, const char *pagename, 74char *cgit_pageurl(const char *reponame, const char *pagename,
74 const char *query) 75 const char *query)
75{ 76{
76 return cgit_fileurl(reponame,pagename,0,query); 77 return cgit_fileurl(reponame,pagename,0,query);
77} 78}
78 79
79const char *cgit_repobasename(const char *reponame) 80const char *cgit_repobasename(const char *reponame)
80{ 81{
81 /* I assume we don't need to store more than one repo basename */ 82 /* I assume we don't need to store more than one repo basename */
82 static char rvbuf[1024]; 83 static char rvbuf[1024];
83 int p; 84 int p;
84 const char *rv; 85 const char *rv;
85 strncpy(rvbuf,reponame,sizeof(rvbuf)); 86 strncpy(rvbuf,reponame,sizeof(rvbuf));
86 if(rvbuf[sizeof(rvbuf)-1]) 87 if(rvbuf[sizeof(rvbuf)-1])
87 die("cgit_repobasename: truncated repository name '%s'", reponame); 88 die("cgit_repobasename: truncated repository name '%s'", reponame);
88 p = strlen(rvbuf)-1; 89 p = strlen(rvbuf)-1;
89 /* strip trailing slashes */ 90 /* strip trailing slashes */
90 while(p && rvbuf[p]=='/') rvbuf[p--]=0; 91 while(p && rvbuf[p]=='/') rvbuf[p--]=0;
91 /* strip trailing .git */ 92 /* strip trailing .git */
92 if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) { 93 if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) {
93 p -= 3; rvbuf[p--] = 0; 94 p -= 3; rvbuf[p--] = 0;
94 } 95 }
95 /* strip more trailing slashes if any */ 96 /* strip more trailing slashes if any */
96 while( p && rvbuf[p]=='/') rvbuf[p--]=0; 97 while( p && rvbuf[p]=='/') rvbuf[p--]=0;
97 /* find last slash in the remaining string */ 98 /* find last slash in the remaining string */
98 rv = strrchr(rvbuf,'/'); 99 rv = strrchr(rvbuf,'/');
99 if(rv) 100 if(rv)
100 return ++rv; 101 return ++rv;
101 return rvbuf; 102 return rvbuf;
102} 103}
103 104
104char *cgit_currurl() 105char *cgit_currurl()
105{ 106{
@@ -372,220 +373,182 @@ void cgit_print_docstart(struct cgit_context *ctx)
372{ 373{
373 html(cgit_doctype); 374 html(cgit_doctype);
374 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); 375 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
375 html("<head>\n"); 376 html("<head>\n");
376 html("<title>"); 377 html("<title>");
377 html_txt(ctx->page.title); 378 html_txt(ctx->page.title);
378 html("</title>\n"); 379 html("</title>\n");
379 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); 380 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
380 if (ctx->cfg.robots && *ctx->cfg.robots) 381 if (ctx->cfg.robots && *ctx->cfg.robots)
381 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots); 382 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots);
382 html("<link rel='stylesheet' type='text/css' href='"); 383 html("<link rel='stylesheet' type='text/css' href='");
383 html_attr(ctx->cfg.css); 384 html_attr(ctx->cfg.css);
384 html("'/>\n"); 385 html("'/>\n");
385 html("</head>\n"); 386 html("</head>\n");
386 html("<body>\n"); 387 html("<body>\n");
387} 388}
388 389
389void cgit_print_docend() 390void cgit_print_docend()
390{ 391{
391 html("</td>\n</tr>\n</table>\n</body>\n</html>\n"); 392 html("</td>\n</tr>\n</table>\n</body>\n</html>\n");
392} 393}
393 394
394int print_branch_option(const char *refname, const unsigned char *sha1, 395int print_branch_option(const char *refname, const unsigned char *sha1,
395 int flags, void *cb_data) 396 int flags, void *cb_data)
396{ 397{
397 char *name = (char *)refname; 398 char *name = (char *)refname;
398 html_option(name, name, ctx.qry.head); 399 html_option(name, name, ctx.qry.head);
399 return 0; 400 return 0;
400} 401}
401 402
402int print_archive_ref(const char *refname, const unsigned char *sha1, 403int print_archive_ref(const char *refname, const unsigned char *sha1,
403 int flags, void *cb_data) 404 int flags, void *cb_data)
404{ 405{
405 struct tag *tag; 406 struct tag *tag;
406 struct taginfo *info; 407 struct taginfo *info;
407 struct object *obj; 408 struct object *obj;
408 char buf[256], *url; 409 char buf[256], *url;
409 unsigned char fileid[20]; 410 unsigned char fileid[20];
410 int *header = (int *)cb_data; 411 int *header = (int *)cb_data;
411 412
412 if (prefixcmp(refname, "refs/archives")) 413 if (prefixcmp(refname, "refs/archives"))
413 return 0; 414 return 0;
414 strncpy(buf, refname+14, sizeof(buf)); 415 strncpy(buf, refname+14, sizeof(buf));
415 obj = parse_object(sha1); 416 obj = parse_object(sha1);
416 if (!obj) 417 if (!obj)
417 return 1; 418 return 1;
418 if (obj->type == OBJ_TAG) { 419 if (obj->type == OBJ_TAG) {
419 tag = lookup_tag(sha1); 420 tag = lookup_tag(sha1);
420 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) 421 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag)))
421 return 0; 422 return 0;
422 hashcpy(fileid, tag->tagged->sha1); 423 hashcpy(fileid, tag->tagged->sha1);
423 } else if (obj->type != OBJ_BLOB) { 424 } else if (obj->type != OBJ_BLOB) {
424 return 0; 425 return 0;
425 } else { 426 } else {
426 hashcpy(fileid, sha1); 427 hashcpy(fileid, sha1);
427 } 428 }
428 if (!*header) { 429 if (!*header) {
429 html("<h1>download</h1>\n"); 430 html("<h1>download</h1>\n");
430 *header = 1; 431 *header = 1;
431 } 432 }
432 url = cgit_pageurl(ctx.qry.repo, "blob", 433 url = cgit_pageurl(ctx.qry.repo, "blob",
433 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid), 434 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid),
434 buf)); 435 buf));
435 html_link_open(url, NULL, "menu"); 436 html_link_open(url, NULL, "menu");
436 html_txt(strlpart(buf, 20)); 437 html_txt(strlpart(buf, 20));
437 html_link_close(); 438 html_link_close();
438 return 0; 439 return 0;
439} 440}
440 441
441void add_hidden_formfields(int incl_head, int incl_search, char *page) 442void add_hidden_formfields(int incl_head, int incl_search, char *page)
442{ 443{
443 char *url; 444 char *url;
444 445
445 if (!ctx.cfg.virtual_root) { 446 if (!ctx.cfg.virtual_root) {
446 url = fmt("%s/%s", ctx.qry.repo, page); 447 url = fmt("%s/%s", ctx.qry.repo, page);
447 if (ctx.qry.path) 448 if (ctx.qry.path)
448 url = fmt("%s/%s", url, ctx.qry.path); 449 url = fmt("%s/%s", url, ctx.qry.path);
449 html_hidden("url", url); 450 html_hidden("url", url);
450 } 451 }
451 452
452 if (incl_head && strcmp(ctx.qry.head, ctx.repo->defbranch)) 453 if (incl_head && strcmp(ctx.qry.head, ctx.repo->defbranch))
453 html_hidden("h", ctx.qry.head); 454 html_hidden("h", ctx.qry.head);
454 455
455 if (ctx.qry.sha1) 456 if (ctx.qry.sha1)
456 html_hidden("id", ctx.qry.sha1); 457 html_hidden("id", ctx.qry.sha1);
457 if (ctx.qry.sha2) 458 if (ctx.qry.sha2)
458 html_hidden("id2", ctx.qry.sha2); 459 html_hidden("id2", ctx.qry.sha2);
459 460
460 if (incl_search) { 461 if (incl_search) {
461 if (ctx.qry.grep) 462 if (ctx.qry.grep)
462 html_hidden("qt", ctx.qry.grep); 463 html_hidden("qt", ctx.qry.grep);
463 if (ctx.qry.search) 464 if (ctx.qry.search)
464 html_hidden("q", ctx.qry.search); 465 html_hidden("q", ctx.qry.search);
465 } 466 }
466} 467}
467 468
469char *hc(struct cgit_cmd *cmd, const char *page)
470{
471 return (strcmp(cmd->name, page) ? NULL : "active");
472}
473
468void cgit_print_pageheader(struct cgit_context *ctx) 474void cgit_print_pageheader(struct cgit_context *ctx)
469{ 475{
470 static const char *default_info = "This is cgit, a fast webinterface for git repositories"; 476 struct cgit_cmd *cmd = cgit_get_cmd(ctx);
471 int header = 0;
472 char *url;
473 477
474 html("<table id='layout' summary=''>\n"); 478 html("<table id='header'>\n");
475 html("<tr><td id='sidebar'>\n"); 479 html("<tr>\n");
476 html("<table class='sidebar' cellspacing='0' summary=''>\n"); 480 html("<td class='logo' rowspan='2'><a href='");
477 html("<tr><td class='sidebar'>\n<a href='");
478 html_attr(cgit_rooturl()); 481 html_attr(cgit_rooturl());
479 htmlf("'><img src='%s' alt='cgit'/></a>\n", 482 html("'><img src='");
480 ctx->cfg.logo); 483 html_attr(ctx->cfg.logo);
481 html("</td></tr>\n<tr><td class='sidebar'>\n"); 484 html("'/></a></td>\n");
482 if (ctx->repo) { 485 html("<td class='main'>");
483 html("<h1 class='first'>"); 486 if (ctx->repo)
484 html_txt(strrpart(ctx->repo->name, 20)); 487 html_txt(ctx->repo->name);
485 html("</h1>\n"); 488 else
489 html_txt(ctx->cfg.root_title);
490 html("</td></tr>\n");
491 html("<tr><td class='sub'>");
492 if (ctx->repo)
486 html_txt(ctx->repo->desc); 493 html_txt(ctx->repo->desc);
487 if (ctx->repo->owner) { 494 else
488 html("<h1>owner</h1>\n"); 495 html_txt(ctx->cfg.index_info);
489 html_txt(ctx->repo->owner); 496 html("</td></tr>\n");
490 } 497 html("</table>\n");
491 html("<h1>navigate</h1>\n"); 498
492 reporevlink(NULL, "summary", NULL, "menu", ctx->qry.head, 499 html("<table class='tabs'><tr><td>\n");
493 NULL, NULL); 500 if (ctx->repo) {
494 cgit_log_link("log", NULL, "menu", ctx->qry.head, NULL, NULL, 501 reporevlink(NULL, "summary", NULL, hc(cmd, "summary"),
495 0, NULL, NULL); 502 ctx->qry.head, NULL, NULL);
496 cgit_tree_link("tree", NULL, "menu", ctx->qry.head, 503 cgit_refs_link("refs", NULL, hc(cmd, "refs"), ctx->qry.head,
497 ctx->qry.sha1, NULL); 504 ctx->qry.sha1, NULL);
498 cgit_commit_link("commit", NULL, "menu", ctx->qry.head, 505 cgit_log_link("log", NULL, hc(cmd, "log"), ctx->qry.head,
499 ctx->qry.sha1); 506 NULL, NULL, 0, NULL, NULL);
500 cgit_diff_link("diff", NULL, "menu", ctx->qry.head, 507 cgit_tree_link("tree", NULL, hc(cmd, "tree"), ctx->qry.head,
508 ctx->qry.sha1, NULL);
509 cgit_commit_link("commit", NULL, hc(cmd, "commit"),
510 ctx->qry.head, ctx->qry.sha1);
511 cgit_diff_link("diff", NULL, hc(cmd, "diff"), ctx->qry.head,
501 ctx->qry.sha1, ctx->qry.sha2, NULL); 512 ctx->qry.sha1, ctx->qry.sha2, NULL);
502 cgit_patch_link("patch", NULL, "menu", ctx->qry.head, 513 cgit_patch_link("patch", NULL, hc(cmd, "patch"), ctx->qry.head,
503 ctx->qry.sha1); 514 ctx->qry.sha1);
504
505 for_each_ref(print_archive_ref, &header);
506
507 if (ctx->repo->clone_url || ctx->cfg.clone_prefix) {
508 html("<h1>clone</h1>\n");
509 if (ctx->repo->clone_url)
510 url = ctx->repo->clone_url;
511 else
512 url = fmt("%s%s", ctx->cfg.clone_prefix,
513 ctx->repo->url);
514 html("<a class='menu' href='");
515 html_attr(url);
516 html("' title='");
517 html_attr(url);
518 html("'>\n");
519 html_txt(strrpart(url, 20));
520 html("</a>\n");
521 }
522
523 html("<h1>branch</h1>\n");
524 html("<form method='get' action=''>\n");
525 add_hidden_formfields(0, 1, ctx->qry.page);
526 // html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>");
527 html("<select name='h' onchange='this.form.submit();'>\n");
528 for_each_branch_ref(print_branch_option, ctx->qry.head);
529 html("</select>\n");
530 // html("</td><td>");
531 html("<noscript><input type='submit' id='switch-btn' value='switch'/></noscript>\n");
532 // html("</td></tr></table>");
533 html("</form>\n");
534
535 html("<h1>search</h1>\n");
536 html("<form method='get' action='");
537 if (ctx->cfg.virtual_root)
538 html_attr(cgit_fileurl(ctx->qry.repo, "log",
539 ctx->qry.path, NULL));
540 html("'>\n");
541 add_hidden_formfields(1, 0, "log");
542 html("<select name='qt'>\n");
543 html_option("grep", "log msg", ctx->qry.grep);
544 html_option("author", "author", ctx->qry.grep);
545 html_option("committer", "committer", ctx->qry.grep);
546 html("</select>\n");
547 html("<input class='txt' type='text' name='q' value='");
548 html_attr(ctx->qry.search);
549 html("'/>\n");
550 html("</form>\n");
551 } else { 515 } else {
552 if (!ctx->cfg.index_info || html_include(ctx->cfg.index_info)) 516 html("<a class='active' href='");
553 html(default_info); 517 html_attr(cgit_rooturl());
518 html("'>index</a>\n");
554 } 519 }
555 520 html("</td></tr></table>\n");
556 html("</td></tr></table></td>\n"); 521 html("<div class='content'>");
557
558 html("<td id='content'>\n");
559} 522}
560 523
561void cgit_print_filemode(unsigned short mode) 524void cgit_print_filemode(unsigned short mode)
562{ 525{
563 if (S_ISDIR(mode)) 526 if (S_ISDIR(mode))
564 html("d"); 527 html("d");
565 else if (S_ISLNK(mode)) 528 else if (S_ISLNK(mode))
566 html("l"); 529 html("l");
567 else if (S_ISGITLINK(mode)) 530 else if (S_ISGITLINK(mode))
568 html("m"); 531 html("m");
569 else 532 else
570 html("-"); 533 html("-");
571 html_fileperm(mode >> 6); 534 html_fileperm(mode >> 6);
572 html_fileperm(mode >> 3); 535 html_fileperm(mode >> 3);
573 html_fileperm(mode); 536 html_fileperm(mode);
574} 537}
575 538
576void cgit_print_snapshot_links(const char *repo, const char *head, 539void cgit_print_snapshot_links(const char *repo, const char *head,
577 const char *hex, int snapshots) 540 const char *hex, int snapshots)
578{ 541{
579 const struct cgit_snapshot_format* f; 542 const struct cgit_snapshot_format* f;
580 char *filename; 543 char *filename;
581 544
582 for (f = cgit_snapshot_formats; f->suffix; f++) { 545 for (f = cgit_snapshot_formats; f->suffix; f++) {
583 if (!(snapshots & f->bit)) 546 if (!(snapshots & f->bit))
584 continue; 547 continue;
585 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex, 548 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex,
586 f->suffix); 549 f->suffix);
587 cgit_snapshot_link(filename, NULL, NULL, (char *)head, 550 cgit_snapshot_link(filename, NULL, NULL, (char *)head,
588 (char *)hex, filename); 551 (char *)hex, filename);
589 html("<br/>"); 552 html("<br/>");
590 } 553 }
591} 554}