Skip to content
Permalink
Browse files

Address review by @felixfontein

Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Jul 7, 2017
1 parent e5fd904 commit 932ffecb9a08a8075adf4e5f0bf0e9897d2bacfb
Showing with 16 additions and 9 deletions.
  1. +6 −2 nikola/metadata_extractors.py
  2. +2 −2 nikola/plugin_categories.py
  3. +3 −1 nikola/post.py
  4. +5 −4 nikola/utils.py
@@ -73,7 +73,7 @@ def check_conditions(post, filename: str, conditions: list, config: dict, source
"""Check the conditions for a metadata extractor."""
for ct, arg in conditions:
if any((
ct == MetaCondition.config_bool and (arg not in config or (arg in config and not config[arg])),
ct == MetaCondition.config_bool and not config.get(arg, False),
ct == MetaCondition.config_present and arg not in config,
ct == MetaCondition.extension and not filename.endswith(arg),
ct == MetaCondition.compiler and post.compiler.name != arg,
@@ -141,9 +141,10 @@ class NikolaMetadata(MetadataExtractor):
priority = MetaPriority.normal
supports_write = True
split_metadata_re = re.compile('\n\n')
nikola_re = re.compile('^\s*\.\. (.*?): (.*)')
nikola_re = re.compile(r'^\s*\.\. (.*?): (.*)')

def _extract_metadata_from_text(self, source_text: str) -> dict:
"""Extract metadata from text."""
outdict = {}
for line in source_text.split('\n'):
match = self.nikola_re.match(line)
@@ -153,6 +154,7 @@ def _extract_metadata_from_text(self, source_text: str) -> dict:

def write_metadata(self, metadata: dict, comment_wrap=False) -> str:
"""Write metadata in this extractor’s format."""
metadata = metadata.copy()
order = ('title', 'slug', 'date', 'tags', 'category', 'link', 'description', 'type')
f = '.. {0}: {1}'
meta = []
@@ -187,6 +189,7 @@ class YAMLMetadata(MetadataExtractor):
priority = MetaPriority.specialized

def _extract_metadata_from_text(self, source_text: str) -> dict:
"""Extract metadata from text."""
import yaml
meta = yaml.safe_load(source_text[4:])
# We expect empty metadata to be '', not None
@@ -215,6 +218,7 @@ class TOMLMetadata(MetadataExtractor):
priority = MetaPriority.specialized

def _extract_metadata_from_text(self, source_text: str) -> dict:
"""Extract metadata from text."""
import toml
return toml.loads(source_text[4:])

@@ -406,7 +406,7 @@ def _extract_metadata_from_text(self, source_text: str) -> dict:
def split_metadata_from_text(self, source_text: str) -> (str, str):
"""Split text into metadata and content (both strings)."""
if self.split_metadata_re is None:
return source_text
return source_text, source_text
else:
split_result = self.split_metadata_re.split(source_text.lstrip(), maxsplit=1)
if len(split_result) == 1:
@@ -416,7 +416,7 @@ def split_metadata_from_text(self, source_text: str) -> (str, str):
return split_result[0], split_result[-1]

def extract_text(self, source_text: str) -> dict:
"""Split file, return metadata and the content."""
"""Extract metadata from text (also calls ``split_metadata_from_text``)."""
split = self.split_metadata_from_text(source_text)
meta = self._extract_metadata_from_text(split[0])
return meta
@@ -1006,7 +1006,9 @@ def get_meta(post, lang):
used_extractor = None

config = getattr(post, 'config', None)
metadata_extractors_by = getattr(post, 'metadata_extractors_by', metadata_extractors.default_metadata_extractors_by())
metadata_extractors_by = getattr(post, 'metadata_extractors_by')
if metadata_extractors_by is None:
metadata_extractors_by = metadata_extractors.default_metadata_extractors_by()

# If meta file exists, use it
meta.update(get_metadata_from_meta_file(post.metadata_path, post, config, lang, metadata_extractors_by))
@@ -1442,8 +1442,11 @@ def write_metadata(data, metadata_format=None, comment_wrap=False, site=None, co
Recommended usage: pass `site`, `comment_wrap`, and optionally `compiler`. Other options are for backwards compatibility.
"""
# API compatibility
if metadata_format is None:
if metadata_format is None and site is not None:
metadata_format = site.config.get('METADATA_FORMAT', 'nikola').lower()
if metadata_format is None:
metadata_format = 'nikola'

if site is None:
import nikola.metadata_extractors
metadata_extractors_by = nikola.metadata_extractors.default_metadata_extractors_by()
@@ -1465,12 +1468,10 @@ def write_metadata(data, metadata_format=None, comment_wrap=False, site=None, co
if extractor and extractor.supports_write:
extractor.check_requirements()
return extractor.write_metadata(data, comment_wrap)
elif metadata_format is None:
pass # Quiet fallback to Nikola
else:
LOGGER.warn('Writing METADATA_FORMAT %s is not supported, using "nikola" format', metadata_format)

if metadata_format not in ('rest_docinfo', 'markdown_meta'):
if metadata_format not in ('nikola', 'rest_docinfo', 'markdown_meta'):
LOGGER.warn('Unknown METADATA_FORMAT %s, using "nikola" format', metadata_format)

if metadata_format == 'rest_docinfo':

0 comments on commit 932ffec

Please sign in to comment.
You can’t perform that action at this time.