From 75fdf1363e4d74c07ee075be34f839a9776c8fb2 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sun, 7 Oct 2018 13:49:14 +0900 Subject: [PATCH] latex: escape \releasename and \indexname --- sphinx/templates/latex/latex.tex_t | 4 ++-- sphinx/util/template.py | 5 +++++ sphinx/util/texescape.py | 11 ++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/sphinx/templates/latex/latex.tex_t b/sphinx/templates/latex/latex.tex_t index d2b251efe..e1aeae4f9 100644 --- a/sphinx/templates/latex/latex.tex_t +++ b/sphinx/templates/latex/latex.tex_t @@ -48,7 +48,7 @@ \author{<%= author %>} \newcommand{\sphinxlogo}{<%= logo %>} <%- if releasename or release %> -\renewcommand{\releasename}{<%= releasename or _('Release') %>} +\renewcommand{\releasename}{<%= releasename or _('Release') | e %>} <%- else %> \renewcommand{\releasename}{} <%- endif %> @@ -63,6 +63,6 @@ <%= body %> <%= atendofbody %> <%= indices %> -\renewcommand{\indexname}{<%= _('Index') %>} +\renewcommand{\indexname}{<%= _('Index') | e %>} <%= printindex %> \end{document} diff --git a/sphinx/util/template.py b/sphinx/util/template.py index 970b20177..01c772069 100644 --- a/sphinx/util/template.py +++ b/sphinx/util/template.py @@ -16,6 +16,7 @@ from jinja2.sandbox import SandboxedEnvironment from sphinx import package_dir from sphinx.jinja2glue import SphinxFileSystemLoader from sphinx.locale import get_translator +from sphinx.util import texescape if False: # For type annotation @@ -72,6 +73,10 @@ class LaTeXRenderer(SphinxRenderer): template_path = os.path.join(package_dir, 'templates', 'latex') super(LaTeXRenderer, self).__init__(template_path) + # use texescape as escape filter + self.env.filters['e'] = texescape.escape + self.env.filters['escape'] = texescape.escape + # use JSP/eRuby like tagging instead because curly bracket; the default # tagging of jinja2 is not good for LaTeX sources. self.env.variable_start_string = '<%=' diff --git a/sphinx/util/texescape.py b/sphinx/util/texescape.py index 8d37e0f60..8d3bd5371 100644 --- a/sphinx/util/texescape.py +++ b/sphinx/util/texescape.py @@ -11,6 +11,10 @@ from __future__ import unicode_literals +if False: + # For type annotation + from typing import Dict # NOQA + tex_replacements = [ # map TeX special chars ('$', r'\$'), @@ -117,11 +121,16 @@ tex_replacements = [ ('Ω', r'\(\Omega\)'), ] -tex_escape_map = {} +tex_escape_map = {} # type: Dict[int, unicode] tex_replace_map = {} tex_hl_escape_map_new = {} +def escape(s): + # type: (unicode) -> unicode + return s.translate(tex_escape_map) + + def init(): # type: () -> None for a, b in tex_replacements: