Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
run tests for metadata extraction again
  • Loading branch information
ralsina committed Jul 20, 2015
1 parent 94e695d commit 378ae40
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
35 changes: 11 additions & 24 deletions nikola/post.py
Expand Up @@ -836,36 +836,23 @@ def get_metadata_from_file(source_path, config=None, lang=None):


def _get_metadata_from_file(meta_data):
"""Parse file contents and obtain metadata.
>>> g = _get_metadata_from_file
>>> list(g([]).values())
[]
>>> str(g(["======","FooBar","======"])["title"])
'FooBar'
>>> str(g(["FooBar","======"])["title"])
'FooBar'
>>> str(g(["#FooBar"])["title"])
'FooBar'
>>> str(g([".. title: FooBar"])["title"])
'FooBar'
>>> 'title' in g(["","",".. title: FooBar"])
False
>>> 'title' in g(["",".. title: FooBar"]) # for #520
True
>>> 'title' in g([".. foo: bar","","FooBar", "------"]) # for #1895
True
>>> 1 == 0
True
"""
Extract metadata from a post's source file.
"""
meta = {}
if not meta_data:
return meta

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)))

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

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

meta = {}

# First, get metadata from the beginning of the file,
# up to first empty line
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -14,7 +14,7 @@
class PyTest(TestCommand):
def finalize_options(self):
TestCommand.finalize_options(self)
self.test_args = ['--doctest-modules']
self.test_args = []
self.test_suite = True

def run_tests(self):
Expand Down
13 changes: 13 additions & 0 deletions tests/test_utils.py
Expand Up @@ -322,5 +322,18 @@ def test_dict_input_lang(self):
self.assertEqual(inp['zz'], cn)


def test_get_metadata_from_file():
# These were doctests and not running :-P
from nikola.post import _get_metadata_from_file
g = _get_metadata_from_file
assert list(g([]).values()) == []
assert str(g(["======","FooBar","======"])["title"]) == 'FooBar'
assert str(g(["FooBar","======"])["title"]) == 'FooBar'
assert str(g(["#FooBar"])["title"]) == 'FooBar'
assert str(g([".. title: FooBar"])["title"]) == 'FooBar'
assert 'title' not in g(["","",".. title: FooBar"])
assert 'title' in g(["",".. title: FooBar"])
assert 'title' in g([".. foo: bar","","FooBar", "------"])

if __name__ == '__main__':
unittest.main()

0 comments on commit 378ae40

Please sign in to comment.