aboutsummaryrefslogtreecommitdiff
path: root/g2x.c
blob: 33e2d447a7c2eec9a017b6bcf961c47b712ffea0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <string.h>

#define BUFSIZE 4096
#define html(s) fputs(s,stdout)
#define getbuf() fgets(buf,BUFSIZE,stdin)
#define xmlput(n)\
	for(pos = n; buf[pos] != '\n'; pos++) {\
		switch(buf[pos]) {\
			default: putchar(buf[pos]); break;\
			case '>' : html("&gt;"); break;\
			case '<' : html("&lt;"); break;\
			case '&' : html("&amp;"); break;\
			case '\'': html("&apos;"); break;\
			case '"' : html("&quot;"); break;\
		}\
	}

int main(void)
{
	char buf[BUFSIZE];
	int pos, list_state = 0;

	html(
		"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
		"<!DOCTYPE html PUBLIC ""\"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
		"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n"
		"<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>\n"
		"<link rel=\"stylesheet\" type=\"text/css\" href=\"/style.css\"/>\n"
		"<title></title></head><body>\n"
	);

	while (getbuf() != NULL)
	{
		if (!strncmp(buf,"###",3)) {
			html("<h3>"); xmlput(4); html("</h3>");
		} else if (!strncmp(buf,"##",2)) {
			html("<h2>"); xmlput(3); html("</h2>");
		} else if (buf[0] == '#') {
			html("<h1>"); xmlput(2); html("</h1>");
		}
		else if (buf[0] == '>') {
			fputs("<p><blockquote>",stdout); xmlput(2);
			fputs("</blockquote></p>\n",stdout);
		}
		else if (buf[0] == '*') {
			if (list_state == 0) html("<ul>\n");
			html("<li>"); xmlput(2); html("</li>\n");
			list_state = 1;
		} else if (list_state == 1) {
			html("</ul>\n");
			list_state = 0;
		}
		else if (!strncmp(buf,"=>",2)) {
			html("<p><a href=\"");
			for (pos = 3; buf[pos] != '.' && buf[pos] != ' ';)
				putchar(buf[pos++]);
			if (buf[pos] == '.') {
				int i = 0; char suffix[12];
				while (buf[pos] != ' ')
					suffix[i++] = buf[pos++];
				suffix[i] = '\0';
				if (!strncmp(suffix,".gmi",4)) html(".html");
				else html(suffix);
			} html("\">"); xmlput(pos+1); html("</a></p>\n");
		}
		else if (!strncmp(buf,"```",3)) {
			getbuf(); html("<pre><code>");
			while (strncmp(buf,"```",3)) {
				xmlput(0); putchar('\n'); getbuf();
			} html("</code></pre>\n");
		} else { html("<p>"); xmlput(0); html("</p>\n"); }
	} html("</body>"); html("</html>");
}