]>
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)
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";
15 static char *http_date(time_t t
)
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
);
28 static long ttl_seconds(long ttl
)
31 return 60 * 60 * 24 * 365;
36 void cgit_print_error(char *msg
)
38 html("<div class='error'>");
45 if (cgit_virtual_root
)
46 return fmt("%s/", cgit_virtual_root
);
48 return cgit_script_name
;
51 char *cgit_repourl(const char *reponame
)
53 if (cgit_virtual_root
) {
54 return fmt("%s/%s/", cgit_virtual_root
, reponame
);
56 return fmt("?r=%s", reponame
);
60 char *cgit_fileurl(const char *reponame
, const char *pagename
,
61 const char *filename
, const char *query
)
63 if (cgit_virtual_root
) {
65 return fmt("%s/%s/%s/%s?%s", cgit_virtual_root
, reponame
,
66 pagename
, filename
?filename
:"", query
);
68 return fmt("%s/%s/%s/", cgit_virtual_root
, reponame
,
72 return fmt("?r=%s&p=%s&%s", reponame
, pagename
, query
);
74 return fmt("?r=%s&p=%s", reponame
, pagename
);
78 char *cgit_pageurl(const char *reponame
, const char *pagename
,
81 return cgit_fileurl(reponame
,pagename
,0,query
);
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
);
94 return fmt("%s/", cgit_virtual_root
);
97 static char *repolink(char *title
, char *class, char *page
, char *head
,
114 if (cgit_virtual_root
) {
115 html_attr(cgit_virtual_root
);
116 if (cgit_virtual_root
[strlen(cgit_virtual_root
) - 1] != '/')
118 html_attr(cgit_repo
->url
);
119 if (cgit_repo
->url
[strlen(cgit_repo
->url
) - 1] != '/')
128 html(cgit_script_name
);
130 html_attr(cgit_repo
->url
);
131 if (cgit_repo
->url
[strlen(cgit_repo
->url
) - 1] != '/')
141 if (head
&& strcmp(head
, cgit_repo
->defbranch
)) {
147 return fmt("%s", delim
);
150 static void reporevlink(char *page
, char *name
, char *title
, char *class,
151 char *head
, char *rev
, char *path
)
155 delim
= repolink(title
, class, page
, head
, path
);
156 if (rev
&& strcmp(rev
, cgit_query_head
)) {
166 void cgit_tree_link(char *name
, char *title
, char *class, char *head
,
167 char *rev
, char *path
)
169 reporevlink("tree", name
, title
, class, head
, rev
, path
);
172 void cgit_log_link(char *name
, char *title
, char *class, char *head
,
173 char *rev
, char *path
, int ofs
)
177 delim
= repolink(title
, class, "log", head
, path
);
178 if (rev
&& strcmp(rev
, cgit_query_head
)) {
194 void cgit_commit_link(char *name
, char *title
, char *class, char *head
,
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] = '.';
203 reporevlink("commit", name
, title
, class, head
, rev
, NULL
);
206 void cgit_diff_link(char *name
, char *title
, char *class, char *head
,
207 char *new_rev
, char *old_rev
, char *path
)
211 delim
= repolink(title
, class, "diff", head
, path
);
212 if (new_rev
&& strcmp(new_rev
, cgit_query_head
)) {
228 void cgit_print_date(time_t secs
, char *format
)
233 time
= gmtime(&secs
);
234 strftime(buf
, sizeof(buf
)-1, format
, time
);
238 void cgit_print_age(time_t t
, time_t max_relative
, char *format
)
245 if (secs
> max_relative
&& max_relative
>= 0) {
246 cgit_print_date(t
, format
);
250 if (secs
< TM_HOUR
* 2) {
251 htmlf("<span class='age-mins'>%.0f min.</span>",
252 secs
* 1.0 / TM_MIN
);
255 if (secs
< TM_DAY
* 2) {
256 htmlf("<span class='age-hours'>%.0f hours</span>",
257 secs
* 1.0 / TM_HOUR
);
260 if (secs
< TM_WEEK
* 2) {
261 htmlf("<span class='age-days'>%.0f days</span>",
262 secs
* 1.0 / TM_DAY
);
265 if (secs
< TM_MONTH
* 2) {
266 htmlf("<span class='age-weeks'>%.0f weeks</span>",
267 secs
* 1.0 / TM_WEEK
);
270 if (secs
< TM_YEAR
* 2) {
271 htmlf("<span class='age-months'>%.0f months</span>",
272 secs
* 1.0 / TM_MONTH
);
275 htmlf("<span class='age-years'>%.0f years</span>",
276 secs
* 1.0 / TM_YEAR
);
279 void cgit_print_docstart(char *title
, struct cacheitem
*item
)
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
)));
292 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version
);
293 html("<link rel='stylesheet' type='text/css' href='");
300 void cgit_print_docend()
302 html("</td></tr></table>");
303 html("</body>\n</html>\n");
306 void cgit_print_pageheader(char *title
, int show_search
)
308 html("<table id='layout'>");
309 html("<tr><td id='header'><a href='");
310 html_attr(cgit_rooturl());
312 html_txt(cgit_root_title
);
313 html("</a></td><td id='logo'>");
315 html_attr(cgit_logo_link
);
316 htmlf("'><img src='%s' alt='logo'/></a>", cgit_logo
);
318 html("<tr><td id='crumb'>");
319 if (cgit_query_repo
) {
320 html_txt(cgit_repo
->name
);
322 html_txt(cgit_query_head
);
324 reporevlink(NULL
, "summary", NULL
, NULL
, cgit_query_head
,
327 cgit_log_link("log", NULL
, NULL
, cgit_query_head
,
328 cgit_query_sha1
, cgit_query_path
, 0);
330 cgit_tree_link("tree", NULL
, NULL
, cgit_query_head
,
331 cgit_query_sha1
, NULL
);
333 cgit_commit_link("commit", NULL
, NULL
, cgit_query_head
,
336 cgit_diff_link("diff", NULL
, NULL
, cgit_query_head
,
337 cgit_query_sha1
, cgit_query_sha2
,
340 html_txt("Index of repositories");
343 html("<td id='search'>");
345 html("<form method='get' action='");
346 html_attr(cgit_currurl());
348 if (!cgit_virtual_root
) {
350 html_hidden("r", cgit_query_repo
);
352 html_hidden("p", cgit_query_page
);
355 html_hidden("h", cgit_query_head
);
357 html_hidden("id", cgit_query_sha1
);
359 html_hidden("id2", cgit_query_sha2
);
360 html("<input type='text' name='q' value='");
361 html_attr(cgit_query_search
);
365 html("<tr><td id='content' colspan='2'>");
368 void cgit_print_snapshot_start(const char *mimetype
, const char *filename
,
369 struct cacheitem
*item
)
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
)));