# qbq2latex Created by Dan Schafer & Evan Silberman, 2006,
# Part of the ItALX project.

# Version 1.0 - Initial Release

"""
This file is part of the ItALX Project.

ItALX is free software; you can redistribute it and/or modify
it under the terms of the GNU Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

ItALX is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have recieved a copy of the GNU General Public License
along with ItALX; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""

import os
import re
import sys

# This function should parse an answer and replace _text_ with \req{text}
def parseUnderscores(text):
	return re.sub("_([^_]*)_","\\\\req{\\1}",text)

# This function should parse a question and replace [foo] with \pguide{foo}
def parsePGuide(text):
	return re.sub("\[([^\]\[]*)\]","\\pguide{\\1}",text)

# This function should parse a question and replace (*) with \powermark
def parsePowerMark(text):
	return re.sub("(*)","\\powermark",text)

def parseAll(text):
	return parsePGuide(parseUnderscores(parsePowerMark(text)))

def convertQuestionText(lines):
	output = ""
	currText = ""
	currState = 0
	bonusInProg = 0
	#State info: 0=Default, 1=Tossup, 2=TossAns, 3=Bonus, 4=BonusQuest, 5=BonusAns, 6=List
	for nextLine in lines:
		if len(nextLine) == 0:
			output += parseAll(currText)
			currText = ""
			if currState != 6:
				output += "}\n"
			else:
				output += "\n"
			continue
		if nextLine[0] == "%":
			continue
		if "TOSSUP" in nextLine:
			if bonusInProg == 1: #if this isn't the first tossup of the file:
				output += "\\end{bonus}\n\n"
				bonusInProg = 0
			currState = 1
			output += "\\tossup{"
			continue
		if ": " in nextLine and currState == 1:
			currState = 2
			output += "{"
			nextLine = nextLine.split(": ")[1]
		if "BONUS" in nextLine:
			if bonusInProg == 1: #if this isn't the first tossup of the file:
				output += "\\end{bonus}\n\n"
			currState = 3
			output += "\\begin{bonus}{"
			continue
		if "A: " in nextLine and currState == 4:
			currState = 5
			output += "\\bonusanswer{"
			nextLine = nextLine[nextLine.find(":")+2:]
		if ": " in nextLine and (currState == 3 or currState == 4 or currState == 5):
			currState = 4
			output += "\\bonusquestion{"+parseAll(nextLine[:nextLine.find(":")])+"}{"
			nextLine = nextLine[nextLine.find(":")+2:]
		if "LIST" in nextLine and currState == 3:
			currState = 6
			continue

		if currState == 6:
			output += "\listitem{"+parseAll(nextLine)+"}\n";
		else:
			currText += nextLine;
			
	output += "\\end{bonus}\n\n"

	return output
	

def startConvert(readname="",writename=""):
	if readname == "":
		readname = raw_input("Filename: ")
	file = open(readname, "r")
	lines = file.readlines()
	file.close()
	newLines = [i[:-1] for i in lines]
	if writename == "":
		print convertQuestionText(newLines)
	else:
		file = open(writename, "w")
		file.write(convertQuestionText(newLines))
		file.close()
	
def main():
	choice = -1
	if len(sys.argv) == 2:
		startConvert(sys.argv[1])
		return
	if len(sys.argv) >= 3:
		startConvert(sys.argv[1],sys.argv[2])
		return
	while choice != 2:
		#os.system("clear")
		print "Main Menu:"
		print "  1. Convert questions to ItALX"
		print "  2. Exit"
		choice = input("?")
		if choice == 1:
			startConvert();

if (__name__ == "__main__"):
	main()

