-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles.py
66 lines (51 loc) · 1.69 KB
/
files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from .objects import FullArticle
from string import Template
default_template = """# $title
## $description
## Information:
+ Link: $url
+ Source: $source
+ Author: $author
+ Date published: $publish_date
+ Date scraped: $scrape_date
## Article:

$formatted_content
## Tags:
$technical_tags
### Autogenerated tags:
$auto_tags
"""
default_templater = Template(default_template)
def article_to_md(article: FullArticle) -> str:
return default_templater.substitute(**generate_substitution_mapping(article))
def generate_substitution_mapping(
article: FullArticle, obsidian_tags: bool = False
) -> dict[str, str]:
technical_tags: str = ""
auto_tags: str = ""
if article.tags.interesting:
technical_tag_lists = [
f"***{tags.name}***: {' '.join(tags.values)}"
for tags in article.tags.interesting
]
technical_tags = "\n".join(technical_tag_lists)
if article.tags.automatic:
if obsidian_tags:
auto_tags = "[[" + "]] [[".join(article.tags.automatic) + "]]"
else:
auto_tags = " ".join(article.tags.automatic)
return {
"title": article.title,
"description": article.description,
"url": str(article.url),
"image_url": str(article.image_url),
"source": article.source,
"author": article.author or article.source,
"publish_date": article.publish_date.strftime("%d %B %Y, %H:%M"),
"scrape_date": article.inserted_at.strftime("%d %B %Y, %H:%M"),
"formatted_content": article.formatted_content,
"raw_content": article.content,
"technical_tags": technical_tags,
"auto_tags": auto_tags,
}