#! /usr/bin/env python

# This software carries the following license:
#
# Modified BSD license
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Steinar Knutsen

"""htmltrans.py 0.1

Generates nested definition lists in HTML based on documents indenting. (Tabs
only.) An empty line ends all lists, a line correctly indented with tabs,
but containing only space will insert a <p> without affecting the list
structure.

The first line of the source document is taken to be a title, the second
line is ignored.

Lines starting with '*' (or with a number of tabs before) is assumed to
an item in an unordered list.

Words surrounded by '_' are assumed to be emphasized.

The code has more than its share of sillinesses.
"""

import re, string

class translator:
	tabcounter = re.compile(r"^(\t*)")

	def __init__(self, plaintext):
		self.plaintext = plaintext
		self.html = []

	def indentone(self, stack, prevline):
		if len(stack) == 0:
			stack.append('<dt>')
			self.html.append('<dl><dt>\n')
			self.html.append(prevline)
			self.html.append('<dd>\n')
			stack.append('<dd>')
		else:
			i = 1
			state = stack[-i]
			while state == '<li>':
				i = i+1
				state = stack[-i]
			if state == '<dt>':
				self.html.append('<dt>\n')
				self.html.append(prevline)
				self.html.append('<dd>\n')
				stack.append('<dd>')
			elif state == '<dd>':
				self.html.append('<dl><dt>\n')
				stack.append('<dt>')
				self.html.append(prevline)
				self.html.append('<dd>\n')
				stack.append('<dd>')


	def outdentone(self, stack, prevline):
		state = stack.pop()
		if state == '<dt>':
			self.html.append(prevline)
			self.html.append('</dl>\n')
			try:
				if stack[-1] == '<dd>':
					stack.pop()
			except:
				pass
		elif state == '<dd>':
			self.html.append(prevline)
		elif state == '<li>':
			self.html.append(prevline)
			self.html.append('</ul>')
			self.outdentone(stack, '')
	
	def emptystack(self, stack):
		for element in stack:
			if element == '<dt>':
				self.html.append('</dl>\n')
			if element == '<li>':
				self.html.append('</ul>\n')
		return []

	def translate(self):
		emphasis = previndentlevel = currindentlevel = 0
		prevline = ''
		stack = []
		self.html.append('<title>')
		self.html.append(self.plaintext[0][:-1])
		self.html.append('</title>\n')
		for line in self.plaintext[2:]:
			currindentlevel = self.tabcounter.match(line).end(1)
			indentdiff = currindentlevel - previndentlevel
			if indentdiff > 0:
				for n in range(indentdiff):
					self.indentone(stack, prevline)
					prevline = ''
			elif indentdiff < 0:
				for n in range(-indentdiff):
					self.outdentone(stack, prevline)
					prevline = ''
			else:
				self.html.append(prevline)

			if line[currindentlevel] == '*':
				if len(stack) == 0 or stack[-1] != '<li>':
					self.html.append('<ul><li>\n')
					line = '\t' * currindentlevel  + line[currindentlevel+1:]
					stack.append('<li>')
				elif stack[-1] == '<li>':
					self.html.append('<li>')
					line = '\t' * currindentlevel  + line[currindentlevel+1:]

			if len(string.strip(line)) == 0:
				if not ' ' in line:
					stack = self.emptystack(stack)
				self.html.append('<p>')

			tmpline = string.split(line, '_')
			tmpline2 = [tmpline[0]]
			for element in tmpline[1:]:
				if emphasis:
					tmpline2.append('</em>')
					tmpline2.append(element)
					emphasis = 0
				else:
					tmpline2.append('<em>')
					tmpline2.append(element)
					emphasis = 1
			line = string.join(tmpline2, '')

			prevline = line
			previndentlevel = currindentlevel
		self.html.append(prevline)
		stack = self.emptystack(stack)
		self.HTML = string.join(self.html, '')
		return self.HTML

if __name__ == '__main__':
	import sys
	data = sys.stdin.readlines()
	x = translator(data)
	sys.stdout.write(x.translate())

