-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert-markdown.py
More file actions
executable file
·341 lines (286 loc) · 11.6 KB
/
convert-markdown.py
File metadata and controls
executable file
·341 lines (286 loc) · 11.6 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env python3
"""
Convert Obsidian-formatted Markdown files to HTML for GitHub Pages.
Handles YAML frontmatter, wiki-style links, and standard markdown.
"""
import os
import re
import yaml
import markdown
from pathlib import Path
from markdown.extensions import tables, fenced_code, toc
import html
def parse_frontmatter(content):
"""Extract YAML frontmatter from markdown content."""
frontmatter = {}
body = content
if content.startswith('---'):
parts = content.split('---', 2)
if len(parts) >= 3:
try:
frontmatter = yaml.safe_load(parts[1])
body = parts[2].strip()
except yaml.YAMLError:
pass
return frontmatter, body
def convert_wiki_links(content):
"""Convert Obsidian wiki-style links to HTML links."""
# Pattern: [[path/to/file|Display Text]] or [[path/to/file]]
def replace_link(match):
full_match = match.group(0)
link_content = match.group(1)
if '|' in link_content:
path, display = link_content.split('|', 1)
else:
path = link_content
# Use the last part of the path as display text
display = path.split('/')[-1].replace('-', ' ').title()
# Convert file path to HTML path
html_path = path.strip()
# Map source directory names to output directory names
html_path = html_path.replace('domain-1-organizational-complexity/', 'domain-1/')
html_path = html_path.replace('domain-2-new-solutions/', 'domain-2/')
html_path = html_path.replace('domain-3-continuous-improvement/', 'domain-3/')
html_path = html_path.replace('domain-4-migration-modernization/', 'domain-4/')
if not html_path.endswith('.html'):
html_path = html_path.replace('.md', '.html')
if '/' not in html_path:
html_path += '.html'
else:
# Add .html if path doesn't have it
html_path += '.html'
return f'<a href="{html_path}">{display.strip()}</a>'
# Replace wiki-style links
content = re.sub(r'\[\[([^\]]+)\]\]', replace_link, content)
return content
def create_html_page(title, content, frontmatter=None, base_path='..'):
"""Create a complete HTML page with navigation and styling."""
# Extract metadata for page header
domain = frontmatter.get('domain', '') if frontmatter else ''
domain_name = frontmatter.get('domain_name', '') if frontmatter else ''
task = frontmatter.get('task', '') if frontmatter else ''
weight = frontmatter.get('weight', '') if frontmatter else ''
breadcrumb = ''
if domain:
breadcrumb = f'''
<div class="breadcrumb">
<a href="{base_path}/study/index.html">Study Materials</a> ›
<a href="{base_path}/study/domain-{domain}.html">Domain {domain}</a>
{f' › Task {task}' if task else ''}
</div>
'''
metadata_html = ''
if frontmatter:
meta_items = []
if domain_name:
meta_items.append(f'<span class="meta-item"><strong>Domain:</strong> {domain_name}</span>')
if weight:
meta_items.append(f'<span class="meta-item"><strong>Weight:</strong> {weight}</span>')
if task:
meta_items.append(f'<span class="meta-item"><strong>Task:</strong> {task}</span>')
if meta_items:
metadata_html = f'''
<div class="page-metadata">
{' '.join(meta_items)}
</div>
'''
html_template = f'''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{html.escape(title)} - AWS SA Pro Kit</title>
<link rel="stylesheet" href="{base_path}/styles/study.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
</head>
<body>
<button id="darkModeToggle" class="dark-mode-toggle" aria-label="Toggle dark mode">
<span class="icon">🌙</span>
</button>
<nav class="top-nav">
<div class="nav-container">
<a href="{base_path}/index.html" class="nav-brand">AWS SA Pro Kit</a>
<div class="nav-links">
<a href="{base_path}/index.html">Home</a>
<a href="{base_path}/study/index.html" class="active">Study Materials</a>
<a href="{base_path}/exam/index.html">Practice Exam</a>
<a href="https://github.com/bkondakor/aws-sa-pro-kit" target="_blank">GitHub</a>
</div>
</div>
</nav>
<div class="container">
{breadcrumb}
<main class="content">
<article>
{metadata_html}
{content}
</article>
</main>
<aside class="sidebar">
<div class="sidebar-section">
<h3>Navigation</h3>
<ul>
<li><a href="{base_path}/study/index.html">Study Home</a></li>
<li><a href="{base_path}/study/domain-1.html">Domain 1 (26%)</a></li>
<li><a href="{base_path}/study/domain-2.html">Domain 2 (29%)</a></li>
<li><a href="{base_path}/study/domain-3.html">Domain 3 (25%)</a></li>
<li><a href="{base_path}/study/domain-4.html">Domain 4 (20%)</a></li>
<li><a href="{base_path}/study/comparisons/index.html">🔀 Service Comparisons</a></li>
</ul>
</div>
<div class="sidebar-section">
<h3>Quick Links</h3>
<ul>
<li><a href="{base_path}/study/cheatsheet.html" style="background: linear-gradient(135deg, #FFB84D 0%, #FF8C42 100%); padding: 8px 12px; border-radius: 6px; color: #0f0f1e; font-weight: 700; display: block; text-align: center; margin-bottom: 8px;">📋 Cheatsheet</a></li>
<li><a href="{base_path}/exam/index.html">Practice Exam</a></li>
<li><a href="https://aws.amazon.com/certification/certified-solutions-architect-professional/" target="_blank">Official Exam</a></li>
</ul>
</div>
</aside>
</div>
<footer class="footer">
<p>AWS Solutions Architect Professional Exam Preparation Kit</p>
<p>Last updated: {frontmatter.get('last_updated', '2025') if frontmatter else '2025'}</p>
</footer>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script>hljs.highlightAll();</script>
<script>
// Dark Mode Functionality
const darkModeToggle = document.getElementById('darkModeToggle');
const icon = darkModeToggle.querySelector('.icon');
// Check for saved theme preference or default to light mode
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'dark') {{
document.body.classList.add('dark-mode');
icon.textContent = '☀️';
}}
// Toggle dark mode
darkModeToggle.addEventListener('click', () => {{
document.body.classList.toggle('dark-mode');
// Update icon and save preference
if (document.body.classList.contains('dark-mode')) {{
icon.textContent = '☀️';
localStorage.setItem('theme', 'dark');
}} else {{
icon.textContent = '🌙';
localStorage.setItem('theme', 'light');
}}
}});
</script>
</body>
</html>'''
return html_template
def convert_markdown_to_html(md_content, title='Study Material'):
"""Convert markdown content to HTML."""
# Parse frontmatter
frontmatter, body = parse_frontmatter(md_content)
# Convert wiki-style links
body = convert_wiki_links(body)
# Convert markdown to HTML
md = markdown.Markdown(extensions=[
'tables',
'fenced_code',
'toc',
'nl2br',
'sane_lists'
])
html_content = md.convert(body)
# Extract title from frontmatter or content
if frontmatter and 'title' in frontmatter:
title = frontmatter['title']
return html_content, frontmatter, title
def process_markdown_file(input_path, output_path, base_path='..'):
"""Process a single markdown file and convert to HTML."""
print(f"Processing: {input_path}")
with open(input_path, 'r', encoding='utf-8') as f:
md_content = f.read()
html_content, frontmatter, title = convert_markdown_to_html(md_content)
full_html = create_html_page(title, html_content, frontmatter, base_path)
# Ensure output directory exists
output_path.parent.mkdir(parents=True, exist_ok=True)
with open(output_path, 'w', encoding='utf-8') as f:
f.write(full_html)
print(f"Created: {output_path}")
return title, frontmatter
def main():
"""Main conversion process."""
project_root = Path(__file__).parent
study_output = project_root / 'study'
# Create output directory
study_output.mkdir(exist_ok=True)
# Track all files for navigation
domain_files = {
1: [],
2: [],
3: [],
4: []
}
# Convert INDEX.md to the main study page
index_md = project_root / 'INDEX.md'
if index_md.exists():
process_markdown_file(
index_md,
study_output / 'index.html',
base_path='..'
)
# Convert MASTER_STUDY_PLAN.md
plan_md = project_root / 'MASTER_STUDY_PLAN.md'
if plan_md.exists():
process_markdown_file(
plan_md,
study_output / 'study-plan.html',
base_path='..'
)
# Convert AWS-SA-PRO-CHEATSHEET.md
cheatsheet_md = project_root / 'AWS-SA-PRO-CHEATSHEET.md'
if cheatsheet_md.exists():
process_markdown_file(
cheatsheet_md,
study_output / 'cheatsheet.html',
base_path='..'
)
# Process comparison files
comparisons_dir = project_root / 'comparisons'
if comparisons_dir.exists():
comparisons_output = study_output / 'comparisons'
comparisons_output.mkdir(exist_ok=True)
for md_file in sorted(comparisons_dir.glob('*.md')):
if md_file.name != '.gitkeep':
output_file = comparisons_output / md_file.with_suffix('.html').name
process_markdown_file(
md_file,
output_file,
base_path='../..'
)
# Process each domain
domain_names = {
1: 'organizational-complexity',
2: 'new-solutions',
3: 'continuous-improvement',
4: 'migration-modernization'
}
for domain_num in range(1, 5):
domain_dir = project_root / f'domain-{domain_num}-{domain_names[domain_num]}'
if not domain_dir.exists():
continue
domain_output = study_output / f'domain-{domain_num}'
domain_output.mkdir(exist_ok=True)
# Process all markdown files in domain
for md_file in sorted(domain_dir.glob('*.md')):
output_file = domain_output / md_file.with_suffix('.html').name
title, frontmatter = process_markdown_file(
md_file,
output_file,
base_path='../..'
)
domain_files[domain_num].append({
'path': f'domain-{domain_num}/{output_file.name}',
'title': title,
'frontmatter': frontmatter,
'filename': md_file.stem
})
print("\n✅ Conversion complete!")
print(f"Output directory: {study_output}")
return domain_files
if __name__ == '__main__':
main()