# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/45_helper.path_accepted_string.ipynb.

# %% auto 0
__all__ = ['CHARACTER_ORDERING_LIST', 'DECORATING_CHARACTERS', 'NONEFFECTIVE_CHARACTERS', 'TO_REMOVE', 'TO_UNDERSCORE',
           'TO_SUBSTITUTE', 'latex_to_path_accepted_string']

# %% ../../nbs/45_helper.path_accepted_string.ipynb 2
import re

from pathvalidate import sanitize_filename

# %% ../../nbs/45_helper.path_accepted_string.ipynb 4
CHARACTER_ORDERING_LIST =\
    ['A', 'a', r'\Alpha', r'\alpha', 'B', 'b', r'\Beta', r'\beta', 'C', 'c', r'\Gamma',
     r'\gamma', 'D', 'd', r'\Delta', r'\delta', 'E', 'e', r'\Epsilon', r'\epsilon',
     'F', 'f', 'G', 'g', 'H', 'h', r'\Eta', r'\eta', 'I', 'i', r'\Iota', r'\iota',
     'J', 'j', 'K', 'k', r'\Kappa', r'\kappa', 'L', 'l', r'\Lambda', r'\lambda', 'M',
     'm', r'\Mu', r'\mu', 'N', 'n', r'\Nu', r'\nu', 'O', 'o', r'\Omicron', r'\omicron'
     'P', 'p', r'\Pi', r'\pi', r'\Phi', r'\phi', r'\Psi', r'\psi', 'Q', 'q', 'R', 'r', 
     r'\Rho', r'\rho', 'S', 's', r'\Sigma', r'\sigma', 'T', 't', r'\Theta', r'\theta',
     r'\Tau', r'\tau', 'U', 'u', r'\Upsilon', r'\upsilon', 'V', 'v', 'W', 'w', r'\Omega', r'\omega',
     'X', 'x', '\Chi', '\chi', 'Y', 'y', 'Z', 'z', '\Zeta', '\zeta', '*', r'\bullet']
DECORATING_CHARACTERS =\
    [r'\tilde', r'\hat', r'\overline', r'\bar', r'\mathscr', r'\mathcal',
     r'\mathfrak', r'\\operatorname', r'\\text', r'\\bf']
NONEFFECTIVE_CHARACTERS =\
    ['^', '_', '{', '}', '(', ')', '[', ']']

# %% ../../nbs/45_helper.path_accepted_string.ipynb 5
TO_REMOVE = [
    '.', '$', ':', '?', '!', '#', '%', '&',
    '<', '>', '*', '?', '"', '@', '`', '|',  
    'mathscr', 'mathbf', 'mathrm', 'mathfrak', 'mathcal', 'mathbb', 'operatorname',
    'boldsymbol', 'bf',
    'text', 'begin', 'end', 'equation' , 'aligned', 'array', 'pmatrix', 'bmatrix',
    'quad', 'longrightarrow', 'rightarrow', 'left', 'right', 'longmapsto', 'mapsto',
    'stackrel']
TO_UNDERSCORE = [' ', '-', '^', '(', ',', '/', '{', '}', '[', ']', '(', ')', '\\', '=',]
TO_SUBSTITUTE = {
    '*': 'star',
    '+': 'plus',
    'leqslant': 'leq',
    'geqslant': 'geq',
    '\'': '_prime'
}

# TODO: make a universal latex to path string; it seems that latex.convert
# might do something different when naming files.

def latex_to_path_accepted_string(latex: str) -> str:
    """Convert a latex string to a path accepted string
    """
    for to_underscore in TO_UNDERSCORE:
        latex, _ = re.subn(re.escape(to_underscore), '_', latex)
    for symbol_to_substitute, substitute_with in TO_SUBSTITUTE.items():
        latex, _ = re.subn(re.escape(symbol_to_substitute), substitute_with, latex)
    for to_remove in TO_REMOVE:
        latex, _ = re.subn(re.escape(to_remove), '', latex)
    latex, _ = re.subn('_+', '_', latex)
    latex, _ = re.subn('^_', '', latex)
    latex, _ = re.subn('_$', '', latex)
    latex = sanitize_filename(latex)
    return latex
