Skip to content

Commit 5385b80

Browse files
author
Roberto Alsina
committedMay 29, 2017
Get metadata from reSt docinfo and title. Don't get title from regexps
1 parent 0f2a3c3 commit 5385b80

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed
 

‎nikola/plugins/compile/rest/__init__.py

+32
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,38 @@ class CompileRest(PageCompiler):
5959
demote_headers = True
6060
logger = None
6161

62+
def read_metadata(self, post, file_metadata_regexp=None, unslugify_titles=False, lang=None):
63+
"""Read the metadata from a post, and return a metadata dict."""
64+
if lang is None:
65+
lang = LocaleBorg().current_lang
66+
source = post.translated_source_path(lang)
67+
68+
with io.open(source, 'r', encoding='utf-8') as inf:
69+
document = docutils.core.publish_doctree(
70+
inf.read(), reader_name='standalone',
71+
settings_overrides={'expose_internals':
72+
['refnames', 'do_not_expose'],
73+
'report_level': 5})
74+
meta = {}
75+
if 'title' in document:
76+
meta['title'] = document['title']
77+
for docinfo in document.traverse(docutils.nodes.docinfo):
78+
for element in docinfo.children:
79+
if element.tagname == 'field': # custom fields (e.g. summary)
80+
name_elem, body_elem = element.children
81+
name = name_elem.astext()
82+
value = body_elem.astext()
83+
elif element.tagname == 'authors': # author list
84+
name = element.tagname
85+
value = [element.astext() for element in element.children]
86+
else: # standard fields (e.g. address)
87+
name = element.tagname
88+
value = element.astext()
89+
name = name.lower()
90+
91+
meta[name] = value
92+
return meta
93+
6294
def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
6395
"""Compile reST into HTML strings."""
6496
# If errors occur, this will be added to the line number reported by

‎nikola/post.py

-24
Original file line numberDiff line numberDiff line change
@@ -1005,24 +1005,6 @@ def get_metadata_from_file(source_path, config=None, lang=None):
10051005
string.punctuation)))
10061006

10071007

1008-
def _get_title_from_contents(meta_data):
1009-
"""Extract title from file contents, LAST RESOURCE."""
1010-
piece = meta_data[:]
1011-
title = None
1012-
for i, line in enumerate(piece):
1013-
if re_rst_title.findall(line) and i > 0:
1014-
title = meta_data[i - 1].strip()
1015-
break
1016-
if (re_rst_title.findall(line) and i >= 0 and
1017-
re_rst_title.findall(meta_data[i + 2])):
1018-
title = meta_data[i + 1].strip()
1019-
break
1020-
if re_md_title.findall(line):
1021-
title = re_md_title.findall(line)[0]
1022-
break
1023-
return title
1024-
1025-
10261008
def _get_metadata_from_file(meta_data):
10271009
"""Extract metadata from a post's source file."""
10281010
meta = {}
@@ -1065,12 +1047,6 @@ def _get_metadata_from_file(meta_data):
10651047
if match[0]:
10661048
meta[match[0]] = match[1]
10671049

1068-
# If we have no title, try to get it from document
1069-
if 'title' not in meta:
1070-
t = _get_title_from_contents(meta_data)
1071-
if t is not None:
1072-
meta['title'] = t
1073-
10741050
return meta
10751051

10761052

0 commit comments

Comments
 (0)
Failed to load comments.