Teascade.net/build.py

126 lines
4.3 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()
md_list = glob.glob("src/*.md")
for md in md_list:
f = open(md, 'r', encoding="utf-8")
file_content = f.read()
f.close()
name = md[4:][:len(md)-7]
content = content.replace("$" + name + "_content$", markdown.markdown(file_content, output_format="html5"))
return content
def generate_page(template, page):
content = template.replace("$page$", page);
rel = {
"about": "/",
"games": "/games",
"blog": "/blog",
"saltosion": "/saltosion",
"blogtext": "/"
}
md_list = glob.glob("src/*.md")
md_list.append("blogtext")
for md in md_list:
md = md.replace("\\", "/")
name = md.split("src/")[-1].split(".md")[0]
if page != name:
content = content.replace("$relative_" + name + "$", rel[name])
content = content.replace("$" + name + "_class$", 'class="hiddenarea"')
content = content.replace("$" + name + "_btn_class$", '')
else:
content = content.replace("$relative_" + name + "$", "#")
content = content.replace("$" + name + "_class$", "")
content = content.replace("$" + name + "_btn_class$", 'class="active"')
return content;
def build_blogposts():
blogposts = glob.glob("src/blogposts/*.md")
for path in blogposts:
with open(path, "r", encoding="utf-8") as f:
with open(path.replace("src/blogposts", "blogposts").replace(".md", ".html"), "w+", encoding="utf-8") as f2:
f2.write(markdown.markdown(f.read(), output_format="html5"))
with open("src/blogposts/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 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>'
regex = re.compile(r"\[itch\$[0-9]*\]")
while regex.search(markdown) is not None:
search = regex.search(markdown)
split = [markdown[:search.start()], markdown[search.end():]]
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]
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]
return markdown
def generate():
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!")
with open('saltosion/index.html', 'w+', encoding="utf-8") as f:
f.write(generate_page(template, "saltosion"))
print(" Saltosion done!")
print("Done!\n")
generate()