-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·105 lines (91 loc) · 3.25 KB
/
app.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from flask import Flask, render_template, request
import os
import glob
import markdown
from get_article import get_article
from pathlib import Path
Path(f"{os.getcwd()}/templates/articles").mkdir(parents=True, exist_ok=True)
articles = sorted(glob.glob(f"{os.getcwd()}/articles/**/**/**/*", recursive=False))
try:
for i in range(len(articles) - 1):
if "__pycache__" in articles[i]:
del articles[i]
except IndexError:
pass
article_dirs = articles
articles = []
for article in article_dirs:
if "__pycache__" in article:
continue
print(article)
article = article.split(os.getcwd())[1]
arr = article.split("/")
del arr[0]
article = ".".join(arr).split(".py", maxsplit=1)[0].split("articles.")[1]
dates = arr
del dates[0]
info = get_article(article)
info["year"] = dates[0]
info["month"] = dates[1]
info["day"] = dates[2]
info["endpoint"] = dates[3].split(".py")[0]
info[
"full_endpoint"
] = f"/{info['year']}/{info['month']}/{info['day']}/{info['endpoint']}"
if info is not None:
articles.append(info)
# articles.reverse()
app = Flask(__name__, static_url_path="/public/", static_folder="public")
@app.route("/")
def root():
return render_template("index.html", articles=articles)
@app.route("/<string:year>/<string:month>/<string:day>/<string:article_name>")
def article_route(year, month, day, article_name):
try:
a = to_html(article_name, year, month, day)
return render_template("article.html", content=a)
except FileNotFoundError:
return page_not_found("Error")
@app.errorhandler(404)
def page_not_found(e):
del e
return render_template("404.html", path=request.path)
extension_configs = {"codehilite": {"use_pygments": True}}
def to_html(article_name: str, year, month, day):
found = None
for i in articles:
if (
i["endpoint"] == article_name.lower()
and i["year"] == year
and i["month"].lower() == month.lower()
and i["day"] == day
):
found = i
break
if found is None:
raise FileNotFoundError("Not Found")
try:
found["html"]
except KeyError:
with open(f"markdown/{found['md']}", encoding="utf-8") as content:
content = content.read()
html = markdown.markdown(
content,
extensions=[
"markdown.extensions.codehilite", # Ref: https://python-markdown.github.io/extensions/code_hilite
"markdown.extensions.fenced_code", # Ref: https://python-markdown.github.io/extensions/fenced_code
"markdown.extensions.attr_list", # Ref: https://python-markdown.github.io/extensions/attr_list/
"markdown.extensions.tables", # Ref: https://python-markdown.github.io/extensions/tables/
],
)
found["html"] = found["md"].split(".md")[0] + ".html"
with open(
f"templates/articles/{found['html']}", "w", encoding="utf-8"
) as f:
f.seek(0)
f.write(html)
f.truncate()
f.close()
return found
if __name__ == "__main__":
app.run(port=9756, debug=True)