Skip to content

File markdown.py

File List > mkdoxy > markdown.py

Go to the documentation of this file

from typing import List


def escape(s: str) -> str:
    ret = s.replace("*", "\\*")
    ret = ret.replace("_", "\\_")
    ret = ret.replace("<", "&lt;")
    ret = ret.replace(">", "&gt;")
    return ret.replace("|", "\|")


class MdRenderer:
    def __init__(self):
        self.outputoutput = ""
        self.eol_flageol_flag = True

    def write(self, s: str):
        self.outputoutput += s
        self.eol_flageol_flag = False

    def eol(self):
        if not self.eol_flageol_flag:
            self.outputoutput += "\n"
            self.eol_flageol_flag = True


class Md:
    def __init__(self, children: List["Md"]):
        self.childrenchildren = children

    def append(self, child: "Md"):
        self.childrenchildren.append(child)

    def extend(self, child: List["Md"]):
        self.childrenchildren.extend(child)


class Text:
    def __init__(self, text: str):
        self.texttext = text

    def render(self, f: MdRenderer, indent: str):
        if self.texttext:
            f.write(escape(self.texttext))


class Br:
    def __init__(self):
        pass

    def render(self, f: MdRenderer, indent: str):
        f.write("\n\n")


class MdHint(Md):
    def __init__(self, children: List[Md], typ: str, title: str):
        Md.__init__(self, children)
        self.titletitle = title
        self.typtyp = typ

    def render(self, f: MdRenderer, indent: str):
        f.write(f"::: {self.typ} {self.title}" + "\n")
        for child in self.childrenchildren:
            child.render(f, "")
        f.write(":::\n")


class MdBold(Md):
    def __init__(self, children: List[Md]):
        Md.__init__(self, children)

    def render(self, f: MdRenderer, indent: str):
        f.write("**")
        for child in self.childrenchildren:
            child.render(f, "")
        f.write("**")


class MdImage:
    def __init__(self, url: str):
        self.urlurl = url

    def render(self, f: MdRenderer, indent: str):
        f.write(f"![Image]({self.url})")


class Code:
    def __init__(self, text: str):
        self.texttext = text

    def render(self, f: MdRenderer, indent: str):
        f.write(f"`{self.text}`")


class MdCodeBlock:
    def __init__(self, lines: List[str]):
        self.lineslines = lines

    def append(self, line: str):
        self.lineslines.append(line)

    def render(self, f: MdRenderer, indent: str):
        f.write("```\n")
        for line in self.lineslines:
            f.write(line)
            f.write("\n")
        f.write("```\n")


class MdBlockQuote(Md):
    def __init__(self, children: List[Md]):
        Md.__init__(self, children)

    def render(self, f: MdRenderer, indent: str):
        f.write("\n")
        for child in self.childrenchildren:
            f.write("> ")
            child.render(f, "")
            f.write("\n")


class MdItalic(Md):
    def __init__(self, children: List[Md]):
        Md.__init__(self, children)

    def render(self, f: MdRenderer, indent: str):
        f.write("_")
        for child in self.childrenchildren:
            child.render(f, "")
        f.write("_")


class MdParagraph(Md):
    def __init__(self, children: List[Md]):
        Md.__init__(self, children)

    def render(self, f: MdRenderer, indent: str):
        for child in self.childrenchildren:
            child.render(f, indent)
        f.eol()


class MdLink(Md):
    def __init__(self, children: List[Md], url: str):
        Md.__init__(self, children)
        self.urlurl = url

    def render(self, f: MdRenderer, indent: str):
        f.write("[")
        for child in self.childrenchildren:
            child.render(f, "")
        f.write(f"]({self.url})")


class MdList(Md):
    def __init__(self, children: List[Md]):
        Md.__init__(self, children)

    def render(self, f: MdRenderer, indent: str):
        f.eol()
        for child in self.childrenchildren:
            if not isinstance(child, MdList):
                f.write(f"{indent}* ")
            child.render(f, f"{indent}  ")


class MdLine:
    def __init__(self):
        pass

    def render(self, f: MdRenderer, indent: str):
        f.eol()
        f.write("----------------------------------------")
        f.eol()


class MdHeader(Md):
    def __init__(self, level: int, children: List[Md]):
        Md.__init__(self, children)
        self.levellevel = level

    def render(self, f: MdRenderer, indent: str):
        f.write("#" * self.levellevel + " ")
        for child in self.childrenchildren:
            child.render(f, f"{indent}")
        f.write("\n")
        f.eol()


class MdTableCell(Md):
    def __init__(self, children: List[Md]):
        Md.__init__(self, children)

    def render(self, f: MdRenderer, indent: str):
        for child in self.childrenchildren:
            child.render(f, indent)


class MdTableRow(Md):
    def __init__(self, children: List[Md]):
        Md.__init__(self, children)

    def render(self, f: MdRenderer, indent: str):
        f.eol()
        f.write("|")
        for child in self.childrenchildren:
            child.render(f, "")
            f.write("|")
        f.eol()


class MdTable(Md):
    def __init__(self):
        Md.__init__(self, [])

    def render(self, f: MdRenderer, indent: str):
        is_first = True
        f.eol()
        for child in self.childrenchildren:
            child.render(f, "")
            if is_first:
                for _ in range(len(child.children)):
                    f.write("|-----")
                f.write("|")
            is_first = False
        f.write("\n\n")