Teascade.net/build.py

147 lines
5.1 KiB
Python
Raw Normal View History

2016-11-03 10:54:57 +01:00
# -*- coding: utf-8 -*-
2016-11-03 00:24:27 +01:00
import markdown
import glob
2016-11-03 19:10:38 +01:00
import re
2016-11-03 00:24:27 +01:00
def generate_template():
2016-11-03 10:54:57 +01:00
content_file = open('src/template.html', 'r', encoding="utf-8")
2016-11-03 00:24:27 +01:00
content = content_file.read()
content_file.close()
2016-11-03 10:54:57 +01:00
about_file = open('src/about.md', 'r', encoding="utf-8")
2016-11-03 00:24:27 +01:00
about = about_file.read()
about_file.close()
2016-11-03 10:54:57 +01:00
games_file = open('src/games.md', 'r', encoding="utf-8")
2016-11-03 00:24:27 +01:00
games = games_file.read()
games_file.close()
2016-11-03 10:54:57 +01:00
blog_file = open('src/blog.md', 'r', encoding="utf-8")
2016-11-03 00:24:27 +01:00
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 = ''
2016-11-03 00:24:27 +01:00
if (page == "about"):
rel_about = "#"
about_class = ""
about_btn_class = 'class="active"'
2016-11-03 00:24:27 +01:00
elif (page == "games"):
rel_games = "#"
games_class = ""
games_btn_class = 'class="active"'
2016-11-03 00:24:27 +01:00
elif (page == "blog"):
rel_blog = "#"
blog_class = ""
blog_btn_class = 'class="active"'
2016-11-03 00:24:27 +01:00
elif (page == "blogtext"):
rel_blogtext = "#"
blogtext_class = ""
2016-11-03 00:24:27 +01:00
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);
2016-11-03 00:24:27 +01:00
return content;
def build_blogposts():
blogposts = glob.glob("blogposts-md/*.md")
for path in blogposts:
2016-11-03 10:54:57 +01:00
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:
2016-11-03 00:24:27 +01:00
f2.write(markdown.markdown(f.read(), output_format="html5"))
2016-11-03 10:54:57 +01:00
with open("blogposts-md/blogposts.json", "r", encoding="utf-8") as f:
with open("blogposts/blogposts.json", "w+", encoding="utf-8") as f2:
2016-11-03 00:24:27 +01:00
f2.write("window.blogpostDataStr = '" + f.read().replace("\n", "") + "';")
2016-11-03 19:10:38 +01:00
def apply_custom_markdown(markdown):
itch_embed_template = '<iframe class="itch-embedded" 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>'
2016-11-03 19:10:38 +01:00
2016-11-04 15:29:32 +01:00
regex = re.compile(r"\[itch\$[0-9]*\]")
2016-11-03 19:10:38 +01:00
while regex.search(markdown) is not None:
search = regex.search(markdown)
2016-11-04 15:29:32 +01:00
split = [markdown[:search.start()], markdown[search.end():]]
2016-11-03 19:10:38 +01:00
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]
2016-11-04 15:29:32 +01:00
link_template = '<a class="js-link" href="$link$">$text$</a>'
regex = re.compile(r"\[[ -ö]*\]\$\([A-z0-9,./]*\)")
while regex.search(markdown, re.IGNORECASE) is not None:
search = regex.search(markdown)
split = [markdown[:search.start()], markdown[search.end():]]
parts = markdown[(search.start() + 1):][:(search.end() - search.start() - 2)].split("]$(")
markdown = split[0] + link_template.replace("$text$", parts[0]).replace("$link$", parts[1]) + split[1]
2016-11-03 19:10:38 +01:00
return markdown
2016-11-03 00:24:27 +01:00
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")
2016-11-03 19:10:38 +01:00
print("Applying custom markdown..")
template = apply_custom_markdown(template)
2016-11-04 15:29:32 +01:00
print("Done!\n")
2016-11-03 19:10:38 +01:00
2016-11-03 00:24:27 +01:00
print("Generating pages..")
2016-11-03 10:54:57 +01:00
with open('index.html', 'w+', encoding="utf-8") as f:
2016-11-03 00:24:27 +01:00
f.write(generate_page(template, "about"))
print("About (index.html) done!")
2016-11-03 10:54:57 +01:00
with open('games/index.html', 'w+', encoding="utf-8") as f:
2016-11-03 00:24:27 +01:00
f.write(generate_page(template, "games"))
print("Games done!")
2016-11-03 10:54:57 +01:00
with open('blog/index.html', 'w+', encoding="utf-8") as f:
2016-11-03 00:24:27 +01:00
f.write(generate_page(template, "blog"))
print("Blog done!")
2016-11-03 10:54:57 +01:00
with open('blogtext/index.html', 'w+', encoding="utf-8") as f:
2016-11-03 00:24:27 +01:00
f.write(generate_page(template, "blogtext"))
print("Blogtext done!")
print("Done!\n")
generate()