]> git.cameronkatri.com Git - cgit.git/blob - ui-shared.c
added snapshot filename to the link
[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
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_fileurl(const char *reponame, const char *pagename,
61 const char *filename, const char *query)
62 {
63 if (cgit_virtual_root) {
64 if (query)
65 return fmt("%s/%s/%s/%s?%s", cgit_virtual_root, reponame,
66 pagename, filename?filename:"", 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_pageurl(const char *reponame, const char *pagename,
79 const char *query)
80 {
81 return cgit_fileurl(reponame,pagename,0,query);
82 }
83
84
85 char *cgit_currurl()
86 {
87 if (!cgit_virtual_root)
88 return cgit_script_name;
89 else if (cgit_query_page)
90 return fmt("%s/%s/%s/", cgit_virtual_root, cgit_query_repo, cgit_query_page);
91 else if (cgit_query_repo)
92 return fmt("%s/%s/", cgit_virtual_root, cgit_query_repo);
93 else
94 return fmt("%s/", cgit_virtual_root);
95 }
96
97 static char *repolink(char *title, char *class, char *page, char *head,
98 char *path)
99 {
100 char *delim = "?";
101
102 html("<a");
103 if (title) {
104 html(" title='");
105 html_attr(title);
106 html("'");
107 }
108 if (class) {
109 html(" class='");
110 html_attr(class);
111 html("'");
112 }
113 html(" href='");
114 if (cgit_virtual_root) {
115 html_attr(cgit_virtual_root);
116 if (cgit_virtual_root[strlen(cgit_virtual_root) - 1] != '/')
117 html("/");
118 html_attr(cgit_repo->url);
119 if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/')
120 html("/");
121 if (page) {
122 html(page);
123 html("/");
124 if (path)
125 html_attr(path);
126 }
127 } else {
128 html(cgit_script_name);
129 html("?url=");
130 html_attr(cgit_repo->url);
131 if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/')
132 html("/");
133 if (page) {
134 html(page);
135 html("/");
136 if (path)
137 html_attr(path);
138 }
139 delim = "&amp;";
140 }
141 if (head && strcmp(head, cgit_repo->defbranch)) {
142 html(delim);
143 html("h=");
144 html_attr(head);
145 delim = "&amp;";
146 }
147 return fmt("%s", delim);
148 }
149
150 static void reporevlink(char *page, char *name, char *title, char *class,
151 char *head, char *rev, char *path)
152 {
153 char *delim;
154
155 delim = repolink(title, class, page, head, path);
156 if (rev && strcmp(rev, cgit_query_head)) {
157 html(delim);
158 html("id=");
159 html_attr(rev);
160 }
161 html("'>");
162 html_txt(name);
163 html("</a>");
164 }
165
166 void cgit_tree_link(char *name, char *title, char *class, char *head,
167 char *rev, char *path)
168 {
169 reporevlink("tree", name, title, class, head, rev, path);
170 }
171
172 void cgit_log_link(char *name, char *title, char *class, char *head,
173 char *rev, char *path, int ofs)
174 {
175 char *delim;
176
177 delim = repolink(title, class, "log", head, path);
178 if (rev && strcmp(rev, cgit_query_head)) {
179 html(delim);
180 html("id=");
181 html_attr(rev);
182 delim = "&";
183 }
184 if (ofs > 0) {
185 html(delim);
186 html("ofs=");
187 htmlf("%d", ofs);
188 }
189 html("'>");
190 html_txt(name);
191 html("</a>");
192 }
193
194 void cgit_commit_link(char *name, char *title, char *class, char *head,
195 char *rev)
196 {
197 if (strlen(name) > cgit_max_msg_len && cgit_max_msg_len >= 15) {
198 name[cgit_max_msg_len] = '\0';
199 name[cgit_max_msg_len - 1] = '.';
200 name[cgit_max_msg_len - 2] = '.';
201 name[cgit_max_msg_len - 3] = '.';
202 }
203 reporevlink("commit", name, title, class, head, rev, NULL);
204 }
205
206 void cgit_diff_link(char *name, char *title, char *class, char *head,
207 char *new_rev, char *old_rev, char *path)
208 {
209 char *delim;
210
211 delim = repolink(title, class, "diff", head, path);
212 if (new_rev && strcmp(new_rev, cgit_query_head)) {
213 html(delim);
214 html("id=");
215 html_attr(new_rev);
216 delim = "&amp;";
217 }
218 if (old_rev) {
219 html(delim);
220 html("id2=");
221 html_attr(old_rev);
222 }
223 html("'>");
224 html_txt(name);
225 html("</a>");
226 }
227
228 void cgit_print_date(time_t secs, char *format)
229 {
230 char buf[64];
231 struct tm *time;
232
233 time = gmtime(&secs);
234 strftime(buf, sizeof(buf)-1, format, time);
235 html_txt(buf);
236 }
237
238 void cgit_print_age(time_t t, time_t max_relative, char *format)
239 {
240 time_t now, secs;
241
242 time(&now);
243 secs = now - t;
244
245 if (secs > max_relative && max_relative >= 0) {
246 cgit_print_date(t, format);
247 return;
248 }
249
250 if (secs < TM_HOUR * 2) {
251 htmlf("<span class='age-mins'>%.0f min.</span>",
252 secs * 1.0 / TM_MIN);
253 return;
254 }
255 if (secs < TM_DAY * 2) {
256 htmlf("<span class='age-hours'>%.0f hours</span>",
257 secs * 1.0 / TM_HOUR);
258 return;
259 }
260 if (secs < TM_WEEK * 2) {
261 htmlf("<span class='age-days'>%.0f days</span>",
262 secs * 1.0 / TM_DAY);
263 return;
264 }
265 if (secs < TM_MONTH * 2) {
266 htmlf("<span class='age-weeks'>%.0f weeks</span>",
267 secs * 1.0 / TM_WEEK);
268 return;
269 }
270 if (secs < TM_YEAR * 2) {
271 htmlf("<span class='age-months'>%.0f months</span>",
272 secs * 1.0 / TM_MONTH);
273 return;
274 }
275 htmlf("<span class='age-years'>%.0f years</span>",
276 secs * 1.0 / TM_YEAR);
277 }
278
279 void cgit_print_docstart(char *title, struct cacheitem *item)
280 {
281 html("Content-Type: text/html; charset=utf-8\n");
282 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime));
283 htmlf("Expires: %s\n", http_date(item->st.st_mtime +
284 ttl_seconds(item->ttl)));
285 html("\n");
286 html(cgit_doctype);
287 html("<html>\n");
288 html("<head>\n");
289 html("<title>");
290 html_txt(title);
291 html("</title>\n");
292 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
293 html("<link rel='stylesheet' type='text/css' href='");
294 html_attr(cgit_css);
295 html("'/>\n");
296 html("</head>\n");
297 html("<body>\n");
298 }
299
300 void cgit_print_docend()
301 {
302 html("</td></tr></table>");
303 html("</body>\n</html>\n");
304 }
305
306 void cgit_print_pageheader(char *title, int show_search)
307 {
308 html("<table id='layout'>");
309 html("<tr><td id='header'><a href='");
310 html_attr(cgit_rooturl());
311 html("'>");
312 html_txt(cgit_root_title);
313 html("</a></td><td id='logo'>");
314 html("<a href='");
315 html_attr(cgit_logo_link);
316 htmlf("'><img src='%s' alt='logo'/></a>", cgit_logo);
317 html("</td></tr>");
318 html("<tr><td id='crumb'>");
319 if (cgit_query_repo) {
320 html_txt(cgit_repo->name);
321 html(" (");
322 html_txt(cgit_query_head);
323 html(") : &nbsp;");
324 reporevlink(NULL, "summary", NULL, NULL, cgit_query_head,
325 NULL, NULL);
326 html(" ");
327 cgit_log_link("log", NULL, NULL, cgit_query_head,
328 cgit_query_sha1, cgit_query_path, 0);
329 html(" ");
330 cgit_tree_link("tree", NULL, NULL, cgit_query_head,
331 cgit_query_sha1, NULL);
332 html(" ");
333 cgit_commit_link("commit", NULL, NULL, cgit_query_head,
334 cgit_query_sha1);
335 html(" ");
336 cgit_diff_link("diff", NULL, NULL, cgit_query_head,
337 cgit_query_sha1, cgit_query_sha2,
338 cgit_query_path);
339 } else {
340 html_txt("Index of repositories");
341 }
342 html("</td>");
343 html("<td id='search'>");
344 if (show_search) {
345 html("<form method='get' action='");
346 html_attr(cgit_currurl());
347 html("'>");
348 if (!cgit_virtual_root) {
349 if (cgit_query_repo)
350 html_hidden("r", cgit_query_repo);
351 if (cgit_query_page)
352 html_hidden("p", cgit_query_page);
353 }
354 if (cgit_query_head)
355 html_hidden("h", cgit_query_head);
356 if (cgit_query_sha1)
357 html_hidden("id", cgit_query_sha1);
358 if (cgit_query_sha2)
359 html_hidden("id2", cgit_query_sha2);
360 html("<input type='text' name='q' value='");
361 html_attr(cgit_query_search);
362 html("'/></form>");
363 }
364 html("</td></tr>");
365 html("<tr><td id='content' colspan='2'>");
366 }
367
368 void cgit_print_snapshot_start(const char *mimetype, const char *filename,
369 struct cacheitem *item)
370 {
371 htmlf("Content-Type: %s\n", mimetype);
372 htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename);
373 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime));
374 htmlf("Expires: %s\n", http_date(item->st.st_mtime +
375 ttl_seconds(item->ttl)));
376 html("\n");
377 }
378
379 /* vim:set sw=8: */