diff --git a/README.md b/README.md index 90343a0..954393a 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This repository hosts the source code for my personal website. I have enough experience to create interesting websites, but I want my personal web page to be simple and easy to understand. I want it to be straightforward, with text as the main component. -Even though it looks pretty basic, the site has a lot more going on. It also has the source code for a simple build script that runs on push and turns markdown files into HTML pages. +Even though it looks pretty basic, the website has a lot more going on. This repository also hosts the source code for a simple build script that runs on push and renders markdown files into HTML pages. At the moment, the site is hosted on CloudFlare Pages, but once GitVerse Pages is released, I'm thinking about moving it there. diff --git a/build.py b/build.py index 55927ce..aa8b770 100644 --- a/build.py +++ b/build.py @@ -1,80 +1,98 @@ import os import shutil import json - import mistune -render = mistune.create_markdown( - plugins=['math', 'strikethrough', 'footnotes', 'table', 'url', 'task_lists', 'abbr', 'mark', 'subscript', 'spoiler'] +markdown_renderer = mistune.create_markdown( + plugins=[ + "math", + "strikethrough", + "footnotes", + "table", + "url", + "task_lists", + "abbr", + "mark", + "subscript", + "spoiler", + ] ) -try: - shutil.rmtree("build") -except: - pass +shutil.rmtree("build", ignore_errors=True) +os.makedirs("build") -os.mkdir("build") +with open("template.html", "r") as template_file: + template_text = template_file.read() -template_file = open('template.html', "r") -template_text = template_file.read() -template_file.close() +with open("meta.json", "r") as meta_file: + base_meta = json.load(meta_file) -base_meta_file = open('meta.json', "r") -base_meta = json.loads(base_meta_file.read()) -base_meta_file.close() -def parse_meta(data): - result = "" +def parse_meta(meta_data): + """Generate meta tags for the HTML head section.""" + meta_tags = [] - for key, value in data['tags'].items(): - result += f'\n' - - for key, value in data['og'].items(): - result += f'\n' + for key, value in meta_data.get("tags", {}).items(): + meta_tags.append(f'') - return result + for key, value in meta_data.get("og", {}).items(): + meta_tags.append(f'') -def gen_file(directory, filename): - file = open(directory + filename, "r") - md_text = file.read() - file.close() + return "\n".join(meta_tags) + + +def generate_html_content(directory, filename): + """Generate HTML content from a markdown file and meta data.""" + file_path = os.path.join(directory, filename) + + with open(file_path, "r") as file: + md_text = file.read() + + meta_path = os.path.join(directory, "meta.json") + meta = base_meta + if os.path.exists(meta_path): + with open(meta_path, "r") as meta_file: + meta = json.load(meta_file) - try: - file = open(directory + 'meta.json', "r") - meta = json.loads(file.read()) - file.close() - except: - meta = base_meta - meta_data = parse_meta(meta) - return template_text.replace("{{%CONTENT%}}", render(md_text)).replace("{{%TITLE%}}", meta['title']).replace("{{%META%}}", meta_data) + return ( + template_text.replace("{{%CONTENT%}}", markdown_renderer(md_text)) + .replace("{{%TITLE%}}", meta.get("title", "")) + .replace("{{%META%}}", meta_data) + ) -def go_through(directory): - for filename in os.listdir(directory): - _, _, fier = directory.partition('/') - if len(fier) != 0: fier += "/" - if len(filename.split(".")) == 1: - os.makedirs(f'build/{fier}{filename}') - go_through(directory + "/" + filename) +def copy_static_files(static_dir, build_dir="build"): + """Copy static files and directories to the build folder.""" + for element in os.listdir(static_dir): + src = os.path.join(static_dir, element) + dst = os.path.join(build_dir, element) + + if os.path.isdir(src): + shutil.copytree(src, dst) else: - if filename.split(".")[1] == "json": - continue - for ofn in os.listdir("static"): - try: - if len(ofn.split(".")) == 1: - shutil.copytree(f"{os.getcwd()}/static/{ofn}", f"{os.getcwd()}/build/{fier}{ofn}") - else: - shutil.copy(f"{os.getcwd()}/static/{ofn}", f"{os.getcwd()}/build/{fier}{ofn}") - except: - pass + shutil.copy(src, dst) - content = gen_file(f"{os.getcwd()}/{directory}/", filename) - loc = fier + filename.split(".")[0] + '.html' - file = open(f"{os.getcwd()}/build/{loc}", "a") - file.write(content) - file.close() +def process_content_directory(source_dir, target_dir): + """Recursively process the content directory and generate HTML files.""" + for filename in os.listdir(source_dir): + source_path = os.path.join(source_dir, filename) + target_path = os.path.join(target_dir, filename) -go_through("content") + if os.path.isdir(source_path): + os.makedirs(target_path, exist_ok=True) + process_content_directory(source_path, target_path) + else: + if filename.endswith(".md"): + html_filename = os.path.splitext(filename)[0] + ".html" + output_file = os.path.join(target_dir, html_filename) + content = generate_html_content(source_dir, filename) + + with open(output_file, "w") as file: + file.write(content) + + +copy_static_files("static") +process_content_directory("content", "build") diff --git a/content/index.md b/content/index.md index e20b503..d383e76 100644 --- a/content/index.md +++ b/content/index.md @@ -1,5 +1,5 @@ # Hello! -I'm Viktor, a student from Moscow with a passion for learning and exploration. My interests span IT, Economics, and Mathematics. As a developer, I have a diverse range of things, including web development, microcontrollers, and robotic devices. I aim to integrate all my areas of interest into my work, striving for excellence and personal growth. +I'm Viktor, a student from Moscow with a passion for learning and exploration. My interests span IT, Economics, and Mathematics. As a developer, I am interested in a diverse range of things, including web development, microcontrollers, and robotic devices. I aim to integrate all my areas of interest into my work, striving for excellence and personal growth. You can contact me via [email](mailto:tech@arbuz.icu). diff --git a/.htaccess b/static/.htaccess similarity index 100% rename from .htaccess rename to static/.htaccess diff --git a/static/robots.txt b/static/robots.txt new file mode 100644 index 0000000..e69de29