Skip to content

Commit a5b84be

Browse files
committed
Add code block, inline code, and anchor link support to build-pages.py
- Add convert_code_blocks for fenced code blocks with syntax highlighting classes - Add convert_inline_code for backtick inline code to <code> tags - Add slugify function to generate anchor IDs from header text - Update convert_headers to add id attributes for working anchor links - Add CSS styles for code and pre elements in page template - Regenerate all HTML pages with proper code formatting
1 parent e95fa41 commit a5b84be

File tree

9 files changed

+802
-592
lines changed

9 files changed

+802
-592
lines changed

assignments/assignment-1/index.html

Lines changed: 112 additions & 97 deletions
Large diffs are not rendered by default.

assignments/assignment-2/index.html

Lines changed: 111 additions & 99 deletions
Large diffs are not rendered by default.

assignments/assignment-3/index.html

Lines changed: 108 additions & 91 deletions
Large diffs are not rendered by default.

assignments/assignment-4/index.html

Lines changed: 131 additions & 130 deletions
Large diffs are not rendered by default.

assignments/assignment-5/index.html

Lines changed: 74 additions & 57 deletions
Large diffs are not rendered by default.

assignments/final-project/index.html

Lines changed: 92 additions & 73 deletions
Large diffs are not rendered by default.

assignments/index.html

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,29 @@
157157
.content tr:hover {
158158
background: var(--surface-color);
159159
}
160+
.content code {
161+
background: var(--surface-color);
162+
padding: 0.2em 0.4em;
163+
border-radius: 4px;
164+
font-family: 'Fira Code', 'Monaco', 'Consolas', monospace;
165+
font-size: 0.9em;
166+
color: var(--primary-color);
167+
}
168+
.content pre {
169+
background: var(--surface-color);
170+
padding: 1rem;
171+
border-radius: var(--radius-md);
172+
overflow-x: auto;
173+
margin: 1rem 0;
174+
border: 1px solid var(--border-color);
175+
}
176+
.content pre code {
177+
background: none;
178+
padding: 0;
179+
color: var(--text-secondary);
180+
font-size: 0.875rem;
181+
line-height: 1.6;
182+
}
160183
footer {
161184
background: var(--surface-color);
162185
border-top: 1px solid var(--border-color);
@@ -199,9 +222,9 @@ <h1>Course Assignments</h1>
199222
</header>
200223

201224
<main class="content">
202-
<h1>Course Assignments</h1>
225+
<h1 id="course-assignments">Course Assignments</h1>
203226
<p>Welcome to the assignments for PSYC 51.17: Models of Language and Communication.</p>
204-
<h2>Submission Instructions</h2>
227+
<h2 id="submission-instructions">Submission Instructions</h2>
205228
<p>All assignments are submitted via GitHub Classroom. Click the "Accept Assignment" button on each assignment page to get started. This will create a personal repository where you'll complete your work.</p>
206229
<p><strong>Submission Process:</strong></p>
207230
<ol>
@@ -211,7 +234,7 @@ <h2>Submission Instructions</h2>
211234
<li>Commit and push your changes before the deadline</li>
212235
<li>Your latest commit before the deadline will be graded</li>
213236
</ol>
214-
<h2>Assignment Schedule</h2>
237+
<h2 id="assignment-schedule">Assignment Schedule</h2>
215238
<table>
216239
<thead><tr>
217240
<th>#</th>
@@ -265,34 +288,34 @@ <h2>Assignment Schedule</h2>
265288
</tr>
266289
</tbody>
267290
</table>
268-
<h2>Late Policy</h2>
291+
<h2 id="late-policy">Late Policy</h2>
269292
<p>Assignments receive a 10% deduction for each week late, rounded up to the nearest whole week. The final project must be submitted on time.</p>
270-
<h2>Grading</h2>
293+
<h2 id="grading">Grading</h2>
271294
<p>Each assignment is graded on:</p>
272295
<ul>
273296
<li><strong>Correctness</strong>: Does your code produce the expected outputs?</li>
274297
<li><strong>Code Quality</strong>: Is your code well-organized and documented?</li>
275298
<li><strong>Understanding</strong>: Do your explanations demonstrate understanding of the concepts?</li>
276299
</ul>
277-
<h2>Getting Help</h2>
300+
<h2 id="getting-help">Getting Help</h2>
278301
<ul>
279302
<li><strong>Office Hours</strong>: By appointment</li>
280303
<li><strong>Discord</strong>: Join our class server for discussions</li>
281304
<li><strong>GitHub Issues</strong>: Report bugs or ask questions on the assignment repositories</li>
282305
</ul>
283306
<hr>
284-
<h2>Assignments</h2>
285-
<h3><a href="./assignment-1/">Assignment 1: ELIZA Chatbot</a></h3>
307+
<h2 id="assignments">Assignments</h2>
308+
<h3 id="assignment-1-eliza-chatbotassignment-1"><a href="./assignment-1/">Assignment 1: ELIZA Chatbot</a></h3>
286309
<p>Build a pattern-matching chatbot based on Weizenbaum's classic ELIZA program. Learn about string manipulation, regular expressions, and the foundations of conversational AI.</p>
287-
<h3><a href="./assignment-2/">Assignment 2: SPAM Classifier</a></h3>
310+
<h3 id="assignment-2-spam-classifierassignment-2"><a href="./assignment-2/">Assignment 2: SPAM Classifier</a></h3>
288311
<p>Develop a text classification system to identify spam messages. Explore feature engineering, tokenization, and evaluation metrics.</p>
289-
<h3><a href="./assignment-3/">Assignment 3: Wikipedia Embeddings</a></h3>
312+
<h3 id="assignment-3-wikipedia-embeddingsassignment-3"><a href="./assignment-3/">Assignment 3: Wikipedia Embeddings</a></h3>
290313
<p>Compare different text embedding methods on Wikipedia articles. Visualize semantic relationships and evaluate embedding quality.</p>
291-
<h3><a href="./assignment-4/">Assignment 4: Customer Service Chatbot</a></h3>
314+
<h3 id="assignment-4-customer-service-chatbotassignment-4"><a href="./assignment-4/">Assignment 4: Customer Service Chatbot</a></h3>
292315
<p>Create a context-aware customer service chatbot using transformer-based models. Implement retrieval and response generation.</p>
293-
<h3><a href="./assignment-5/">Assignment 5: Build GPT</a></h3>
316+
<h3 id="assignment-5-build-gptassignment-5"><a href="./assignment-5/">Assignment 5: Build GPT</a></h3>
294317
<p>Implement and train a small GPT model from scratch. Understand the transformer architecture, attention mechanisms, and autoregressive generation.</p>
295-
<h3><a href="./final-project/">Final Project: Research Project</a></h3>
318+
<h3 id="final-project-research-projectfinal-project"><a href="./final-project/">Final Project: Research Project</a></h3>
296319
<p>Conduct an independent research project applying concepts from the course. Present your findings to the class.</p>
297320
</main>
298321

scripts/build-pages.py

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,35 @@ def convert_latex_table(text):
4848
return text
4949

5050

51+
def slugify(text):
52+
"""Convert text to URL-friendly slug for anchor IDs."""
53+
slug = text.lower()
54+
slug = re.sub(r"[^\w\s-]", "", slug)
55+
slug = re.sub(r"[\s_]+", "-", slug)
56+
slug = slug.strip("-")
57+
return slug
58+
59+
5160
def convert_headers(html):
52-
"""Convert markdown headers to HTML."""
53-
html = re.sub(r"^#### (.+)$", r"<h4>\1</h4>", html, flags=re.MULTILINE)
54-
html = re.sub(r"^### (.+)$", r"<h3>\1</h3>", html, flags=re.MULTILINE)
55-
html = re.sub(r"^## (.+)$", r"<h2>\1</h2>", html, flags=re.MULTILINE)
56-
html = re.sub(r"^# (.+)$", r"<h1>\1</h1>", html, flags=re.MULTILINE)
61+
"""Convert markdown headers to HTML with anchor IDs."""
62+
63+
def replace_header(match, tag):
64+
text = match.group(1)
65+
slug = slugify(text)
66+
return f'<{tag} id="{slug}">{text}</{tag}>'
67+
68+
html = re.sub(
69+
r"^#### (.+)$", lambda m: replace_header(m, "h4"), html, flags=re.MULTILINE
70+
)
71+
html = re.sub(
72+
r"^### (.+)$", lambda m: replace_header(m, "h3"), html, flags=re.MULTILINE
73+
)
74+
html = re.sub(
75+
r"^## (.+)$", lambda m: replace_header(m, "h2"), html, flags=re.MULTILINE
76+
)
77+
html = re.sub(
78+
r"^# (.+)$", lambda m: replace_header(m, "h1"), html, flags=re.MULTILINE
79+
)
5780
return html
5881

5982

@@ -65,6 +88,42 @@ def convert_inline_formatting(html):
6588
return html
6689

6790

91+
def convert_code_blocks(text):
92+
"""Convert fenced code blocks (```) to HTML pre/code tags."""
93+
lines = text.split("\n")
94+
result = []
95+
in_code_block = False
96+
code_lines = []
97+
lang = ""
98+
99+
for line in lines:
100+
if line.strip().startswith("```") and not in_code_block:
101+
in_code_block = True
102+
lang = line.strip()[3:].strip()
103+
code_lines = []
104+
elif line.strip() == "```" and in_code_block:
105+
in_code_block = False
106+
code_content = "\n".join(code_lines)
107+
code_content = (
108+
code_content.replace("&", "&amp;")
109+
.replace("<", "&lt;")
110+
.replace(">", "&gt;")
111+
)
112+
lang_class = f' class="language-{lang}"' if lang else ""
113+
result.append(f"<pre><code{lang_class}>{code_content}</code></pre>")
114+
elif in_code_block:
115+
code_lines.append(line)
116+
else:
117+
result.append(line)
118+
119+
return "\n".join(result)
120+
121+
122+
def convert_inline_code(text):
123+
"""Convert backtick inline code to HTML code tags."""
124+
return re.sub(r"`([^`]+)`", r"<code>\1</code>", text)
125+
126+
68127
def convert_links(html):
69128
"""Convert markdown links to HTML, handling internal vs external links."""
70129

@@ -340,13 +399,14 @@ def wrap_paragraphs(html):
340399

341400
def parse_markdown_to_html(markdown_text):
342401
"""Convert markdown text to HTML."""
343-
# Pre-processing
344402
html = strip_latex_preamble(markdown_text)
345403
html = convert_latex_href(html)
346404
html = convert_latex_table(html)
347405

406+
html = convert_code_blocks(html)
348407
html = convert_headers(html)
349408
html = convert_inline_formatting(html)
409+
html = convert_inline_code(html)
350410
html = convert_tables(html)
351411
html = convert_lists(html)
352412
html = convert_horizontal_rules(html)
@@ -533,6 +593,29 @@ def get_page_template(title, nav_active, content, depth=1):
533593
.content tr:hover {{
534594
background: var(--surface-color);
535595
}}
596+
.content code {{
597+
background: var(--surface-color);
598+
padding: 0.2em 0.4em;
599+
border-radius: 4px;
600+
font-family: 'Fira Code', 'Monaco', 'Consolas', monospace;
601+
font-size: 0.9em;
602+
color: var(--primary-color);
603+
}}
604+
.content pre {{
605+
background: var(--surface-color);
606+
padding: 1rem;
607+
border-radius: var(--radius-md);
608+
overflow-x: auto;
609+
margin: 1rem 0;
610+
border: 1px solid var(--border-color);
611+
}}
612+
.content pre code {{
613+
background: none;
614+
padding: 0;
615+
color: var(--text-secondary);
616+
font-size: 0.875rem;
617+
line-height: 1.6;
618+
}}
536619
footer {{
537620
background: var(--surface-color);
538621
border-top: 1px solid var(--border-color);

0 commit comments

Comments
 (0)