#! /usr/bin/env python3.3
import sys
texf=open("./doc/g10_prof_report.tex")
outputf=open("./doc/g10_lab09_report.html",'w')
sys.stdout=outputf
texflines=[(i.split('\n'))[0] for i in texf.readlines()]
#print(repr(texflines))
ignorefunc=['\documentclass','\\usepackage','\date','\maketitle']
titlefunc='\title'
authorfunc='\author'
textttfunc='\texttt'
sample="\section {hi this is awesome }\\"
leftparen=0
rightparen=0
sectioncount=0

#\\
def slashfunc(strg):
	global leftparen, rightparen
	ind=strg.find("\\")
	if(ind != -1):
		leftparen+=1
		rightparen+=1
		return "<br>"
	else:
		return ""
		
#\title
def titlefunc(strg):
	global leftparen, rightparen
	ind=strg.find('\title')
	if(ind != -1):
		leftparen+=1
		rightparen+=1
		return '<h1 align="center"><b>'+strg[1+strg.find('{'):strg.find('}')]+"</b></h1>"
	else:
		return ""
#\section
def sectionfunc(strg):
	global leftparen, rightparen, sectioncount
	ind=strg.find('\section')
	if(ind != -1):
		sectioncount+=1
		leftparen+=1
		rightparen+=1
		return "<h1><b>"+strg[1+strg.find('{'):strg.find('}')]+"</b></h1>"
	else:
		return ""

#\sunsection
def sectionfunc(strg):
	global leftparen, rightparen
	ind=strg.find('\subsection')
	if(ind != -1):
		leftparen+=1
		rightparen+=1
		return "<h1><b>"+strg[1+strg.find('{'):strg.find('}')]+"</b></h1>"
	else:
		return ""
#\textbf
def textbffunc(strg):
	global leftparen, rightparen
	ind=strg.find('\textbf')
	if(ind != -1):
		leftparen+=1
		rightparen+=1
		return "<h3><b>"+strg[1+strg.find('{'):strg.find('}')]+"</b></h3>"+slashfunc(strg)
	else:
		return ""

#\includegraphics
def graphfunc(strg):
	global leftparen, rightparen
	ind=strg.find('\includegraphics')
	if(ind != -1):
		leftparen+=1
		rightparen+=1
		return '<img src="'+'./data/'+strg[1+strg.find('{'):strg.find('}')]+'.png" align="center">'
	else:
		return ""
#\texttt
def textttfunc(strg):
	global leftparen, rightparen
	ind=strg.find('\texttt')
	if(ind != -1):
		leftparen+=1
		rightparen+=strg.count('}')
		return "<h3>"+strg[1+strg.find('{'):strg.find('}')]+"</h3>"+slashfunc(strg)
	else:
		return ""
#\author
authorsyntax=0;
def authorfunc(strg):
	global leftparen
	ind=strg.find('\author')
	if(ind != -1):
		leftparen+=1
		authorsyntax=1;
		return "<h3>"+strg[1+strg.find('{'):strg.find('\\')]+"</h3>"
	else:
		return ""

def nobracfunc(strg):
	ind1=strg.find('{')
	ind2=strg.find('}')
	ind3=strg.find('\\')
	if(ind1 == -1 and ind2 == -1):
		if(ind3==-1):
			ind3=len(strg)
			return "<p>"+strg[0:len(strg)]+"</p>"
		else:
			return "<p>"+strg[0:ind3]+"</p>"
	else:
		return ""
		
def syntfunc():
	global leftparen, rightparen, authorfunc
	if(leftparen==rightparen):
		authorsyntax=0;
	return 0
	
#print(sectionfunc(sample))
#print(textbffunc(sample))
#print(slashfunc(sample))
print("<html>")
print("<head>")
print("<title>g10_gen_html.py</title>")
print("</head>")
print("<body>")
for hline in texflines:
	if(sectioncount==3):
		break
	lineformat=""
	if(sectionfunc(hline)!=""):
		lineformat=sectionfunc(hline)
	elif(textbffunc(hline)!=""):
		lineformat=textbffunc(hline)
	#elif(slashfunc(hline)!=""):
	#	lineformat=slashfunc(hline)
	elif(graphfunc(hline)!=""):
		lineformat=graphfunc(hline)
	elif(textttfunc(hline)!=""):
		lineformat=textttfunc(hline)
	elif(authorfunc(hline)!=""):
		lineformat=authorfunc(hline)
	elif(titlefunc(hline)!=""):
		lineformat=titlefunc(hline)
	elif(nobracfunc(hline)!=""):
		lineformat=nobracfunc(hline)
	else:
		lineformat=""		
	garbage=syntfunc()
	if(authorsyntax==1):
		print('<p align="center">'+lineformat+"</p>")
	else:
		print(lineformat)
print("</body>")
print("</html>")
outputf.close()
