rewrite the header functions into a class

This commit is contained in:
Andreas Zweili 2020-10-12 22:09:47 +02:00
parent e088d91fda
commit aef724aa43
2 changed files with 44 additions and 45 deletions

View File

@ -3,18 +3,14 @@ import url2markdown.header as header
def test_undefined_topics(create_article):
article = create_article()
result = header._build_header_dict(article)
string_header = header.Header(article, [])
result = string_header._build_header_dict()
assert result['topics'][0] == 'to_categorise'
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)
string_header = header.Header(article, [])
string = string_header.build_header()
assert string == '- meta:\n - topics: [[foo]] [[bar]] [[baz]]\n - date: [[2020-10-09]]\n - authors: [[Daniel Waldmeier]] [[Kei Zuefall]]\n - url: https://www.20min.ch/story/corona-zahlen-auf-einen-blick-803083076953\n'

View File

@ -1,47 +1,50 @@
from datetime import datetime
def _get_topics_from_cli():
pass
class Header():
def __init__(self, article, cli_topics):
self.article = article
self.cli_topics = cli_topics
def _build_header_dict(article):
header = {}
header['topics'] = _get_topics_from_cli()
header['date'] = []
if article.authors:
header['authors'] = article.authors
if article.keywords:
if header['topics']:
header['topics'].append(article.keywords)
def _build_header_dict(self):
header = {}
header['topics'] = self.cli_topics
header['date'] = []
if self.article.authors:
header['authors'] = self.article.authors
if self.article.keywords:
if header['topics']:
header['topics'].append(self.article.keywords)
else:
header['topics'] = self.article.keywords
if not header['topics']:
header['topics'] = ['to_categorise']
if self.article.url:
header['url'] = self.article.url
if self.article.publish_date:
header['date'].append(
self.article.publish_date.strftime('%Y-%m-%d'))
else:
header['topics'] = article.keywords
if not header['topics']:
header['topics'] = ['to_categorise']
if article.url:
header['url'] = article.url
if article.publish_date:
header['date'].append(article.publish_date.strftime('%Y-%m-%d'))
else:
header['date'].append(datetime.now().strftime('%Y-%m-%d'))
return header
header['date'].append(datetime.now().strftime('%Y-%m-%d'))
return header
def _build_header_string(raw_header):
header = "- meta:\n"
for key, value in raw_header.items():
if value and type(value) == list:
content = ""
for child in value:
content += " [[" + child + "]]"
header += " - {0}:".format(key) + content + "\n"
elif value:
content = " " + str(value)
header += " - {0}:".format(key) + content + "\n"
return header
def _build_header_string(self, raw_header):
header = "- meta:\n"
for key, value in raw_header.items():
if value and type(value) == list:
content = ""
for child in value:
content += " [[" + child + "]]"
header += " - {0}:".format(key) + content + "\n"
elif value:
content = " " + str(value)
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
def build_header(self):
raw_header = self._build_header_dict()
self.header = self._build_header_string(raw_header)
return self.header