From c191669089061a5082dba997ca43d1db233c1b72 Mon Sep 17 00:00:00 2001 From: jwansek Date: Wed, 30 Mar 2022 17:53:21 +0100 Subject: added python latex escaping --- jinja_helpers.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'jinja_helpers.py') diff --git a/jinja_helpers.py b/jinja_helpers.py index 7f51881..a9e0e2e 100644 --- a/jinja_helpers.py +++ b/jinja_helpers.py @@ -91,6 +91,26 @@ def len_(obj): def get_source_numlines(source): return "%d lines (%d characters)" % (source.count("\n"), len(source)) +#https://stackoverflow.com/questions/16259923/how-can-i-escape-latex-special-characters-inside-django-templates +def tex_escape(text): + conv = { + '&': r'\&', + '%': r'\%', + '$': r'\$', + '#': r'\#', + '_': r'\_', + '{': r'\{', + '}': r'\}', + '~': r'\textasciitilde{}', + '^': r'\^{}', + '\\': r'\textbackslash{}', + '<': r'\textless{}', + '>': r'\textgreater{}', + } + regex = re.compile('|'.join(re.escape(str(key)) for key in sorted(conv.keys(), key = lambda item: - len(item)))) + # print(text, regex.sub(lambda match: conv[match.group()], text)) + return regex.sub(lambda match: conv[match.group()], text) + def _get_helpers(): import reflect import os -- cgit v1.2.3