/* notes-to-html.c - written by Alexis Wilke for Made to Order Software Corp. (c) 2002-2009 */

/*

Copyright (c) 2002-2009 Made to Order Software Corp.

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and
associated documentation files (the "Software"), to
deal in the Software without restriction, including
without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/



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

int main(int argc, char *argv[])
{
	char		buf[256], *start, *s, *end;
	int		empty, indent;
	FILE		*f;

	if(argc <= 1) {
		fprintf(stderr, "Notes to HTML -- Copyright Made to Order Software Corp. (c) 2002-2009\n");
		fprintf(stderr, "Written by Alexis Wilke\n\n");
		fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
		exit(1);
	}

	empty = 1;
	indent = 0;
	f = fopen(argv[1], "rb");
	while(fgets(buf, sizeof(buf), f) != NULL) {
		s = buf;
		while(isspace(*s) && *s != '\n' && *s != '\r' && *s != '\0') {
			/* keep this space in the output */
			printf("%c", *s);
			s++;
		}
		if(*s == '\n' || *s == '\r' || *s == '\0') {
			empty = 1;
			if(indent != 0) {
				indent = 0;
				printf("</ul></ul>");
			}
			printf("\n");
			continue;
		}
		start = s;
		if(*start == '*' || (start[0] == 'o' && isspace(start[1]))) {
			start = "<br/><br/><center><hr noshade size=\"2\" width=\"80%\"/></center><br/><font color=\"#ff000\" style=\"font-size: 20px\">";
			end = "</font>";

			if(indent != 0) {
				indent = 0;
				printf("</ul></ul>");
			}
		}
		else if(*start == '.') {
			start = "<ul><b>";
			end = "</b></ul>";

			if(indent != 0) {
				indent = 0;
				printf("</ul></ul>");
			}
		}
		else {
			if(isspace(buf[0]) && indent == 0) {
				indent = 1;
				printf("<ul><ul>");
			}
			if(empty) {
				empty = 0;
				start = "<p>";
				end = "";
			}
			else {
				start = "";
				end = "";
			}
		}

		printf("%s", start);

		while(*s != '\0') {
			if(strncasecmp(s, "ft2sswf", 7) == 0) {
				printf("<i>ft2sswf</i>");
				s += 7;
			}
			else if(strncasecmp(s, "sswf", 4) == 0) {
				printf("<i>sswf</i>");
				s += 4;
			}
			else if(strncasecmp(s, "swf_dump", 8) == 0) {
				printf("<i>swf_dump</i>");
				s += 8;
			}
			else {
				switch(*s) {
				case '<':
					printf("&lt;");
					break;

				case '>':
					printf("&gt;");
					break;

				case '&':
					printf("&amp;");
					break;

				case '"':
					printf("&quot;");
					break;

				case '\r':
				case '\n':
					*s-- = '\0';
					break;

				default:
					printf("%c", *s);
					break;

				}
				s++;
			}
		}

		printf("%s\n", end);
	}
	fclose(f);

	return 0;
}

