#include
#include
#define BUFSIZE 4096
#define html(s) fputs(s,stdout)
#define xmlput(n)\
for(pos = n; buf[pos] != '\n'; pos++) {\
switch(buf[pos]) {\
default: putchar(buf[pos]); break;\
case '>' : html(">"); break;\
case '<' : html("<"); break;\
case '&' : html("&"); break;\
case '\'': html("'"); break;\
case '"' : html("""); break;\
}\
}
int main(void)
{
char buf[BUFSIZE];
int pos, list_state = 0;
html(
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
);
// Reading a line from stdin into buf
while (fgets(buf,BUFSIZE,stdin) != NULL)
{
if (!strncmp(buf,"# ",2))
{// Match # for first header
html("
");
xmlput(2);
html("
\n");
}
else if (!strncmp(buf,"## ",3))
{// Match ## for second header
html("
");
xmlput(3);
html("
\n");
}
else if (!strncmp(buf,"### ",4))
{// Match ### for third header
html("
");
xmlput(4);
html("
\n");
}
else if (!strncmp(buf,"> ",2))
{// Match "> " for blockquote
html("
");
xmlput(2);
html("
\n");
}
else if (!strncmp(buf,"* ",2))
{// Match * for lists
if (list_state == 0) html("
\n");
html("
"); xmlput(2); html("
\n");
list_state = 1; // in a list
}
else if (list_state == 1)
{// In a set of lists
html("
\n");
list_state = 0; // out of a list
}
// FIXME "out of list" skips the reading of next line.
else if (!strncmp(buf,"=>",2)) // Match links
{// Match => for hyperlinks
html("
\n");
}
else if (!strncmp(buf,"```",3))
{// Match ``` codeblocks, ignore the block-name
// TODO don't ignore the block-name
fgets(buf,BUFSIZE,stdin); html("
");
while (strncmp(buf,"```",3))
{
xmlput(0);
putchar('\n');
fgets(buf,BUFSIZE,stdin);
}
html("
\n");
}
else // Match paragraphs
{
html("
");
xmlput(0);
html("
\n");
}
} html(""); html("");
// I love the gemini protocol!
}