]> gitweb.ps.run Git - ps-cgit/blob - ui-shared.c
Add ui-tag.c
[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
11 const char cgit_doctype[] =
12 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
13 "  \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
14
15 static char *http_date(time_t t)
16 {
17         static char day[][4] =
18                 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
19         static char month[][4] =
20                 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
21                  "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
22         struct tm *tm = gmtime(&t);
23         return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
24                    tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
25                    tm->tm_hour, tm->tm_min, tm->tm_sec);
26 }
27
28 static long ttl_seconds(long ttl)
29 {
30         if (ttl<0)
31                 return 60 * 60 * 24 * 365;
32         else
33                 return ttl * 60;
34 }
35
36 void cgit_print_error(char *msg)
37 {
38         html("<div class='error'>");
39         html_txt(msg);
40         html("</div>\n");
41 }
42
43 char *cgit_rooturl()
44 {
45         if (cgit_virtual_root)
46                 return fmt("%s/", cgit_virtual_root);
47         else
48                 return cgit_script_name;
49 }
50
51 char *cgit_repourl(const char *reponame)
52 {
53         if (cgit_virtual_root) {
54                 return fmt("%s/%s/", cgit_virtual_root, reponame);
55         } else {
56                 return fmt("?r=%s", reponame);
57         }
58 }
59
60 char *cgit_pageurl(const char *reponame, const char *pagename,
61                    const char *query)
62 {
63         if (cgit_virtual_root) {
64                 if (query)
65                         return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame,
66                                    pagename, query);
67                 else
68                         return fmt("%s/%s/%s/", cgit_virtual_root, reponame,
69                                    pagename);
70         } else {
71                 if (query)
72                         return fmt("?r=%s&amp;p=%s&amp;%s", reponame, pagename, query);
73                 else
74                         return fmt("?r=%s&amp;p=%s", reponame, pagename);
75         }
76 }
77
78 char *cgit_currurl()
79 {
80         if (!cgit_virtual_root)
81                 return cgit_script_name;
82         else if (cgit_query_page)
83                 return fmt("%s/%s/%s/", cgit_virtual_root, cgit_query_repo, cgit_query_page);
84         else if (cgit_query_repo)
85                 return fmt("%s/%s/", cgit_virtual_root, cgit_query_repo);
86         else
87                 return fmt("%s/", cgit_virtual_root);
88 }
89
90 static char *repolink(char *title, char *class, char *page, char *head,
91                       char *path)
92 {
93         char *delim = "?";
94
95         html("<a");
96         if (title) {
97                 html(" title='");
98                 html_attr(title);
99                 html("'");
100         }
101         if (class) {
102                 html(" class='");
103                 html_attr(class);
104                 html("'");
105         }
106         html(" href='");
107         if (cgit_virtual_root) {
108                 html_attr(cgit_virtual_root);
109                 if (cgit_virtual_root[strlen(cgit_virtual_root) - 1] != '/')
110                         html("/");
111                 html_attr(cgit_repo->url);
112                 if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/')
113                         html("/");
114                 if (page) {
115                         html(page);
116                         html("/");
117                         if (path)
118                                 html_attr(path);
119                 }
120         } else {
121                 html(cgit_script_name);
122                 html("?url=");
123                 html_attr(cgit_repo->url);
124                 if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/')
125                         html("/");
126                 if (page) {
127                         html(page);
128                         html("/");
129                         if (path)
130                                 html_attr(path);
131                 }
132                 delim = "&amp;";
133         }
134         if (head && strcmp(head, cgit_repo->defbranch)) {
135                 html(delim);
136                 html("h=");
137                 html_attr(head);
138                 delim = "&amp;";
139         }
140         return fmt("%s", delim);
141 }
142
143 static void reporevlink(char *page, char *name, char *title, char *class,
144                         char *head, char *rev, char *path)
145 {
146         char *delim;
147
148         delim = repolink(title, class, page, head, path);
149         if (rev && strcmp(rev, cgit_query_head)) {
150                 html(delim);
151                 html("id=");
152                 html_attr(rev);
153         }
154         html("'>");
155         html_txt(name);
156         html("</a>");
157 }
158
159 void cgit_tree_link(char *name, char *title, char *class, char *head,
160                     char *rev, char *path)
161 {
162         reporevlink("tree", name, title, class, head, rev, path);
163 }
164
165 void cgit_log_link(char *name, char *title, char *class, char *head,
166                    char *rev, char *path, int ofs)
167 {
168         char *delim;
169
170         delim = repolink(title, class, "log", head, path);
171         if (rev && strcmp(rev, cgit_query_head)) {
172                 html(delim);
173                 html("id=");
174                 html_attr(rev);
175                 delim = "&";
176         }
177         if (ofs > 0) {
178                 html(delim);
179                 html("ofs=");
180                 htmlf("%d", ofs);
181         }
182         html("'>");
183         html_txt(name);
184         html("</a>");
185 }
186
187 void cgit_commit_link(char *name, char *title, char *class, char *head,
188                       char *rev)
189 {
190         if (strlen(name) > cgit_max_msg_len && cgit_max_msg_len >= 15) {
191                 name[cgit_max_msg_len] = '\0';
192                 name[cgit_max_msg_len - 1] = '.';
193                 name[cgit_max_msg_len - 2] = '.';
194                 name[cgit_max_msg_len - 3] = '.';
195         }
196         reporevlink("commit", name, title, class, head, rev, NULL);
197 }
198
199 void cgit_diff_link(char *name, char *title, char *class, char *head,
200                     char *new_rev, char *old_rev, char *path)
201 {
202         char *delim;
203
204         delim = repolink(title, class, "diff", head, path);
205         if (new_rev && strcmp(new_rev, cgit_query_head)) {
206                 html(delim);
207                 html("id=");
208                 html_attr(new_rev);
209                 delim = "&amp;";
210         }
211         if (old_rev) {
212                 html(delim);
213                 html("id2=");
214                 html_attr(old_rev);
215         }
216         html("'>");
217         html_txt(name);
218         html("</a>");
219 }
220
221 void cgit_object_link(struct object *obj)
222 {
223         char *page, *arg, *url;
224
225         if (obj->type == OBJ_COMMIT) {
226                 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL,
227                                  cgit_query_head, sha1_to_hex(obj->sha1));
228                 return;
229         } else if (obj->type == OBJ_TREE) {
230                 page = "tree";
231                 arg = "id";
232         } else {
233                 page = "blob";
234                 arg = "id";
235         }
236
237         url = cgit_pageurl(cgit_query_repo, page,
238                            fmt("%s=%s", arg, sha1_to_hex(obj->sha1)));
239         html_link_open(url, NULL, NULL);
240         htmlf("%s %s", typename(obj->type),
241               sha1_to_hex(obj->sha1));
242         html_link_close();
243 }
244
245 void cgit_print_date(time_t secs, char *format)
246 {
247         char buf[64];
248         struct tm *time;
249
250         time = gmtime(&secs);
251         strftime(buf, sizeof(buf)-1, format, time);
252         html_txt(buf);
253 }
254
255 void cgit_print_age(time_t t, time_t max_relative, char *format)
256 {
257         time_t now, secs;
258
259         time(&now);
260         secs = now - t;
261
262         if (secs > max_relative && max_relative >= 0) {
263                 cgit_print_date(t, format);
264                 return;
265         }
266
267         if (secs < TM_HOUR * 2) {
268                 htmlf("<span class='age-mins'>%.0f min.</span>",
269                       secs * 1.0 / TM_MIN);
270                 return;
271         }
272         if (secs < TM_DAY * 2) {
273                 htmlf("<span class='age-hours'>%.0f hours</span>",
274                       secs * 1.0 / TM_HOUR);
275                 return;
276         }
277         if (secs < TM_WEEK * 2) {
278                 htmlf("<span class='age-days'>%.0f days</span>",
279                       secs * 1.0 / TM_DAY);
280                 return;
281         }
282         if (secs < TM_MONTH * 2) {
283                 htmlf("<span class='age-weeks'>%.0f weeks</span>",
284                       secs * 1.0 / TM_WEEK);
285                 return;
286         }
287         if (secs < TM_YEAR * 2) {
288                 htmlf("<span class='age-months'>%.0f months</span>",
289                       secs * 1.0 / TM_MONTH);
290                 return;
291         }
292         htmlf("<span class='age-years'>%.0f years</span>",
293               secs * 1.0 / TM_YEAR);
294 }
295
296 void cgit_print_docstart(char *title, struct cacheitem *item)
297 {
298         html("Content-Type: text/html; charset=utf-8\n");
299         htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime));
300         htmlf("Expires: %s\n", http_date(item->st.st_mtime +
301                                          ttl_seconds(item->ttl)));
302         html("\n");
303         html(cgit_doctype);
304         html("<html>\n");
305         html("<head>\n");
306         html("<title>");
307         html_txt(title);
308         html("</title>\n");
309         htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
310         html("<link rel='stylesheet' type='text/css' href='");
311         html_attr(cgit_css);
312         html("'/>\n");
313         html("</head>\n");
314         html("<body>\n");
315 }
316
317 void cgit_print_docend()
318 {
319         html("</td></tr></table>");
320         html("</body>\n</html>\n");
321 }
322
323 void cgit_print_pageheader(char *title, int show_search)
324 {
325         html("<table id='layout'>");
326         html("<tr><td id='header'><a href='");
327         html_attr(cgit_rooturl());
328         html("'>");
329         html_txt(cgit_root_title);
330         html("</a></td><td id='logo'>");
331         html("<a href='");
332         html_attr(cgit_logo_link);
333         htmlf("'><img src='%s' alt='logo'/></a>", cgit_logo);
334         html("</td></tr>");
335         html("<tr><td id='crumb'>");
336         if (cgit_query_repo) {
337                 html_txt(cgit_repo->name);
338                 html(" (");
339                 html_txt(cgit_query_head);
340                 html(") : &nbsp;");
341                 reporevlink(NULL, "summary", NULL, NULL, cgit_query_head,
342                             NULL, NULL);
343                 html(" ");
344                 cgit_log_link("log", NULL, NULL, cgit_query_head,
345                               cgit_query_sha1, cgit_query_path, 0);
346                 html(" ");
347                 cgit_tree_link("tree", NULL, NULL, cgit_query_head,
348                                cgit_query_sha1, NULL);
349                 html(" ");
350                 cgit_commit_link("commit", NULL, NULL, cgit_query_head,
351                               cgit_query_sha1);
352                 html(" ");
353                 cgit_diff_link("diff", NULL, NULL, cgit_query_head,
354                                cgit_query_sha1, cgit_query_sha2,
355                                cgit_query_path);
356         } else {
357                 html_txt("Index of repositories");
358         }
359         html("</td>");
360         html("<td id='search'>");
361         if (show_search) {
362                 html("<form method='get' action='");
363                 html_attr(cgit_currurl());
364                 html("'>");
365                 if (!cgit_virtual_root) {
366                         if (cgit_query_repo)
367                                 html_hidden("r", cgit_query_repo);
368                         if (cgit_query_page)
369                                 html_hidden("p", cgit_query_page);
370                 }
371                 if (cgit_query_head)
372                         html_hidden("h", cgit_query_head);
373                 if (cgit_query_sha1)
374                         html_hidden("id", cgit_query_sha1);
375                 if (cgit_query_sha2)
376                         html_hidden("id2", cgit_query_sha2);
377                 html("<input type='text' name='q' value='");
378                 html_attr(cgit_query_search);
379                 html("'/></form>");
380         }
381         html("</td></tr>");
382         html("<tr><td id='content' colspan='2'>");
383 }
384
385 void cgit_print_snapshot_start(const char *mimetype, const char *filename,
386                                struct cacheitem *item)
387 {
388         htmlf("Content-Type: %s\n", mimetype);
389         htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename);
390         htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime));
391         htmlf("Expires: %s\n", http_date(item->st.st_mtime +
392                                          ttl_seconds(item->ttl)));
393         html("\n");
394 }