-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconvert_tutorials.py
More file actions
373 lines (308 loc) · 10.1 KB
/
convert_tutorials.py
File metadata and controls
373 lines (308 loc) · 10.1 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env python3
"""
Convert egglog tutorial files (.egg) to HTML format.
Comments (lines starting with ;;) become text, code becomes markdown code blocks.
"""
import os
import re
from pathlib import Path
from typing import List, Tuple
import markdown
import html
def parse_egg_file(file_path: Path) -> List[Tuple[str, str]]:
"""
Parse an .egg file and return a list of (type, content) tuples.
type is either 'comment' or 'code'
"""
with open(file_path, "r", encoding="utf-8") as f:
lines = f.readlines()
blocks = []
current_block = []
current_type = None
title = None
for line in lines:
stripped = line.strip()
# Determine line type
if stripped.startswith(";;"):
line_type = "comment"
# Remove the comment prefix and clean up
content = line[2:]
elif stripped == "" and current_type == "comment":
# Empty line continues comment block
line_type = "comment"
content = line
elif stripped == "":
# Empty line in code or between blocks
if current_block:
line_type = current_type
content = line
else:
continue
else:
line_type = "code"
content = line
if title is None and line_type == "comment":
# Extract title from the first comment block
title = content.strip()
continue
# Handle block transitions
if current_type is None:
current_type = line_type
elif current_type != line_type:
# Type changed, save current block and start new one
if current_block:
blocks.append((current_type, "".join(current_block)))
current_block = []
current_type = line_type
current_block.append(content)
# Add final block
if current_block:
blocks.append((current_type, "".join(current_block)))
return title, blocks
def highlight_egglog_syntax(code: str) -> str:
"""Apply simple syntax highlighting to egglog code"""
# Define egglog keywords and their CSS classes
keywords = "|".join(
[
"run-with",
"let-scheduler",
"with-ruleset",
"print-size",
"print-function",
"rewrite",
"rule",
"run-schedule",
"run",
"function",
"datatype",
"sort",
"relation",
"constructor",
"let",
"set",
"union",
"extract",
"check",
"push",
"pop",
"query",
"birewrite",
"seq",
"repeat",
"print-size"
"saturate",
"fail",
"ruleset",
"when"
]
)
pattern = r"\b(" + keywords + r")\b"
replacement = r'<span class="keyword">\1</span>'
# Apply highlighting
highlighted = code
# for pattern, replacement in keywords.items():
highlighted = re.sub(pattern, replacement, highlighted, flags=re.MULTILINE)
return highlighted
def extract_title(blocks: List[Tuple[str, str]]) -> str:
"""Extract title from the first comment block"""
block_type, content = blocks[0]
assert (
block_type == "comment"
), "First block should be a comment for title extraction"
return content
def blocks_to_html(blocks: List[Tuple[str, str]]) -> str:
"""Convert parsed blocks to HTML using markdown parser"""
html_parts = []
# Initialize markdown parser with useful extensions
md = markdown.Markdown(extensions=["fenced_code", "tables"])
for block_type, content in blocks:
if block_type == "comment":
# Convert comment to markdown, then to HTML
if content:
# Convert the comment content as markdown
html_content = md.convert(content)
html_parts.append(html_content)
elif block_type == "code":
# Convert code to HTML with custom syntax highlighting
content = content.rstrip()
if content:
# Apply syntax highlighting
highlighted_code = highlight_egglog_syntax(content)
# Wrap in pre/code tags instead of using markdown
html_content = f'<pre><code class="language-egglog">{highlighted_code}</code></pre>'
html_parts.append(html_content)
return "\n".join(html_parts)
def get_tutorial_files(tutorial_dir: Path) -> List[Path]:
"""Get all .egg files in tutorial directory, sorted"""
egg_files = list(tutorial_dir.glob("0*.egg"))
return sorted(egg_files)
def generate_navigation(current_file: Path, all_files: List[Path]) -> str:
"""Generate navigation bar"""
nav_items = []
for i, file_path in enumerate(all_files):
name = file_path.stem
title = name.replace("-", " ").title()
if file_path == current_file:
nav_items.append(f'<span class="current">{title}</span>')
else:
html_name = f"{name}.html"
nav_items.append(f'<a href="{html_name}">{title}</a>')
return f"""
<nav class="tutorial-nav">
<div class="nav-container">
<h1>Tutorial for egglog</h1>
<div class="nav-links">
{' | '.join(nav_items)}
</div>
</div>
</nav>
"""
def generate_html_page(file_path: Path, all_files: List[Path]) -> str:
"""Generate complete HTML page for a tutorial file"""
title, blocks = parse_egg_file(file_path)
content = blocks_to_html(blocks)
navigation = generate_navigation(file_path, all_files)
return f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{title} - egglog tutorial</title>
<style>
body {{
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
line-height: 1.6;
color: #333;
max-width: 900px;
margin: 0 auto;
padding: 0 20px;
background-color: #fafafa;
}}
.tutorial-nav {{
background: #2c3e50;
color: white;
padding: 1rem 0;
margin: -20px -20px 2rem -20px;
border-bottom: 3px solid #3498db;
}}
.nav-container {{
max-width: 900px;
margin: 0 auto;
padding: 0 20px;
}}
.tutorial-nav h1 {{
margin: 0 0 0.5rem 0;
color: #ecf0f1;
font-size: 1.5rem;
}}
.nav-links a {{
color: #3498db;
text-decoration: none;
margin: 0 0.5rem;
padding: 0.3rem 0.6rem;
border-radius: 4px;
transition: background-color 0.2s;
}}
.nav-links a:hover {{
background-color: rgba(52, 152, 219, 0.2);
}}
.nav-links .current {{
background-color: #3498db;
color: white;
padding: 0.3rem 0.6rem;
border-radius: 4px;
margin: 0 0.5rem;
}}
h1 {{
color: #2c3e50;
border-bottom: 2px solid #3498db;
padding-bottom: 0.5rem;
}}
p {{
margin: 1rem 0;
text-align: justify;
}}
code {{
background: #f8f9fa;
border: 1px solid #e9ecef;
border-radius: 3px;
padding: 0.2rem 0.4rem;
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
font-size: 0.9rem;
}}
pre {{
background: #f8f9fa;
border: 1px solid #e9ecef;
border-radius: 6px;
padding: 1rem;
overflow-x: auto;
margin: 1.5rem 0;
}}
pre code {{
background: none;
border: none;
padding: 0;
font-size: 0.9rem;
line-height: 1.4;
}}
/* egglog syntax highlighting */
.keyword {{ color: #e74c3c; font-weight: bold; }}
.language-egglog {{
color: #2c3e50;
}}
footer {{
margin-top: 3rem;
padding: 2rem 0;
text-align: center;
border-top: 1px solid #e9ecef;
color: #6c757d;
font-size: 0.9rem;
}}
@media (max-width: 600px) {{
.nav-links {{
font-size: 0.9rem;
}}
.nav-links a, .nav-links .current {{
margin: 0 0.2rem;
padding: 0.2rem 0.4rem;
}}
}}
</style>
</head>
<body>
{navigation}
<main>
<h1>{title}</h1>
{content}
</main>
<footer>
<p>Generated from <code>{file_path.name}</code> | <a href="https://github.com/egraphs-good/egglog" target="_blank">The egglog project</a></p>
</footer>
</body>
</html>"""
def main():
"""Main conversion function"""
# Get tutorial directory
tutorial_dir = Path(__file__).parent
if not tutorial_dir.exists():
print(f"Tutorial directory not found: {tutorial_dir}")
return
# Get all tutorial files
tutorial_files = get_tutorial_files(tutorial_dir)
if not tutorial_files:
print("No .egg files found in tutorial directory")
return
print(f"Found {len(tutorial_files)} tutorial files:")
for file_path in tutorial_files:
print(f" - {file_path.name}")
# Convert each file
for file_path in tutorial_files:
html_content = generate_html_page(file_path, tutorial_files)
# Write HTML file
output_file = file_path.with_suffix(".html")
with open(output_file, "w", encoding="utf-8") as f:
f.write(html_content)
print(f"Generated: {output_file}")
print("\nConversion complete! Open any .html file in your browser.")
if __name__ == "__main__":
main()