-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmigrate_posts.py
More file actions
170 lines (137 loc) · 5.21 KB
/
migrate_posts.py
File metadata and controls
170 lines (137 loc) · 5.21 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
#!/usr/bin/env python3
"""
Script para migrar posts de Jekyll a Hugo (formato Blowfish)
"""
import os
import re
from pathlib import Path
# Directorios
jekyll_posts = Path("_posts")
hugo_posts = Path("content/posts")
# Crear directorio si no existe
hugo_posts.mkdir(parents=True, exist_ok=True)
def clean_title(title: str) -> str:
"""Limpia el titulo para evitar problemas con TOML/YAML"""
# Remover comillas existentes
title = title.strip().strip('"').strip("'")
# Remover comillas dobles internas en lugar de escaparlas
title = title.replace('"', "'")
return title
def parse_tags(tags_str: str) -> list:
"""Parsea tags de Jekyll (separados por espacio) a lista"""
tags_str = tags_str.strip()
# Si ya es formato array YAML [tag1, tag2], extraer contenido
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
# Separar por espacios o comas
tags = [t.strip() for t in re.split(r'[\s,]+', tags_str) if t.strip()]
return tags
def convert_post(jekyll_file: Path) -> bool:
"""Convierte un post de Jekyll a Hugo"""
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
# Extraer front matter
match = re.match(r'^---\s*\n(.*?)\n---\s*\n(.*)', content, re.DOTALL)
if not match:
print(f"No se pudo parsear: {jekyll_file.name}")
return False
front_matter = match.group(1)
body = match.group(2)
# Parsear datos del front matter
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)
categories_match = re.search(r'^categories:\s*(.+?)\s*$', front_matter, re.MULTILINE)
# Extraer imagen del header si existe
overlay_image = re.search(r'overlay_image:\s*(.+?)\s*$', front_matter, re.MULTILINE)
# Construir nuevo front matter para Hugo
new_front_matter = "---\n"
# Titulo
if title_match:
title = clean_title(title_match.group(1))
new_front_matter += f'title: "{title}"\n'
else:
# Usar nombre del archivo como titulo
filename_title = jekyll_file.stem
filename_title = re.sub(r'^\d{4}-\d{2}-\d{2}-', '', filename_title)
filename_title = filename_title.replace('-', ' ').title()
new_front_matter += f'title: "{filename_title}"\n'
# Fecha - extraer del nombre del archivo o del front matter
date_str = None
filename_date = re.match(r'^(\d{4}-\d{2}-\d{2})', jekyll_file.name)
if filename_date:
date_str = filename_date.group(1)
elif 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:
date_str = date_parsed.group(1)
if date_str:
new_front_matter += f'date: {date_str}\n'
# Autor
if author_match:
author = author_match.group(1).strip()
new_front_matter += f'author: "{author}"\n'
# Tags
if tags_match:
tags = parse_tags(tags_match.group(1))
if tags:
new_front_matter += f'tags: {tags}\n'
# Categories
if categories_match:
categories = parse_tags(categories_match.group(1))
if categories:
new_front_matter += f'categories: {categories}\n'
# Description (excerpt)
if excerpt_match:
excerpt = clean_title(excerpt_match.group(1))
new_front_matter += f'description: "{excerpt}"\n'
# Imagen destacada
if overlay_image:
img_url = overlay_image.group(1).strip()
new_front_matter += f'featuredImage: "{img_url}"\n'
new_front_matter += "draft: false\n"
new_front_matter += "---\n\n"
# Contenido final
new_content = new_front_matter + body
# Nombre del archivo (sin la fecha al inicio)
filename = jekyll_file.name
filename = re.sub(r'^\d{4}-\d{2}-\d{2}-', '', filename)
# Guardar
output_file = hugo_posts / filename
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("Iniciando migracion de posts Jekyll a Hugo...\n")
if not jekyll_posts.exists():
print(f"Error: No se encontro el directorio {jekyll_posts}")
return
posts = sorted(jekyll_posts.glob("*.md"))
migrated = 0
failed = 0
for post in posts:
if convert_post(post):
migrated += 1
print(f"OK {post.name}")
else:
failed += 1
print(f"FAIL {post.name}")
print(f"\nResumen:")
print(f" Migrados: {migrated}")
print(f" Fallidos: {failed}")
print(f" Total: {len(posts)}")
if __name__ == "__main__":
main()