Skip to content

Commit

Permalink
Don’t silence syntax errors and other exceptions that occur while rea…
Browse files Browse the repository at this point in the history
…ding metadata
  • Loading branch information
Kwpolska committed Apr 14, 2018
1 parent 8b43762 commit e03fc62
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion nikola/metadata_extractors.py
Expand Up @@ -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
Expand Down
45 changes: 23 additions & 22 deletions nikola/post.py
Expand Up @@ -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)
Expand All @@ -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'
Expand Down

0 comments on commit e03fc62

Please sign in to comment.