From 1e96a1adcad10909902d23cbf9661d7372a9c3d1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?=
 <benjamin.haettasch@fachschaft.informatik.tu-darmstadt.de>
Date: Sat, 30 Jan 2021 14:53:07 +0100
Subject: [PATCH] Add and adapt django-tex dependency

Load dependency
Use custom escaping environment
Adapt installation guidelines
---
 AKModel/environment.py | 26 ++++++++++++++++++++++++++
 AKPlanning/settings.py | 11 +++++++++++
 INSTALL.md             |  1 +
 requirements.txt       |  1 +
 4 files changed, 39 insertions(+)
 create mode 100644 AKModel/environment.py

diff --git a/AKModel/environment.py b/AKModel/environment.py
new file mode 100644
index 00000000..5883361b
--- /dev/null
+++ b/AKModel/environment.py
@@ -0,0 +1,26 @@
+# environment.py
+import re
+
+from django_tex.environment import environment
+
+# Used to filter all very special UTF-8 chars that are probably not contained in the LaTeX fonts
+# and would hence cause compilation errors
+utf8_replace_pattern = re.compile(u'[^\u0000-\u206F]', re.UNICODE)
+
+def latex_escape_utf8(value):
+    """
+    Escape latex special chars and remove invalid utf-8 values
+
+    :param value: string to escape
+    :type value: str
+    :return: escaped string
+    :rtype: str
+    """
+    return utf8_replace_pattern.sub('', value).replace('&', '\&').replace('_', '\_').replace('#', '\#').replace('$', '\$').replace('%', '\%').replace('{', '\{').replace('}', '\}')
+
+def improved_tex_environment(**options):
+    env = environment(**options)
+    env.filters.update({
+        'latex_escape_utf8': latex_escape_utf8,
+    })
+    return env
diff --git a/AKPlanning/settings.py b/AKPlanning/settings.py
index 0dbfeb5c..a383072c 100644
--- a/AKPlanning/settings.py
+++ b/AKPlanning/settings.py
@@ -51,6 +51,7 @@ INSTALLED_APPS = [
     'rest_framework',
     'simple_history',
     'registration',
+    'django_tex',
 ]
 
 MIDDLEWARE = [
@@ -84,6 +85,14 @@ TEMPLATES = [
             ],
         },
     },
+    {
+        'NAME': 'tex',
+        'BACKEND': 'django_tex.engine.TeXEngine',
+        'APP_DIRS': True,
+        'OPTIONS': {
+            'environment': 'AKModel.environment.improved_tex_environment',
+        }
+    },
 ]
 
 WSGI_APPLICATION = 'AKPlanning.wsgi.application'
@@ -136,6 +145,8 @@ LANGUAGES = [
 
 INTERNAL_IPS = ['127.0.0.1', '::1']
 
+LATEX_INTERPRETER = 'pdflatex'
+
 # Static files (CSS, JavaScript, Images)
 # https://docs.djangoproject.com/en/2.2/howto/static-files/
 
diff --git a/INSTALL.md b/INSTALL.md
index 040a5aa8..54a002e3 100644
--- a/INSTALL.md
+++ b/INSTALL.md
@@ -12,6 +12,7 @@ AKPlanning has two types of requirements: System requirements are dependent on o
 
 * Python 3.7 incl. development tools
 * Virtualenv
+* pdflatex & beamer class (`texlive-latex-base texlive-latex-recommended texlive-latex-extra texlive-fonts-extra`)
 * for production using uwsgi:
   * C compiler e.g. gcc
   * uwsgi
diff --git a/requirements.txt b/requirements.txt
index e843156c..10b706f0 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -7,4 +7,5 @@ djangorestframework==3.12.2
 django-simple-history==2.12.0
 django-registration-redux==2.9
 django-debug-toolbar==3.2
+django-tex==1.1.8.post1
 mysqlclient==2.0.3  # for production deployment
-- 
GitLab