This repository has been archived by the owner on Jan 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generatehtml.py
68 lines (60 loc) · 2.8 KB
/
generatehtml.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
import updatedb
from flask import render_template
import core
POST_TEMPLATE = "post.html"
INDEX_TEMPLATE = "index.html"
INDEX_HTML = "index.html"
def create_post(post):
""" Create HTML file for one post from database.
Function takes arguments: string "latest", string "all" or int ID of the post which exist in databse.
At the end, rebuild_index() function called to rebuild index.html main page.
post_ids - array, it can contain even one ID -> then loop will run only once.
post_data - will contain list (array) of the post items from mysql for given post id
post_dict - array, yaml like dictionary of lists of post items
dictionary is useful if you want to write into main page.
"""
post_ids = []
post_dict = []
if post == "latest":
post_ids = updatedb.getID("latest")
elif post == "all":
print("Generating all posts pages.")
post_ids = updatedb.getID("all")
else:
post_ids = [post]
for ids in post_ids:
post_dict.clear()
post_data = updatedb.getContents(ids)
post_dict.append(dict(post_title = post_data[1], post_author = post_data[0], post_url = post_data[3], post_date = post_data[2], post_body = post_data[4]))
print(" loop: write file " + post_data[3] + " for title: " + post_data[1])
write_html(render_post(post_dict), post_data[3])
rebuild_index()
def render_post(dict_list):
""" Renreds html structure of the post with jinja2 template. Expecting dictionary containing only one post. """
print("Rendering html structure of the post.")
return render_template(POST_TEMPLATE, dict_list=dict_list, blog_title=core.blog_title)
def render_index(dict_list):
""" Renders html structure of the post with jinja2 template. Expecting dictionary with all the posts as list. """
print("Rendering index")
return render_template(INDEX_TEMPLATE, dict_list=dict_list, blog_title=core.blog_title)
def write_html(data, filename):
""" Simple function to write contents into file. """
print("Writing file.. " + filename)
fullpath = core.website_dir + filename
html = open(fullpath, "w")
html.write(str(data))
html.close()
print(" closed file "+filename)
def rebuild_index():
""" Rebuilds index page of the blog. """
post_ids = updatedb.getID("all")
post_dict = []
print("Building all articles")
for ids in post_ids:
post_data = updatedb.getContents(ids)
post_dict.append(dict(post_title = post_data[1], post_author = post_data[0], post_url = post_data[3], post_date = post_data[2], post_body = post_data[4]))
print(" queued article id " + post_dict[-1]["post_title"])
write_html(render_index(post_dict), INDEX_HTML)
print("Building index done. Written " + INDEX_HTML)
if __name__ == '__main__':
print("run pythion3 backend.py")