Skip to content

Commit

Permalink
Handle selection login in _include_path
Browse files Browse the repository at this point in the history
  • Loading branch information
holden-nelson committed Jan 12, 2022
1 parent 2e1b37b commit cfdf0ad
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
7 changes: 5 additions & 2 deletions pelican/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,13 @@ def _include_path(self, path, extensions=None):
if any(fnmatch.fnmatch(basename, ignore) for ignore in ignores):
return False

ext = os.path.splitext(basename)[1][1:]
if extensions is False or ext in extensions:
if extensions is False:
return True

for ext in extensions:
if basename.endswith(f'.{ext}'):
return True

return False

def get_files(self, paths, exclude=[], extensions=None):
Expand Down
17 changes: 12 additions & 5 deletions pelican/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,8 +829,15 @@ def update(self, d):
self[key] = value

def get_format(self, filename):
ext = ReaderTree._rec_get_fmt_from_filename(self.tree_dd, filename)
return ext[1:]
try:
ext = ReaderTree._rec_get_fmt_from_filename(self.tree_dd, filename)
return ext[1:]
except TypeError:
return ''

def has_reader(self, filename):
fmt = self.get_format(filename)
return fmt in self

def as_dict(self):
return ReaderTree._rec_dd_to_dict(self.tree_dd)
Expand Down Expand Up @@ -885,14 +892,14 @@ def _rec_del_item(d, intended_key):
def _rec_get_fmt_from_filename(d, filename):
if '.' in filename:
file, ext = os.path.splitext(filename)
fmt = ext[1:]
fmt = ext[1:] if ext else ext

if fmt in d:
next_component = ReaderTree._rec_get_fmt_from_filename(d[fmt], file)
return '.'.join([next_component, fmt])
elif '' in d:
return fmt
return ''
else:
raise TypeError("No valid extension found")
raise TypeError('No reader found for file.')
else:
return ''
3 changes: 3 additions & 0 deletions pelican/tests/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def test_include_path(self):
ignored_file = os.path.join(CUR_DIR, 'content', 'ignored1.rst')
self.assertFalse(include_path(ignored_file))

compound_file = os.path.join(CUR_DIR, 'content', 'compound.md.html')
self.assertTrue(include_path(compound_file, extensions=('md.html',)))

def test_get_files_exclude(self):
"""Test that Generator.get_files() properly excludes directories.
"""
Expand Down
11 changes: 10 additions & 1 deletion pelican/tests/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,10 +992,19 @@ def test_update(self):
def test_get_format(self):
html_ext = self.reader_classes.get_format('text.html')
md_ext = self.reader_classes.get_format('another.md')
compound_ext = self.reader_classes.get_format('compound.md.html')
compound_ext = self.reader_classes.get_format('dots.compound.md.html')
no_ext = self.reader_classes.get_format('no_extension')
bar_ext = self.reader_classes.get_format('file.bar')

self.assertEqual(html_ext, 'html')
self.assertEqual(md_ext, 'md')
self.assertEqual(compound_ext, 'md.html')
self.assertEqual(no_ext, '')
self.assertEqual(bar_ext, '')

def test_has_reader(self):
has_reader = self.reader_classes.has_reader
self.assertTrue(has_reader('text.html'))
self.assertFalse(has_reader('no_ext'))
print(has_reader('bad_ext.bar'))
self.assertFalse(has_reader('bad_ext.bar'))

0 comments on commit cfdf0ad

Please sign in to comment.