# -*- coding: utf-8 -*-

__author__ = 'stevenkaplan'
from sources.functions import *
from sefaria.model import *
from parsing_utilities.XML_to_JaggedArray import XML_to_JaggedArray
from sefaria.helper.schema import *
import bleach
import os
def reorder_modify(text):
    return bleach.clean(text, strip=True)

post_info = {}
post_info["language"] = "en"
post_info["server"] = "http://ste.sandbox.sefaria.org"
allowed_tags = ["book", "intro", "bibl", "part", "p", "ftnote", "img", "title", "ol", "footnotes", "appendix", "h1"]
allowed_attributes = ["id", "src"]
p = re.compile("\d+a?\.")

files = [f for f in os.listdir(".") if f.endswith(".xml")]
for f in files:
    post_info["versionTitle"] = "Commentary on the Torah by Ramban (Nachmanides). Translated and annotated by Charles B. Chavel. New York, Shilo Pub. House, 1971-1976"
    post_info["versionSource"] = "https://www.nli.org.il/he/books/NNL_ALEPH002108945/NLI"
    title = re.search("Ramban\-(.*?)\.xml", f).group(1)
    title = "Ramban on {}".format(title)
    versionInfo = [["Index Title", title],
                   ["Version Title",
                    "Commentary on the Torah by Ramban (Nachmanides). Translated and annotated by Charles B. Chavel. New York, Shilo Pub. House, 1971-1976"],
                   ["Language", "he"],
                   ["Version Source", "https://www.nli.org.il/he/books/NNL_ALEPH002108945/NLI"],
                   ["Version Notes", ""]]
    parser = XML_to_JaggedArray(title, f, allowed_tags, allowed_attributes, post_info, change_name=True, image_dir="./images",
                                titled=True, print_bool=True, remove_chapter=False, versionInfo=versionInfo)
    parser.set_funcs(reorder_test=lambda x: x.tag == "h1", reorder_modify=reorder_modify)
    try:
        parser.run()
    except:
        print(f)
        print("PROBLEM")
