]>
git.cameronkatri.com Git - cgit.git/blob - ui-shared.c
1 /* ui-shared.c: common web output functions
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
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";
17 static char *http_date(time_t t
)
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
);
30 void cgit_print_error(char *msg
)
32 html("<div class='error'>");
39 if (ctx
.cfg
.virtual_root
)
40 return fmt("%s/", ctx
.cfg
.virtual_root
);
42 return ctx
.cfg
.script_name
;
45 char *cgit_repourl(const char *reponame
)
47 if (ctx
.cfg
.virtual_root
) {
48 return fmt("%s/%s/", ctx
.cfg
.virtual_root
, reponame
);
50 return fmt("?r=%s", reponame
);
54 char *cgit_fileurl(const char *reponame
, const char *pagename
,
55 const char *filename
, const char *query
)
60 if (ctx
.cfg
.virtual_root
) {
61 tmp
= fmt("%s/%s/%s/%s", ctx
.cfg
.virtual_root
, reponame
,
62 pagename
, (filename
? filename
:""));
65 tmp
= fmt("?url=%s/%s/%s", reponame
, pagename
,
66 (filename
? filename
: ""));
70 tmp
= fmt("%s%s%s", tmp
, delim
, query
);
74 char *cgit_pageurl(const char *reponame
, const char *pagename
,
77 return cgit_fileurl(reponame
,pagename
,0,query
);
80 const char *cgit_repobasename(const char *reponame
)
82 /* I assume we don't need to store more than one repo basename */
83 static char rvbuf
[1024];
86 strncpy(rvbuf
,reponame
,sizeof(rvbuf
));
87 if(rvbuf
[sizeof(rvbuf
)-1])
88 die("cgit_repobasename: truncated repository name '%s'", reponame
);
90 /* strip trailing slashes */
91 while(p
&& rvbuf
[p
]=='/') rvbuf
[p
--]=0;
92 /* strip trailing .git */
93 if(p
>=3 && !strncmp(&rvbuf
[p
-3],".git",4)) {
94 p
-= 3; rvbuf
[p
--] = 0;
96 /* strip more trailing slashes if any */
97 while( p
&& rvbuf
[p
]=='/') rvbuf
[p
--]=0;
98 /* find last slash in the remaining string */
99 rv
= strrchr(rvbuf
,'/');
107 if (!ctx
.cfg
.virtual_root
)
108 return ctx
.cfg
.script_name
;
109 else if (ctx
.qry
.page
)
110 return fmt("%s/%s/%s/", ctx
.cfg
.virtual_root
, ctx
.qry
.repo
, ctx
.qry
.page
);
111 else if (ctx
.qry
.repo
)
112 return fmt("%s/%s/", ctx
.cfg
.virtual_root
, ctx
.qry
.repo
);
114 return fmt("%s/", ctx
.cfg
.virtual_root
);
117 static char *repolink(char *title
, char *class, char *page
, char *head
,
134 if (ctx
.cfg
.virtual_root
) {
135 html_attr(ctx
.cfg
.virtual_root
);
136 if (ctx
.cfg
.virtual_root
[strlen(ctx
.cfg
.virtual_root
) - 1] != '/')
138 html_attr(ctx
.repo
->url
);
139 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
148 html(ctx
.cfg
.script_name
);
150 html_attr(ctx
.repo
->url
);
151 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
161 if (head
&& strcmp(head
, ctx
.repo
->defbranch
)) {
167 return fmt("%s", delim
);
170 static void reporevlink(char *page
, char *name
, char *title
, char *class,
171 char *head
, char *rev
, char *path
)
175 delim
= repolink(title
, class, page
, head
, path
);
176 if (rev
&& strcmp(rev
, ctx
.qry
.head
)) {
186 void cgit_tree_link(char *name
, char *title
, char *class, char *head
,
187 char *rev
, char *path
)
189 reporevlink("tree", name
, title
, class, head
, rev
, path
);
192 void cgit_log_link(char *name
, char *title
, char *class, char *head
,
193 char *rev
, char *path
, int ofs
, char *grep
, char *pattern
)
197 delim
= repolink(title
, class, "log", head
, path
);
198 if (rev
&& strcmp(rev
, ctx
.qry
.head
)) {
204 if (grep
&& pattern
) {
223 void cgit_commit_link(char *name
, char *title
, char *class, char *head
,
226 if (strlen(name
) > ctx
.cfg
.max_msg_len
&& ctx
.cfg
.max_msg_len
>= 15) {
227 name
[ctx
.cfg
.max_msg_len
] = '\0';
228 name
[ctx
.cfg
.max_msg_len
- 1] = '.';
229 name
[ctx
.cfg
.max_msg_len
- 2] = '.';
230 name
[ctx
.cfg
.max_msg_len
- 3] = '.';
232 reporevlink("commit", name
, title
, class, head
, rev
, NULL
);
235 void cgit_refs_link(char *name
, char *title
, char *class, char *head
,
236 char *rev
, char *path
)
238 reporevlink("refs", name
, title
, class, head
, rev
, path
);
241 void cgit_snapshot_link(char *name
, char *title
, char *class, char *head
,
242 char *rev
, char *archivename
)
244 reporevlink("snapshot", name
, title
, class, head
, rev
, archivename
);
247 void cgit_diff_link(char *name
, char *title
, char *class, char *head
,
248 char *new_rev
, char *old_rev
, char *path
)
252 delim
= repolink(title
, class, "diff", head
, path
);
253 if (new_rev
&& strcmp(new_rev
, ctx
.qry
.head
)) {
269 void cgit_patch_link(char *name
, char *title
, char *class, char *head
,
272 reporevlink("patch", name
, title
, class, head
, rev
, NULL
);
275 void cgit_object_link(struct object
*obj
)
277 char *page
, *arg
, *url
;
279 if (obj
->type
== OBJ_COMMIT
) {
280 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj
->sha1
)), NULL
, NULL
,
281 ctx
.qry
.head
, sha1_to_hex(obj
->sha1
));
283 } else if (obj
->type
== OBJ_TREE
) {
286 } else if (obj
->type
== OBJ_TAG
) {
294 url
= cgit_pageurl(ctx
.qry
.repo
, page
,
295 fmt("%s=%s", arg
, sha1_to_hex(obj
->sha1
)));
296 html_link_open(url
, NULL
, NULL
);
297 htmlf("%s %s", typename(obj
->type
),
298 sha1_to_hex(obj
->sha1
));
302 void cgit_print_date(time_t secs
, char *format
)
309 time
= gmtime(&secs
);
310 strftime(buf
, sizeof(buf
)-1, format
, time
);
314 void cgit_print_age(time_t t
, time_t max_relative
, char *format
)
323 if (secs
> max_relative
&& max_relative
>= 0) {
324 cgit_print_date(t
, format
);
328 if (secs
< TM_HOUR
* 2) {
329 htmlf("<span class='age-mins'>%.0f min.</span>",
330 secs
* 1.0 / TM_MIN
);
333 if (secs
< TM_DAY
* 2) {
334 htmlf("<span class='age-hours'>%.0f hours</span>",
335 secs
* 1.0 / TM_HOUR
);
338 if (secs
< TM_WEEK
* 2) {
339 htmlf("<span class='age-days'>%.0f days</span>",
340 secs
* 1.0 / TM_DAY
);
343 if (secs
< TM_MONTH
* 2) {
344 htmlf("<span class='age-weeks'>%.0f weeks</span>",
345 secs
* 1.0 / TM_WEEK
);
348 if (secs
< TM_YEAR
* 2) {
349 htmlf("<span class='age-months'>%.0f months</span>",
350 secs
* 1.0 / TM_MONTH
);
353 htmlf("<span class='age-years'>%.0f years</span>",
354 secs
* 1.0 / TM_YEAR
);
357 void cgit_print_http_headers(struct cgit_context
*ctx
)
359 if (ctx
->page
.mimetype
&& ctx
->page
.charset
)
360 htmlf("Content-Type: %s; charset=%s\n", ctx
->page
.mimetype
,
362 else if (ctx
->page
.mimetype
)
363 htmlf("Content-Type: %s\n", ctx
->page
.mimetype
);
364 if (ctx
->page
.filename
)
365 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
367 htmlf("Last-Modified: %s\n", http_date(ctx
->page
.modified
));
368 htmlf("Expires: %s\n", http_date(ctx
->page
.expires
));
372 void cgit_print_docstart(struct cgit_context
*ctx
)
375 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
378 html_txt(ctx
->page
.title
);
380 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version
);
381 if (ctx
->cfg
.robots
&& *ctx
->cfg
.robots
)
382 htmlf("<meta name='robots' content='%s'/>\n", ctx
->cfg
.robots
);
383 html("<link rel='stylesheet' type='text/css' href='");
384 html_attr(ctx
->cfg
.css
);
390 void cgit_print_docend()
392 html("</td>\n</tr>\n</table>\n</body>\n</html>\n");
395 int print_branch_option(const char *refname
, const unsigned char *sha1
,
396 int flags
, void *cb_data
)
398 char *name
= (char *)refname
;
399 html_option(name
, name
, ctx
.qry
.head
);
403 int print_archive_ref(const char *refname
, const unsigned char *sha1
,
404 int flags
, void *cb_data
)
407 struct taginfo
*info
;
410 unsigned char fileid
[20];
411 int *header
= (int *)cb_data
;
413 if (prefixcmp(refname
, "refs/archives"))
415 strncpy(buf
, refname
+14, sizeof(buf
));
416 obj
= parse_object(sha1
);
419 if (obj
->type
== OBJ_TAG
) {
420 tag
= lookup_tag(sha1
);
421 if (!tag
|| parse_tag(tag
) || !(info
= cgit_parse_tag(tag
)))
423 hashcpy(fileid
, tag
->tagged
->sha1
);
424 } else if (obj
->type
!= OBJ_BLOB
) {
427 hashcpy(fileid
, sha1
);
430 html("<h1>download</h1>\n");
433 url
= cgit_pageurl(ctx
.qry
.repo
, "blob",
434 fmt("id=%s&path=%s", sha1_to_hex(fileid
),
436 html_link_open(url
, NULL
, "menu");
437 html_txt(strlpart(buf
, 20));
442 void add_hidden_formfields(int incl_head
, int incl_search
, char *page
)
446 if (!ctx
.cfg
.virtual_root
) {
447 url
= fmt("%s/%s", ctx
.qry
.repo
, page
);
449 url
= fmt("%s/%s", url
, ctx
.qry
.path
);
450 html_hidden("url", url
);
453 if (incl_head
&& strcmp(ctx
.qry
.head
, ctx
.repo
->defbranch
))
454 html_hidden("h", ctx
.qry
.head
);
457 html_hidden("id", ctx
.qry
.sha1
);
459 html_hidden("id2", ctx
.qry
.sha2
);
463 html_hidden("qt", ctx
.qry
.grep
);
465 html_hidden("q", ctx
.qry
.search
);
469 char *hc(struct cgit_cmd
*cmd
, const char *page
)
471 return (strcmp(cmd
->name
, page
) ? NULL
: "active");
474 void cgit_print_pageheader(struct cgit_context
*ctx
)
476 struct cgit_cmd
*cmd
= cgit_get_cmd(ctx
);
478 html("<table id='header'>\n");
480 html("<td class='logo' rowspan='2'><a href='");
481 if (ctx
->cfg
.logo_link
)
482 html_attr(ctx
->cfg
.logo_link
);
484 html_attr(cgit_rooturl());
485 html("'><img src='");
486 html_attr(ctx
->cfg
.logo
);
487 html("'/></a></td>\n");
488 html("<td class='main'>");
491 html_attr(cgit_rooturl());
492 html("'>index</a> / ");
493 html_txt(ctx
->repo
->name
);
495 html_txt(ctx
->cfg
.root_title
);
496 html("</td></tr>\n");
497 html("<tr><td class='sub'>");
499 html_txt(ctx
->repo
->desc
);
501 html_txt(ctx
->cfg
.index_info
);
502 html("</td></tr>\n");
505 html("<table class='tabs'><tr><td>\n");
507 reporevlink(NULL
, "summary", NULL
, hc(cmd
, "summary"),
508 ctx
->qry
.head
, NULL
, NULL
);
509 cgit_refs_link("refs", NULL
, hc(cmd
, "refs"), ctx
->qry
.head
,
510 ctx
->qry
.sha1
, NULL
);
511 cgit_log_link("log", NULL
, hc(cmd
, "log"), ctx
->qry
.head
,
512 NULL
, NULL
, 0, NULL
, NULL
);
513 cgit_tree_link("tree", NULL
, hc(cmd
, "tree"), ctx
->qry
.head
,
514 ctx
->qry
.sha1
, NULL
);
515 cgit_commit_link("commit", NULL
, hc(cmd
, "commit"),
516 ctx
->qry
.head
, ctx
->qry
.sha1
);
517 cgit_diff_link("diff", NULL
, hc(cmd
, "diff"), ctx
->qry
.head
,
518 ctx
->qry
.sha1
, ctx
->qry
.sha2
, NULL
);
519 cgit_patch_link("patch", NULL
, hc(cmd
, "patch"), ctx
->qry
.head
,
521 html("</td><td class='branch'>");
522 html("<form method='get' action=''>\n");
523 add_hidden_formfields(0, 1, ctx
->qry
.page
);
524 html("<select name='h' onchange='this.form.submit();'>\n");
525 for_each_branch_ref(print_branch_option
, ctx
->qry
.head
);
527 html("<input type='submit' name='' value='switch'/>");
529 html("<a class='active' href='");
530 html_attr(cgit_rooturl());
531 html("'>index</a>\n");
533 html("</td></tr></table>\n");
534 html("<div class='content'>");
537 void cgit_print_filemode(unsigned short mode
)
541 else if (S_ISLNK(mode
))
543 else if (S_ISGITLINK(mode
))
547 html_fileperm(mode
>> 6);
548 html_fileperm(mode
>> 3);
552 void cgit_print_snapshot_links(const char *repo
, const char *head
,
553 const char *hex
, int snapshots
)
555 const struct cgit_snapshot_format
* f
;
558 for (f
= cgit_snapshot_formats
; f
->suffix
; f
++) {
559 if (!(snapshots
& f
->bit
))
561 filename
= fmt("%s-%s%s", cgit_repobasename(repo
), hex
,
563 cgit_snapshot_link(filename
, NULL
, NULL
, (char *)head
,
564 (char *)hex
, filename
);