diff --git a/pelican/generators.py b/pelican/generators.py index e18531becc..4403ee5c08 100644 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -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): diff --git a/pelican/readers.py b/pelican/readers.py index efe61c531f..ed6a04d8b3 100644 --- a/pelican/readers.py +++ b/pelican/readers.py @@ -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) @@ -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 '' diff --git a/pelican/tests/test_generators.py b/pelican/tests/test_generators.py index 1bc8aff0c1..327935d05d 100644 --- a/pelican/tests/test_generators.py +++ b/pelican/tests/test_generators.py @@ -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. """ diff --git a/pelican/tests/test_readers.py b/pelican/tests/test_readers.py index 86917f9523..afb990ea92 100644 --- a/pelican/tests/test_readers.py +++ b/pelican/tests/test_readers.py @@ -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'))