Skip to content
Permalink
Browse files
Fix #3025 -- add METADATA_VALUE_MAPPING setting
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Apr 14, 2018
1 parent c30320c commit f4ee3827c1d4cafdd755fb8f775b9b37dd82132d
Showing with 35 additions and 2 deletions.
  1. +2 −0 CHANGES.txt
  2. +13 −1 docs/manual.rst
  3. +13 −0 nikola/conf.py.in
  4. +1 −0 nikola/metadata_extractors.py
  5. +1 −0 nikola/post.py
  6. +5 −1 nikola/utils.py
@@ -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
@@ -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
~~~~~~~~~~~~~~~~~~

@@ -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
@@ -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 = {}

@@ -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."""
@@ -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)
@@ -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.

0 comments on commit f4ee382

Please sign in to comment.