Text -> HTML | JavaScript |
/*
txt2htm.c
https://mind.kittttttan.info/c/txt2htm
Usage on commandline:
txt2htm < sample.txt > sample.html
*/
#include <stdio.h>
int main()
{
int c, n;
puts("<html>");
puts("<body>");
printf("<p>");
while ((c = getchar()) != EOF) {
switch (c) {
case '\n':
n = 1;
while ((c = getchar()) == '\n') {
++n;
}
if (c == EOF) { goto loopend; }
if (n > 1) {
printf("</p>\n\n<p>");
} else {
printf("<br>\n");
}
putchar(c);
break;
/* escape HTML */
case '&':
printf("&");
break;
case '<':
printf("<");
break;
case '>':
printf(">");
break;
case '"':
printf(""");
break;
default:
putchar(c);
break;
}
}
loopend:
puts("</p>");
puts("</body>");
puts("</html>");
return 0;
}