-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic_generator.py
115 lines (93 loc) · 3.42 KB
/
static_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import os
import markdown
from jinja2 import Template
# Define paths
output_dir = os.getcwd()
blog_dir = 'content/blog_posts'
template_dir = 'templates/'
# Function to remove old static website HTML files
def remove_old_static_html_files(directory):
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
# Check if the file is a static HTML file generated by the script
if os.path.isfile(file_path) and filename.endswith('.html') and filename not in ['home.html', 'resume.html']:
os.remove(file_path)
# Remove old static HTML files
remove_old_static_html_files(output_dir)
# List markdown files and sort by modification time
markdown_files = [f for f in os.listdir(blog_dir) if f.endswith('.md')]
markdown_files.sort(key=lambda f: os.path.getmtime(os.path.join(blog_dir, f)), reverse=True)
# Determine the most recent blog post
most_recent_post = os.path.splitext(markdown_files[0])[0] if markdown_files else None
print(most_recent_post)
# Read and render the home page
with open(os.path.join(template_dir, 'home.html'), 'r') as file:
home_content = file.read()
# Update the blog link to point to the most recent post
if most_recent_post:
home_content = home_content.replace('href="blog"', f'href="{most_recent_post}.html"')
with open(os.path.join(output_dir, 'index.html'), 'w') as file:
file.write(home_content)
# Read and render the resume page
resume_content = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Resume</title>
</head>
<body>
<h1>Resume</h1>
<p>Download the resume from the original site.</p>
</body>
</html>
"""
with open(os.path.join(output_dir, 'resume.html'), 'w') as file:
file.write(resume_content)
# Read and render the blog posts
with open(os.path.join(template_dir, 'blog_post_temp.html'), 'r') as file:
blog_template_content = file.read()
blog_template = Template(blog_template_content)
# List markdown files
markdown_files = [f for f in os.listdir(blog_dir) if f.endswith('.md')]
markdown_files.sort(key=lambda f: os.path.getmtime(os.path.join(blog_dir, f)), reverse=True)
# Generate blog post pages
for markdown_file in markdown_files:
post_name = os.path.splitext(markdown_file)[0]
with open(os.path.join(blog_dir, markdown_file), 'r', encoding='utf-8') as file:
markdown_content = file.read()
html_content = markdown.markdown(markdown_content)
blog_links = ''.join(
f'<li><a href="{os.path.splitext(f)[0]}.html">{os.path.splitext(f)[0]}</a></li>'
for f in markdown_files
)
rendered_content = blog_template.render(
blog_post_content=html_content,
blog_post_links=blog_links
)
with open(os.path.join(output_dir, f'{post_name}.html'), 'w') as file:
file.write(rendered_content)
# Generate a main blog page
blog_index_content = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blog</title>
</head>
<body>
<h1>Blog</h1>
<ul>
"""
for markdown_file in markdown_files:
post_name = os.path.splitext(markdown_file)[0]
blog_index_content += f'<li><a href="{post_name}.html">{post_name}</a></li>\n'
blog_index_content += """
</ul>
</body>
</html>
"""
with open(os.path.join(output_dir, 'blog.html'), 'w') as file:
file.write(blog_index_content)