diff options
Diffstat (limited to 'html.c')
-rw-r--r-- | html.c | 100 |
1 files changed, 100 insertions, 0 deletions
@@ -0,0 +1,100 @@ | |||
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 | fputs(txt, stdout); | ||
24 | } | ||
25 | |||
26 | void htmlf(const char *format, ...) | ||
27 | { | ||
28 | va_list args; | ||
29 | |||
30 | va_start(args, format); | ||
31 | vprintf(format, args); | ||
32 | va_end(args); | ||
33 | } | ||
34 | |||
35 | void html_txt(char *txt) | ||
36 | { | ||
37 | char *t = txt; | ||
38 | while(*t){ | ||
39 | int c = *t; | ||
40 | if (c=='<' || c=='>' || c=='&') { | ||
41 | *t = '\0'; | ||
42 | html(txt); | ||
43 | *t = c; | ||
44 | if (c=='>') | ||
45 | html(">"); | ||
46 | else if (c=='<') | ||
47 | html("<"); | ||
48 | else if (c=='&') | ||
49 | html("&"); | ||
50 | txt = t+1; | ||
51 | } | ||
52 | t++; | ||
53 | } | ||
54 | if (t!=txt) | ||
55 | html(txt); | ||
56 | } | ||
57 | |||
58 | |||
59 | void html_attr(char *txt) | ||
60 | { | ||
61 | char *t = txt; | ||
62 | while(*t){ | ||
63 | int c = *t; | ||
64 | if (c=='<' || c=='>' || c=='\'') { | ||
65 | *t = '\0'; | ||
66 | html(txt); | ||
67 | *t = c; | ||
68 | if (c=='>') | ||
69 | html(">"); | ||
70 | else if (c=='<') | ||
71 | html("<"); | ||
72 | else if (c=='\'') | ||
73 | html(""e;"); | ||
74 | txt = t+1; | ||
75 | } | ||
76 | t++; | ||
77 | } | ||
78 | if (t!=txt) | ||
79 | html(txt); | ||
80 | } | ||
81 | |||
82 | void html_link_open(char *url, char *title, char *class) | ||
83 | { | ||
84 | html("<a href='"); | ||
85 | html_attr(url); | ||
86 | if (title) { | ||
87 | html("' title='"); | ||
88 | html_attr(title); | ||
89 | } | ||
90 | if (class) { | ||
91 | html("' class='"); | ||
92 | html_attr(class); | ||
93 | } | ||
94 | html("'>"); | ||
95 | } | ||
96 | |||
97 | void html_link_close(void) | ||
98 | { | ||
99 | html("</a>"); | ||
100 | } | ||