Skip to content

Commit

Permalink
Fix #3025 -- add METADATA_VALUE_MAPPING setting
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Apr 14, 2018
1 parent c30320c commit f4ee382
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Expand Up @@ -4,6 +4,8 @@ New in master
Features
--------

* New ``METADATA_VALUE_MAPPING`` setting to allow for flexible global
modification of metadata (Issue #3025)
* Recognize both TEASER_END and (new) END_TEASER (Issue #3010)
* New MARKDOWN_EXTENSION_CONFIGS setting (Issue #2970)
* Replace ``flowr.js`` with ``justified-layout.js`` by Flickr
Expand Down
14 changes: 13 additions & 1 deletion docs/manual.rst
Expand Up @@ -580,6 +580,18 @@ For Hugo, use:
The following source names are supported: ``yaml``, ``toml``, ``rest_docinfo``, ``markdown_metadata``.

Additionally, you can use ``METADATA_VALUE_MAPPING`` to perform any extra conversions on metadata for **all** posts of a given format (``nikola`` metadata is also supported). A few examples:

.. code:: python
METADATA_VALUE_MAPPING = {
"yaml": {"keywords": lambda value: ', '.join(value)}, # yaml: 'keywords' list -> str
"nikola": {
"widgets": lambda value: value.split(', '), # nikola: 'widgets' comma-separated string -> list
"tags": str.lower # nikola: force lowercase 'tags' (input would be string)
}
}
Multilingual posts
~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -1772,7 +1784,7 @@ to one of "disqus", "intensedebate", "livefyre", "moot", "facebook", "isso" or "
* For isso, it is the URL of isso (must be world-accessible, encoded with
Punycode (if using Internationalized Domain Names) and **have a trailing slash**,
default ``http://localhost:8080/``)
* For commento it's the URL of the commento instance as required by the ``serverUrl``
* For commento it's the URL of the commento instance as required by the ``serverUrl``
parameter in commento's documentation.

To use comments in a visible site, you should register with the service and
Expand Down
13 changes: 13 additions & 0 deletions nikola/conf.py.in
Expand Up @@ -1179,6 +1179,19 @@ MARKDOWN_EXTENSIONS = ['markdown.extensions.fenced_code', 'markdown.extensions.c
# }
# Other examples: https://getnikola.com/handbook.html#mapping-metadata-from-other-formats

# Map metadata between types/values. (Runs after METADATA_MAPPING.)
# Supported formats: nikola, ${_METADATA_MAPPING_FORMATS}
# The value on the right should be a dict of callables.
# METADATA_VALUE_MAPPING = {}
# Examples:
# METADATA_VALUE_MAPPING = {
# "yaml": {"keywords": lambda value: ', '.join(value)}, # yaml: 'keywords' list -> str
# "nikola": {
# "widgets": lambda value: value.split(', '), # nikola: 'widgets' comma-separated string -> list
# "tags": str.lower # nikola: force lowercase 'tags' (input would be string)
# }
# }

# Additional metadata that is added to a post when creating a new_post
# ADDITIONAL_METADATA = {}

Expand Down
1 change: 1 addition & 0 deletions nikola/metadata_extractors.py
Expand Up @@ -142,6 +142,7 @@ class NikolaMetadata(MetadataExtractor):
supports_write = True
split_metadata_re = re.compile('\n\n')
nikola_re = re.compile(r'^\s*\.\. (.*?): (.*)')
map_from = 'nikola' # advertised in values mapping only

def _extract_metadata_from_text(self, source_text: str) -> dict:
"""Extract metadata from text."""
Expand Down
1 change: 1 addition & 0 deletions nikola/post.py
Expand Up @@ -973,6 +973,7 @@ def get_metadata_from_file(source_path, post, config, lang, metadata_extractors_
found_in_priority = True
used_extractor = extractor
# Map metadata from other platforms to names Nikola expects (Issue #2817)
# Map metadata values (Issue #3025)
map_metadata(new_meta, extractor.map_from, config)

meta.update(new_meta)
Expand Down
6 changes: 5 additions & 1 deletion nikola/utils.py
Expand Up @@ -1932,12 +1932,16 @@ def rss_writer(rss_obj, output_path):
def map_metadata(meta, key, config):
"""Map metadata from other platforms to Nikola names.
This uses the METADATA_MAPPING setting (via ``config``) and modifies the dict in place.
This uses the METADATA_MAPPING and METADATA_VALUE_MAPPING settings (via ``config``) and modifies the dict in place.
"""
for foreign, ours in config.get('METADATA_MAPPING', {}).get(key, {}).items():
if foreign in meta:
meta[ours] = meta[foreign]

for meta_key, hook in config.get('METADATA_VALUE_MAPPING', {}).get(key, {}).items():
if meta_key in meta:
meta[meta_key] = hook(meta[meta_key])


class ClassificationTranslationManager(object):
"""Keeps track of which classifications could be translated as which others.
Expand Down

0 comments on commit f4ee382

Please sign in to comment.