Teascade.net/build.py

135 lines
4.5 KiB
Python

# -*- coding: utf-8 -*-
import markdown
import glob
import re
def generate_template():
content_file = open('src/template.html', 'r', encoding="utf-8")
content = content_file.read()
content_file.close()
about_file = open('src/about.md', 'r', encoding="utf-8")
about = about_file.read()
about_file.close()
games_file = open('src/games.md', 'r', encoding="utf-8")
games = games_file.read()
games_file.close()
blog_file = open('src/blog.md', 'r', encoding="utf-8")
blog = blog_file.read()
blog_file.close()
content = content.replace("$about_content$", markdown.markdown(about, output_format="html5"))
content = content.replace("$games_content$", markdown.markdown(games, output_format="html5"))
content = content.replace("$blog_content$", markdown.markdown(blog, output_format="html5"))
return content
def generate_page(template, page):
content = template.replace("$page$", page);
rel_about = "/"
rel_games = "/games"
rel_blog = "/blog"
about_class = 'class="hiddenarea"'
games_class = 'class="hiddenarea"'
blog_class = 'class="hiddenarea"'
blogtext_class = 'class="hiddenarea"'
about_btn_class = ''
games_btn_class = ''
blog_btn_class = ''
if (page == "about"):
rel_about = "#"
about_class = ""
about_btn_class = 'class="active"'
elif (page == "games"):
rel_games = "#"
games_class = ""
games_btn_class = 'class="active"'
elif (page == "blog"):
rel_blog = "#"
blog_class = ""
blog_btn_class = 'class="active"'
elif (page == "blogtext"):
rel_blogtext = "#"
blogtext_class = ""
content = content.replace("$relative_about$", rel_about);
content = content.replace("$relative_games$", rel_games);
content = content.replace("$relative_blog$", rel_blog);
content = content.replace("$about_class$", about_class);
content = content.replace("$games_class$", games_class);
content = content.replace("$blog_class$", blog_class);
content = content.replace("$blogtext_class$", blogtext_class);
content = content.replace("$about_btn_class$", about_btn_class);
content = content.replace("$games_btn_class$", games_btn_class);
content = content.replace("$blog_btn_class$", blog_btn_class);
return content;
def build_blogposts():
blogposts = glob.glob("blogposts-md/*.md")
for path in blogposts:
with open(path, "r", encoding="utf-8") as f:
with open(path.replace("blogposts-md", "blogposts").replace(".md", ".html"), "w+", encoding="utf-8") as f2:
f2.write(markdown.markdown(f.read(), output_format="html5"))
with open("blogposts-md/blogposts.json", "r", encoding="utf-8") as f:
with open("blogposts/blogposts.json", "w+", encoding="utf-8") as f2:
f2.write("window.blogpostDataStr = '" + f.read().replace("\n", "") + "';")
def apply_custom_markdown(markdown):
itch_embed_template = '<iframe frameborder="0" src="https://itch.io/embed/$id$?linkback=true&amp;bg_color=222&amp;fg_color=fff&amp;link_color=ff0091&amp;border_color=303030" width="552" height="167"></iframe>'
regex = re.compile("\[itch\$[0-9]*\]")
while regex.search(markdown) is not None:
search = regex.search(markdown)
split = regex.split(markdown)
curr_id = int(markdown[(search.start() + 6):][:(search.end() - search.start() - 7)])
markdown = split[0] + itch_embed_template.replace("$id$", str(curr_id)) + split[1]
return markdown
def generate():
#md = markdown.markdown("hi **this is** a test markdown _right?_ `wait, let's test _the *code* block_`", output_format="html5")
print("Building blogposts..")
build_blogposts()
print("Done!\n")
print("Generating template..")
template = generate_template()
print("Done!\n")
print("Applying custom markdown..")
template = apply_custom_markdown(template)
print("Done!n")
print("Generating pages..")
with open('index.html', 'w+', encoding="utf-8") as f:
f.write(generate_page(template, "about"))
print("About (index.html) done!")
with open('games/index.html', 'w+', encoding="utf-8") as f:
f.write(generate_page(template, "games"))
print("Games done!")
with open('blog/index.html', 'w+', encoding="utf-8") as f:
f.write(generate_page(template, "blog"))
print("Blog done!")
with open('blogtext/index.html', 'w+', encoding="utf-8") as f:
f.write(generate_page(template, "blogtext"))
print("Blogtext done!")
print("Done!\n")
generate()