

			
def funCreateHtmlSurah(surahDict):
	verses= surahDict['verses']
	wordCount = 0
	replace_array_str = ""
	for v, verse in enumerate(verses):
		
		text = verse['text'].upper()
		text = text.replace("\"", "");
		words = text.split(' ')
		wordstr = ",".join(words);
		replace_array_str += wordstr;
		
	replace_array_str = "\"" + replace_array_str[0: len(replace_array_str)-1 ]	 + "\""
	replace_array_str   = replace_array_str.replace(",", "\", \"")
	replace_array_str   = replace_array_str.replace(  "."  , "")

	replace_array_str   = replace_array_str.replace(  "\"\","  , "")
	
	htmltemplate = funReadFileString("template12.html");

	htmltemplate = htmltemplate.replace('REPLACE_ARRAY_HERE', replace_array_str)
	htmltemplate = htmltemplate.replace('REPLACE_SURAH_HERE', str(surahDict['surah']) )


	with open('html/Quran Memorization Surah ' + str(surahDict['surah']) + '.html', 'w') as outfile:  
		outfile.write(htmltemplate)

	
    
def funReadFileString(fileName):
	read_data = ""
	#encoding otherwise will get ufeff1 when converting to int
	with open(fileName, encoding='utf-8-sig') as f:
		read_data = f.read()
	return read_data
	
def funDictRow(line):
	columns =line.split("|")	
	dictrow ={'surah':0,'verse':0,'text':''}
	if len(columns) == 3:
		dictrow['surah']= int(columns[0].strip())
		dictrow['verse'] = int(columns[1])
		dictrow['text'] = columns[2]
		dictrow['numWords'] = len(columns[2].split(' '))
	return dictrow

def funCreateRecordList(fileContents):
	#x: 1|1|En el nombre de Alá, el Compasivo, el Misericordioso! 
	#restructure data into json
	# {surah, numVerses, verses[{verse,text,lenVerse}]}
	returnRecordList =[]
	#all parent entries in json are dicts
	surahList = {}
	currentSurah = 0
	lines = fileContents.split("\n")
	for line in lines:
		srRow = funDictRow(line)
		if srRow['surah'] == 0:
			continue #pass this iteration , this is for the end of record balnk lines
		if srRow['surah'] != currentSurah:
			#create new surah dict record, with array of verses
			currentSurah = srRow['surah']
			#clear makes the dict immutable so not use ?
			surahList = {}
			surahList['surah'] = srRow['surah']
			surahList['numVerses'] = 1
			surahList['numWords'] = srRow['numWords']
			surahList['verses'] = []
			#lets delete surah key before appending
			srRow.pop('surah')
			surahList['verses'].append(srRow)
			returnRecordList.append(surahList)
		else:
			srRow.pop('surah')
			surahList['verses'].append(srRow)
			surahList['numVerses'] = surahList['numVerses']  + 1
			surahList['numWords'] = surahList['numWords']  + srRow['numWords']
	return returnRecordList

    		    		    		    		    		    		
if __name__ == '__main__':
	recordList = funCreateRecordList(\
		funReadFileString("English-Ahmed-Ali-100.csv"))
		
	#should be a record of dicts of dicts
	for k in recordList:
		funCreateHtmlSurah(k)
	