Skip to content

Commit

Permalink
Merge pull request sphinx-doc#9670 from jonppe/fix-download-file-with…
Browse files Browse the repository at this point in the history
…-special-characters

Fix download file with special characters
  • Loading branch information
tk0miya committed Sep 25, 2021
2 parents 274ee48 + b2bd115 commit d75e317
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 2 deletions.
4 changes: 3 additions & 1 deletion sphinx/writers/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import os
import posixpath
import re
import urllib.parse
import warnings
from typing import TYPE_CHECKING, Iterable, Tuple, cast

Expand Down Expand Up @@ -589,7 +590,8 @@ def visit_download_reference(self, node: Element) -> None:
self.context.append('</a>')
elif 'filename' in node:
atts['class'] += ' internal'
atts['href'] = posixpath.join(self.builder.dlpath, node['filename'])
atts['href'] = posixpath.join(self.builder.dlpath,
urllib.parse.quote(node['filename']))
self.body.append(self.starttag(node, 'a', '', **atts))
self.context.append('</a>')
else:
Expand Down
4 changes: 3 additions & 1 deletion sphinx/writers/html5.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import os
import posixpath
import re
import urllib.parse
import warnings
from typing import TYPE_CHECKING, Iterable, Tuple, cast

Expand Down Expand Up @@ -529,7 +530,8 @@ def visit_download_reference(self, node: Element) -> None:
self.context.append('</a>')
elif 'filename' in node:
atts['class'] += ' internal'
atts['href'] = posixpath.join(self.builder.dlpath, node['filename'])
atts['href'] = posixpath.join(self.builder.dlpath,
urllib.parse.quote(node['filename']))
self.body.append(self.starttag(node, 'a', '', **atts))
self.context.append('</a>')
else:
Expand Down
Empty file.
1 change: 1 addition & 0 deletions tests/roots/test-root/includes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Testing downloadable files

Download :download:`img.png` here.
Download :download:`this <subdir/img.png>` there.
Download :download:`file with special characters <file_with_special_#_chars.xyz>`.

Test file and literal inclusion
===============================
Expand Down
6 changes: 6 additions & 0 deletions tests/test_build_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,12 @@ def test_html_download(app):
assert (app.outdir / matched.group(1)).exists()
assert matched.group(1) == filename

pattern = ('<a class="reference download internal" download="" '
'href="(_downloads/.*/)(file_with_special_%23_chars.xyz)">')
matched = re.search(pattern, result)
assert matched
assert (app.outdir / matched.group(1) / "file_with_special_#_chars.xyz").exists()


@pytest.mark.sphinx('html', testroot='roles-download')
def test_html_download_role(app, status, warning):
Expand Down

0 comments on commit d75e317

Please sign in to comment.