Skip to content

Commit

Permalink
fix #1898
Browse files Browse the repository at this point in the history
  • Loading branch information
ralsina committed Jul 20, 2015
1 parent 105446b commit 74f16c1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion CHANGES.txt
Expand Up @@ -4,7 +4,7 @@ New in master
Bugfixes
--------

* Fix reSt and markdown title extraction from documents (Issue #1895)
* Fix reSt and markdown title extraction from documents (Issue #1895, #1898)
* Minor improvements in the extending document.
* Re-add the hack to kill nikola auto on ^C (Issue #1893)

Expand Down
44 changes: 21 additions & 23 deletions nikola/post.py
Expand Up @@ -857,40 +857,38 @@ def _get_metadata_from_file(meta_data):
True
"""

# Skip up to one empty line at the beginning (for txt2tags)

if not meta_data[0]:
meta_data = meta_data[1:]

meta = {}
seen_meta_flag = False
re_md_title = re.compile(r'^{0}([^{0}].*)'.format(re.escape('#')))
# Assuming rst titles are going to be at least 4 chars long
# otherwise this detects things like ''' wich breaks other markups.
re_rst_title = re.compile(r'^([{0}]{{4,}})'.format(re.escape(
string.punctuation)))

# First, get metadata from the beginning of the file,
# up to first empty line

for i, line in enumerate(meta_data):
# txt2tags requires an empty line at the beginning
# and since we are here because it's a 1-file post
# let's be flexible on what we accept, so, skip empty
# first lines.
if not line and not seen_meta_flag and i > 0:
if not line:
break
if 'title' not in meta:
match = re_meta(line, 'title')
if match[0]:
meta['title'] = match[1]
if 'title' not in meta:
match = re_meta(line)
if match[0]:
meta[match[0]] = match[1]
seen_meta_flag = True

# If we have no title, try to get it from document
if 'title' not in meta:
for j, line in enumerate(meta_data[i:]):
if re_rst_title.findall(line) and i > 0:
meta['title'] = meta_data[i - 1].strip()
if 'title' not in meta:
break
if (re_rst_title.findall(line) and i >= 0 and
re_rst_title.findall(meta_data[i + 2])):
meta['title'] = meta_data[i + 1].strip()
if 'title' not in meta:
break
if re_md_title.findall(line):
meta['title'] = re_md_title.findall(line)[0]

match = re_meta(line)
if match[0]:
meta[match[0]] = match[1]
seen_meta_flag = True
break

return meta

Expand Down

0 comments on commit 74f16c1

Please sign in to comment.