-
Notifications
You must be signed in to change notification settings - Fork 7
372 lines (299 loc) · 16.8 KB
/
Copy pathsync-content.yml
File metadata and controls
372 lines (299 loc) · 16.8 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
name: Auto-sync Jekyll Content
on:
push:
paths:
- 'demos/**/README.md'
- 'experiments/**/README.md'
- 'snippets/**/README.md'
- 'snippets/**/*.md'
- 'snippets/**/*.py'
workflow_dispatch:
jobs:
sync-content:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Create sync script
run: |
cat > sync_content.py << 'SCRIPT_END'
import os
import re
from datetime import datetime
from pathlib import Path
# Create directories if they don't exist
Path("_demos").mkdir(exist_ok=True)
Path("_experiments").mkdir(exist_ok=True)
Path("_snippets").mkdir(exist_ok=True)
# Sync demos
if Path("demos").exists():
for demo_dir in Path("demos").iterdir():
if demo_dir.is_dir() and (demo_dir / "README.md").exists():
readme_path = demo_dir / "README.md"
content = readme_path.read_text()
# Extract title from first # heading
title_match = re.search(r'^# (.+)$', content, re.MULTILINE)
title = title_match.group(1) if title_match else demo_dir.name
# Extract metadata from content
tags = []
if "## Tags" in content:
tags_section = content.split("## Tags")[1].split("##")[0]
tags = [tag.strip("- \n") for tag in tags_section.strip().split("\n") if tag.strip()]
technologies = []
if "## Technologies" in content:
tech_section = content.split("## Technologies")[1].split("##")[0]
technologies = [tech.strip("- \n") for tech in tech_section.strip().split("\n") if tech.strip()]
difficulty = "medium"
if "## Difficulty" in content:
diff_section = content.split("## Difficulty")[1].split("##")[0]
difficulty = diff_section.strip().lower()
# Get description (first paragraph after title, stop at next heading)
desc_match = re.search(r'^# .+\n\n([^#]+?)(?:\n#|\n\n#|\Z)', content, re.MULTILINE | re.DOTALL)
description = desc_match.group(1).strip() if desc_match else "Demo showcasing AI capabilities"
# Format tags and technologies as YAML lists
tags_yaml = "\n".join([f" - {tag}" for tag in tags]) if tags else ""
tech_yaml = "\n".join([f" - {tech}" for tech in technologies]) if technologies else ""
# Create Jekyll file with proper front matter
jekyll_content = f"""---
title: "{title}"
date: {datetime.now().strftime('%Y-%m-%d')}
description: "{description}"
layout: demo
difficulty: {difficulty}
source_folder: "demos/{demo_dir.name}"
"""
if tags_yaml:
jekyll_content += f"\ntags:\n{tags_yaml}"
if tech_yaml:
jekyll_content += f"\ntechnologies:\n{tech_yaml}"
jekyll_content += f"""
---
{content}
<div class='source-links'>
<h3>Full Source Code</h3>
<a href='https://github.com/aws-samples/sample-ai-possibilities/tree/main/demos/{demo_dir.name}' class='btn btn-primary'>
View on GitHub
</a>
</div>
"""
# Clean up extra indentation
jekyll_content = jekyll_content.replace("\n ", "\n")
# Write to _demos
output_path = Path("_demos") / f"{demo_dir.name}.md"
output_path.write_text(jekyll_content)
print(f"Synced demo: {demo_dir.name}")
# Sync experiments
if Path("experiments").exists():
for exp_dir in Path("experiments").iterdir():
if exp_dir.is_dir() and (exp_dir / "README.md").exists():
readme_path = exp_dir / "README.md"
content = readme_path.read_text()
title_match = re.search(r'^# (.+)$', content, re.MULTILINE)
title = title_match.group(1) if title_match else exp_dir.name
# Extract metadata from content
tags = []
if "## Tags" in content:
tags_section = content.split("## Tags")[1].split("##")[0]
tags = [tag.strip("- \n") for tag in tags_section.strip().split("\n") if tag.strip()]
technologies = []
if "## Technologies" in content:
tech_section = content.split("## Technologies")[1].split("##")[0]
technologies = [tech.strip("- \n") for tech in tech_section.strip().split("\n") if tech.strip()]
difficulty = "medium"
if "## Difficulty" in content:
diff_section = content.split("## Difficulty")[1].split("##")[0]
difficulty = diff_section.strip().lower()
# Get description - try to get content under Overview section first
description = "Experimental AI implementation"
if "## Overview" in content:
overview_match = re.search(r'## Overview\s*\n\n([^#]+?)(?:\n#|\n\n#|\Z)', content, re.MULTILINE | re.DOTALL)
if overview_match:
description = overview_match.group(1).strip()
else:
# Fallback to first paragraph after title
desc_match = re.search(r'^# .+\n\n([^#]+?)(?:\n#|\n\n#|\Z)', content, re.MULTILINE | re.DOTALL)
if desc_match:
description = desc_match.group(1).strip()
# Format tags and technologies as YAML lists
tags_yaml = "\n".join([f" - {tag}" for tag in tags]) if tags else ""
tech_yaml = "\n".join([f" - {tech}" for tech in technologies]) if technologies else ""
jekyll_content = f"""---
title: "{title}"
date: {datetime.now().strftime('%Y-%m-%d')}
description: "{description}"
layout: experiment
difficulty: {difficulty}
source_folder: "experiments/{exp_dir.name}"
"""
if tags_yaml:
jekyll_content += f"\ntags:\n{tags_yaml}"
if tech_yaml:
jekyll_content += f"\ntechnologies:\n{tech_yaml}"
jekyll_content += f"""
---
{content}
<div class='source-links'>
<h3>View Source</h3>
<a href='https://github.com/aws-samples/sample-ai-possibilities/tree/main/experiments/{exp_dir.name}' class='btn btn-primary'>
View on GitHub
</a>
</div>
"""
# Clean up indentation
jekyll_content = jekyll_content.replace("\n ", "\n")
output_path = Path("_experiments") / f"{exp_dir.name}.md"
output_path.write_text(jekyll_content)
print(f"Synced experiment: {exp_dir.name}")
# Sync snippets
if Path("snippets").exists():
# First check for files directly in snippets folder
for snippet_file in Path("snippets").iterdir():
if snippet_file.is_file() and snippet_file.suffix in ['.py', '.md']:
content = snippet_file.read_text()
if snippet_file.suffix == '.py':
# Extract docstring
docstring_match = re.search(r'"""(.+?)"""', content, re.DOTALL)
description = docstring_match.group(1).strip() if docstring_match else "Code snippet"
jekyll_content = f"""---
title: "{snippet_file.stem.replace('-', ' ').replace('_', ' ').title()}"
date: {datetime.now().strftime('%Y-%m-%d')}
layout: snippet
language: python
description: "{description}"
source_file: "snippets/{snippet_file.name}"
---
# {snippet_file.stem.replace('-', ' ').replace('_', ' ').title()}
{description}
```python
{content}
```
<div class='source-links'>
<a href='https://github.com/aws-samples/sample-ai-possibilities/blob/main/snippets/{snippet_file.name}' class='btn btn-primary'>
View Raw File
</a>
</div>
"""
else:
jekyll_content = f"""---
title: "{snippet_file.stem.replace('-', ' ').replace('_', ' ').title()}"
date: {datetime.now().strftime('%Y-%m-%d')}
layout: snippet
source_file: "snippets/{snippet_file.name}"
---
{content}
"""
# Clean up indentation
jekyll_content = jekyll_content.replace("\n ", "\n")
output_path = Path("_snippets") / f"{snippet_file.stem}.md"
output_path.write_text(jekyll_content)
print(f"Synced snippet: {snippet_file.name}")
# Now check for README files in subdirectories (like demos and experiments)
for snippet_dir in Path("snippets").iterdir():
if snippet_dir.is_dir():
# Look for README.md in subdirectory
readme_path = snippet_dir / "README.md"
if readme_path.exists():
content = readme_path.read_text()
# Extract title from first # heading
title_match = re.search(r'^# (.+)$', content, re.MULTILINE)
title = title_match.group(1) if title_match else snippet_dir.name
# Extract metadata from content
tags = []
if "## Tags" in content:
tags_section = content.split("## Tags")[1].split("##")[0]
tags = [tag.strip("- \n") for tag in tags_section.strip().split("\n") if tag.strip()]
technologies = []
if "## Technologies" in content:
tech_section = content.split("## Technologies")[1].split("##")[0]
technologies = [tech.strip("- \n") for tech in tech_section.strip().split("\n") if tech.strip()]
difficulty = "medium"
if "## Difficulty" in content:
diff_section = content.split("## Difficulty")[1].split("##")[0]
difficulty = diff_section.strip().lower()
# Get description (first paragraph after title, stop at next heading)
desc_match = re.search(r'^# .+\n\n([^#]+?)(?:\n#|\n\n#|\Z)', content, re.MULTILINE | re.DOTALL)
description = desc_match.group(1).strip() if desc_match else "Code snippet"
# Format tags and technologies as YAML lists
tags_yaml = "\n".join([f" - {tag}" for tag in tags]) if tags else ""
tech_yaml = "\n".join([f" - {tech}" for tech in technologies]) if technologies else ""
jekyll_content = f"""---
title: "{title}"
date: {datetime.now().strftime('%Y-%m-%d')}
description: "{description}"
layout: snippet
difficulty: {difficulty}
source_folder: "snippets/{snippet_dir.name}"
"""
if tags_yaml:
jekyll_content += f"\ntags:\n{tags_yaml}"
if tech_yaml:
jekyll_content += f"\ntechnologies:\n{tech_yaml}"
jekyll_content += f"""
---
{content}
<div class='source-links'>
<h3>View Source</h3>
<a href='https://github.com/aws-samples/sample-ai-possibilities/tree/main/snippets/{snippet_dir.name}' class='btn btn-primary'>
View on GitHub
</a>
</div>
"""
# Clean up indentation
jekyll_content = jekyll_content.replace("\n ", "\n")
output_path = Path("_snippets") / f"{snippet_dir.name}.md"
output_path.write_text(jekyll_content)
print(f"Synced snippet: {snippet_dir.name}")
# Also look for individual .py files in subdirectories
for py_file in snippet_dir.glob("*.py"):
if py_file.name != "__init__.py": # Skip __init__.py files
content = py_file.read_text()
# Extract docstring
docstring_match = re.search(r'"""(.+?)"""', content, re.DOTALL)
description = docstring_match.group(1).strip() if docstring_match else "Code snippet"
jekyll_content = f"""---
title: "{py_file.stem.replace('-', ' ').replace('_', ' ').title()}"
date: {datetime.now().strftime('%Y-%m-%d')}
layout: snippet
language: python
description: "{description}"
source_file: "snippets/{snippet_dir.name}/{py_file.name}"
---
# {py_file.stem.replace('-', ' ').replace('_', ' ').title()}
{description}
```python
{content}
```
<div class='source-links'>
<a href='https://github.com/aws-samples/sample-ai-possibilities/blob/main/snippets/{snippet_dir.name}/{py_file.name}' class='btn btn-primary'>
View Raw File
</a>
</div>
"""
# Clean up indentation
jekyll_content = jekyll_content.replace("\n ", "\n")
output_path = Path("_snippets") / f"{snippet_dir.name}-{py_file.stem}.md"
output_path.write_text(jekyll_content)
print(f"Synced snippet: {snippet_dir.name}/{py_file.name}")
print("\nContent sync complete!")
SCRIPT_END
- name: Run sync script
run: python sync_content.py
- name: Commit and push changes
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add _demos/ _experiments/ _snippets/
if git diff --staged --quiet; then
echo "No changes to commit"
else
git commit -m "Auto-sync content from source folders [skip ci]"
git push
fi