some build script updates

This commit is contained in:
grey-cat-1908 2024-08-20 14:31:29 +03:00
parent a7532dbeaf
commit ffcddffb62
5 changed files with 77 additions and 59 deletions

View file

@ -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. 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. At the moment, the site is hosted on CloudFlare Pages, but once GitVerse Pages is released, I'm thinking about moving it there.

128
build.py
View file

@ -1,80 +1,98 @@
import os import os
import shutil import shutil
import json import json
import mistune import mistune
render = mistune.create_markdown( markdown_renderer = mistune.create_markdown(
plugins=['math', 'strikethrough', 'footnotes', 'table', 'url', 'task_lists', 'abbr', 'mark', 'subscript', 'spoiler'] plugins=[
"math",
"strikethrough",
"footnotes",
"table",
"url",
"task_lists",
"abbr",
"mark",
"subscript",
"spoiler",
]
) )
try: shutil.rmtree("build", ignore_errors=True)
shutil.rmtree("build") os.makedirs("build")
except:
pass
os.mkdir("build") with open("template.html", "r") as template_file:
template_text = template_file.read()
template_file = open('template.html', "r") with open("meta.json", "r") as meta_file:
template_text = template_file.read() base_meta = json.load(meta_file)
template_file.close()
base_meta_file = open('meta.json', "r")
base_meta = json.loads(base_meta_file.read())
base_meta_file.close()
def parse_meta(data): def parse_meta(meta_data):
result = "" """Generate meta tags for the HTML head section."""
meta_tags = []
for key, value in data['tags'].items(): for key, value in meta_data.get("tags", {}).items():
result += f'<meta name="{key}" content="{value}">\n' meta_tags.append(f'<meta name="{key}" content="{value}">')
for key, value in data['og'].items(): for key, value in meta_data.get("og", {}).items():
result += f'<meta property="og:{key}" content="{value}">\n' meta_tags.append(f'<meta property="og:{key}" content="{value}">')
return result return "\n".join(meta_tags)
def gen_file(directory, filename):
file = open(directory + filename, "r")
md_text = file.read()
file.close()
try: def generate_html_content(directory, filename):
file = open(directory + 'meta.json', "r") """Generate HTML content from a markdown file and meta data."""
meta = json.loads(file.read()) file_path = os.path.join(directory, filename)
file.close()
except: with open(file_path, "r") as file:
meta = base_meta 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)
meta_data = parse_meta(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: def copy_static_files(static_dir, build_dir="build"):
os.makedirs(f'build/{fier}{filename}') """Copy static files and directories to the build folder."""
go_through(directory + "/" + filename) 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: else:
if filename.split(".")[1] == "json": shutil.copy(src, dst)
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
content = gen_file(f"{os.getcwd()}/{directory}/", filename)
loc = fier + filename.split(".")[0] + '.html'
file = open(f"{os.getcwd()}/build/{loc}", "a") def process_content_directory(source_dir, target_dir):
file.write(content) """Recursively process the content directory and generate HTML files."""
file.close() 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")

View file

@ -1,5 +1,5 @@
# Hello! # 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). You can contact me via [email](mailto:tech@arbuz.icu).

0
static/robots.txt Normal file
View file