From 3e4ebb72bded8f48b888b5952a5661662b59bbb4 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 b6745ea1..f01056ac 100644
--- a/AKPlanning/settings.py
+++ b/AKPlanning/settings.py
@@ -52,6 +52,7 @@ INSTALLED_APPS = [
     'simple_history',
     'registration',
     'bootstrap_datepicker_plus',
+    'django_tex',
 ]
 
 MIDDLEWARE = [
@@ -85,6 +86,14 @@ TEMPLATES = [
             ],
         },
     },
+    {
+        'NAME': 'tex',
+        'BACKEND': 'django_tex.engine.TeXEngine',
+        'APP_DIRS': True,
+        'OPTIONS': {
+            'environment': 'AKModel.environment.improved_tex_environment',
+        }
+    },
 ]
 
 WSGI_APPLICATION = 'AKPlanning.wsgi.application'
@@ -137,6 +146,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 176ecddc..c4d0c932 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -8,5 +8,6 @@ django-simple-history==3.0.0
 django-registration-redux==2.9
 django-debug-toolbar==3.2.1
 django-bootstrap-datepicker-plus==3.0.5
+django-tex==1.1.8.post1
 mysqlclient==2.0.3  # for production deployment
 pytz==2021.1
-- 
GitLab