]> gitweb.ps.run Git - ps-cgit/blob - ui-shared.c
Introduce html.h
[ps-cgit] / 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 "html.h"
11
12 const char cgit_doctype[] =
13 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
14 "  \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
15
16 static char *http_date(time_t t)
17 {
18         static char day[][4] =
19                 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
20         static char month[][4] =
21                 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
22                  "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
23         struct tm *tm = gmtime(&t);
24         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_hour, tm->tm_min, tm->tm_sec);
27 }
28
29 static long ttl_seconds(long ttl)
30 {
31         if (ttl<0)
32                 return 60 * 60 * 24 * 365;
33         else
34                 return ttl * 60;
35 }
36
37 void cgit_print_error(char *msg)
38 {
39         html("<div class='error'>");
40         html_txt(msg);
41         html("</div>\n");
42 }
43
44 char *cgit_rooturl()
45 {
46         if (ctx.cfg.virtual_root)
47                 return fmt("%s/", ctx.cfg.virtual_root);
48         else
49                 return ctx.cfg.script_name;
50 }
51
52 char *cgit_repourl(const char *reponame)
53 {
54         if (ctx.cfg.virtual_root) {
55                 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame);
56         } else {
57                 return fmt("?r=%s", reponame);
58         }
59 }
60
61 char *cgit_fileurl(const char *reponame, const char *pagename,
62                    const char *filename, const char *query)
63 {
64         char *tmp;
65         char *delim;
66
67         if (ctx.cfg.virtual_root) {
68                 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame,
69                           pagename, (filename ? filename:""));
70                 delim = "?";
71         } else {
72                 tmp = fmt("?url=%s/%s/%s", reponame, pagename,
73                           (filename ? filename : ""));
74                 delim = "&";
75         }
76         if (query)
77                 tmp = fmt("%s%s%s", tmp, delim, query);
78         return tmp;
79 }
80
81 char *cgit_pageurl(const char *reponame, const char *pagename,
82                    const char *query)
83 {
84         return cgit_fileurl(reponame,pagename,0,query);
85 }
86
87 const char *cgit_repobasename(const char *reponame)
88 {
89         /* I assume we don't need to store more than one repo basename */
90         static char rvbuf[1024];
91         int p;
92         const char *rv;
93         strncpy(rvbuf,reponame,sizeof(rvbuf));
94         if(rvbuf[sizeof(rvbuf)-1])
95                 die("cgit_repobasename: truncated repository name '%s'", reponame);
96         p = strlen(rvbuf)-1;
97         /* strip trailing slashes */
98         while(p && rvbuf[p]=='/') rvbuf[p--]=0;
99         /* strip trailing .git */
100         if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) {
101                 p -= 3; rvbuf[p--] = 0;
102         }
103         /* strip more trailing slashes if any */
104         while( p && rvbuf[p]=='/') rvbuf[p--]=0;
105         /* find last slash in the remaining string */
106         rv = strrchr(rvbuf,'/');
107         if(rv)
108                 return ++rv;
109         return rvbuf;
110 }
111
112 char *cgit_currurl()
113 {
114         if (!ctx.cfg.virtual_root)
115                 return ctx.cfg.script_name;
116         else if (ctx.qry.page)
117                 return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page);
118         else if (ctx.qry.repo)
119                 return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo);
120         else
121                 return fmt("%s/", ctx.cfg.virtual_root);
122 }
123
124 static char *repolink(char *title, char *class, char *page, char *head,
125                       char *path)
126 {
127         char *delim = "?";
128
129         html("<a");
130         if (title) {
131                 html(" title='");
132                 html_attr(title);
133                 html("'");
134         }
135         if (class) {
136                 html(" class='");
137                 html_attr(class);
138                 html("'");
139         }
140         html(" href='");
141         if (ctx.cfg.virtual_root) {
142                 html_attr(ctx.cfg.virtual_root);
143                 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
144                         html("/");
145                 html_attr(ctx.repo->url);
146                 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
147                         html("/");
148                 if (page) {
149                         html(page);
150                         html("/");
151                         if (path)
152                                 html_attr(path);
153                 }
154         } else {
155                 html(ctx.cfg.script_name);
156                 html("?url=");
157                 html_attr(ctx.repo->url);
158                 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
159                         html("/");
160                 if (page) {
161                         html(page);
162                         html("/");
163                         if (path)
164                                 html_attr(path);
165                 }
166                 delim = "&amp;";
167         }
168         if (head && strcmp(head, ctx.repo->defbranch)) {
169                 html(delim);
170                 html("h=");
171                 html_attr(head);
172                 delim = "&amp;";
173         }
174         return fmt("%s", delim);
175 }
176
177 static void reporevlink(char *page, char *name, char *title, char *class,
178                         char *head, char *rev, char *path)
179 {
180         char *delim;
181
182         delim = repolink(title, class, page, head, path);
183         if (rev && strcmp(rev, ctx.qry.head)) {
184                 html(delim);
185                 html("id=");
186                 html_attr(rev);
187         }
188         html("'>");
189         html_txt(name);
190         html("</a>");
191 }
192
193 void cgit_tree_link(char *name, char *title, char *class, char *head,
194                     char *rev, char *path)
195 {
196         reporevlink("tree", name, title, class, head, rev, path);
197 }
198
199 void cgit_log_link(char *name, char *title, char *class, char *head,
200                    char *rev, char *path, int ofs, char *grep, char *pattern)
201 {
202         char *delim;
203
204         delim = repolink(title, class, "log", head, path);
205         if (rev && strcmp(rev, ctx.qry.head)) {
206                 html(delim);
207                 html("id=");
208                 html_attr(rev);
209                 delim = "&";
210         }
211         if (grep && pattern) {
212                 html(delim);
213                 html("qt=");
214                 html_attr(grep);
215                 delim = "&";
216                 html(delim);
217                 html("q=");
218                 html_attr(pattern);
219         }
220         if (ofs > 0) {
221                 html(delim);
222                 html("ofs=");
223                 htmlf("%d", ofs);
224         }
225         html("'>");
226         html_txt(name);
227         html("</a>");
228 }
229
230 void cgit_commit_link(char *name, char *title, char *class, char *head,
231                       char *rev)
232 {
233         if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
234                 name[ctx.cfg.max_msg_len] = '\0';
235                 name[ctx.cfg.max_msg_len - 1] = '.';
236                 name[ctx.cfg.max_msg_len - 2] = '.';
237                 name[ctx.cfg.max_msg_len - 3] = '.';
238         }
239         reporevlink("commit", name, title, class, head, rev, NULL);
240 }
241
242 void cgit_refs_link(char *name, char *title, char *class, char *head,
243                     char *rev, char *path)
244 {
245         reporevlink("refs", name, title, class, head, rev, path);
246 }
247
248 void cgit_snapshot_link(char *name, char *title, char *class, char *head,
249                         char *rev, char *archivename)
250 {
251         reporevlink("snapshot", name, title, class, head, rev, archivename);
252 }
253
254 void cgit_diff_link(char *name, char *title, char *class, char *head,
255                     char *new_rev, char *old_rev, char *path)
256 {
257         char *delim;
258
259         delim = repolink(title, class, "diff", head, path);
260         if (new_rev && strcmp(new_rev, ctx.qry.head)) {
261                 html(delim);
262                 html("id=");
263                 html_attr(new_rev);
264                 delim = "&amp;";
265         }
266         if (old_rev) {
267                 html(delim);
268                 html("id2=");
269                 html_attr(old_rev);
270         }
271         html("'>");
272         html_txt(name);
273         html("</a>");
274 }
275
276 void cgit_patch_link(char *name, char *title, char *class, char *head,
277                      char *rev)
278 {
279         reporevlink("patch", name, title, class, head, rev, NULL);
280 }
281
282 void cgit_object_link(struct object *obj)
283 {
284         char *page, *arg, *url;
285
286         if (obj->type == OBJ_COMMIT) {
287                 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL,
288                                  ctx.qry.head, sha1_to_hex(obj->sha1));
289                 return;
290         } else if (obj->type == OBJ_TREE) {
291                 page = "tree";
292                 arg = "id";
293         } else if (obj->type == OBJ_TAG) {
294                 page = "tag";
295                 arg = "id";
296         } else {
297                 page = "blob";
298                 arg = "id";
299         }
300
301         url = cgit_pageurl(ctx.qry.repo, page,
302                            fmt("%s=%s", arg, sha1_to_hex(obj->sha1)));
303         html_link_open(url, NULL, NULL);
304         htmlf("%s %s", typename(obj->type),
305               sha1_to_hex(obj->sha1));
306         html_link_close();
307 }
308
309 void cgit_print_date(time_t secs, char *format)
310 {
311         char buf[64];
312         struct tm *time;
313
314         if (!secs)
315                 return;
316         time = gmtime(&secs);
317         strftime(buf, sizeof(buf)-1, format, time);
318         html_txt(buf);
319 }
320
321 void cgit_print_age(time_t t, time_t max_relative, char *format)
322 {
323         time_t now, secs;
324
325         if (!t)
326                 return;
327         time(&now);
328         secs = now - t;
329
330         if (secs > max_relative && max_relative >= 0) {
331                 cgit_print_date(t, format);
332                 return;
333         }
334
335         if (secs < TM_HOUR * 2) {
336                 htmlf("<span class='age-mins'>%.0f min.</span>",
337                       secs * 1.0 / TM_MIN);
338                 return;
339         }
340         if (secs < TM_DAY * 2) {
341                 htmlf("<span class='age-hours'>%.0f hours</span>",
342                       secs * 1.0 / TM_HOUR);
343                 return;
344         }
345         if (secs < TM_WEEK * 2) {
346                 htmlf("<span class='age-days'>%.0f days</span>",
347                       secs * 1.0 / TM_DAY);
348                 return;
349         }
350         if (secs < TM_MONTH * 2) {
351                 htmlf("<span class='age-weeks'>%.0f weeks</span>",
352                       secs * 1.0 / TM_WEEK);
353                 return;
354         }
355         if (secs < TM_YEAR * 2) {
356                 htmlf("<span class='age-months'>%.0f months</span>",
357                       secs * 1.0 / TM_MONTH);
358                 return;
359         }
360         htmlf("<span class='age-years'>%.0f years</span>",
361               secs * 1.0 / TM_YEAR);
362 }
363
364 void cgit_print_docstart(char *title, struct cacheitem *item)
365 {
366         html("Content-Type: text/html; charset=" PAGE_ENCODING "\n");
367         htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime));
368         htmlf("Expires: %s\n", http_date(item->st.st_mtime +
369                                          ttl_seconds(item->ttl)));
370         html("\n");
371         html(cgit_doctype);
372         html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
373         html("<head>\n");
374         html("<title>");
375         html_txt(title);
376         html("</title>\n");
377         htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
378         if (ctx.cfg.robots && *ctx.cfg.robots)
379                 htmlf("<meta name='robots' content='%s'/>\n", ctx.cfg.robots);
380         html("<link rel='stylesheet' type='text/css' href='");
381         html_attr(ctx.cfg.css);
382         html("'/>\n");
383         html("</head>\n");
384         html("<body>\n");
385 }
386
387 void cgit_print_docend()
388 {
389         html("</td>\n</tr>\n</table>\n</body>\n</html>\n");
390 }
391
392 int print_branch_option(const char *refname, const unsigned char *sha1,
393                         int flags, void *cb_data)
394 {
395         char *name = (char *)refname;
396         html_option(name, name, ctx.qry.head);
397         return 0;
398 }
399
400 int print_archive_ref(const char *refname, const unsigned char *sha1,
401                        int flags, void *cb_data)
402 {
403         struct tag *tag;
404         struct taginfo *info;
405         struct object *obj;
406         char buf[256], *url;
407         unsigned char fileid[20];
408         int *header = (int *)cb_data;
409
410         if (prefixcmp(refname, "refs/archives"))
411                 return 0;
412         strncpy(buf, refname+14, sizeof(buf));
413         obj = parse_object(sha1);
414         if (!obj)
415                 return 1;
416         if (obj->type == OBJ_TAG) {
417                 tag = lookup_tag(sha1);
418                 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag)))
419                         return 0;
420                 hashcpy(fileid, tag->tagged->sha1);
421         } else if (obj->type != OBJ_BLOB) {
422                 return 0;
423         } else {
424                 hashcpy(fileid, sha1);
425         }
426         if (!*header) {
427                 html("<h1>download</h1>\n");
428                 *header = 1;
429         }
430         url = cgit_pageurl(ctx.qry.repo, "blob",
431                            fmt("id=%s&amp;path=%s", sha1_to_hex(fileid),
432                                buf));
433         html_link_open(url, NULL, "menu");
434         html_txt(strlpart(buf, 20));
435         html_link_close();
436         return 0;
437 }
438
439 void add_hidden_formfields(int incl_head, int incl_search, char *page)
440 {
441         char *url;
442
443         if (!ctx.cfg.virtual_root) {
444                 url = fmt("%s/%s", ctx.qry.repo, page);
445                 if (ctx.qry.path)
446                         url = fmt("%s/%s", url, ctx.qry.path);
447                 html_hidden("url", url);
448         }
449
450         if (incl_head && strcmp(ctx.qry.head, ctx.repo->defbranch))
451                 html_hidden("h", ctx.qry.head);
452
453         if (ctx.qry.sha1)
454                 html_hidden("id", ctx.qry.sha1);
455         if (ctx.qry.sha2)
456                 html_hidden("id2", ctx.qry.sha2);
457
458         if (incl_search) {
459                 if (ctx.qry.grep)
460                         html_hidden("qt", ctx.qry.grep);
461                 if (ctx.qry.search)
462                         html_hidden("q", ctx.qry.search);
463         }
464 }
465
466 void cgit_print_pageheader(char *title, int show_search)
467 {
468         static const char *default_info = "This is cgit, a fast webinterface for git repositories";
469         int header = 0;
470         char *url;
471
472         html("<table id='layout' summary=''>\n");
473         html("<tr><td id='sidebar'>\n");
474         html("<table class='sidebar' cellspacing='0' summary=''>\n");
475         html("<tr><td class='sidebar'>\n<a href='");
476         html_attr(cgit_rooturl());
477         htmlf("'><img src='%s' alt='cgit'/></a>\n",
478               ctx.cfg.logo);
479         html("</td></tr>\n<tr><td class='sidebar'>\n");
480         if (ctx.repo) {
481                 html("<h1 class='first'>");
482                 html_txt(strrpart(ctx.repo->name, 20));
483                 html("</h1>\n");
484                 html_txt(ctx.repo->desc);
485                 if (ctx.repo->owner) {
486                         html("<h1>owner</h1>\n");
487                         html_txt(ctx.repo->owner);
488                 }
489                 html("<h1>navigate</h1>\n");
490                 reporevlink(NULL, "summary", NULL, "menu", ctx.qry.head,
491                             NULL, NULL);
492                 cgit_log_link("log", NULL, "menu", ctx.qry.head, NULL, NULL,
493                               0, NULL, NULL);
494                 cgit_tree_link("tree", NULL, "menu", ctx.qry.head,
495                                ctx.qry.sha1, NULL);
496                 cgit_commit_link("commit", NULL, "menu", ctx.qry.head,
497                               ctx.qry.sha1);
498                 cgit_diff_link("diff", NULL, "menu", ctx.qry.head,
499                                ctx.qry.sha1, ctx.qry.sha2, NULL);
500                 cgit_patch_link("patch", NULL, "menu", ctx.qry.head,
501                                 ctx.qry.sha1);
502
503                 for_each_ref(print_archive_ref, &header);
504
505                 if (ctx.repo->clone_url || ctx.cfg.clone_prefix) {
506                         html("<h1>clone</h1>\n");
507                         if (ctx.repo->clone_url)
508                                 url = ctx.repo->clone_url;
509                         else
510                                 url = fmt("%s%s", ctx.cfg.clone_prefix,
511                                           ctx.repo->url);
512                         html("<a class='menu' href='");
513                         html_attr(url);
514                         html("' title='");
515                         html_attr(url);
516                         html("'>\n");
517                         html_txt(strrpart(url, 20));
518                         html("</a>\n");
519                 }
520
521                 html("<h1>branch</h1>\n");
522                 html("<form method='get' action=''>\n");
523                 add_hidden_formfields(0, 1, ctx.qry.page);
524 //              html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>");
525                 html("<select name='h' onchange='this.form.submit();'>\n");
526                 for_each_branch_ref(print_branch_option, ctx.qry.head);
527                 html("</select>\n");
528 //              html("</td><td>");
529                 html("<noscript><input type='submit' id='switch-btn' value='switch'/></noscript>\n");
530 //              html("</td></tr></table>");
531                 html("</form>\n");
532
533                 html("<h1>search</h1>\n");
534                 html("<form method='get' action='");
535                 if (ctx.cfg.virtual_root)
536                         html_attr(cgit_fileurl(ctx.qry.repo, "log",
537                                                ctx.qry.path, NULL));
538                 html("'>\n");
539                 add_hidden_formfields(1, 0, "log");
540                 html("<select name='qt'>\n");
541                 html_option("grep", "log msg", ctx.qry.grep);
542                 html_option("author", "author", ctx.qry.grep);
543                 html_option("committer", "committer", ctx.qry.grep);
544                 html("</select>\n");
545                 html("<input class='txt' type='text' name='q' value='");
546                 html_attr(ctx.qry.search);
547                 html("'/>\n");
548                 html("</form>\n");
549         } else {
550                 if (!ctx.cfg.index_info || html_include(ctx.cfg.index_info))
551                         html(default_info);
552         }
553
554         html("</td></tr></table></td>\n");
555
556         html("<td id='content'>\n");
557 }
558
559
560 void cgit_print_snapshot_start(const char *mimetype, const char *filename,
561                                struct cacheitem *item)
562 {
563         htmlf("Content-Type: %s\n", mimetype);
564         htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename);
565         htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime));
566         htmlf("Expires: %s\n", http_date(item->st.st_mtime +
567                                          ttl_seconds(item->ttl)));
568         html("\n");
569 }
570
571 void cgit_print_filemode(unsigned short mode)
572 {
573         if (S_ISDIR(mode))
574                 html("d");
575         else if (S_ISLNK(mode))
576                 html("l");
577         else if (S_ISGITLINK(mode))
578                 html("m");
579         else
580                 html("-");
581         html_fileperm(mode >> 6);
582         html_fileperm(mode >> 3);
583         html_fileperm(mode);
584 }
585
586 /* vim:set sw=8: */