automatically copy linked static files

This commit is contained in:
Oliver Urs Lenz 2018-07-11 15:54:47 +02:00
commit 048ea4dc0c
32 changed files with 424 additions and 224 deletions

View file

@ -147,7 +147,7 @@ class Generator(object):
parent_path, subdir = os.path.split(os.path.join(self.path, e))
exclusions_by_dirpath.setdefault(parent_path, set()).add(subdir)
files = []
files = set()
ignores = self.settings['IGNORE_FILES']
for path in paths:
# careful: os.path.join() will add a slash when path == ''.
@ -170,33 +170,41 @@ class Generator(object):
for f in temp_files:
fp = os.path.join(reldir, f)
if self._include_path(fp, extensions):
files.append(fp)
files.add(fp)
elif os.path.exists(root) and self._include_path(path, extensions):
files.append(path) # can't walk non-directories
files.add(path) # can't walk non-directories
return files
def add_source_path(self, content):
def add_source_path(self, content, static=False):
"""Record a source file path that a Generator found and processed.
Store a reference to its Content object, for url lookups later.
"""
location = content.get_relative_source_path()
self.context['filenames'][location] = content
key = 'static_content' if static else 'generated_content'
self.context[key][location] = content
def _add_failed_source_path(self, path):
def _add_failed_source_path(self, path, static=False):
"""Record a source file path that a Generator failed to process.
(For example, one that was missing mandatory metadata.)
The path argument is expected to be relative to self.path.
"""
self.context['filenames'][posixize_path(os.path.normpath(path))] = None
key = 'static_content' if static else 'generated_content'
self.context[key][posixize_path(os.path.normpath(path))] = None
def _is_potential_source_path(self, path):
def _is_potential_source_path(self, path, static=False):
"""Return True if path was supposed to be used as a source file.
(This includes all source files that have been found by generators
before this method is called, even if they failed to process.)
The path argument is expected to be relative to self.path.
"""
return (posixize_path(os.path.normpath(path))
in self.context['filenames'])
key = 'static_content' if static else 'generated_content'
return (posixize_path(os.path.normpath(path)) in self.context[key])
def add_static_links(self, content):
"""Add file links in content to context to be processed as Static
content.
"""
self.context['static_links'] |= content.get_static_links()
def _update_context(self, items):
"""Update the context with the given items from the currrent
@ -596,6 +604,7 @@ class ArticlesGenerator(CachingGenerator):
elif article.status == "draft":
all_drafts.append(article)
self.add_source_path(article)
self.add_static_links(article)
def _process(arts):
origs, translations = process_translations(
@ -702,6 +711,7 @@ class PagesGenerator(CachingGenerator):
elif page.status == "draft":
draft_pages.append(page)
self.add_source_path(page)
self.add_static_links(page)
def _process(pages):
origs, translations = process_translations(
@ -753,9 +763,12 @@ class StaticGenerator(Generator):
def generate_context(self):
self.staticfiles = []
for f in self.get_files(self.settings['STATIC_PATHS'],
exclude=self.settings['STATIC_EXCLUDES'],
extensions=False):
linked_files = {os.path.join(self.path, path)
for path in self.context['static_links']}
found_files = self.get_files(self.settings['STATIC_PATHS'],
exclude=self.settings['STATIC_EXCLUDES'],
extensions=False)
for f in linked_files | found_files:
# skip content source files unless the user explicitly wants them
if self.settings['STATIC_EXCLUDE_SOURCES']:
@ -770,7 +783,7 @@ class StaticGenerator(Generator):
context_signal=signals.static_generator_context,
context_sender=self)
self.staticfiles.append(static)
self.add_source_path(static)
self.add_source_path(static, static=True)
self._update_context(('staticfiles',))
signals.static_generator_finalized.send(self)