import re
import klammer_base

class Subsuper(klammer_base.Klammer_base):
    def __init__(self, K, position):
        super().__init__(K)
        self.position = position
        """
        print("Subsuper:", self.args)

        parts = self.args.strip().split()
        if len(parts) != 2:
            raise Exception(f"Two arguments required, but argument is: {self.args.strip()}")
        self.base, self.script = parts
        """

    def html(self):
        return f"{self.base}<{self.position}>{self.script}</sub>"

    def tex(self):
        if self.position == "sub":
            return f"${self.base}_{self.script}$"
        else:
            return f"${self.base}^{self.script}$" 

def html_fontify(text, font_symbol, font_size):
    cls = {"r" : "",
           "i" :"ritalic",
           "b" : "bold",
           "s" : "sansserif",
           "t" : "monospace",
           "c" : "code"}[font_symbol]
    result = text
    if cls:
        if font_symbol == "c":
            result = re.sub(" ", "&#160;", result)
        cls = f'class="{cls}"'
    sty = ""
    if font_size != 1:
        sty = f'style="font-size: {float(font_size) * 100}%"'
    attr = (cls + " " + sty).strip()
    if attr:
        result = f'<span {attr}>{result}</span>'
    return result


def tex_fontify(text, font_symbol, font_size):
    command = {"r" : "\\rmfamily",
               "i" :"\\it",
               "b" : "\\bfseries",
               "s" : "\\sffamily",
               "t" : "\\ttfamily",
               "c" : "\\ttfamily"}[font_symbol]
    result = f"{{{command}\\scalefont{{{font_size}}}{{{text}}}}}"
    return result
