]> gitweb.ps.run Git - ps-cgit/blob - html.c
Add caching infrastructure
[ps-cgit] / html.c
1 #include "cgit.h"
2
3 char *fmt(const char *format, ...)
4 {
5         static char buf[8][1024];
6         static int bufidx;
7         int len;
8         va_list args;
9
10         bufidx++;
11         bufidx &= 7;
12
13         va_start(args, format);
14         len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args);
15         va_end(args);
16         if (len>sizeof(buf[bufidx]))
17                 die("[html.c] string truncated: %s", format);
18         return buf[bufidx];
19 }
20
21 void html(const char *txt)
22 {
23         write(htmlfd, txt, strlen(txt));
24 }
25
26 void htmlf(const char *format, ...)
27 {
28         static char buf[65536];
29         va_list args;
30
31         va_start(args, format);
32         vsnprintf(buf, sizeof(buf), format, args);
33         va_end(args);
34         html(buf);
35 }
36
37 void html_txt(char *txt)
38 {
39         char *t = txt;
40         while(*t){
41                 int c = *t;
42                 if (c=='<' || c=='>' || c=='&') {
43                         *t = '\0';
44                         html(txt);
45                         *t = c;
46                         if (c=='>')
47                                 html("&gt;");
48                         else if (c=='<')
49                                 html("&lt;");
50                         else if (c=='&')
51                                 html("&amp;");
52                         txt = t+1;
53                 }
54                 t++;
55         }
56         if (t!=txt)
57                 html(txt);
58 }
59
60
61 void html_attr(char *txt)
62 {
63         char *t = txt;
64         while(*t){
65                 int c = *t;
66                 if (c=='<' || c=='>' || c=='\'') {
67                         *t = '\0';
68                         html(txt);
69                         *t = c;
70                         if (c=='>')
71                                 html("&gt;");
72                         else if (c=='<')
73                                 html("&lt;");
74                         else if (c=='\'')
75                                 html("&quote;");
76                         txt = t+1;
77                 }
78                 t++;
79         }
80         if (t!=txt)
81                 html(txt);
82 }
83
84 void html_link_open(char *url, char *title, char *class)
85 {
86         html("<a href='");
87         html_attr(url);
88         if (title) {
89                 html("' title='");
90                 html_attr(title);
91         }
92         if (class) {
93                 html("' class='");
94                 html_attr(class);
95         }
96         html("'>");
97 }
98
99 void html_link_close(void)
100 {
101         html("</a>");
102 }