Aoi-hosizora/markdown-gen

KaTeX render helper

Aoi-hosizora opened this issue · 2 comments

https://www.codecogs.com/latex/eqneditor.php

%5Cdpi%7B120%7D%20%5Cbg_white%20%5Clarge%20%5Cbegin%7Baligned%7D%20y%27_x%20%26%3D%20%5Cbegin%7Bcases%7D%20%5Cfrac%7B%5Cpartial%20y%7D%7B%5Cpartial%20x%7D%20%26%5Ctext%7Bif%20%7D%201%20%5C%5C%20%5Cfrac%7B%5Ctext%7Bd%7D%20y%7D%7B%5Ctext%7Bd%7D%20x%7D%20%26%5Ctext%7Bother%7D%20%5Cend%7Bcases%7D%20%5C%5C%20%5C%3B%5C%5C%20X%20%26%3D%20%5Cbegin%7Bpmatrix%7D%20x_%7B11%7D%20%26%20x_%7B12%7D%20%26%20%5Cdots%20%26%20x_%7B1n%7D%20%5C%5C%20x_%7B21%7D%20%26%20x_%7B22%7D%20%26%20%5Cdots%20%26%20x_%7B2n%7D%20%5C%5C%20%5Cvdots%20%26%20%5Cvdots%20%26%20%26%20%5Cvdots%20%5C%5C%20x_%7Bm1%7D%20%26%20x_%7Bm2%7D%20%26%20%5Cdots%20%26%20x_%7Bmn%7D%20%5C%5C%20%5Cend%7Bpmatrix%7D%20%5C%5C%20%5C%3B%5C%5C%20Z%20%26%3D%20%5C%3B%3F%20%5Cend%7Baligned%7D
\dpi{120} \bg_white \large \begin{aligned} y'_x %26%3D \begin{cases} \frac{\partial y}{\partial x} %26\text{if } 1 \\ \frac{\text{d} y}{\text{d} x} %26\text{other} \end{cases} \\ \%3B\\ X %26%3D \begin{pmatrix} x_{11} %26 x_{12} %26 \dots %26 x_{1n} \\ x_{21} %26 x_{22} %26 \dots %26 x_{2n} \\ \vdots %26 \vdots %26 %26 \vdots \\ x_{m1} %26 x_{m2} %26 \dots %26 x_{mn} \\ \end{pmatrix} \\ \%3B\\ Z %26%3D \%3B%3F \end{aligned}
\begin{aligned}

y'_x &= \begin{cases}
\frac{\partial y}{\partial x} &\text{if } 1 \\
\frac{\text{d} y}{\text{d} x} &\text{other}
\end{cases} \\

\;\\

X &= \begin{pmatrix}
x_{11} & x_{12} & \dots & x_{1n} \\
x_{21} & x_{22} & \dots & x_{2n} \\
\vdots & \vdots &       & \vdots \\
x_{m1} & x_{m2} & \dots & x_{mn} \\
\end{pmatrix} \\

\;\\

Z &= \;?

\end{aligned}

image

class KatexImageOption:
    """
    Option for --katex_image
    """

    def __init__(self, do_option: bool):
        self.do_option: bool = not not do_option

    def parse(self, content: str) -> (str, str):
        def sub_parse(content: str, *, is_block: bool = False) -> str:
            # alt
            alt_katex = katex.strip(" \t\n$").replace('\n', ' ')
            # src
            new_katex = alt_katex
            new_katex = new_katex.replace('\\R', '\\mathbb{R}').replace('\\N', '\\mathbb{N}').replace('\\Z', '\\mathbb{Z}').replace('\\C', '\\mathbb{C}')
            new_katex = new_katex.replace('\\llbracket', '\u27E6').replace('\\rrbracket', '\u27E7')
            new_katex = new_katex.replace('\\argmax', '\\arg\\!\\!\\max').replace('\\argmin', '\\arg\\!\\!\\min')
            if new_katex.count('\\\\') != 0:
                new_katex = '\\begin{gathered} %s \\end{gathered}' % new_katex
            new_katex = ('\\large ' if is_block else '') + new_katex
            new_katex = urllib.parse.quote(new_katex)

            return alt_katex, new_katex

        if self.do_option:
            # style
            block_css = 'background-color: #FFFFFF; padding: 6px 0 3px 0; margin: 9px 0'
            inline_css = 'background-color: #FFFFFF; padding: 3px 3px -2px 3px; display: inline-block;'

            # block
            equation_block_re = re.compile(r'((?<!\\)\$\$(?:.+?)(?<!\\)\$\$)', re.DOTALL)
            old_katexes = equation_block_re.findall(content)
            new_katexes = [katex for katex in old_katexes]
            for idx, katex in enumerate(new_katexes):
                alt_katex, new_katex = sub_parse(katex, is_block=True)
                new_katexes[idx] = '<div align="center" style="{}"><image alt="{}" src="https://math.now.sh?from={}" /></div>'.format(block_css, alt_katex, new_katex)
            for idx, old_katex in enumerate(old_katexes):
                content = content.replace(old_katex, new_katexes[idx])

            # inline
            equation_block_re = re.compile(r'((?<!\\)\$.+?(?<!\\)\$)')
            old_katexes = equation_block_re.findall(content)
            new_katexes = [katex for katex in old_katexes]
            for idx, katex in enumerate(new_katexes):
                alt_katex, new_katex = sub_parse(katex, is_block=False)
                new_katexes[idx] = '<span style="{}"><image alt="{}" src="https://math.now.sh?inline={}" /></span>'.format(inline_css, alt_katex, new_katex)
            for idx, old_katex in enumerate(old_katexes):
                content = content.replace(old_katex, new_katexes[idx])

        return content