Skip to content

Commit 9655599

Browse files
authored
Merge pull request #1060 from Paldari/bugfix/download-absolute-path
Issue 1059: Fix downloadable abs path
2 parents f6dcc89 + abb7ff9 commit 9655599

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

sphinxcontrib/confluencebuilder/util.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -257,19 +257,19 @@ def find_env_abspath(env, out_dir, path: str) -> Path | None:
257257
# later stage
258258
return None
259259

260-
if normalized_path.is_absolute():
261-
abs_path = normalized_path
260+
# some generated nodes will prefix the path of an asset with `/`,
261+
# with the intent of that path being the root from the
262+
# configured source directory instead of an absolute path on the
263+
# system -- check the environment's source directory first before
264+
# checking the full system's path
265+
if normalized_path.parts[0] == os.sep:
266+
abs_path = Path(env.srcdir, *normalized_path.parts[1:])
262267

263-
# some generated nodes will prefix the path of an asset with `/`,
264-
# with the intent of that path being the root from the
265-
# configured source directory instead of an absolute path on the
266-
# system -- check the environment's source directory first before
267-
# checking the full system's path
268-
if normalized_path.name[0] == os.sep:
269-
new_path = Path(env.srcdir, normalized_path.name[1:])
268+
if abs_path.is_file():
269+
return abs_path
270270

271-
if new_path.is_file():
272-
abs_path = new_path
271+
if normalized_path.is_absolute():
272+
abs_path = normalized_path
273273
else:
274274
abs_path = Path(env.srcdir, normalized_path)
275275

4.27 KB
Binary file not shown.

tests/unit-tests/datasets/download/index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@ download
88
:download:`../../assets/example.pdf`
99

1010
:download:`label<../../assets/example.pdf>`
11+
12+
:download:`files/example.pdf`
13+
14+
.. Absolute filename taken as relative to the top source directory
15+
16+
:download:`/files/example.pdf`

tests/unit-tests/test_sphinx_download.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,25 @@ def test_storage_sphinx_download_defaults(self):
1919
out_dir = self.build(self.dataset)
2020

2121
with parse('index', out_dir) as data:
22-
# view-file
23-
view_file_macro = data.find('ac:structured-macro',
22+
# view-file(s)
23+
#
24+
# We have a couple of asset downloads in this example. A mixture
25+
# of paths to sanity check download paths function. All use the
26+
# same/replicated asset type and each will result in a single file
27+
# upload, so we should have three matching view-file macros.
28+
view_file_macros = data.find_all('ac:structured-macro',
2429
{'ac:name': 'view-file'})
25-
self.assertIsNotNone(view_file_macro)
30+
self.assertEqual(len(view_file_macros), 3)
2631

27-
view_file_name = view_file_macro.find('ac:parameter',
28-
{'ac:name': 'name'})
29-
self.assertIsNotNone(view_file_name)
32+
for view_file_macro in view_file_macros:
33+
view_file_name = view_file_macro.find('ac:parameter',
34+
{'ac:name': 'name'})
35+
self.assertIsNotNone(view_file_name)
3036

31-
attachment_ref = view_file_name.find('ri:attachment')
32-
self.assertIsNotNone(attachment_ref)
33-
self.assertTrue(attachment_ref.has_attr('ri:filename'))
34-
self.assertEqual(attachment_ref['ri:filename'], 'example.pdf')
37+
attachment_ref = view_file_name.find('ri:attachment')
38+
self.assertIsNotNone(attachment_ref)
39+
self.assertTrue(attachment_ref.has_attr('ri:filename'))
40+
self.assertEqual(attachment_ref['ri:filename'], 'example.pdf')
3541

3642
# link to file
3743
file_link = data.find('ac:link')

0 commit comments

Comments
 (0)