Commits

Andrey Vlasovskikh  committed 1b211f3

Cleanup of include/exclude settings

  • Participants
  • Parent commits b9222a0

Comments (0)

Files changed (1)

 template_filters = {
     'markdownify':  markdown,
 }
-keep_filenames = [".htaccess"]
-ignore_filenames = map(re.compile, ["^[\.#_].*", ".*~$", ".*\.s[uvw][a-z]$"])
 retcode = 0
 
 
+default_site = {
+    'include': ['.htaccess'],
+    'exclude_patterns': [
+        r'^[\.#_].*',
+        r'.*~$',
+        r'.*\.s[uvw][a-z]$',  # *.swp files, etc.
+    ],
+}
+
+
 def all_files(basedir):
     for path, dirs, files in os.walk(basedir):
         for filename in files:
         raise ValueError("cannot merge '{0!r}' and '{1!r}'".format(x1, x2))
 
 
-def is_file_visible(filename, exclude=None):
-    if not exclude:
-        exclude = []
+def is_file_visible(filename, site):
     parts = filename.split(os.path.sep)
-    if filename in keep_filenames:
+    exclude = site.get('exclude', [])
+    exclude_patterns = site.get('exclude_patterns', [])
+    if filename in site.get('include', []):
         return True
-    elif any([r.match(part) for r in ignore_filenames for part in parts]):
+    elif any(re.match(pattern, part)
+             for pattern in exclude_patterns
+             for part in parts):
         return False
     elif any(filename.startswith(path) for path in exclude):
         return False
 
 
 def load_file(basedir, filename, site):
-    exclude = site.get('exclude', [])
-    if not is_file_visible(filename, exclude):
+    if not is_file_visible(filename, site):
         return None
     return {
         'files': [{'url': path2url(filename), 'source': filename}],
 
 
 def load_page(basedir, filename, site):
-    exclude = site.get('exclude', [])
-    if not is_file_visible(filename, exclude):
+    if not is_file_visible(filename, site):
         return None
     name, suffix = os.path.splitext(filename)
     if suffix in file_filters:
     if '_posts' not in parts:
         return None
     _, name = os.path.split(filename)
-    exclude = site.get('exclude', [])
-    if not is_file_visible(name, exclude):
+    if not is_file_visible(name, site):
         return None
     name, suffix = os.path.splitext(name)
     m = post_re.match(name)
 def load_site(basedir):
     log('Loading source files...')
     site = load_yaml_mapping(os.path.join(basedir, '_config.yml'))
+    site = merge(site, default_site)
     site['time'] = datetime.utcnow()
     n = 0
     for i, abspath in enumerate(all_files(basedir)):