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
75
76
|
#include <stdio.h>
#include <string.h>
#define BUFSIZE 4096
#define xmlput(n)\
for(pos = n; buf[pos] != '\n'; pos++) {\
switch(buf[pos]) {\
default: putchar(buf[pos]); break;\
case '>' : fputs(">",stdout); break;\
case '<' : fputs("<",stdout); break;\
case '&' : fputs("&",stdout); break;\
case '\'': fputs("'",stdout); break;\
case '"' : fputs(""",stdout); break;\
}\
}
#define html(s) fputs(s,stdout)
#define getbuf() fgets(buf,BUFSIZE,stdin)
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>"); continue;
} else if (!strncmp(buf,"##",2)) {
html("<h2>"); xmlput(3); html("</h3>"); continue;
} else if (buf[0] == '#') {
html("<h1>"); xmlput(2); html("</h1>"); continue;
}
else if (buf[0] == '>') {
fputs("<blockquote>",stdout); xmlput(2);
fputs("</blockquote>\n",stdout); continue;
}
else if (buf[0] == '*') {
if (list_state == 0) html("<ul>\n");
html("<li>"); xmlput(2); html("</li>\n");
list_state = 1;
continue;
} else if (list_state == 1) {
html("</ul>\n");
list_state = 0;
continue;
}
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[4];
while ((suffix[i++] = buf[pos++]) != ' ');
if (!strncmp(suffix,".gmi",4)) html(".html");
else html(suffix);
} html("\">"); xmlput(pos); html("</a></p>\n");
continue;
}
else if (!strncmp(buf,"```",3)) {
getbuf(); html("<pre><code>");
while (strncmp(buf,"```",3)) {
xmlput(0); putchar('\n'); getbuf();
} html("</code></pre>\n");
continue;
} html("<p>"); xmlput(0); html("</p>\n");
} html("</body>"); html("</html>");
}
|