Special template for old blog posts

This commit is contained in:
grey-cat-1908 2024-08-24 08:49:19 +03:00
parent db194c3d95
commit 22e678d52b
3 changed files with 16 additions and 52 deletions

1
blog_template.html Normal file
View file

@ -0,0 +1 @@
<!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta property="og:site_name" content="Arbuz?">{{%META%}}<link rel="stylesheet" href="/custom.css"><link rel="stylesheet" href="https://use.fontawesome.com/releases/v6.4.2/css/all.css"><link rel="icon" href="https://avatars.githubusercontent.com/u/61203964"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css"><script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/go.min.js"></script><script>hljs.highlightAll()</script><title>{{%TITLE%}}</title></head><body><nav><b>mrkrk</b><a href="/">Home</a><a href="/projects">Projects</a></nav><main class="mc">{{%CONTENT%}}</main><script>MathJax={tex:{inlineMath:[["$","$"],["\\(","\\)"]]},svg:{fontCache:"global"}}</script><script type="text/javascript" id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js"></script></body></html>

View file

@ -24,6 +24,9 @@ os.makedirs("build")
with open("template.html", "r") as template_file: with open("template.html", "r") as template_file:
template_text = template_file.read() template_text = template_file.read()
with open("blog_template.html", "r") as blog_template_file:
blog_template_text = blog_template_file.read()
with open("meta.json", "r") as meta_file: with open("meta.json", "r") as meta_file:
base_meta = json.load(meta_file) base_meta = json.load(meta_file)
@ -38,11 +41,11 @@ def parse_meta(meta_data):
for key, value in meta_data.get("og", {}).items(): for key, value in meta_data.get("og", {}).items():
meta_tags.append(f'<meta property="og:{key}" content="{value}">') meta_tags.append(f'<meta property="og:{key}" content="{value}">')
return "\n".join(meta_tags) return "".join(meta_tags)
def generate_html_content(directory, filename): def generate_html_content(directory, filename, is_blog=False):
"""Generate HTML content from a markdown file and meta data.""" """Generate HTML content from a markdown file and metadata."""
file_path = os.path.join(directory, filename) file_path = os.path.join(directory, filename)
with open(file_path, "r") as file: with open(file_path, "r") as file:
@ -56,8 +59,10 @@ def generate_html_content(directory, filename):
meta_data = parse_meta(meta) meta_data = parse_meta(meta)
template_to_use = blog_template_text if is_blog else template_text
return ( return (
template_text.replace("{{%CONTENT%}}", markdown_renderer(md_text)) template_to_use.replace("{{%CONTENT%}}", markdown_renderer(md_text))
.replace("{{%TITLE%}}", meta.get("title", "")) .replace("{{%TITLE%}}", meta.get("title", ""))
.replace("{{%META%}}", meta_data) .replace("{{%META%}}", meta_data)
) )
@ -86,9 +91,13 @@ def process_content_directory(source_dir, target_dir):
process_content_directory(source_path, target_path) process_content_directory(source_path, target_path)
else: else:
if filename.endswith(".md"): if filename.endswith(".md"):
is_blog = "blog" in os.path.relpath(source_path, start="content").split(
os.sep
)
html_filename = os.path.splitext(filename)[0] + ".html" html_filename = os.path.splitext(filename)[0] + ".html"
output_file = os.path.join(target_dir, html_filename) output_file = os.path.join(target_dir, html_filename)
content = generate_html_content(source_dir, filename) content = generate_html_content(source_dir, filename, is_blog=is_blog)
with open(output_file, "w") as file: with open(output_file, "w") as file:
file.write(content) file.write(content)

View file

@ -1,47 +1 @@
<!DOCTYPE html> <!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta property="og:site_name" content="mrkrk">{{%META%}}<link rel="stylesheet" href="/custom.css"><link rel="icon" href="https://avatars.githubusercontent.com/u/61203964"><title>{{%TITLE%}}</title></head><body><nav><b>mrkrk</b><a href="/">Home</a><a href="/projects">Projects</a></nav><main class="mc">{{%CONTENT%}}</main></body></html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta property="og:site_name" content="Arbuz?"/>
{{%META%}}
<link rel="stylesheet" href="/custom.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v6.4.2/css/all.css">
<link rel="icon" href="https://avatars.githubusercontent.com/u/61203964">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/go.min.js"></script>
<script>hljs.highlightAll();</script>
<title>{{%TITLE%}}</title>
</head>
<body>
<nav>
<b>mrkrk</b>
<a href="/">Home</a>
<a href="/projects">Projects</a>
</nav>
<main class="mc">
{{%CONTENT%}}
</main>
<script>
MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']]
},
svg: {
fontCache: 'global'
}
};
</script>
<script type="text/javascript" id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js">
</script>
</body>
</html>