|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Convert system-design-101 guides to Jekyll blog posts for atakuzi.github.io |
| 4 | +""" |
| 5 | + |
| 6 | +import os |
| 7 | +import re |
| 8 | +from pathlib import Path |
| 9 | +from datetime import datetime |
| 10 | + |
| 11 | +def extract_frontmatter(content): |
| 12 | + """Extract YAML frontmatter from markdown content.""" |
| 13 | + match = re.match(r'^---\n(.*?)\n---\n(.*)$', content, re.DOTALL) |
| 14 | + if match: |
| 15 | + frontmatter = match.group(1) |
| 16 | + body = match.group(2) |
| 17 | + return frontmatter, body |
| 18 | + return None, content |
| 19 | + |
| 20 | +def parse_frontmatter(frontmatter_text): |
| 21 | + """Parse YAML frontmatter into a dictionary.""" |
| 22 | + data = {} |
| 23 | + for line in frontmatter_text.split('\n'): |
| 24 | + if ':' in line: |
| 25 | + key, value = line.split(':', 1) |
| 26 | + key = key.strip() |
| 27 | + value = value.strip().strip('"\'') |
| 28 | + if key == 'categories' or key == 'tags': |
| 29 | + # Skip, will process next |
| 30 | + continue |
| 31 | + data[key] = value |
| 32 | + elif line.strip().startswith('- '): |
| 33 | + # This is a list item |
| 34 | + item = line.strip()[2:].strip('"\'') |
| 35 | + if 'tags' not in data: |
| 36 | + data['tags'] = [] |
| 37 | + data['tags'].append(item) |
| 38 | + return data |
| 39 | + |
| 40 | +def create_jekyll_frontmatter(original_data): |
| 41 | + """Create Jekyll-compatible frontmatter.""" |
| 42 | + title = original_data.get('title', 'Untitled') |
| 43 | + description = original_data.get('description', '') |
| 44 | + created_at = original_data.get('createdAt', datetime.now().strftime('%Y-%m-%d')) |
| 45 | + tags = original_data.get('tags', []) |
| 46 | + |
| 47 | + # Build the frontmatter |
| 48 | + frontmatter = f"""--- |
| 49 | +layout: post |
| 50 | +title: "{title}" |
| 51 | +subtitle: "{description}" |
| 52 | +date: {created_at} |
| 53 | +tags: {tags} |
| 54 | +--- |
| 55 | +""" |
| 56 | + return frontmatter |
| 57 | + |
| 58 | +def convert_guide_to_post(guide_path, output_dir): |
| 59 | + """Convert a single guide file to Jekyll post format.""" |
| 60 | + # Read the guide content |
| 61 | + with open(guide_path, 'r', encoding='utf-8') as f: |
| 62 | + content = f.read() |
| 63 | + |
| 64 | + # Extract and parse frontmatter |
| 65 | + frontmatter_text, body = extract_frontmatter(content) |
| 66 | + if not frontmatter_text: |
| 67 | + print(f"Warning: No frontmatter found in {guide_path}") |
| 68 | + return None |
| 69 | + |
| 70 | + original_data = parse_frontmatter(frontmatter_text) |
| 71 | + |
| 72 | + # Create Jekyll frontmatter |
| 73 | + jekyll_frontmatter = create_jekyll_frontmatter(original_data) |
| 74 | + |
| 75 | + # Combine frontmatter and body |
| 76 | + jekyll_content = jekyll_frontmatter + '\n' + body |
| 77 | + |
| 78 | + # Generate output filename |
| 79 | + created_at = original_data.get('createdAt', datetime.now().strftime('%Y-%m-%d')) |
| 80 | + guide_name = Path(guide_path).stem |
| 81 | + output_filename = f"{created_at}-{guide_name}.md" |
| 82 | + output_path = output_dir / output_filename |
| 83 | + |
| 84 | + # Write the Jekyll post |
| 85 | + with open(output_path, 'w', encoding='utf-8') as f: |
| 86 | + f.write(jekyll_content) |
| 87 | + |
| 88 | + return output_path |
| 89 | + |
| 90 | +def main(): |
| 91 | + """Main conversion function.""" |
| 92 | + # Define paths |
| 93 | + guides_dir = Path('/home/user/system-design-101/data/guides') |
| 94 | + output_dir = Path('/home/user/atakuzi.github.io/_posts') |
| 95 | + |
| 96 | + # Get all guide files |
| 97 | + guide_files = sorted(guides_dir.glob('*.md')) |
| 98 | + |
| 99 | + print(f"Found {len(guide_files)} guide files to convert") |
| 100 | + |
| 101 | + converted = 0 |
| 102 | + failed = 0 |
| 103 | + |
| 104 | + for guide_file in guide_files: |
| 105 | + try: |
| 106 | + result = convert_guide_to_post(guide_file, output_dir) |
| 107 | + if result: |
| 108 | + converted += 1 |
| 109 | + if converted % 50 == 0: |
| 110 | + print(f"Converted {converted} files...") |
| 111 | + else: |
| 112 | + failed += 1 |
| 113 | + except Exception as e: |
| 114 | + print(f"Error converting {guide_file}: {e}") |
| 115 | + failed += 1 |
| 116 | + |
| 117 | + print(f"\n✅ Conversion complete!") |
| 118 | + print(f" Successfully converted: {converted}") |
| 119 | + print(f" Failed: {failed}") |
| 120 | + print(f" Output directory: {output_dir}") |
| 121 | + |
| 122 | +if __name__ == '__main__': |
| 123 | + main() |
0 commit comments