Support compound file extensions. Fix #2780

Before this commit, the file extension matcher could not match compound
file extensions; e.g., '.md.html' would match `.html`
This commit is contained in:
Holden Nelson 2020-10-29 12:21:47 -06:00
commit d44d27f093
3 changed files with 16 additions and 1 deletions

3
RELEASE.md Normal file
View file

@ -0,0 +1,3 @@
Release type: minor
Support matching compound file extensions for custom readers; e.g., `.md.html`

View file

@ -7,6 +7,7 @@ from collections import defaultdict
from functools import partial
from itertools import chain, groupby
from operator import attrgetter
from pathlib import Path
from jinja2 import (BaseLoader, ChoiceLoader, Environment, FileSystemLoader,
PrefixLoader, TemplateNotFound)
@ -123,7 +124,7 @@ class Generator:
if any(fnmatch.fnmatch(basename, ignore) for ignore in ignores):
return False
ext = os.path.splitext(basename)[1][1:]
ext = ''.join(Path(basename).suffixes)[1:]
if extensions is False or ext in extensions:
return True

View file

@ -41,6 +41,17 @@ class TestGenerator(unittest.TestCase):
ignored_file = os.path.join(CUR_DIR, 'content', 'ignored1.rst')
self.assertFalse(include_path(ignored_file))
def test_include_path_handles_compound_extensions(self):
"""
Test that Generator._include_path properly handles
compound extensions such as .md.html
"""
filename = os.path.join(CUR_DIR, 'content', 'article.md.html')
include_path = self.generator._include_path
self.assertTrue(include_path(filename, extensions=('md.html',)))
self.assertFalse(include_path(filename, extensions=('md',)))
self.assertFalse(include_path(filename, extensions=('html',)))
def test_get_files_exclude(self):
"""Test that Generator.get_files() properly excludes directories.
"""