summaryrefslogtreecommitdiff
path: root/sphinx/writers
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2022-10-13 17:37:07 +0100
committerGitHub <noreply@github.com>2022-10-13 17:37:07 +0100
commitfa6d42597f2c1259ccdd9166763657bd9c2a316e (patch)
tree6c7e2890fd47da95caff1293456b04a69c7ccda0 /sphinx/writers
parente008e162005fa51a04b61f4271d4e766cf7c671f (diff)
downloadsphinx-git-fa6d42597f2c1259ccdd9166763657bd9c2a316e.tar.gz
URI-escape image filenames (#10268)
Without this change, local images with `#` in their name result in incorrect URLs There is already a similar call to `urllib.parse.quote` for file downloads, suggesting this is a sensible approach. Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Co-authored-by: Takeshi KOMIYA <i.tkomiya@gmail.com>
Diffstat (limited to 'sphinx/writers')
-rw-r--r--sphinx/writers/html.py2
-rw-r--r--sphinx/writers/html5.py2
-rw-r--r--sphinx/writers/latex.py11
3 files changed, 9 insertions, 6 deletions
diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py
index 48183204d..751b2f352 100644
--- a/sphinx/writers/html.py
+++ b/sphinx/writers/html.py
@@ -620,7 +620,7 @@ class HTMLTranslator(SphinxTranslator, BaseTranslator):
# rewrite the URI if the environment knows about it
if olduri in self.builder.images:
node['uri'] = posixpath.join(self.builder.imgpath,
- self.builder.images[olduri])
+ urllib.parse.quote(self.builder.images[olduri]))
if 'scale' in node:
# Try to figure out image height and width. Docutils does that too,
diff --git a/sphinx/writers/html5.py b/sphinx/writers/html5.py
index 1a0b6f28e..344ae7f16 100644
--- a/sphinx/writers/html5.py
+++ b/sphinx/writers/html5.py
@@ -567,7 +567,7 @@ class HTML5Translator(SphinxTranslator, BaseTranslator):
# rewrite the URI if the environment knows about it
if olduri in self.builder.images:
node['uri'] = posixpath.join(self.builder.imgpath,
- self.builder.images[olduri])
+ urllib.parse.quote(self.builder.images[olduri]))
if 'scale' in node:
# Try to figure out image height and width. Docutils does that too,
diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py
index 0e2250c17..846d365d1 100644
--- a/sphinx/writers/latex.py
+++ b/sphinx/writers/latex.py
@@ -1319,14 +1319,17 @@ class LaTeXTranslator(SphinxTranslator):
if include_graphics_options:
options = '[%s]' % ','.join(include_graphics_options)
base, ext = path.splitext(uri)
+
if self.in_title and base:
# Lowercase tokens forcely because some fncychap themes capitalize
# the options of \sphinxincludegraphics unexpectedly (ex. WIDTH=...).
- self.body.append(r'\lowercase{\sphinxincludegraphics%s}{{%s}%s}' %
- (options, base, ext))
+ cmd = r'\lowercase{\sphinxincludegraphics%s}{{%s}%s}' % (options, base, ext)
else:
- self.body.append(r'\sphinxincludegraphics%s{{%s}%s}' %
- (options, base, ext))
+ cmd = r'\sphinxincludegraphics%s{{%s}%s}' % (options, base, ext)
+ # escape filepath for includegraphics, https://tex.stackexchange.com/a/202714/41112
+ if '#' in base:
+ cmd = r'{\catcode`\#=12' + cmd + '}'
+ self.body.append(cmd)
self.body.extend(post)
def depart_image(self, node: Element) -> None: