diff --git a/AKModel/environment.py b/AKModel/environment.py new file mode 100644 index 0000000000000000000000000000000000000000..5883361b0d8a80f647e131185dabca8aaa4d4bd7 --- /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 0dbfeb5c69e6e8d902aaa0179dbdde356786ff14..a383072cb2d350d67917d93990f8e53d74469d18 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 040a5aa8e59a7fe6ffdb9e531b00977468af993c..54a002e3d63a8730bdadab29334c13447f72b8ff 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 e843156cfe4a49885bbb4dfd2c858d4a7331bc30..10b706f07657755ab5cfbdf867059ac9ff10bf91 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