-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmigrate_drafts.py
More file actions
131 lines (102 loc) · 3.92 KB
/
migrate_drafts.py
File metadata and controls
131 lines (102 loc) · 3.92 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
#!/usr/bin/env python3
"""
Script para migrar drafts de Jekyll a Hugo
"""
import re
from pathlib import Path
jekyll_drafts = Path("_drafts")
hugo_posts = Path("content/posts")
hugo_posts.mkdir(parents=True, exist_ok=True)
def clean_title(title: str) -> str:
title = title.strip().strip('"').strip("'")
title = title.replace('"', "'")
return title
def parse_tags(tags_str: str) -> list:
tags_str = tags_str.strip()
if tags_str.startswith('[') and tags_str.endswith(']'):
inner = tags_str[1:-1]
tags = [t.strip() for t in inner.split(',') if t.strip()]
return tags
tags = [t.strip() for t in re.split(r'[\s,]+', tags_str) if t.strip()]
return tags
def convert_draft(jekyll_file: Path) -> bool:
try:
with open(jekyll_file, 'r', encoding='utf-8') as f:
content = f.read()
except Exception as e:
print(f"Error leyendo {jekyll_file.name}: {e}")
return False
match = re.match(r'^---\s*\n(.*?)\n---\s*\n(.*)', content, re.DOTALL)
if not match:
# Si no tiene front matter, crear uno basico
title = jekyll_file.stem.replace('-', ' ').title()
new_content = f'---\ntitle: "{title}"\ndraft: true\n---\n\n{content}'
output_file = hugo_posts / jekyll_file.name
with open(output_file, 'w', encoding='utf-8') as f:
f.write(new_content)
return True
front_matter = match.group(1)
body = match.group(2)
title_match = re.search(r'^title:\s*["\']?(.*?)["\']?\s*$', front_matter, re.MULTILINE)
date_match = re.search(r'^date:\s*(.+?)\s*$', front_matter, re.MULTILINE)
author_match = re.search(r'^author:\s*(.+?)\s*$', front_matter, re.MULTILINE)
tags_match = re.search(r'^tags:\s*(.+?)\s*$', front_matter, re.MULTILINE)
excerpt_match = re.search(r'^excerpt:\s*["\']?(.*?)["\']?\s*$', front_matter, re.MULTILINE)
overlay_image = re.search(r'overlay_image:\s*(.+?)\s*$', front_matter, re.MULTILINE)
new_front_matter = "---\n"
if title_match:
title = clean_title(title_match.group(1))
new_front_matter += f'title: "{title}"\n'
else:
filename_title = jekyll_file.stem.replace('-', ' ').title()
new_front_matter += f'title: "{filename_title}"\n'
if date_match:
date_val = date_match.group(1).strip()
date_parsed = re.match(r'(\d{4}-\d{2}-\d{2})', date_val)
if date_parsed:
new_front_matter += f'date: {date_parsed.group(1)}\n'
if author_match:
author = author_match.group(1).strip()
new_front_matter += f'author: "{author}"\n'
if tags_match:
tags = parse_tags(tags_match.group(1))
if tags:
new_front_matter += f'tags: {tags}\n'
if excerpt_match:
excerpt = clean_title(excerpt_match.group(1))
new_front_matter += f'description: "{excerpt}"\n'
if overlay_image:
img_url = overlay_image.group(1).strip()
new_front_matter += f'featuredImage: "{img_url}"\n'
# Marcar como draft
new_front_matter += "draft: true\n"
new_front_matter += "---\n\n"
new_content = new_front_matter + body
output_file = hugo_posts / jekyll_file.name
try:
with open(output_file, 'w', encoding='utf-8') as f:
f.write(new_content)
return True
except Exception as e:
print(f"Error escribiendo {output_file}: {e}")
return False
def main():
print("Migrando drafts de Jekyll a Hugo...\n")
if not jekyll_drafts.exists():
print(f"No se encontro {jekyll_drafts}")
return
drafts = sorted(jekyll_drafts.glob("*.md"))
migrated = 0
failed = 0
for draft in drafts:
if convert_draft(draft):
migrated += 1
print(f"OK {draft.name}")
else:
failed += 1
print(f"FAIL {draft.name}")
print(f"\nResumen:")
print(f" Migrados: {migrated}")
print(f" Fallidos: {failed}")
if __name__ == "__main__":
main()