Subversion Repositories IRC-Archiver

Rev

Rev 12 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 nishi 1
/* $Id: html.c 11 2024-08-30 05:20:32Z nishi $ */
2
 
3
#include "web_html.h"
4
 
5
#include "ia_util.h"
6
 
7
#include <time.h>
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <string.h>
11
 
12
extern char* webroot;
13
 
14
char* web_html_escape(const char* html) {
15
	char* str = malloc(strlen(html) * 5 + 1);
16
	int i;
17
	int incr = 0;
18
	for(i = 0; html[i] != 0; i++) {
19
		if(html[i] == '&') {
20
			str[incr++] = '&';
21
			str[incr++] = 'a';
22
			str[incr++] = 'm';
23
			str[incr++] = 'p';
24
			str[incr++] = ';';
25
		} else if(html[i] == '<') {
26
			str[incr++] = '&';
27
			str[incr++] = 'l';
28
			str[incr++] = 't';
29
			str[incr++] = ';';
30
		} else if(html[i] == '>') {
31
			str[incr++] = '&';
32
			str[incr++] = 'g';
33
			str[incr++] = 't';
34
			str[incr++] = ';';
35
		} else {
36
			str[incr++] = html[i];
37
			;
38
		}
39
	}
40
	str[incr] = 0;
41
	return str;
42
}
43
 
44
int web_html_generate(const char* name, web_range_t range) {
45
	char* path = ia_strcat4(webroot, "/", name, ".html");
46
	FILE* f = fopen(path, "w");
47
	if(f != NULL) {
48
		char* htmlesc = web_html_escape(name);
49
		char* title = ia_strcat("Archive: ", htmlesc);
50
		fprintf(f, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n");
51
		fprintf(f, "<html>\n");
52
		fprintf(f, "	<head>\n");
53
		fprintf(f, "		<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">\n");
54
		fprintf(f, "		<title>%s</title>\n", title);
55
		fprintf(f, "	</head>\n");
56
		fprintf(f, "	<body>\n");
57
		fprintf(f, "	</body>\n");
58
		fprintf(f, "</html>\n");
59
		fclose(f);
60
		free(title);
61
		free(htmlesc);
62
	}
63
	free(path);
64
}