Skip to content
Browse files
Get metadata from reSt docinfo and title. Don't get title from regexps
  • Loading branch information
Roberto Alsina committed May 29, 2017
1 parent 0f2a3c3 commit 5385b808d9922b4311e2e5802478287e479d9dcd
Showing 2 changed files with 32 additions and 24 deletions.
@@ -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, 'r', encoding='utf-8') as inf:
document = docutils.core.publish_doctree(, reader_name='standalone',
['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
@@ -1005,24 +1005,6 @@ def get_metadata_from_file(source_path, config=None, lang=None):

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()
if (re_rst_title.findall(line) and i >= 0 and
re_rst_title.findall(meta_data[i + 2])):
title = meta_data[i + 1].strip()
if re_md_title.findall(line):
title = re_md_title.findall(line)[0]
return title

def _get_metadata_from_file(meta_data):
"""Extract metadata from a post's source file."""
meta = {}
@@ -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

0 comments on commit 5385b80

Please sign in to comment.