import xml.etree.ElementTree as ET

"""
elemToString
This takes in content, a node, and returns the inner text
"""
def elemToString(content):
    return ET.tostring(content, encoding='utf8', method='text')

"""
cleanElemToString
This takes in content, a node, and returns the inner text with only one space between
words and no line breaks
"""
def cleanElemToString(content):
    string = elemToString(content)
    return filter(lambda x: x != "\n", string).replace("  ", "")

def stageDirElem(content):
    string = ""
    for children in content.findall("./*"):
        if children.tag == "{http://www.tei-c.org/ns/1.0}lb":
            string += "\n<br>\n"
        else:
            toString = ET.tostring(children, encoding='utf8', method='text')
            string += filter(lambda x: x != "\n", toString).replace("  ", "")
    return string

def printSingleLine(line, targetFile):
    targetFile.write(filter(lambda x: x != "\n", line).replace("  ", ""))

"""
stageDirInLine 
This gets the stage directions in the middle of a line and writes them to our file.
This takes in content, a stage directions XML node,  and a targetFile, the file object with write privileges.
"""
def stageDirInLine(content, targetFile):
    xmlstr = stageDirElem(content)
    targetFile.write("<i>%s</i>" % xmlstr)
"""
printSingleLine
This writes a string to file after removing extra spaces and all line breaks
This takes in line, a string, and targetFile, a file object with write privileges.
"""
def printSingleLine(line, targetFile):
    targetFile.write(filter(lambda x: x != "\n", line).replace("  ", ""))


"""
speaker
This writes the speaker's name to file and returns it to use as the key for the dictionary.
This takes in content, a speaker node, and a targetFile, a file object with write privileges.
"""
def speaker(content, targetFile):
        xmlstr = cleanElemToString(content)
        targetFile.write('\n<br>\n<span class = "character">%s</span> '% xmlstr)
        return xmlstr

def writeFormatting(className):
    formatting.write("\n$('.%s').on('click', function(e){\n" % className)
    formatting.write("e.preventDefault();\n")
    formatting.write("$('.%s').toggleClass('strikethrough');\n});\n" % className)

"""
getLines
This will write all the lines that one character speaks and the in-line stage directions to a file.
It takes in content, a node with tag 'ab', and a targetFile, a file object with write privilege.
"""
def getLines(content, targetFile):
    line = ""
    numLines = 0
    listOfSD = []
    for words in content.findall("./*"):
        # If the child is a milestone, it prints out the previous line, the next line number, and resets
        if ((words.tag == "{http://www.tei-c.org/ns/1.0}milestone") and (words.get('unit') == "ftln")):
            numLines += 1
            printSingleLine(line, targetFile)
            if numLines > 0:
                targetFile.write("</span>")
            targetFile.write('\n<br>\n<span class="lineNum">%s</span>' % words.get('n')[4:])
            targetFile.write('<span class = "%s">' % words.get('n').replace(".", "-"))