def _ascci_env(): env = Environment( loader=CleanLoader(os.path.dirname(__file__)), lstrip_blocks=True, trim_blocks=True, undefined=StrictUndefined, ) env.globals["len"] = len env.globals["unreachable"] = unreachable try: c = converter() def math(s): assert isinstance(s, list) for x in s: assert isinstance(x, str) res = [c.convert(_) for _ in s] print(res) return res env.globals["math"] = math except ImportError: def math(s): return s + "($pip install flatlatex for unicode math)" env.globals["math"] = math template = env.get_template("ascii.tpl.j2") return env, template
def render_Directive(self, d): cont = "".join(d.value) if d.role == "math": assert False from flatlatex import converter c = converter() return ("math", c.convert(cont)) return ("directive", f"{d.domain}:{d.role}:`{cont}`")
def render_BlockDirective(self, directive): if directive.directive_name == "note": return urwid.Padding( urwid.LineBox(self.render(directive.inner), title="Note", title_align="left"), left=2, right=2, ) elif directive.directive_name == "math": assert False args0 = [a for a in directive.args0 if a] inner = directive.inner content = " ".join(directive.args0) if content: if inner: assert len(inner.children) == 1 content = content + inner.children[0].value else: assert len(inner.children) == 1 content = inner.children[0].value from flatlatex import converter c = converter() return urwid.Padding(urwid.Text(("math", c.convert(content))), left=2) inn = [ blank, Text([("param", ".. " + directive.directive_name + "::")] + directive.args0), ] if directive.inner: inn.append(urwid.Padding(self.render(directive.inner), left=4)), return urwid.Pile(inn)
#!/usr/bin/env python3 import flatlatex, string c = flatlatex.converter() template = ' ("%s" . ?%s)\n' with open('prettify-latex-autogen.el', 'w') as f: f.write("(defvar prettify-mode--latex-autogen-alist '(\n") # Letters with special fonts and their {sub,super}scripts for l in string.ascii_letters: for kw in ('mathbb', 'mathcal', 'mathbf'): f.write(template % (r"\\%s{%s}" % (kw, l), c.convert(r'\%s{%s}' % (kw, l)))) # for m in ('^', '_'): # maybe = c.convert(f".{m}{l}")[1] # if maybe not in (m, '['): # f.write(template % (f"{m}{l}", maybe)) # Numeric {sub,super}scripts # for i in range(10): # for m in ('^', '_'): # f.write(template % (f"{m}{i}", c.convert(f".{m}{i}")[1])) f.write(r"))")
def render_BlockMath(self, math): from flatlatex import converter c = converter() return urwid.Padding(urwid.Text(("math", c.convert(math.value))), left=2)
def render_Math(self, d): cont = "".join(d.value) from flatlatex import converter c = converter() return ("math", c.convert(cont))