rewrite the header functions into a class
This commit is contained in:
parent
e088d91fda
commit
aef724aa43
|
@ -3,18 +3,14 @@ import url2markdown.header as header
|
||||||
|
|
||||||
def test_undefined_topics(create_article):
|
def test_undefined_topics(create_article):
|
||||||
article = 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'
|
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):
|
def test_dict_to_string(create_article):
|
||||||
article = create_article()
|
article = create_article()
|
||||||
article.keywords = ['foo', 'bar', 'baz']
|
article.keywords = ['foo', 'bar', 'baz']
|
||||||
raw_header = header._build_header_dict(article)
|
string_header = header.Header(article, [])
|
||||||
string = header._build_header_string(raw_header)
|
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'
|
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'
|
||||||
|
|
|
@ -1,47 +1,50 @@
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
def _get_topics_from_cli():
|
class Header():
|
||||||
pass
|
def __init__(self, article, cli_topics):
|
||||||
|
self.article = article
|
||||||
|
self.cli_topics = cli_topics
|
||||||
|
|
||||||
|
|
||||||
def _build_header_dict(article):
|
def _build_header_dict(self):
|
||||||
header = {}
|
header = {}
|
||||||
header['topics'] = _get_topics_from_cli()
|
header['topics'] = self.cli_topics
|
||||||
header['date'] = []
|
header['date'] = []
|
||||||
if article.authors:
|
if self.article.authors:
|
||||||
header['authors'] = article.authors
|
header['authors'] = self.article.authors
|
||||||
if article.keywords:
|
if self.article.keywords:
|
||||||
if header['topics']:
|
if header['topics']:
|
||||||
header['topics'].append(article.keywords)
|
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:
|
else:
|
||||||
header['topics'] = article.keywords
|
header['date'].append(datetime.now().strftime('%Y-%m-%d'))
|
||||||
if not header['topics']:
|
return header
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def _build_header_string(raw_header):
|
def _build_header_string(self, raw_header):
|
||||||
header = "- meta:\n"
|
header = "- meta:\n"
|
||||||
for key, value in raw_header.items():
|
for key, value in raw_header.items():
|
||||||
if value and type(value) == list:
|
if value and type(value) == list:
|
||||||
content = ""
|
content = ""
|
||||||
for child in value:
|
for child in value:
|
||||||
content += " [[" + child + "]]"
|
content += " [[" + child + "]]"
|
||||||
header += " - {0}:".format(key) + content + "\n"
|
header += " - {0}:".format(key) + content + "\n"
|
||||||
elif value:
|
elif value:
|
||||||
content = " " + str(value)
|
content = " " + str(value)
|
||||||
header += " - {0}:".format(key) + content + "\n"
|
header += " - {0}:".format(key) + content + "\n"
|
||||||
return header
|
return header
|
||||||
|
|
||||||
|
|
||||||
def build_header(article):
|
def build_header(self):
|
||||||
raw_header = _build_header_dict(article)
|
raw_header = self._build_header_dict()
|
||||||
header = _build_header_string(raw_header)
|
self.header = self._build_header_string(raw_header)
|
||||||
return header
|
return self.header
|
||||||
|
|
Loading…
Reference in New Issue