From d00a8e52f5694889f9e9050f16b69a25dbdc2ca6 Mon Sep 17 00:00:00 2001 From: brandongc Date: Fri, 5 Jan 2018 11:01:34 -0800 Subject: [PATCH] Refactor `utils.copy_media_files` for more flexibility (#1370) --- docs/about/release-notes.md | 4 ++++ mkdocs/utils/__init__.py | 34 ++++++++++++++++++---------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/docs/about/release-notes.md b/docs/about/release-notes.md index 9229024e..08bad39b 100644 --- a/docs/about/release-notes.md +++ b/docs/about/release-notes.md @@ -21,6 +21,10 @@ The current and past members of the MkDocs team. * [@d0ugal](https://github.com/d0ugal/) * [@waylan](https://github.com/waylan/) +## Development Version + +* Refactor `copy_media_files` util function for more flexibility (#1370). + ## Version 0.17.2 (2017-11-15) * Bugfix: Correct `extra_*` config setting regressions (#1335 & #1336). diff --git a/mkdocs/utils/__init__.py b/mkdocs/utils/__init__.py index a252beba..47901f7c 100644 --- a/mkdocs/utils/__init__.py +++ b/mkdocs/utils/__init__.py @@ -41,6 +41,14 @@ else: # pragma: no cover log = logging.getLogger(__name__) +markdown_extensions = [ + '.markdown', + '.mdown', + '.mkdn', + '.mkd', + '.md' +] + def yaml_load(source, loader=yaml.Loader): """ @@ -145,11 +153,13 @@ def clean_directory(directory): os.unlink(path) -def copy_media_files(from_dir, to_dir, exclude=None, dirty=False): +def copy_media_files(from_dir, to_dir, + exclude=['*{0}'.format(x) for x in markdown_extensions], dirty=False): """ Recursively copy all files except markdown and exclude[ed] files into another directory. `exclude` accepts a list of Unix shell-style wildcards (`['*.py', '*.pyc']`). + Note that `exclude` only operates on file names, not directories. """ for (source_dir, dirnames, filenames) in os.walk(from_dir, followlinks=True): @@ -168,15 +178,14 @@ def copy_media_files(from_dir, to_dir, exclude=None, dirty=False): dirnames[:] = [d for d in dirnames if not d.startswith('.')] for filename in filenames: - if not is_markdown_file(filename): - source_path = os.path.join(source_dir, filename) - output_path = os.path.join(output_dir, filename) + source_path = os.path.join(source_dir, filename) + output_path = os.path.join(output_dir, filename) - # Do not copy when using --dirty if the file has not been modified - if dirty and (modified_time(source_path) < modified_time(output_path)): - continue + # Do not copy when using --dirty if the file has not been modified + if dirty and (modified_time(source_path) < modified_time(output_path)): + continue - copy_file(source_path, output_path) + copy_file(source_path, output_path) def get_html_path(path): @@ -221,14 +230,7 @@ def is_markdown_file(path): http://superuser.com/questions/249436/file-extension-for-markdown-files """ - ext = os.path.splitext(path)[1].lower() - return ext in [ - '.markdown', - '.mdown', - '.mkdn', - '.mkd', - '.md', - ] + return any(fnmatch.fnmatch(path.lower(), '*{0}'.format(x)) for x in markdown_extensions) def is_css_file(path):