-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfurther_reading.py
47 lines (35 loc) · 1.29 KB
/
further_reading.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
from src.dataclasses.content_data import ContentData
# TODO: Fetch link titles?
class FurtherReadingLinksPreprocessor:
@classmethod
def process_page(cls, page):
cls.process_entity(page)
@classmethod
def process_entity(cls, entity):
data: ContentData = entity.data
if not cls.is_supported_content(data):
return
links = data.meta.get("links", None)
if links is None:
return
if type(links) is not list:
print(
f" - [FURTHER READING PREPROCESS] meta links are not valid. Should be a list"
)
further_reading_md = "\n**Further Reading**:\n\n"
for link in links:
if type(link) is not dict:
return
link_name = link.get("name")
link_url = link.get("url")
link_md = f"- [{link_name}]({link_url})"
further_reading_md += f"{link_md}\n"
data.content = f"{data.content}\n{further_reading_md}"
print(f' - [PREPROCESS] Rendered further reading section for "{data.title}"')
@classmethod
def is_supported_content(cls, data: ContentData):
if not isinstance(data, ContentData):
return False
if data.content == "":
return False
return True