-
Notifications
You must be signed in to change notification settings - Fork 2
/
blog.py
144 lines (109 loc) · 3.58 KB
/
blog.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from flask import Blueprint, render_template, send_from_directory, send_file
import os
import os.path
import markdown
import datetime
from typing import List, Tuple
import json
from gradient import generate_social_hero
from io import BytesIO
blog_blueprint = Blueprint(
"blog_blueprint", __name__, template_folder="templates", url_prefix="/blog"
)
POSTS_DIR = "blog/posts/"
class Post(object):
date: datetime.date
publishdate: datetime.date
title: str
html: str
markdown: str
summary: str
slug: str
hero: str
color1: Tuple[int]
color2: Tuple[int]
tags: List[str]
md_object: markdown.Markdown
def post_exists(post):
return os.path.exists(f"{POSTS_DIR}/{post}/index.md")
def get_post(post):
contents = open(f"{POSTS_DIR}/{post}/index.md", "r").read()
# https://python-markdown.github.io/extensions/
# https://facelessuser.github.io/pymdown-extensions/
# md = markdown.Markdown(extensions=["meta", "tables", "pymdownx.magiclink", "pymdownx.highlight"])
md = markdown.Markdown(extensions=["meta", "tables", "pymdownx.magiclink", "pymdownx.highlight", "pymdownx.superfences"],extension_configs={'pymdownx.highlight': {
'noclasses': True,
'use_pygments': False,
# 'pygments_style': 'material'
}})
def _g(k):
if k in md.Meta and len(md.Meta[k]) > 0:
return md.Meta[k][0]
return ""
rc = Post()
rc.markdown = contents
rc.md_object = md
rc.html = md.convert(contents)
rc.date = _g("date")
rc.publishdate = _g("publishdate")
rc.title = _g("title")
rc.summary = _g("summary")
rc.slug = post
rc.tags = json.loads(_g("tags"))
rc.hero = _g("hero")
return rc
@blog_blueprint.get("/")
def blog():
posts = os.listdir(POSTS_DIR)
post_details = []
for p in posts:
if post_exists(p):
post = get_post(p)
post_details.append(post)
post_details.sort(key=lambda p: p.publishdate, reverse=True)
return render_template("blog.html", posts=post_details)
@blog_blueprint.get("/<post>/")
def blog_post(post):
md = markdown.Markdown(extensions=["meta", "tables", "pymdownx.magiclink", "pymdownx.highlight", "pymdownx.superfences"],extension_configs={'pymdownx.highlight': {
'noclasses': True,
'use_pygments': False,
# 'pygments_style': 'material'
}})
if not post_exists(post):
return "", 404
full_post = get_post(post)
return render_template("blog_post.html", post=full_post)
@blog_blueprint.get("/<post>/hero.png")
def blog_hero(post):
post = get_post(post)
gradients = [
(0xD4145A, 0xFBB03B),
(0x2E3192, 0x1BFFFF),
(0x662D8C, 0xED1E79),
(0x614385, 0x516395),
(0x000000, 0x0f9b0f),
(0x000000, 0xEB5757),
]
directions = ['horizontal']
def _rgb(color_int):
# Extract the Red component
red = (color_int >> 16) & 0xFF
# Extract the Green component
green = (color_int >> 8) & 0xFF
# Extract the Blue component
blue = color_int & 0xFF
return red, green, blue
n = hash(post.title) % len(gradients)
direction = directions[hash(post.title) % len(directions)]
gradient = gradients[n]
c1 = _rgb(gradient[0])
c2 = _rgb(gradient[1])
img = generate_social_hero(c1,c2,post.title,direction)
img_io = BytesIO()
img.save(img_io, 'PNG')
img_io.seek(0)
return send_file(img_io, mimetype='image/png')
@blog_blueprint.get("/<post>/<path:asset>")
def blog_post_asset(post, asset):
dirname = f"blog/posts/{post}"
return send_from_directory(dirname, asset)