Add the PATH_METADATA setting

Sometimes the base filename doesn't have everything you need.

Remember that os.sep is platform dependent, so using it in a regular
expression for this setting may not be portable (boo MS Windows!).
This commit is contained in:
W. Trevor King 2013-01-04 14:30:40 -05:00 committed by Alexis Métaireau
commit bfa8851af0
3 changed files with 10 additions and 2 deletions

View file

@ -59,6 +59,8 @@ Setting name (default value) What doe
For example, if you would like to extract both the For example, if you would like to extract both the
date and the slug, you could set something like: date and the slug, you could set something like:
``'(?P<date>\d{4}-\d{2}-\d{2})_(?P<slug>.*)'``. ``'(?P<date>\d{4}-\d{2}-\d{2})_(?P<slug>.*)'``.
`PATH_METADATA` (``''``) Like ``FILENAME_METADATA``, but parsed from a page's
full path relative to the content source directory.
`DELETE_OUTPUT_DIRECTORY` (``False``) Delete the content of the output directory before `DELETE_OUTPUT_DIRECTORY` (``False``) Delete the content of the output directory before
generating new files. generating new files.
`FILES_TO_COPY` (``()``) A list of files (or directories) to copy from the source (inside the `FILES_TO_COPY` (``()``) A list of files (or directories) to copy from the source (inside the

View file

@ -353,19 +353,24 @@ def parse_path_metadata(path, settings=None, process=None):
>>> import pprint >>> import pprint
>>> settings = { >>> settings = {
... 'FILENAME_METADATA': '(?P<slug>[^.]*).*', ... 'FILENAME_METADATA': '(?P<slug>[^.]*).*',
... 'PATH_METADATA':
... '(?P<category>[^/]*)/(?P<date>\d{4}-\d{2}-\d{2})/.*',
... } ... }
>>> reader = Reader(settings=settings) >>> reader = Reader(settings=settings)
>>> metadata = parse_path_metadata( >>> metadata = parse_path_metadata(
... path='my-cat/2013-01-01/my-slug.html', ... path='my-cat/2013-01-01/my-slug.html',
... settings=settings, ... settings=settings,
... process=reader.process_metadata) ... process=reader.process_metadata)
>>> pprint.pprint(metadata) >>> pprint.pprint(metadata) # doctest: +ELLIPSIS
{'slug': 'my-slug'} {'category': <pelican.urlwrappers.Category object at ...>,
'date': datetime.datetime(2013, 1, 1, 0, 0),
'slug': 'my-slug'}
""" """
metadata = {} metadata = {}
base, ext = os.path.splitext(os.path.basename(path)) base, ext = os.path.splitext(os.path.basename(path))
if settings: if settings:
for key,data in [('FILENAME_METADATA', base), for key,data in [('FILENAME_METADATA', base),
('PATH_METADATA', path),
]: ]:
regexp = settings.get(key) regexp = settings.get(key)
if regexp: if regexp:

View file

@ -77,6 +77,7 @@ _DEFAULT_CONFIG = {'PATH': '.',
'DEFAULT_ORPHANS': 0, 'DEFAULT_ORPHANS': 0,
'DEFAULT_METADATA': (), 'DEFAULT_METADATA': (),
'FILENAME_METADATA': '(?P<date>\d{4}-\d{2}-\d{2}).*', 'FILENAME_METADATA': '(?P<date>\d{4}-\d{2}-\d{2}).*',
'PATH_METADATA': '',
'FILES_TO_COPY': (), 'FILES_TO_COPY': (),
'DEFAULT_STATUS': 'published', 'DEFAULT_STATUS': 'published',
'ARTICLE_PERMALINK_STRUCTURE': '', 'ARTICLE_PERMALINK_STRUCTURE': '',