-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_portfolio_modified.py
More file actions
241 lines (189 loc) · 7.71 KB
/
generate_portfolio_modified.py
File metadata and controls
241 lines (189 loc) · 7.71 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import json
import os
from datetime import UTC, datetime
from pathlib import Path
import markdown2
from jinja2 import Environment, FileSystemLoader
import re
def slugify(text):
text = text.lower()
# Replace spaces and underscores with hyphens
text = re.sub(r"[\s_]+", "-", text)
# Remove all non-alphanumeric characters except hyphens
text = re.sub(r"[^a-z0-9-]", "", text)
return text
def markdown_to_html(text):
# This function is now used for the main blog summaries
import re
text = re.sub(r"\*\*(.*?)\*\*", r"<strong>\1</strong>", text)
return text
def markdown_to_html_resume(text):
# Replace **text** with <strong>text</strong>
import re
text = re.sub(r"\*\*(.*?)\*\*", r"\1", text)
return text
# Load JSON data
with Path("portfolio-mit_v1.json").open(encoding="utf-8") as f:
data = json.load(f)
# Add any extra context if needed
data["current_year"] = datetime.now(tz=UTC).year
if "social_links" in data:
for link in data["social_links"]:
if link.get("svg_path"):
with Path(link["svg_path"]).open(encoding="utf-8") as svg_file:
link["svg_data"] = svg_file.read()
env = Environment(loader=FileSystemLoader("."), autoescape=True)
env.filters["markdown_to_html"] = markdown_to_html
env.filters["markdown_to_html_resume"] = markdown_to_html_resume
# Set up Jinja environment
# env = Environment(loader=FileSystemLoader("."), autoescape=True)
index_template = env.get_template("index_template.html")
resume_template = env.get_template("resume_template.html")
blog_template = env.get_template("blog_template.html")
post_template = env.get_template("post_template.html") # <-- Load the new post template
contact_template = env.get_template("contact_template.html")
projects_template = env.get_template("projects_template.html")
tech_stack_template = env.get_template("tech_stack_template.html")
# env.filters["markdown_to_html"] = markdown_to_html
# env.filters["markdown_to_html_resume"] = markdown_to_html_resume
# Create a directory for the output blog post files if it doesn't exist
Path("posts").mkdir(exist_ok=True)
# 1. First, add a slug to EVERY post object
for post in data.get("blogs", []):
post["slug"] = slugify(post["title"])
# 2. NOW, sort all blog posts by date
blog_posts = sorted(
data.get("blogs", []),
key=lambda x: datetime.strptime(x["publish_date"], "%Y-%m-%d"),
reverse=True,
)
# 3. Now that every post has a slug, add previous/next post information and series part resolution
slug_to_post = {post["slug"]: post for post in blog_posts}
for i, post in enumerate(blog_posts):
post["next_post"] = None
post["previous_post"] = None
if i + 1 < len(blog_posts):
post["next_post"] = blog_posts[i + 1]
if i > 0:
post["previous_post"] = blog_posts[i - 1]
# Dynamically extract series part number from title, e.g. " (Part 2)" -> "2"
part_match = re.search(r"\(Part (\d+)\)", post["title"])
if part_match:
post["part_num"] = part_match.group(1)
for post in blog_posts:
if post.get("previous_part_slug"):
prev_post = slug_to_post.get(post["previous_part_slug"])
if prev_post and prev_post.get("part_num"):
post["previous_part_num"] = prev_post["part_num"]
if post.get("next_part_slug"):
next_post = slug_to_post.get(post["next_part_slug"])
if next_post and next_post.get("part_num"):
post["next_part_num"] = next_post["part_num"]
# 4. Finally, process each post to create its HTML file
for post in blog_posts:
if "markdown_file" in post and post["markdown_file"]:
md_filepath = Path("blog_posts") / post["markdown_file"]
if md_filepath.exists():
with md_filepath.open("r", encoding="utf-8") as f:
markdown_content = f.read()
post["detailed_content"] = markdown2.markdown(
markdown_content, extras=["fenced-code-blocks", "code-friendly"]
)
post_output = post_template.render(post=post, **data)
output_path = Path("posts") / f"{post['slug']}.html"
with output_path.open("w", encoding="utf-8") as f:
f.write(post_output)
else:
print(
f"Warning: Markdown file not found for blog '{post['title']}': {md_filepath}"
)
# Update the main data dictionary with the sorted and processed posts
data["blogs"] = blog_posts
# Collect all unique tags from all blog posts
all_tags = set()
for post in data.get("blogs", []):
# We need to add the slugify function call here as well for the links to work
for tech in post.get("core_technologies", []):
all_tags.add(tech)
# Convert set to a sorted list for consistent order
sorted_tags = sorted(list(all_tags))
# Match featured projects to their starting blog post
for project in data.get("featured_projects", []):
github_url = project.get("github_url")
if github_url:
# Search backwards so we find earliest dates first (Part 1s)
for post in reversed(blog_posts):
if post.get("github_url") == github_url and not post.get(
"previous_part_slug"
):
project["blog_slug"] = post["slug"]
break
# Render the template with the data
html_output = index_template.render(**data)
resume_output = resume_template.render(**data)
projects_output = projects_template.render(**data)
# Find and update the line below to pass the new 'tags' variable
blog_output = blog_template.render(tags=sorted_tags, **data)
# blog_output = blog_template.render(**data)
# Render the new contact page
contact_output = contact_template.render(**data)
# Render the new tech stack page
tech_stack_output = tech_stack_template.render(**data)
# Write the output to an HTML file
with Path("index.html").open("w", encoding="utf-8") as f:
f.write(html_output)
with Path("resume.html").open("w", encoding="utf-8") as f:
f.write(resume_output)
with Path("blog.html").open("w", encoding="utf-8") as f:
f.write(blog_output)
with Path("contact.html").open("w", encoding="utf-8") as f:
f.write(contact_output)
with Path("projects.html").open("w", encoding="utf-8") as f:
f.write(projects_output)
with Path("tech-stack.html").open("w", encoding="utf-8") as f:
f.write(tech_stack_output)
# Generate search index for client-side blog search
search_entries = []
for post in blog_posts:
entry = {
"title": post["title"],
"slug": post["slug"],
"date": post["publish_date"],
"summary": post.get("content", ""),
"tags": post.get("core_technologies", []) + post.get("keywords", []),
}
search_entries.append(entry)
search_index_js = "const SEARCH_INDEX = " + json.dumps(search_entries, indent=2) + ";\n"
with Path("js/search-index.js").open("w", encoding="utf-8") as f:
f.write(search_index_js)
print("HTML files generated successfully!")
print("Individual blog posts have been generated in the 'posts' directory.")
print("Search index generated at js/search-index.js.")
# There are 18 keys in the JSON data that are used in the templates:
# name
# label
# image_path
# contact
# summary
# base_url
# social_links
# core_competencies
# work_experience
# volunteer_experience
# education
# technical_skills_categorized
# interests
# technologies_used
# languages
# references
# awards_certifications
# blogs
# Out of these 18 keys, the following keys are not used in any of the templates:
# volunteer_experience
# interests
# technologies_used ---> mostly redundant with technical_skills_categorized as a replacement and with more detailed breakdown
# references
# awards_certifications
# The following keys are used in the resume template:
# The following keys are used in the index/home template:
# The following keys are used in the blog template: