Skip to content

Commit

Permalink
Get metadata from reSt docinfo and title. Don't get title from regexps
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberto Alsina committed May 29, 2017
1 parent 0f2a3c3 commit 5385b80
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
32 changes: 32 additions & 0 deletions nikola/plugins/compile/rest/__init__.py
Expand Up @@ -59,6 +59,38 @@ class CompileRest(PageCompiler):
demote_headers = True
logger = None

def read_metadata(self, post, file_metadata_regexp=None, unslugify_titles=False, lang=None):
"""Read the metadata from a post, and return a metadata dict."""
if lang is None:
lang = LocaleBorg().current_lang
source = post.translated_source_path(lang)

with io.open(source, 'r', encoding='utf-8') as inf:
document = docutils.core.publish_doctree(
inf.read(), reader_name='standalone',
settings_overrides={'expose_internals':
['refnames', 'do_not_expose'],
'report_level': 5})
meta = {}
if 'title' in document:
meta['title'] = document['title']
for docinfo in document.traverse(docutils.nodes.docinfo):
for element in docinfo.children:
if element.tagname == 'field': # custom fields (e.g. summary)
name_elem, body_elem = element.children
name = name_elem.astext()
value = body_elem.astext()
elif element.tagname == 'authors': # author list
name = element.tagname
value = [element.astext() for element in element.children]
else: # standard fields (e.g. address)
name = element.tagname
value = element.astext()
name = name.lower()

meta[name] = value
return meta

def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
"""Compile reST into HTML strings."""
# If errors occur, this will be added to the line number reported by
Expand Down
24 changes: 0 additions & 24 deletions nikola/post.py
Expand Up @@ -1005,24 +1005,6 @@ def get_metadata_from_file(source_path, config=None, lang=None):
string.punctuation)))


def _get_title_from_contents(meta_data):
"""Extract title from file contents, LAST RESOURCE."""
piece = meta_data[:]
title = None
for i, line in enumerate(piece):
if re_rst_title.findall(line) and i > 0:
title = meta_data[i - 1].strip()
break
if (re_rst_title.findall(line) and i >= 0 and
re_rst_title.findall(meta_data[i + 2])):
title = meta_data[i + 1].strip()
break
if re_md_title.findall(line):
title = re_md_title.findall(line)[0]
break
return title


def _get_metadata_from_file(meta_data):
"""Extract metadata from a post's source file."""
meta = {}
Expand Down Expand Up @@ -1065,12 +1047,6 @@ def _get_metadata_from_file(meta_data):
if match[0]:
meta[match[0]] = match[1]

# If we have no title, try to get it from document
if 'title' not in meta:
t = _get_title_from_contents(meta_data)
if t is not None:
meta['title'] = t

return meta


Expand Down

0 comments on commit 5385b80

Please sign in to comment.