/******************************************************************************
 * Steve Rider - stever@transmission23.com
 * QuickPics
 *   Image galleries for the web made easy
 *
 * html.c - output html
 *
 *****************************************************************************/
/* 18 February 98 */

#include "qp.h"
#include "utility.h"
#include "dir.h"
#include "html.h"


/****************************************************************************
 *
 * HTML routines
 *
 ****************************************************************************/

void HTML_CreateGallery()
{
	dirrec	*dir;
	imgrec	*img = NULL;
	char	avg[80], buf[MAXTEXTLEN];
	FILE	*qp_html;
	int	column = 0, i, tot = 0;

	/* Build root directory index_html */
	qp_html = fopen(QP.index_html,"w");
	if(qp_html == NULL)
	{
		printf("  Unable to open \"%s/%s\".\n",QP.rootdir,QP.index_html);
		return;
	}

	/* Write header */
	fprintf(qp_html,"<html><head>\n");
	fprintf(qp_html,"<title>%s</title>\n",QPDir.title);
	fprintf(qp_html,"</head>\n");
	fprintf(qp_html,"\n");

	/* Write body properties */
	HTML_WriteBodyProperties(qp_html, QP.root_bgcolor, QP.root_textcolor,
		QP.root_linkcolor, QP.root_vlinkcolor, QP.root_alinkcolor);
	fprintf(qp_html,"\n");

	/* Write title */
	HTML_WriteComment(qp_html,"TITLE");
	HTML_WriteFormattedText(qp_html, QPDir.title, QP.root_titlesize,
		QP.root_titlefont, QP.root_titlecolor,
		QP.root_titlebold, QP.root_titleitalic);
	fprintf(qp_html,"\n");

	/* Write toolbar at top */
	if(QP.root_write_toolbar)
	{
		HTML_WriteComment(qp_html,"TOOLBAR");
		HTML_WriteToolbar(qp_html, ".", NULL, NULL, 0);
		fprintf(qp_html,"\n");
	}

	/* Write folder description */
	HTML_WriteComment(qp_html,"FOLDER DESCRIPTION");
	fprintf(qp_html,"<table width=%s border=0 cellpadding=0 cellspacing=2>\n",QP.page_width);
	fprintf(qp_html,"<tr><td>\n");
	HTML_WriteFormattedText(qp_html, QPDir.heading, QP.root_headingsize,
		QP.root_headingfont, QP.root_headingcolor,
		QP.root_headingbold, QP.root_headingitalic);
	fprintf(qp_html,"</td></tr></table>\n");

	for(dir=QPDir.dirs; dir; dir=dir->next)
		tot += dir->num_images;
	sprintf(buf,"(%d folders, %d total images)",QPDir.num_dirs,tot);
	HTML_WriteFormattedText(qp_html, buf, QP.root_propertiessize,
		QP.root_propertiesfont, QP.root_propertiescolor,
		QP.root_propertiesbold, QP.root_propertiesitalic);
	fprintf(qp_html,"<br><br>\n");
	fprintf(qp_html,"\n");

	/* Write images in table */
	HTML_WriteComment(qp_html,"IMAGE THUMBNAILS");
	fprintf(qp_html,"<center>\n");
	fprintf(qp_html,"<table width=%s cellpadding=10 cellspacing=0 border=%d><tr>\n",QP.page_width,QP.root_border);
	fprintf(qp_html,"\n");

	/* Write each cell with 1st picture from each gallery */
	for(dir=QPDir.dirs; dir; dir=dir->next)
	{
		fprintf(qp_html,
			"<td valign=top align=center width=%d%% height=%d>\n",
			(int)(100 / QP.root_cols), QP.thumbnailsize+50);

		/* folder name */
		HTML_WriteFormattedText(qp_html, dir->alias,
			QP.root_foldernamesize,
			QP.root_foldernamefont, QP.root_foldernamecolor,
			QP.root_foldernamebold, QP.root_foldernameitalic);

		/* Check for main image */
		if(dir->mainimage)
			img = imgrec_image(dir->images, dir->mainimage);
		else if(dir->images)
			img = dir->images;


		if(img)
		{
			/* Check for thumbnail validity */
			if(img->thumbnail_width == 0 || img->thumbnail_height == 0)
			{
				fprintf(qp_html,"<table border=4 cellspacing=0 cellpadding=0><tr>\n");
				fprintf(qp_html,"<td align=center valign=center><a href=\"%s/%s\"><font size=\"1\" color=\"#ff0000\">Thumbnail not available</font></a></td>\n",dir->name,isequalcase(QP.index_html,"index.html") ? "" : QP.index_html);
				fprintf(qp_html,"</tr></table>\n");
			}
			else
			{
				fprintf(qp_html,"<table border=4 cellspacing=0 cellpadding=0><tr>\n");
				fprintf(qp_html,"<td><a href=\"%s/%s\"><img src=\"%s/%s\" width=%d height=%d border=%d alt=\"%s\"></a></td>\n",
					dir->name,
					isequalcase(QP.index_html,"index.html")
						? "" : QP.index_html,
					dir->name,
					img->thumbnail_name,
					img->thumbnail_width,
					img->thumbnail_height,
					QP.root_pictureborder,
					img->description);
				fprintf(qp_html,"</tr></table>\n");
			}
		}
		else
		{
			fprintf(qp_html,"<br>\n");
			fprintf(qp_html,"<a href=\"%s/%s\"><font size=\"1\" color=\"#ff0000\">(Subfolder)</font></a><br>\n",dir->name,isequalcase(QP.index_html,"index.html") ? "" : QP.index_html);
			fprintf(qp_html,"<br>\n");
		}
		
		/* Write description under thumbnail */
		bytes_to_kb(QPDir_AvgBytes(dir),avg);
		sprintf(buf,"%d images, approx %s each",dir->num_images, avg);
		HTML_WriteDescription(qp_html, buf, NULL,
			QP.root_descsize, QP.root_descfont, QP.root_desccolor,
			QP.root_descbold, QP.root_descitalic);

		fprintf(qp_html,"</td>\n");
		fprintf(qp_html,"\n");

		column ++;
		if(column >= QP.root_cols)
		{
			fprintf(qp_html,"</tr><tr>\n");
			fprintf(qp_html,"\n");
			column = 0;
		}
	}

	/* Pad table with empty cells */
	for(i=column; i && i<QP.root_cols; i++)
	{
		fprintf(qp_html,"<td valign=top align=center width=%d%% height=%d>\n",(int)(100 / QP.root_cols),QP.thumbnailsize+50);
		fprintf(qp_html,"<br>\n");
		fprintf(qp_html,"</td>\n");
		fprintf(qp_html,"\n");
	}

	fprintf(qp_html,"</tr></table>\n");
	fprintf(qp_html,"\n");
	fprintf(qp_html,"<br><br>\n");
	HTML_WriteFormattedText(qp_html, QP.footer, QP.footersize,
		QP.footerfont, QP.footercolor, QP.footerbold, QP.footeritalic);
	fprintf(qp_html,"\n");
	fprintf(qp_html,"</center>\n");
	fprintf(qp_html,"</body></html>\n");

	fclose(qp_html);
	chmod(QP.index_html,0644);

	/* Now build picture gallery for each dir */
	for(dir=QPDir.dirs; dir; dir=dir->next)
		HTML_WriteIndexPage(dir, QPDir.title);

	printf("\n");
}

void HTML_WriteIndexPage(dirrec *dir, char *title)
{
	FILE	*qp_html;
	char	path[MAXFILENAMELEN], new_title[MAXTEXTLEN];
	char	avg[MAXTEXTLEN], buf[MAXFILENAMELEN];
	dirrec	*subdir;
	imgrec	*img;
	int	column = 0, i;
	int	using_large_dot = 0, allowable_dots;
	int	dots_per_largedot = 1, count = 0;

	sprintf(path,"%s/%s",dir->path,QP.index_html);
	qp_html = fopen(path,"w");
	if(qp_html == NULL)
	{
		printf("  Unable to open \"%s/%s\".\n",QP.rootdir,path);
		return;
	}

	/* Write header */
	fprintf(qp_html,"<html><head>\n");
	fprintf(qp_html,"<title>%s</title>\n",dir->alias);
	fprintf(qp_html,"</head>\n");
	fprintf(qp_html,"\n");

	/* Write body properties */
	HTML_WriteBodyProperties(qp_html, QP.index_bgcolor, QP.index_textcolor,
		QP.index_linkcolor, QP.index_vlinkcolor, QP.index_alinkcolor);
	fprintf(qp_html,"\n");


	/* Build new title */
	HTML_WriteComment(qp_html,"TITLE");
	sprintf(new_title,"%s: %s",title, dir->alias);
	HTML_WriteFormattedText(qp_html, new_title, QP.index_titlesize,
		QP.index_titlefont, QP.index_titlecolor,
		QP.index_titlebold, QP.index_titleitalic);
	fprintf(qp_html,"\n");

	/* Write toolbar at top */
	if(QP.index_write_toolbar)
	{
		HTML_WriteComment(qp_html,"TOOLBAR");
		HTML_WriteToolbar(qp_html, dir->path_home, dir, dir->oldest_parent, 0);
		fprintf(qp_html,"\n");
	}

	/* Write folder description */
	HTML_WriteComment(qp_html,"FOLDER DESCRIPTION");
	fprintf(qp_html,"<table width=%s border=0 cellpadding=0 cellspacing=2>\n",QP.page_width);
	fprintf(qp_html,"<tr><td>\n");
	HTML_WriteFormattedText(qp_html, dir->description, QP.index_headingsize,
		QP.index_headingfont, QP.index_headingcolor,
		QP.index_headingbold, QP.index_headingitalic);
	fprintf(qp_html,"</td></tr></table>\n");

	bytes_to_kb(QPDir_AvgBytes(dir),avg);
	sprintf(buf,"(%d images, approx %s each)",dir->num_images, avg);
	HTML_WriteFormattedText(qp_html, buf, QP.index_propertiessize,
		QP.index_propertiesfont, QP.index_propertiescolor,
		QP.index_propertiesbold, QP.index_propertiesitalic);
	fprintf(qp_html,"<br><br>\n");
	fprintf(qp_html,"\n");

	/* Write subdirectories */
	if(dir->subdirs)
	{
		HTML_WriteComment(qp_html,"SUBDIRECTORIES");
		fprintf(qp_html,"<font size=\"4\">Subfolders:</font><br>\n");
		fprintf(qp_html,"<table width=%s cellspacing=2 border=0><tr>\n",QP.page_width);
		fprintf(qp_html,"<td width=8%%><br></td>\n");
		fprintf(qp_html,"<td width=92%%>\n");

		for(subdir=dir->subdirs; subdir; subdir=subdir->next)
		{
			bytes_to_kb(QPDir_AvgBytes(subdir),avg);
			sprintf(buf,"(%d images, approx %s each)",
				subdir->num_images, avg);
			fprintf(qp_html,"<a href=\"%s/%s\">%s</a> %s<br>\n",
				subdir->name,
				isequalcase(QP.index_html,"index.html") ? "" : QP.index_html,
				subdir->alias,
				buf);

			/* Write subdirectory (recursive call) */
			HTML_WriteIndexPage(subdir,new_title);
		}

		fprintf(qp_html,"</td>\n");
		fprintf(qp_html,"</tr></table>\n");
		fprintf(qp_html,"<br>\n");
		fprintf(qp_html,"\n");
	}

	/* Write images in table */
	HTML_WriteComment(qp_html,"IMAGE THUMBNAILS");
	fprintf(qp_html,"<center>\n");
	fprintf(qp_html,"<table width=%s cellpadding=10 cellspacing=0 border=%d><tr>\n",QP.page_width,QP.index_border);
	fprintf(qp_html,"\n");

	/* Display status info after we do the recursive call */
	sprintf(buf,"  %s/%s: (%d)",BaseFilename(QP.rootdir),
		dir->path, dir->num_images);
	printf(buf);
	fflush(stdout);

	allowable_dots = 80 - (strlen(buf) + 2);
	if(dir->num_images > allowable_dots)
	{
		using_large_dot = 1;
		dots_per_largedot = (dir->num_images / allowable_dots) + 1;
		count = 0;
	}

	/* Write each cell with 1st image from each gallery */
	for(img=dir->images; img; img=img->next)
	{
		fprintf(qp_html,
			"<td valign=top align=center width=%d%% height=%d>\n",
			(int)(100 / QP.index_cols), QP.thumbnailsize+50);

		/* Check for thumbnail validity */
		if(img->thumbnail_width == 0 || img->thumbnail_height == 0)
		{
			fprintf(qp_html,"<table border=4 cellspacing=0 cellpadding=0><tr>\n");
			fprintf(qp_html,"<td align=center valign=center><font size=\"1\" color=\"#ff0000\">Thumbnail not available</font></a><br></td>\n");
			fprintf(qp_html,"</tr></table>\n");
		}
		else
		{
			fprintf(qp_html,"<table border=4 cellspacing=0 cellpadding=0><tr>\n");
			fprintf(qp_html,"<td><a href=\"%s.html\"><img src=\"%s\" width=%d height=%d border=%d alt=\"%s\"></a></td>\n",
				BaseFilename(img->name), img->thumbnail_name,
				img->thumbnail_width, img->thumbnail_height,
				QP.index_pictureborder, img->description);
			fprintf(qp_html,"</tr></table>\n");
		}

		/* Write description under thumbnail */
		HTML_WriteDescription(qp_html, img->description, img->date,
			QP.index_descsize, QP.index_descfont,
			QP.index_desccolor, QP.index_descbold,
			QP.index_descitalic);

		fprintf(qp_html,"</td>\n");
		fprintf(qp_html,"\n");


		/* Write separate html page for each pictures */
		HTML_WritePicPage(dir,img,new_title);

		column ++;
		if(column >= QP.index_cols)
		{
			fprintf(qp_html,"</tr><tr>\n");
			fprintf(qp_html,"\n");
			column = 0;
		}

		if(using_large_dot)
		{
			count++;
			if(count >= dots_per_largedot)
			{
				count = 0;
				printf("o");
			}
		}
		else
			printf(".");
		fflush(stdout);
	}

	/* Pad table with empty cells */
	for(i=column; i && i<QP.index_cols; i++)
	{
		fprintf(qp_html,"<td valign=top align=center width=%d%% height=%d>\n",(int)(100 / QP.index_cols),QP.thumbnailsize+50);
		fprintf(qp_html,"<br>\n");
		fprintf(qp_html,"</td>\n");
		fprintf(qp_html,"\n");
	}

	fprintf(qp_html,"</tr></table>\n");
	fprintf(qp_html,"\n");
	fprintf(qp_html,"<br><br>\n");
	HTML_WriteFormattedText(qp_html, QP.footer, QP.footersize,
		QP.footerfont, QP.footercolor, QP.footerbold, QP.footeritalic);
	fprintf(qp_html,"\n");
	fprintf(qp_html,"</center>\n");
	fprintf(qp_html,"</body></html>\n");

	fclose(qp_html);
	chmod(path,0644);

	printf("\n");
}

void HTML_WritePicPage(dirrec *dir, imgrec *img, char *title)
{
	FILE	*qp_html;
	imgrec	*prev = NULL, *next = NULL, *an_img;
	char	path[MAXFILENAMELEN], k[MAXTEXTLEN], buf[MAXTEXTLEN];
	int	img_num = 0;

	sprintf(path,"%s/%s.html",dir->path,BaseFilename(img->name));
	qp_html = fopen(path,"w");
	if(qp_html == NULL)
	{
		printf("  Unable to open \"%s/%s\".\n",QP.rootdir,path);
		return;
	}

	/* Write header */
	fprintf(qp_html,"<html><head>\n");
	fprintf(qp_html,"<title>%s</title>\n",dir->alias);
	fprintf(qp_html,"</head>\n");
	fprintf(qp_html,"\n");

	/* Write body properties */
	HTML_WriteBodyProperties(qp_html, QP.pic_bgcolor, QP.pic_textcolor,
		QP.pic_linkcolor, QP.pic_vlinkcolor, QP.pic_alinkcolor);
	fprintf(qp_html,"\n");


	/* Write title */
	HTML_WriteComment(qp_html,"TITLE");
	HTML_WriteFormattedText(qp_html, title, QP.pic_titlesize,
		QP.pic_titlefont, QP.pic_titlecolor,
		QP.pic_titlebold, QP.pic_titleitalic);
	fprintf(qp_html,"\n");

	/* Write toolbar at top */
	if(QP.pic_write_toolbar)
	{
		HTML_WriteComment(qp_html,"TOOLBAR");
		HTML_WriteToolbar(qp_html, dir->path_home, dir, dir->oldest_parent, 1);
		fprintf(qp_html,"\n");
	}

	fprintf(qp_html,"<br>\n");
	fprintf(qp_html,"<center>\n");

	/* Determine next and prev img pointers */
	for(an_img=dir->images; an_img; an_img=an_img->next)
	{
		img_num++;
		next = img->next;
		if(an_img == img)
			break;

		prev = an_img;
	}

	/* Draw table for next, prev */
	fprintf(qp_html,"<table width=%d border=0><tr>\n",QP.picture_width);

	/* Draw 'return to index' */
	fprintf(qp_html,"<td width=22%% align=right>\n");
	fprintf(qp_html,"<font face=\"arial\" size=\"2\">\n");
	fprintf(qp_html,"<a href=\"%s\">Return to index</a>\n",isequalcase(QP.index_html,"index.html") ? "" : QP.index_html);
	fprintf(qp_html,"</font>\n");
	fprintf(qp_html,"</td>\n");

	/* Draw 'image x of x' */
	bytes_to_kb(img->bytes,k);
	fprintf(qp_html,"<td width=56%% align=center>\n");
	fprintf(qp_html,"<font face=\"arial\" size=\"2\">\n");
	fprintf(qp_html,"Image %d of %d (%s)\n",img_num, dir->num_images, k);
	fprintf(qp_html,"</font>\n");
	fprintf(qp_html,"</td>\n");

	/* Draw 'prev' */
	fprintf(qp_html,"<td width=11%% align=center>\n");
	fprintf(qp_html,"<font face=\"arial\" size=\"2\">\n");
	if(prev)
	{
		fprintf(qp_html,"<a href=\"%s.html\">Prev</a>\n",
			BaseFilename(prev->name));
	}
	else
	{
		fprintf(qp_html,"<font color=\"%s\">.</font>\n",
			QP.pic_bgcolor);
	}
	fprintf(qp_html,"</font>\n");
	fprintf(qp_html,"</td>\n");

	/* Draw 'next' */
	fprintf(qp_html,"<td width=11%% align=center>\n");
	fprintf(qp_html,"<font face=\"arial\" size=\"2\">\n");
	if(next)
	{
		fprintf(qp_html,"<a href=\"%s.html\">Next</a>\n",
			BaseFilename(next->name));
	}
	else
	{
		fprintf(qp_html,"<font color=\"%s\">.</font>\n",
			QP.pic_bgcolor);
	}
	fprintf(qp_html,"</font>\n");
	fprintf(qp_html,"</td>\n");

	fprintf(qp_html,"</tr></table>\n");
	fprintf(qp_html,"\n");

	/* Draw image */
	HTML_WriteComment(qp_html,"IMAGE");

	if(QP.prev_next_thumbnails)
	{
		/* Write previous image */
		fprintf(qp_html,"<table width=%s border=0><tr>\n",QP.page_width);
		fprintf(qp_html,"<td valign=center align=center width=20%% height=%d>\n", QP.thumbnailsize+50);

		if(prev)
		{
			fprintf(qp_html,"<table border=4 cellspacing=0 cellpadding=0><tr>\n");
			fprintf(qp_html,"<td><a href=\"%s.html\"><img src=\"%s\" width=%d height=%d border=%d alt=\"Prev\"></a></td>\n",
				BaseFilename(prev->name),
				prev->thumbnail_name,
				prev->thumbnail_width,
				prev->thumbnail_height,
				QP.index_pictureborder);
			fprintf(qp_html,"</tr></table>\n");
			sprintf(buf,"%s",prev->description);
			HTML_WriteDescription(qp_html, buf, NULL,
				QP.index_descsize, QP.index_descfont,
				QP.index_desccolor, QP.index_descbold,
				QP.index_descitalic);
		}
		else
		{
			fprintf(qp_html,"<br>\n");
		}
		fprintf(qp_html,"</td>\n");
		fprintf(qp_html,"\n");
	
		/* Write main image */
		fprintf(qp_html,"<td align=center width=60%%>\n");
		fprintf(qp_html,"<img src=\"%s\" width=%d height=%d alt=\"%s\" border=1>\n", img->name, img->width, img->height, img->description);
		fprintf(qp_html,"</td>\n");
		fprintf(qp_html,"\n");
	
		/* Write next image */
		fprintf(qp_html,"<td valign=center align=center width=20%% height=%d>\n", QP.thumbnailsize+50);
		if(next)
		{
			fprintf(qp_html,"<table border=4 cellspacing=0 cellpadding=0><tr>\n");
			fprintf(qp_html,"<td><a href=\"%s.html\"><img src=\"%s\" width=%d height=%d border=%d alt=\"Next\"></a></td>\n",
				BaseFilename(next->name),
				next->thumbnail_name,
				next->thumbnail_width,
				next->thumbnail_height,
				QP.index_pictureborder);
			fprintf(qp_html,"</tr></table>\n");
			sprintf(buf,"%s",next->description);
			HTML_WriteDescription(qp_html, buf, NULL,
				QP.index_descsize, QP.index_descfont,
				QP.index_desccolor, QP.index_descbold,
				QP.index_descitalic);
		}
		else
		{
			fprintf(qp_html,"<br>\n");
		}
		fprintf(qp_html,"</td>\n");
		fprintf(qp_html,"</tr></table>\n");
		fprintf(qp_html,"\n");
	}
	else
	{
		fprintf(qp_html,"<img src=\"%s\" width=%d height=%d alt=\"%s\">\n",img->name, img->width, img->height, img->description);
	}

	/* Draw description */
	fprintf(qp_html,"<table width=%d><tr><td align=center>\n",QP.picture_width-50);
	HTML_WriteDescription(qp_html, img->description, img->date,
		QP.pic_descsize, QP.pic_descfont,
		QP.pic_desccolor, QP.pic_descbold,
		QP.pic_descitalic);
	fprintf(qp_html,"</td></tr></table>\n");
	fprintf(qp_html,"<br>\n");

	fprintf(qp_html,"</center>\n");
	fprintf(qp_html,"</body></html>\n");

	fclose(qp_html);
	chmod(path,0644);
}
		

	

void HTML_WriteBodyProperties(FILE *f, char *bg, char *text, char *link,
	char *vlink, char *alink)
{
	fprintf(f,"<body bgcolor=\"%s\" text=\"%s\" link=\"%s\" vlink=\"%s\" alink=\"%s\">\n", bg, text, link, vlink, alink);
}

void HTML_WriteToolbar(FILE *f, char *relpath, dirrec *current_dir, 
	dirrec *oldest_parent, int picpage)
{
	dirrec	*dir;
	char	sep[20];

	fprintf(f,"<table width=%s border=0 cellpadding=0 cellspacing=2>\n",QP.page_width);
	fprintf(f,"<tr><td>\n");
	fprintf(f,"<font face=\"arial\" size=\"1\">\n");
	if(current_dir == NULL)
		fprintf(f,"Home |\n");
	else
		fprintf(f,"<a href=\"%s/%s\">Home</a> |\n",relpath,isequalcase(QP.index_html,"index.html") ? "" : QP.index_html);	
	for(dir=QPDir.dirs; dir; dir=dir->next)
	{
		if(dir->next)
			strcpy(sep," |");
		else
			sep[0] = 0;
		
		if(dir == oldest_parent)
		{
			if(picpage || dir != current_dir)
			{
				fprintf(f,"<a href=\"%s/%s/%s\"><font color=\"%s\">%s</font></a>%s\n",
					relpath, dir->name,
					isequalcase(QP.index_html, "index.html") ? "" : QP.index_html,
					QP.index_textcolor, dir->alias, sep);
			}
			else
				fprintf(f,"%s%s\n",dir->alias, sep);
		}
		else
			fprintf(f,"<a href=\"%s/%s/%s\">%s</a>%s\n",
				relpath, dir->name,
				isequalcase(QP.index_html, "index.html") ? "" : QP.index_html,
				dir->alias, sep);
	}
	fprintf(f,"</td></tr>\n");
	fprintf(f,"</table>\n");
	fprintf(f,"<br>\n");
	fprintf(f,"\n");
}

void HTML_WriteFormattedText(FILE *f, char *str, int size, char *font,
	char *color, int bold, int italic)
{
	char	bold_on[25], bold_off[25], italic_on[25], italic_off[25];
	char	face[MAXTEXTLEN], s[BUFSIZ];

	/* Reset */
	bold_on[0] = 0;
	bold_off[0] = 0;
	italic_on[0] = 0;
	italic_off[0] = 0;
	face[0] = 0;
	s[0] = 0;

	if(bold)
	{
		strcpy(bold_on,"<b>");
		strcpy(bold_off,"</b>");
	}
	if(italic)
	{
		strcpy(italic_on,"<i>");
		strcpy(italic_off,"</i>");
	}
	if(font && font[0])
		sprintf(face," face=\"%s\"",font);
	if(str && str[0])
		strcpy(s,str);
	else
		strcpy(s,"");
		
	/* Dump line */
	fprintf(f,"<font size=\"%d\" color=\"%s\"%s>%s%s%s%s%s</font><br>\n",
		size, color, face, bold_on, italic_on, s, italic_off, bold_off);
}

void HTML_WriteDescription(FILE *f, char *desc, char *footer, int size,
	char *font, char *color, int bold, int italic)
{
	char	bold_on[25], bold_off[25], italic_on[25], italic_off[25];
	char	face[MAXTEXTLEN];

	/* Reset */
	bold_on[0] = 0;
	bold_off[0] = 0;
	italic_on[0] = 0;
	italic_off[0] = 0;
	face[0] = 0;

	if(bold)
	{
		strcpy(bold_on,"<b>");
		strcpy(bold_off,"</b>");
	}
	if(italic)
	{
		strcpy(italic_on,"<i>");
		strcpy(italic_off,"</i>");
	}
	if(font && font[0])
		sprintf(face," face=\"%s\"",font);
		
	fprintf(f,"<font size=\"%d\" color=\"%s\"%s>\n",size, color, face);
	fprintf(f,"%s%s%s%s%s<br>\n",bold_on, italic_on, desc, italic_off, bold_off);
	if(footer && footer[0])
		fprintf(f,"<i>(%s)</i><br>\n",footer);
	fprintf(f,"</font>");
}

void HTML_WriteComment(FILE *f, char *comment)
{
	fprintf(f,"<!-- %s -->\n",comment);
}
