# unfinished experimental function that was supposed to be a part of the equivalence "comparison" test between different academic databases
# for cleaning titles with obviously stupid things in them to test if they WOULD be the same without them

import re, sys, argparse

# Functions to make title consistent between platforms
faceMarkupTags = [
    "b",
    "i",
    "u",
    "ovl",
    "sup",
    "sub",
    "scp",
    "tt"
]

mathMLTag = "mml"

tagRegex = "[<]\/?[^ ]+?[>]"

def clear_face_markup(title):
    # Get rid of all facemarkup tags
    regexTitle = title
    for tagString in faceMarkupTags:
        newString = "<" + tagString + ">"
        regexTitle = regexTitle.replace(newString, "")
        newString = "</" + tagString + ">"
        regexTitle = regexTitle.replace(newString, "")

    print("regex done: " + regexTitle)
    return regexTitle

def clear_math_ml_tags(title):
    # find all regex matches and remove mathml tags
    regexTitle = title
    regexMatches = re.findall(tagRegex, regexTitle)
    for match in regexMatches:
        if match.find(mathMLTag) != -1:
            regexTitle = regexTitle.replace(match, "")
    return regexTitle

def clear_tags(title):
    facemarkupRemovedTitle = clear_face_markup(title)
    return clear_math_ml_tags(facemarkupRemovedTitle)

def add_subtitles():
    return "Title with added subtitles"

# commandline interface for checking things
def main():
    parser = argparse.ArgumentParser(
        prog="sanitise",
        description="sanitises title input for consistent output"
    )
    parser.add_argument("title", help="Title of the article")
    parser.add_argument("-s", "--subtitle", help="Subtitle (if one provided)")
    args = parser.parse_args()

    print(args)

    tagClearTitle = clear_tags(args.title)

    print(tagClearTitle)
    

if __name__ == "__main__":
    main()
