Skip to content

Commit

Permalink
Support PathLike objects in base_path (#2206)
Browse files Browse the repository at this point in the history
* Support PathLike objects in base_path

This enables this usage in MkDocs (note that specifying as a list already works):

```yml
  - pymdownx.snippets:
      base_path: !relative $docs_dir
```

Currently you get an error:
```
               self.base_path = [os.path.abspath(b) for b in base]
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
           TypeError: 'DocsDirPlaceholder' object is not iterable
```

* Add test
  • Loading branch information
oprypin authored Oct 27, 2023
1 parent 5dd162a commit f4a66a6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pymdownx/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __init__(self, config, md):
"""Initialize."""

base = config.get('base_path')
if isinstance(base, str):
if isinstance(base, (str, os.PathLike)):
base = [base]
self.base_path = [os.path.abspath(b) for b in base]
self.restrict_base_path = config['restrict_base_path']
Expand Down
33 changes: 33 additions & 0 deletions tests/test_extensions/test_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,39 @@ def test_section_no_end(self):
)


class _PathLikeExampleObject:
def __fspath__(self):
return os.path.join(BASE, '_snippets')


class TestSnippetsPathLike(util.MdCase):
"""Test snippet cases with path-like objects."""

extension = [
'pymdownx.snippets'
]

extension_configs = {
'pymdownx.snippets': {
'base_path': _PathLikeExampleObject()
}
}

def test_inline(self):
"""Test inline."""

self.check_markdown(
R'''
---8<--- "a.txt"
''',
R'''
<p>Snippet</p>
''',
True
)



class TestSnippetsFile(util.MdCase):
"""Test snippet file case."""

Expand Down

0 comments on commit f4a66a6

Please sign in to comment.