-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
69 lines (54 loc) · 1.94 KB
/
core.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
67
68
69
import os
from contextlib import suppress
from operator import attrgetter
from pathlib import Path
from bs4 import BeautifulSoup
from markdown import markdown
from models import Article
articles = Path(__file__).parent / "articles"
articles.mkdir(exist_ok=True)
def article_name_to_file_name(aname: str) -> str:
return aname.replace(" ", "_") + ".md"
def save_article(article: Article) -> int:
amount_articles = len(os.listdir(articles))
with open(articles / f"{amount_articles}.json", "w+") as fout:
print(article.json(), file=fout)
return amount_articles
def get_article(article_num: int) -> Article:
with suppress(FileNotFoundError):
with open(articles / f"{article_num}.json", "r") as fin:
return Article.parse_raw(fin.read())
return Article(title="Not found", body="may guapo luke guide you")
def get_html_article(article_num: int) -> str:
article = get_article(article_num)
html_body = __remove_tag(
__center_images(markdown(article.body, extensions=["extra"])), "h1"
)
return f"""
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>{article.title} - ALSWiki</title>
<link rel="stylesheet" href="https://alswiki.github.io/index.css" />
</head>
<body>
<main class="md">
<div class="article">
<h1>{article.title}</h1>
{html_body}
</div>
</main>
</body>
</html>
"""
def __center_images(html: str) -> str:
soup = BeautifulSoup(html, "html.parser")
for p in map(attrgetter("parent"), soup.select("p > img")):
p["align"] = "center"
return str(soup)
def __remove_tag(html: str, tag: str) -> str:
soup = BeautifulSoup(html, "html.parser")
for el in soup.select(tag):
el.replace_with("")
return str(soup)