#!/usr/bin/env python
import string
#import pdb
import pprint
import xmllib
import sys
SIPB = { 
	"start_sipb" : """<html>""",
	"end_sipb" : """</html>""",
	"start_footnote" : "<sup>",
	"end_footnote" : "</sup>",
	"start_title" : "<head><title>",
	"end_title" : "</title></head>"
	
}






class Parser(xmllib.XMLParser):
	# 


	def __init__(self, file=None):
		xmllib.XMLParser.__init__(self)
		self.__data = []
		self.footnotes = []
		self.__out = ""

		if file:
			self.load(file)

	def load(self,file): 
		while 1: 
			s=file.read(512)
			if not s: 
				break
			self.feed(s)
		self.close()

	def start_sipb(self,attrs): 
		self.__data.append(SIPB["start_sipb"])

	def start_title(self,attrs): 
		self.__data.append(SIPB["start_title"])

	def end_title(self):
		line = self.__data.pop()
		if sys.argv[3] == 'title' :
			print line
		line = self.__data.pop() +line
		self.__data.append(line +SIPB["end_title"])


	def start_footnote(self,attrs): 
		self.__data.append(SIPB["start_footnote"])

	def end_footnote(self): 
		ft = self.__data.pop()
		ft2 = self.__data.pop()
		self.footnotes.append(ft)
		ftl = repr(len(self.footnotes))
		self.__data.append(ft2+'<a href="#foot_'+ftl+'" name="back_'+ftl+'">'+ftl+'</a>'+SIPB["end_footnote"]  )

	def unknown_starttag(self,tag,attrs):
		self.__data.append("<"+tag+">")

	def unknown_endtag(self,tag):
		line = self.__data.pop()
		if tag == 'title' : 
			print line
		line = self.__data.pop() +line
		self.__data.append(line + "</"+tag+">")

	def start_link(self,attrs) :
		# Got to make several transformations 
		# here. 
		if attrs.get("href"): 
			self.__data.append('<a href="'+attrs.get("href")+'">')

	def end_link(self): 
		self.__data.append("</a>")

	def handle_data(self,data):
		self.__data.append(data)

	def end_sipb(self): 
		if self.footnotes: 
			self.__data.append("<hr />")
			for f in range(len(self.footnotes)): 
				self.__data.append('<a name="foot_'+repr(f+1)+'" href="back_' +repr(f+1) +'">'+repr(f+1)+':'+self.footnotes[f]+'</a>' )
		
		self.__data.append(SIPB["end_sipb"])
		self.outgo =  string.join(self.__data,"")

try: 
	p = Parser()
	p.load(open(sys.argv[1]))
	open(sys.argv[2],'w').write(p.outgo)

except EOFError: 
	pass
