From a4702fca86c5b1573b2bc70bd7a9c3ecaccabea7 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Thu, 6 Aug 2020 14:19:51 +0200 Subject: [PATCH] Escape special TeX characters --- apps/treasury/templatetags/escape_tex.py | 20 ++++++++++++++++++++ templates/treasury/invoice_sample.tex | 16 +++++++++------- 2 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 apps/treasury/templatetags/escape_tex.py diff --git a/apps/treasury/templatetags/escape_tex.py b/apps/treasury/templatetags/escape_tex.py new file mode 100644 index 00000000..3d635690 --- /dev/null +++ b/apps/treasury/templatetags/escape_tex.py @@ -0,0 +1,20 @@ +# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay +# SPDX-License-Identifier: GPL-3.0-or-later + +from django import template + + +def do_latex_escape(value): + return ( + value.replace("&", "\\&") + .replace("$", "\\$") + .replace("%", "\\%") + .replace("#", "\\#") + .replace("_", "\\_") + .replace("{", "\\{") + .replace("}", "\\}") + ) + + +register = template.Library() +register.filter("escape_tex", do_latex_escape) diff --git a/templates/treasury/invoice_sample.tex b/templates/treasury/invoice_sample.tex index 3c76403e..6562e3df 100644 --- a/templates/treasury/invoice_sample.tex +++ b/templates/treasury/invoice_sample.tex @@ -1,3 +1,5 @@ +{% load escape_tex %} + \nonstopmode \documentclass[11pt]{article} @@ -81,22 +83,22 @@ \def\IBAN{FR76\CodeBanque\CodeGuichet\NCompte\CleRib} \def\CodeBic{{"{"}}{{ obj.bic }}} -\def\FactureNum {{"{"}}{{obj.id}}} % Numéro de facture +\def\FactureNum {{"{"}}{{ obj.id }}} % Numéro de facture \def\FactureAcquittee {% if obj.acquitted %} {oui} {% else %} {non} {% endif %} % Facture acquittée : oui/non -\def\FactureLieu {{"{"}}{{ obj.place }}} % Lieu de l'édition de la facture +\def\FactureLieu {{"{"}}{{ obj.place|safe|escape_tex }}} % Lieu de l'édition de la facture \def\FactureDate {{"{"}}{{ obj.date }}} % Date de l'édition de la facture -\def\FactureObjet {{"{"}}{{ obj.object|safe }} } % Objet du document +\def\FactureObjet {{"{"}}{{ obj.object|safe|escape_tex }} } % Objet du document % Description de la facture -\def\FactureDescr {{"{"}}{{ obj.description|safe }}} +\def\FactureDescr {{"{"}}{{ obj.description|safe|escape_tex }}} % Infos Client -\def\ClientNom{{"{"}}{{obj.name|safe}}} % Nom du client -\def\ClientAdresse{{"{"}}{{ obj.address|safe }}} % Adresse du client +\def\ClientNom{{"{"}}{{ obj.name|safe|escape_tex }}} % Nom du client +\def\ClientAdresse{{"{"}}{{ obj.address|safe|escape_tex }}} % Adresse du client % Liste des produits facturés : Désignation, quantité, prix unitaire HT {% for product in products %} -\AjouterProduit{ {{product.designation|safe}}} { {{product.quantity|safe}}} { {{product.amount_euros|safe}}} { {{product.total_euros|safe}}} +\AjouterProduit{ {{ product.designation|safe|escape_tex }}} { {{ product.quantity|safe|escape_tex }}} { {{ product.amount_euros|safe|escape_tex }}} { {{ product.total_euros|safe|escape_tex }}} {% endfor %} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%