/***************************************************************
 * enriched2html v1.0
 *     Brandon Long (blong@uiuc.edu)
 *     http://hoohoo.ncsa.uiuc.edu/~blong/programs/mutt/#autoview
 *
 * This program is based on the program in Appendix A of rfc1896.
 * It does as much as I can think of to convert text/enriched to text/html
 * (since there are so many viewers for text/html).
 * It is a hack.  It is not perfect, and the html it generates is far
 * from perfect.  I've tried to follow HTML v3.2, but its still not great.
 *
 * Usage:
 *  Pass it the text/enriched body of a message on stdin, and it will 
 *  put the text/html version on stdout.
 */


#include <stdio.h>
#include <ctype.h>

int dofont(int size, char *font, char *color)
{
  printf("<FONT ");
  if (size > 0) printf("SIZE=+%d ",size);
  if (size < 0) printf("SIZE=%d ",size);
  if (color[0]) printf("COLOR=\"%s\" ",color);
  if (font[0]) printf("FACE=\"%s\" ");
  printf(">");
}

#define PT_LEFT  1
#define PT_RIGHT 2
#define PT_IN    3
#define PT_OUT   4

main() {
    int c, i, paramct=0, newlinect=0, nofill=0;
    char token[62], *p;
    int excerpt=0;
    char font[256] = "";
    char color[256] = "";
    int size = 0;
    int colorparam = 0;
    int fontparam = 0;
    int paraparam = 0;
    int paratype = 0;

    while ((c=getc(stdin)) != EOF) {
        if (c == '<') {
            if (newlinect == 1) putc(' ', stdout);
            newlinect = 0;
            c = getc(stdin);
            if (c == '<') {
                if (paramct <= 0) printf("&lt;");
            } else {
                  ungetc(c, stdin);
                  for (i=0, p=token; (c=getc(stdin)) != EOF && c != '>';
                  i++)
                  { if (i < sizeof(token)-1)
                    *p++ = isupper(c) ? tolower(c) : c;
                  }
                  *p = '\0';
                  if (c == EOF) break;
                  if (strcmp(token, "param") == 0)
                      paramct++;
                  else if (strcmp(token, "nofill") == 0) {
                      nofill++;
		      printf("<PRE>\n");
		  }
                  else if (strcmp(token, "/param") == 0)
                      paramct--;
                  else if (strcmp(token, "/nofill") == 0) {
                      nofill--;
		      printf("</PRE>\n");
		  }
                  else if (strcmp(token, "bold") == 0)
		      printf("<B>");
                  else if (strcmp(token, "/bold") == 0)
		      printf("</B>");
                  else if (strcmp(token, "italic") == 0)
		      printf("<I>");
                  else if (strcmp(token, "/italic") == 0)
		      printf("</I>");
                  else if (strcmp(token, "fixed") == 0)
		      printf("<TT>");
                  else if (strcmp(token, "/fixed") == 0)
		      printf("</TT>");
                  else if (strcmp(token, "underline") == 0)
		      printf("<U>");
                  else if (strcmp(token, "/underline") == 0)
		      printf("</U>");
                  else if (strcmp(token, "underline") == 0)
		      printf("<U>");
                  else if (strcmp(token, "/underline") == 0)
		      printf("</U>");
                  else if (strcmp(token, "center") == 0)
		      printf("<CENTER>");
                  else if (strcmp(token, "/center") == 0)
		      printf("</CENTER>");
                  else if (strcmp(token, "flushleft") == 0)
		      printf("<DIV ALIGN=LEFT>");
                  else if (strcmp(token, "/flushleft") == 0)
		      printf("</DIV>");
                  else if (strcmp(token, "flushright") == 0)
		      printf("<DIV ALIGN=RIGHT>");
                  else if (strcmp(token, "/flushright") == 0)
		      printf("</DIV>");
                  else if (strcmp(token, "bigger") == 0) {
		      size+=2;
		      dofont(size,font,color);
		  }
                  else if (strcmp(token, "/bigger") == 0) {
		      size-=2;
		      printf("</FONT>");
		  }
                  else if (strcmp(token, "smaller") == 0) {
		      size-=2;
		      dofont(size,font,color);
		  }
                  else if (strcmp(token, "/smaller") == 0) {
		      size+=2;
		      printf("</FONT>");
		  }
                  else if (strcmp(token, "indent") == 0)
		      printf("<DL><DD>");
                  else if (strcmp(token, "/indent") == 0)
		      printf("</DL>");
		  else if (strcmp(token, "excerpt") == 0)
		      excerpt = 1;
		  else if (strcmp(token, "/excerpt") == 0)
		      excerpt = 0;
                  else if (strcmp(token, "color") == 0) {
		    colorparam = 1;
		  }
                  else if (strcmp(token, "/color") == 0) {
		      color[0] = '\0';
		      colorparam = 0;
		      printf("</FONT>");
		  }
                  else if (strcmp(token, "fontfamily") == 0) {
		    fontparam = 1;
		  }
                  else if (strcmp(token, "/fontfamily") == 0) {
		    font[0] = '\0';
		    fontparam = 0;
		    printf("</FONT>");
		  }
		  else if (strcmp(token, "paraindent") == 0) {
		    paraparam = 1;
		  }
		  else if (strcmp(token, "/paraindent") == 0) {
		    paraparam = 0;
		    if (paratype == PT_LEFT)
		      printf("</DL>");
		    paratype = 0;
		  }
#ifdef NOT_IMPLEMENTED
                  else if (strcmp(token, "flushboth") == 0)
		      printf("<DIV>");
                  else if (strcmp(token, "/flushboth") == 0)
		      printf("</DIV>");
                  else if (strcmp(token, "indentright") == 0)
		      printf("<DL><DD>");
                  else if (strcmp(token, "/indentright") == 0)
		      printf("</DL>");
#endif
              }
        } else {
            if (paramct > 0) {
              ungetc(c, stdin);
              for (i=0, p=token; (c=getc(stdin)) != EOF && c != '<'; i++)
              { 
		if (i < sizeof(token)-1)
                    *p++ = isupper(c) ? tolower(c) : c;
              }
	      if (c == '<') ungetc(c,stdin);
              *p = '\0';
              if (c == EOF) break;
	      if (colorparam) {
		strncpy(color,token,sizeof(color));
		dofont(size,font,color);
	      } else if (fontparam) {
		strncpy(font,token,sizeof(font));
		dofont(size,font,color);
	      }
	      else if (paraparam) {
		if (strcmp(token,"left") == 0) {
		  paratype = PT_LEFT;
		  printf("<DL><DD>");
		}
#ifdef NOT_IMPLEMENTED
		else if (strcmp(token,"right") == 0)
		  paratype = PT_RIGHT;
		else if (strcmp(token,"in") == 0)
		  paratype = PT_IN;
		else if (strcmp(token,"out") == 0)
		  paratype = PT_OUT;
#endif 
	      }
	    }
            else if (c == '\n' && nofill <= 0) {
                if (++newlinect > 1) {
		   printf("<BR>\n");
		   if (excerpt) printf("&gt; ");
		}
	    } else if (c == '>') {
	      printf("&gt;");
	    } else if (c == '&') {
	      printf("&amp");
            } else {
                if (newlinect == 1) putc(' ', stdout);
                newlinect = 0;
                putc(c, stdout);
	    }
     }
    }
     /* The following line is only needed with line-buffering */
     putc('\n', stdout);
     exit(0);
}
