Skip to content
Permalink
Browse files
Don’t silence syntax errors and other exceptions that occur while rea…
…ding metadata
  • Loading branch information
Kwpolska committed Apr 14, 2018
1 parent 8b43762 commit e03fc62e13cd885fc29b91dc08c02648dd637066
Showing with 26 additions and 23 deletions.
  1. +2 −0 CHANGES.txt
  2. +1 −1 nikola/metadata_extractors.py
  3. +23 −22 nikola/post.py
@@ -40,6 +40,8 @@ Features
Bugfixes
--------

* Don’t silence syntax errors and other exceptions that occur while
reading metadata
* Use documented dateutil API for time zone list (Issue #3006)
* Handle trailing slash redirects with query strings correctly in
``nikola serve`` (Issue #3000)
@@ -126,7 +126,7 @@ def default_metadata_extractors_by() -> dict:
return d


def _register_default(extractor: MetadataExtractor) -> MetadataExtractor:
def _register_default(extractor: type) -> type:
"""Register a default extractor."""
_default_extractors.append(extractor())
return extractor
@@ -952,28 +952,6 @@ def get_metadata_from_file(source_path, post, config, lang, metadata_extractors_
source_path += '.' + lang
with io.open(source_path, "r", encoding="utf-8-sig") as meta_file:
source_text = meta_file.read()

meta = {}
used_extractor = None
for priority in metadata_extractors.MetaPriority:
found_in_priority = False
for extractor in metadata_extractors_by['priority'].get(priority, []):
if not metadata_extractors.check_conditions(post, source_path, extractor.conditions, config, source_text):
continue
extractor.check_requirements()
new_meta = extractor.extract_text(source_text)
if new_meta:
found_in_priority = True
used_extractor = extractor
# Map metadata from other platforms to names Nikola expects (Issue #2817)
map_metadata(new_meta, extractor.map_from, config)

meta.update(new_meta)
break

if found_in_priority:
break
return meta, used_extractor
except (UnicodeDecodeError, UnicodeEncodeError):
msg = 'Error reading {0}: Nikola only supports UTF-8 files'.format(source_path)
LOGGER.error(msg)
@@ -982,6 +960,29 @@ def get_metadata_from_file(source_path, post, config, lang, metadata_extractors_
return {}, None


meta = {}
used_extractor = None
for priority in metadata_extractors.MetaPriority:
found_in_priority = False
for extractor in metadata_extractors_by['priority'].get(priority, []):
if not metadata_extractors.check_conditions(post, source_path, extractor.conditions, config, source_text):
continue
extractor.check_requirements()
new_meta = extractor.extract_text(source_text)
if new_meta:
found_in_priority = True
used_extractor = extractor
# Map metadata from other platforms to names Nikola expects (Issue #2817)
map_metadata(new_meta, extractor.map_from, config)

meta.update(new_meta)
break

if found_in_priority:
break
return meta, used_extractor


def get_metadata_from_meta_file(path, post, config, lang, metadata_extractors_by=None):
"""Take a post path, and gets data from a matching .meta file."""
meta_path = os.path.splitext(path)[0] + '.meta'

0 comments on commit e03fc62

Please sign in to comment.