all repos — cgit @ c76a52be5b5af940b8bcc9b3f554f4d44367506a

a hyperfast web frontend for git written in c

html.c (view raw)

  1#include "cgit.h"
  2
  3char *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
 21void html(const char *txt)
 22{
 23	fputs(txt, stdout);
 24}
 25
 26void 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
 35void 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("&gt;");
 46			else if (c=='<')
 47				html("&lt;");
 48			else if (c=='&')
 49				html("&amp;");
 50			txt = t+1;
 51		}
 52		t++;
 53	}
 54	if (t!=txt)
 55		html(txt);
 56}
 57
 58
 59void 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("&gt;");
 70			else if (c=='<')
 71				html("&lt;");
 72			else if (c=='\'')
 73				html("&quote;");
 74			txt = t+1;
 75		}
 76		t++;
 77	}
 78	if (t!=txt)
 79		html(txt);
 80}
 81
 82void 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
 97void html_link_close(void)
 98{
 99	html("</a>");
100}