forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_lesson_pages.py
More file actions
245 lines (208 loc) · 8.77 KB
/
Copy pathbuild_lesson_pages.py
File metadata and controls
245 lines (208 loc) · 8.77 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
#!/usr/bin/env python3
"""Generate static lesson and topic pages from lessons.json for SEO.
Usage:
python3 scripts/build_lesson_pages.py
Output:
docs/lessons/<slug>/index.html — one page per lesson
docs/topics/<domain>/index.html — one page per domain
"""
import json
import os
from pathlib import Path
LESSONS_JSON = Path("data/lessons.json")
LESSONS_DIR = Path("docs/lessons")
TOPICS_DIR = Path("docs/topics")
SITE_URL = "https://misakanet.org"
# Top domains to generate topic pages for
TOP_DOMAINS = [
"devops", "feishu", "fanuc", "development", "rag",
"agent-network", "general", "uncategorized"
]
HTML_TEMPLATE = """\
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{title} — MisakaNet</title>
<meta name="description" content="{description}">
<link rel="canonical" href="{canonical}">
<meta property="og:title" content="{title}">
<meta property="og:description" content="{description}">
<meta property="og:url" content="{canonical}">
<style>
body {{ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; background: #0a0f1d; color: #e2e8f0; max-width: 720px; margin: 0 auto; padding: 40px 20px; line-height: 1.6; }}
a {{ color: #58a6ff; }}
h1 {{ font-size: 24px; margin-bottom: 8px; }}
.meta {{ color: #8b949e; font-size: 13px; margin-bottom: 24px; }}
.section {{ margin-bottom: 24px; }}
.section h2 {{ font-size: 16px; color: #58a6ff; margin-bottom: 8px; }}
.section p {{ color: #c9d1d9; font-size: 14px; }}
.tags {{ display: flex; gap: 8px; flex-wrap: wrap; margin: 16px 0; }}
.tag {{ background: rgba(88,166,255,0.1); color: #58a6ff; padding: 2px 8px; border-radius: 4px; font-size: 12px; }}
.related {{ margin-top: 32px; }}
.related a {{ display: block; padding: 8px 0; border-bottom: 1px solid rgba(88,166,255,0.1); }}
.back {{ margin-top: 32px; font-size: 13px; }}
</style>
</head>
<body>
{body}
<div class="back"><a href="/">← Back to MisakaNet</a> · <a href="/search/">Search lessons</a></div>
</body>
</html>"""
TOPIC_TEMPLATE = """\
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{title} — MisakaNet</title>
<meta name="description" content="{description}">
<link rel="canonical" href="{canonical}">
<meta property="og:title" content="{title}">
<meta property="og:description" content="{description}">
<meta property="og:url" content="{canonical}">
<style>
body {{ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; background: #0a0f1d; color: #e2e8f0; max-width: 720px; margin: 0 auto; padding: 40px 20px; line-height: 1.6; }}
a {{ color: #58a6ff; }}
h1 {{ font-size: 24px; margin-bottom: 8px; }}
.count {{ color: #8b949e; font-size: 14px; margin-bottom: 24px; }}
.lesson {{ padding: 12px 0; border-bottom: 1px solid rgba(88,166,255,0.1); }}
.lesson a {{ font-weight: 600; }}
.lesson .summary {{ color: #8b949e; font-size: 13px; margin-top: 4px; }}
.back {{ margin-top: 32px; font-size: 13px; }}
</style>
</head>
<body>
<h1>{title}</h1>
<div class="count">{count} lessons in this domain</div>
{lessons}
<div class="back"><a href="/">← Back to MisakaNet</a> · <a href="/search/">Search lessons</a></div>
</body>
</html>"""
def slugify(text: str) -> str:
"""Convert text to URL-friendly slug."""
import re
text = text.lower().strip()
text = re.sub(r'[^\w\s-]', '', text)
text = re.sub(r'[\s_]+', '-', text)
text = re.sub(r'-+', '-', text)
return text[:80].strip('-')
def unique_slug(title: str, seen_slugs: dict) -> str:
"""Generate unique slug, adding short hash on collision."""
import hashlib
base = slugify(title)
if base not in seen_slugs:
seen_slugs[base] = title
return base
# Collision: add short hash
short_hash = hashlib.md5(title.encode()).hexdigest()[:6]
return f"{base}-{short_hash}"
def build_lesson_page(lesson: dict) -> str:
"""Generate HTML for a single lesson."""
title = lesson.get("title", "Untitled")
domain = lesson.get("domain", "general")
summary = lesson.get("summary", "")
tags = lesson.get("tags", [])
url = lesson.get("url", "")
source_url = f"https://github.com/Ikalus1988/MisakaNet/blob/main/{url}" if url else ""
slug = lesson.get("_slug", slugify(title))
canonical = f"{SITE_URL}/lessons/{slug}/"
description = f"{summary[:150]}..." if len(summary) > 150 else summary
if not description:
description = f"Verified failure lesson: {title}. From MisakaNet — Git-backed failure lesson network."
tags_html = "".join(f'<span class="tag">{t}</span>' for t in tags[:6])
body = f"""<h1>{title}</h1>
<div class="meta">Domain: <a href="/topics/{domain}/">{domain}</a></div>
<div class="tags">{tags_html}</div>
<div class="section">
<h2>Summary</h2>
<p>{summary or 'See source for full details.'}</p>
</div>"""
if source_url:
body += f'\n<div class="section"><h2>Source</h2><p><a href="{source_url}">View on GitHub →</a></p></div>'
body += f'\n<div class="section"><h2>Search</h2><p><a href="/search/?q={title}">Find related lessons →</a></p></div>'
return HTML_TEMPLATE.format(title=title, description=description, canonical=canonical, body=body)
def build_topic_page(domain: str, lessons: list) -> str:
"""Generate HTML for a topic/domain page."""
title = f"{domain.title()} Lessons"
description = f"{len(lessons)} verified failure lessons about {domain}. From MisakaNet — Git-backed failure lesson network."
canonical = f"{SITE_URL}/topics/{domain}/"
lessons_html = ""
for l in lessons[:20]:
lesson_slug = l.get("_slug", slugify(l.get("title", "")))
lesson_title = l.get("title", "Untitled")
summary = l.get("summary", "")[:120]
lessons_html += f"""
<div class="lesson">
<a href="/lessons/{lesson_slug}/">{lesson_title}</a>
<div class="summary">{summary}</div>
</div>"""
return TOPIC_TEMPLATE.format(
title=title,
description=description,
canonical=canonical,
count=len(lessons),
lessons=lessons_html
)
def generate_sitemap(lesson_slugs: list, domains: list) -> str:
"""Generate sitemap.xml with all pages."""
lines = ['<?xml version="1.0" encoding="UTF-8"?>',
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">']
# Static pages
static = [
("https://misakanet.org/", "weekly", "1.0"),
("https://misakanet.org/search/", "weekly", "0.9"),
]
for url, freq, prio in static:
lines.append(f" <url><loc>{url}</loc><changefreq>{freq}</changefreq><priority>{prio}</priority></url>")
# Topic pages
for domain in domains:
lines.append(f" <url><loc>{SITE_URL}/topics/{domain}/</loc><changefreq>weekly</changefreq><priority>0.8</priority></url>")
# Lesson pages
for slug in lesson_slugs:
lines.append(f" <url><loc>{SITE_URL}/lessons/{slug}/</loc><changefreq>monthly</changefreq><priority>0.6</priority></url>")
lines.append('</urlset>')
return '\n'.join(lines) + '\n'
def main():
lessons = json.loads(LESSONS_JSON.read_text(encoding="utf-8"))
print(f"Loaded {len(lessons)} lessons")
# Build lesson pages with unique slugs
LESSONS_DIR.mkdir(parents=True, exist_ok=True)
seen_slugs = {}
lesson_slugs = []
count = 0
for lesson in lessons:
title = lesson.get("title", "")
if not title:
continue
slug = unique_slug(title, seen_slugs)
lesson["_slug"] = slug
lesson_slugs.append(slug)
out_dir = LESSONS_DIR / slug
out_dir.mkdir(parents=True, exist_ok=True)
html = build_lesson_page(lesson)
(out_dir / "index.html").write_text(html, encoding="utf-8")
count += 1
print(f"Generated {count} lesson pages ({len(seen_slugs)} unique slugs)")
# Build topic pages
TOPICS_DIR.mkdir(parents=True, exist_ok=True)
by_domain = {}
for l in lessons:
domain = l.get("domain", "general")
by_domain.setdefault(domain, []).append(l)
generated_domains = []
for domain in by_domain:
out_dir = TOPICS_DIR / domain
out_dir.mkdir(parents=True, exist_ok=True)
html = build_topic_page(domain, by_domain[domain])
(out_dir / "index.html").write_text(html, encoding="utf-8")
generated_domains.append(domain)
print(f"Generated {len(generated_domains)} topic pages")
# Generate sitemap
sitemap = generate_sitemap(lesson_slugs, generated_domains)
sitemap_path = Path("docs") / "sitemap.xml"
sitemap_path.write_text(sitemap, encoding="utf-8")
print(f"Generated sitemap: {len(lesson_slugs)} lessons + {len(generated_domains)} topics + 2 static = {len(lesson_slugs) + len(generated_domains) + 2} URLs")
if __name__ == "__main__":
main()