url2markdown/url2markdown/header.py

48 lines
1.3 KiB
Python
Raw Normal View History

2020-10-12 21:49:03 +02:00
from datetime import datetime
2020-10-12 15:17:54 +02:00
def _get_topics_from_cli():
pass
def _build_header_dict(article):
header = {}
header['topics'] = _get_topics_from_cli()
2020-10-12 21:49:03 +02:00
header['date'] = []
2020-10-12 15:17:54 +02:00
if article.authors:
header['authors'] = article.authors
if article.keywords:
2020-10-12 16:04:22 +02:00
if header['topics']:
header['topics'].append(article.keywords)
else:
header['topics'] = article.keywords
2020-10-12 15:17:54 +02:00
if not header['topics']:
header['topics'] = ['to_categorise']
2020-10-12 21:49:03 +02:00
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'))
2020-10-12 15:17:54 +02:00
return header
2020-10-12 16:04:22 +02:00
def _build_header_string(raw_header):
header = "- meta:\n"
for key, value in raw_header.items():
2020-10-12 21:49:03 +02:00
if value and type(value) == list:
2020-10-12 16:04:22 +02:00
content = ""
for child in value:
content += " [[" + child + "]]"
2020-10-12 21:49:03 +02:00
header += " - {0}:".format(key) + content + "\n"
elif value:
content = " " + str(value)
header += " - {0}:".format(key) + content + "\n"
2020-10-12 16:04:22 +02:00
return header
def build_header(article):
raw_header = _build_header_dict(article)
header = _build_header_string(raw_header)
return header