きったんの頭

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("&amp;");
        break;
      case '<':
        printf("&lt;");
        break;
      case '>':
        printf("&gt;");
        break;
      case '"':
        printf("&quot;");
        break;

      default:
        putchar(c);
        break;
    }
  }

loopend:
  puts("</p>");

  puts("</body>");
  puts("</html>");

  return 0;
}