import java.io.*;
import java.util.*;

/**
 * Converts a MaxAuthor mark-up file to HTML
 * Creation date: (6/5/2001 4:32:02 PM)
 * @author: Peter Cockerell
 */
class AuthorToHTML {
	public static final char CH_FMT_DELIM = '\\';
	public static final char CH_FMT_START = '+';
	public static final char CH_FMT_END = '-';
	public static final int TAB_STOP = 6;

	private Set styles = new TreeSet();
/**
 * AuthorToXML constructor comment.
 */
public AuthorToHTML() {
	super();
}
/**
 * Returns the style name of a \+Para Style\ tag. Spaces in the name are
 * converted to - except for spaces that follow or precede -.
 * @param line The tag to extract the name from
 * @return The style name
 */
private String getParagraphStyleName(String line) {
	return getParagraphStyleName(line, 0, line.length()-1);
}
/**
 * Returns the style name of a \+Para Style\ tag. Spaces in the name are
 * converted to - except for spaces that follow or precede -.
 * @param line The tag to extract the name from
 * @param start The offset in line of the opening \ character
 * @param start The offset in line of the closing \ character
 * @return The style name
 */
private String getParagraphStyleName(String line, int start, int end) {
	StringBuffer buff = new StringBuffer(line.length());
	boolean lastHyphen = false;
	line = line.substring(start+2, end);
	line = line.trim();
	int len = line.length();
	for (int i = 0; i < len; i++) {
		char ch = line.charAt(i);
		if (ch == ' ') {
			ch = '-';
		}
		if (ch == '-') {
			if (!lastHyphen) {
				buff.append(ch);
				lastHyphen = true;
			}
		} else {
			buff.append(ch);
			lastHyphen = false;
		}
	}

	String result = buff.toString();
	styles.add(result);
	return result;
}
/**
 * Returns true if the given String is a \+Para Style\ one.
 * @param line The line to test
 * @return true of the line is a paragraph formatting one
 */
private boolean isParagraphStyle(String line) {
	int len = line.length();
	if (len < 3) {
		return false;
	}

	int delimCount = 0;
	int firstDelimIndex = -1;
	for (int i = 0; i < len; i++) {
		if (line.charAt(i) == CH_FMT_DELIM) {
			delimCount++;
			if (delimCount > 2) {
				return false;
			} else if (delimCount == 1) {
				firstDelimIndex = i;
			}
			if (i != 0 && i != len-1) {
				return false;
			}
		}
	}
	return delimCount == 2 && CH_FMT_START == line.charAt(firstDelimIndex+1);
}
/**
 * Takes a MacAuthor file from StdIn and outputs the
 * equivalent HTML to StdOut.
 * @param args an array of command-line arguments
 */
public static void main(java.lang.String[] args) {
	new AuthorToHTML().run();
}
/**
 * Outputs the HTML footer on the given Writer
 */
private void outputFooter(BufferedWriter w) {
	try {
		w.write("</body>");
		w.newLine();
		w.flush();
	} catch (IOException e) {
		System.out.println("error writing HTML footer");
		e.printStackTrace();
	}
}
/**
 * Outputs the HTML header on the given Writer
 */
private void outputHeader(BufferedWriter w) {
	try {
		w.write("<html>");
		w.newLine();
		w.write("<head>");
		w.newLine();
		w.write("<link href='aalp.css' type='text/css' rel='stylesheet'>");
		w.newLine();
		w.write("</head>");
		w.newLine();
		w.write("<body>");
		w.newLine();
		w.flush();
	} catch (IOException e) {
		System.out.println("error writing HTML header");
		e.printStackTrace();
	}
}
/**
 * Returns the the paragraph string with embedded \styles\
 * converted to spans and HTML characters escaped.
 * @param line The text line with embedded formats
 * @param result The StringBuffer in which to store the result.
 * @return The HTML for the line
 */
private void processLine(String line, StringBuffer result) {
	int len = line.length();
	int indent = 0;
	boolean needEndSpan = false;
	for (int i = 0; i < len; i++) {
		char ch = line.charAt(i);
		if (ch == CH_FMT_DELIM) {
			char type;
			int start = i;
			if (i < len-1) {
				type = line.charAt(++i);
			} else {
				type = '\000';
			}
			if (type == CH_FMT_START || type == CH_FMT_END) {	
				do {
					i++;
				} while (line.charAt(i) != CH_FMT_DELIM);
				if (type == CH_FMT_START) {
					String style = getParagraphStyleName(line, start, i);
					result.append("<span class='" + style + "'>");
				} else {
					result.append("</span>");
				}
			} else {
				i--;
				result.append('\\');
			}
		} else if (ch == ' ') {
			if (indent >= 0) {
				indent++;
			} else {
				result.append(ch);
			}
		} else if (ch == '\t') {
			if (indent >= 0) {
				indent += TAB_STOP - indent % TAB_STOP;
			} else {
				result.append(ch);
			}
		} else {
			if (indent > 0) {
				result.append("<span style='text-indent:" + indent + "ex'>");
				needEndSpan = true;
			}
			indent = -1;
			if (ch == '&') {
				result.append("&amp;");
			} else if (ch == '<') {
				result.append("&lt;");
			} else if (ch == '>') {
				result.append("&gt;");
			} else {
				result.append(ch);
			}
		}
	}
	if (needEndSpan) {
		result.append("</span>");
	}

}
/**
 * Takes a MacAuthor file from StdIn and outputs the
 * equivalent HTML to StdOut.
 */
private void run() {

	StringBuffer buff = new StringBuffer(1000);

	BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
	BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out));

	outputHeader(w);
	String paraStyle = "";
	StringBuffer outputLine = new StringBuffer(1000);
	while (true) {
		String line;
		try {
			line = r.readLine();
		} catch (IOException e) {
			System.out.println("IOExeption reading i/p");
			line = null;
		}

		if (line == null) {
			break;
		}
		int len = line.length();
		if (isParagraphStyle(line)) {
			paraStyle = getParagraphStyleName(line);
		} else {
			outputLine.setLength(0);
			processLine(line, outputLine);
			try {
				w.write("<p class='" + paraStyle + "'>");
				w.write(outputLine.toString());
				w.write("</p>");
				w.newLine();
				w.flush();
			} catch (IOException e) {
				System.out.println("IOExeption writing o/p");
				e.printStackTrace();
				return;
			}
		}
	}
	outputFooter(w);
	
	Iterator it = styles.iterator();
	System.err.println("** Styles");
	while (it.hasNext()) {
		System.err.println(it.next());
	}
	System.err.println("** End Styles");
			
}
}

