From ccda71858eb25cb3c841575dca6f637423d85e8d Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Mon, 12 Oct 2020 16:04:22 +0200 Subject: [PATCH] add function to create the header --- tests/test_header.py | 8 ++++++++ url2markdown/header.py | 23 +++++++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/tests/test_header.py b/tests/test_header.py index ade8f79..d2c878f 100644 --- a/tests/test_header.py +++ b/tests/test_header.py @@ -10,3 +10,11 @@ def test_undefined_topics(create_article): def test_get_topics_from_cli(): header._get_topics_from_cli() assert False + + +def test_dict_to_string(create_article): + article = create_article() + article.keywords = ['foo', 'bar', 'baz'] + raw_header = header._build_header_dict(article) + string = header._build_header_string(raw_header) + assert string =='- meta:\n - topics: [[foo]] [[bar]] [[baz]]\n - authors: [[Daniel Waldmeier]] [[Kei Zuefall]]\n' diff --git a/url2markdown/header.py b/url2markdown/header.py index b08064d..c239912 100644 --- a/url2markdown/header.py +++ b/url2markdown/header.py @@ -8,12 +8,27 @@ def _build_header_dict(article): if article.authors: header['authors'] = article.authors if article.keywords: - header['topics'].append(article.keywords) + if header['topics']: + header['topics'].append(article.keywords) + else: + header['topics'] = article.keywords if not header['topics']: header['topics'] = ['to_categorise'] return header -def build_header(header): - yaml = "" - return yaml +def _build_header_string(raw_header): + header = "- meta:\n" + for key, value in raw_header.items(): + if key: + content = "" + for child in value: + content += " [[" + child + "]]" + header += " - {0}:".format(key) + content + "\n" + return header + + +def build_header(article): + raw_header = _build_header_dict(article) + header = _build_header_string(raw_header) + return header