Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix download file with special characters #9670

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion sphinx/writers/html.py
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
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
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
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