summaryrefslogtreecommitdiffstats
path: root/jinja_helpers.py
diff options
context:
space:
mode:
authorjwansek <eddie.atten.ea29@gmail.com>2022-03-30 17:53:21 +0100
committerjwansek <eddie.atten.ea29@gmail.com>2022-03-30 17:53:21 +0100
commitc191669089061a5082dba997ca43d1db233c1b72 (patch)
tree67abb8e42ea19623216b2f5628d108d50f78d54b /jinja_helpers.py
parent524fa81d71d335a04ab3610c39c5c8e74eb732a4 (diff)
downloadSmarker-c191669089061a5082dba997ca43d1db233c1b72.tar.gz
Smarker-c191669089061a5082dba997ca43d1db233c1b72.zip
added python latex escaping
Diffstat (limited to 'jinja_helpers.py')
-rw-r--r--jinja_helpers.py20
1 files changed, 20 insertions, 0 deletions
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