move {ARTICLE,PAGE}_DIR -> {ARTICLE,PAGE}_PATHS

Instead of one path a list can be given. This is due to popular request.
Should help people not wanting to use Pelican for blogging.
Maintain backward compatibility though.
Thanks to @ingwinlu for pointing out the change in StaticGenerator.
This commit is contained in:
Ondrej Grover 2014-04-21 11:36:17 +02:00
commit d635a347d1
4 changed files with 52 additions and 42 deletions

View file

@ -111,9 +111,9 @@ Setting name (followed by default value, if any)
``PATH`` Path to content directory to be processed by Pelican. If undefined, ``PATH`` Path to content directory to be processed by Pelican. If undefined,
and content path is not specified via an argument to the ``pelican`` and content path is not specified via an argument to the ``pelican``
command, Pelican will use the current working directory. command, Pelican will use the current working directory.
``PAGE_DIR = 'pages'`` Directory to look at for pages, relative to ``PATH``. ``PAGE_PATHS = ['pages']`` A list of directories to look at for pages, relative to ``PATH``.
``PAGE_EXCLUDES = ()`` A list of directories to exclude when looking for pages. ``PAGE_EXCLUDES = ()`` A list of directories to exclude when looking for pages.
``ARTICLE_DIR = ''`` Directory to look at for articles, relative to ``PATH``. ``ARTICLE_PATHS = ['']`` A list of directories to look at for articles, relative to ``PATH``.
``ARTICLE_EXCLUDES = ('pages',)`` A list of directories to exclude when looking for articles. ``ARTICLE_EXCLUDES = ('pages',)`` A list of directories to exclude when looking for articles.
``OUTPUT_SOURCES = False`` Set to True if you want to copy the articles and pages in their ``OUTPUT_SOURCES = False`` Set to True if you want to copy the articles and pages in their
original format (e.g. Markdown or reStructuredText) to the original format (e.g. Markdown or reStructuredText) to the

View file

@ -110,29 +110,30 @@ class Generator(object):
return True return True
return False return False
def get_files(self, path, exclude=[], extensions=None): def get_files(self, paths, exclude=[], extensions=None):
"""Return a list of files to use, based on rules """Return a list of files to use, based on rules
:param path: the path to search (relative to self.path) :param paths: the list pf paths to search (relative to self.path)
:param exclude: the list of path to exclude :param exclude: the list of path to exclude
:param extensions: the list of allowed extensions (if False, all :param extensions: the list of allowed extensions (if False, all
extensions are allowed) extensions are allowed)
""" """
files = [] files = []
root = os.path.join(self.path, path) for path in paths:
root = os.path.join(self.path, path)
if os.path.isdir(root): if os.path.isdir(root):
for dirpath, dirs, temp_files in os.walk(root, followlinks=True): for dirpath, dirs, temp_files in os.walk(root, followlinks=True):
for e in exclude: for e in exclude:
if e in dirs: if e in dirs:
dirs.remove(e) dirs.remove(e)
reldir = os.path.relpath(dirpath, self.path) reldir = os.path.relpath(dirpath, self.path)
for f in temp_files: for f in temp_files:
fp = os.path.join(reldir, f) fp = os.path.join(reldir, f)
if self._include_path(fp, extensions): if self._include_path(fp, extensions):
files.append(fp) files.append(fp)
elif os.path.exists(root) and self._include_path(path, extensions): elif os.path.exists(root) and self._include_path(path, extensions):
files.append(path) # can't walk non-directories files.append(path) # can't walk non-directories
return files return files
def add_source_path(self, content): def add_source_path(self, content):
@ -462,7 +463,7 @@ class ArticlesGenerator(CachingGenerator):
all_articles = [] all_articles = []
all_drafts = [] all_drafts = []
for f in self.get_files( for f in self.get_files(
self.settings['ARTICLE_DIR'], self.settings['ARTICLE_PATHS'],
exclude=self.settings['ARTICLE_EXCLUDES']): exclude=self.settings['ARTICLE_EXCLUDES']):
article = self.get_cached_data(f, None) article = self.get_cached_data(f, None)
if article is None: if article is None:
@ -586,7 +587,7 @@ class PagesGenerator(CachingGenerator):
all_pages = [] all_pages = []
hidden_pages = [] hidden_pages = []
for f in self.get_files( for f in self.get_files(
self.settings['PAGE_DIR'], self.settings['PAGE_PATHS'],
exclude=self.settings['PAGE_EXCLUDES']): exclude=self.settings['PAGE_EXCLUDES']):
page = self.get_cached_data(f, None) page = self.get_cached_data(f, None)
if page is None: if page is None:
@ -660,20 +661,17 @@ class StaticGenerator(Generator):
def generate_context(self): def generate_context(self):
self.staticfiles = [] self.staticfiles = []
for f in self.get_files(self.settings['STATIC_PATHS'],
# walk static paths extensions=False):
for static_path in self.settings['STATIC_PATHS']: static = self.readers.read_file(
for f in self.get_files( base_path=self.path, path=f, content_class=Static,
static_path, extensions=False): fmt='static', context=self.context,
static = self.readers.read_file( preread_signal=signals.static_generator_preread,
base_path=self.path, path=f, content_class=Static, preread_sender=self,
fmt='static', context=self.context, context_signal=signals.static_generator_context,
preread_signal=signals.static_generator_preread, context_sender=self)
preread_sender=self, self.staticfiles.append(static)
context_signal=signals.static_generator_context, self.add_source_path(static)
context_sender=self)
self.staticfiles.append(static)
self.add_source_path(static)
self._update_context(('staticfiles',)) self._update_context(('staticfiles',))
signals.static_generator_finalized.send(self) signals.static_generator_finalized.send(self)

View file

@ -29,9 +29,9 @@ DEFAULT_THEME = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'themes', 'notmyidea') 'themes', 'notmyidea')
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
'PATH': os.curdir, 'PATH': os.curdir,
'ARTICLE_DIR': '', 'ARTICLE_PATHS': [''],
'ARTICLE_EXCLUDES': ('pages',), 'ARTICLE_EXCLUDES': ('pages',),
'PAGE_DIR': 'pages', 'PAGE_PATHS': ['pages'],
'PAGE_EXCLUDES': (), 'PAGE_EXCLUDES': (),
'THEME': DEFAULT_THEME, 'THEME': DEFAULT_THEME,
'OUTPUT_PATH': 'output', 'OUTPUT_PATH': 'output',
@ -311,6 +311,16 @@ def configure_settings(settings):
key=lambda r: r[0], key=lambda r: r[0],
) )
# move {ARTICLE,PAGE}_DIR -> {ARTICLE,PAGE}_PATHS
for key in ['ARTICLE', 'PAGE']:
old_key = key + '_DIR'
new_key = key + '_PATHS'
if old_key in settings:
logger.warning('Deprecated setting {}, moving it to {} list'.format(
old_key, new_key))
settings[new_key] = [settings[old_key]] # also make a list
del settings[old_key]
# Save people from accidentally setting a string rather than a list # Save people from accidentally setting a string rather than a list
path_keys = ( path_keys = (
'ARTICLE_EXCLUDES', 'ARTICLE_EXCLUDES',
@ -324,13 +334,15 @@ def configure_settings(settings):
'PLUGINS', 'PLUGINS',
'STATIC_PATHS', 'STATIC_PATHS',
'THEME_STATIC_PATHS', 'THEME_STATIC_PATHS',
'ARTICLE_PATHS',
'PAGE_PATHS',
) )
for PATH_KEY in filter(lambda k: k in settings, path_keys): for PATH_KEY in filter(lambda k: k in settings, path_keys):
if isinstance(settings[PATH_KEY], six.string_types): if isinstance(settings[PATH_KEY], six.string_types):
logger.warning("Detected misconfiguration with %s setting " logger.warning("Detected misconfiguration with %s setting "
"(must be a list), falling back to the default" "(must be a list), falling back to the default"
% PATH_KEY) % PATH_KEY)
settings[PATH_KEY] = DEFAULT_CONFIG[PATH_KEY] settings[PATH_KEY] = DEFAULT_CONFIG[PATH_KEY]
for old, new, doc in [ for old, new, doc in [
('LESS_GENERATOR', 'the Webassets plugin', None), ('LESS_GENERATOR', 'the Webassets plugin', None),

View file

@ -372,8 +372,8 @@ class TestPageGenerator(unittest.TestCase):
def test_generate_context(self): def test_generate_context(self):
settings = get_settings(filenames={}) settings = get_settings(filenames={})
settings['PAGE_DIR'] = 'TestPages' # relative to CUR_DIR
settings['CACHE_PATH'] = self.temp_cache settings['CACHE_PATH'] = self.temp_cache
settings['PAGE_PATHS'] = ['TestPages'] # relative to CUR_DIR
settings['DEFAULT_DATE'] = (1970, 1, 1) settings['DEFAULT_DATE'] = (1970, 1, 1)
generator = PagesGenerator( generator = PagesGenerator(