Commit e3c382ae authored by berumuron's avatar berumuron
Browse files

Move extension filtering in utility functions

parent 694c4e3f
......@@ -189,24 +189,43 @@ def list_dirs(path):
yield filename
def list_files(path):
def match_extensions(filename, extensions):
"""Return True if filename extension is part of extensions array.
"""
if len(extensions) > 0:
_, ext = os.path.splitext(filename)
if ext[1:] not in extensions:
return False
return True
def list_files(path, only=[]):
"""List all the files within a path.
"""
for filename in os.listdir(path):
full_path = os.path.join(path, filename)
if os.path.isfile(full_path):
yield filename
# We don't want to yield directories
if not os.path.isfile(full_path):
continue
# If file extension is not part of "only" array, we don't yield it. If
# only is empty, it means we accept all extensions.
if not match_extensions(full_path, only):
continue
def dir_tree(path):
yield filename
def dir_tree(path, only=[]):
"""List all the tree directory under the given path.
"""
for filename in os.listdir(path):
full_path = os.path.join(path, filename)
if os.path.isdir(full_path):
for sub_filename in dir_tree(full_path):
for sub_filename in dir_tree(full_path, only=only):
yield os.path.join(filename, sub_filename)
else:
elif match_extensions(filename, only):
yield filename
......@@ -287,13 +306,9 @@ def main(environment):
# Check that pages directory exists and load Pages from it
pages_path = os.path.join(os.curdir, "pages")
if os.path.isdir(pages_path):
for filename in dir_tree(pages_path):
slug, ext = os.path.splitext(filename)
if ext != ".html":
# Don't consider files which are not HTML
continue
for filename in dir_tree(pages_path, only=["html"]):
# We read the page file and initialize a Page from it
slug, _ = os.path.splitext(filename)
page_filepath = os.path.join(pages_path, filename)
with open(page_filepath, "r") as page_file:
page = Page(slug, page_file.read(), configuration=configuration)
......@@ -319,14 +334,10 @@ def main(environment):
pages.append(serie)
articles_by_series[serie] = []
for filename in list_files(dirpath):
filename_no_ext, ext = os.path.splitext(filename)
slug = os.path.basename(filename_no_ext)
if ext != ".md":
continue
for filename in list_files(dirpath, only=["md"]):
# We read the article file and initialize an Article from it
filename_no_ext, _ = os.path.splitext(filename)
slug = os.path.basename(filename_no_ext)
article_filepath = os.path.join(dirpath, filename)
with open(article_filepath, "r") as article_file:
article = Article(
......@@ -340,14 +351,10 @@ def main(environment):
if serie:
articles_by_series[serie].append(article)
for filename in list_files(articles_path):
filename_no_ext, ext = os.path.splitext(filename)
slug = os.path.basename(filename_no_ext)
if ext != ".md":
continue
for filename in list_files(articles_path, only=["md"]):
# We read the article file and initialize an Article from it
filename_no_ext, _ = os.path.splitext(filename)
slug = os.path.basename(filename_no_ext)
article_filepath = os.path.join(articles_path, filename)
with open(article_filepath, "r") as article_file:
article = Article(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment