Hugoifier

fix(complete): duplicate paginate patch, fragile path construction, TOML injection, error return vs raise Closes #2

lmata 2026-03-12 21:50 trunk
Commit 1f1280be7f5e7c20c01598b36449476187141c24379e2e54315107ded0409a09
1 file changed +8 -16
--- src/utils/complete.py
+++ src/utils/complete.py
@@ -6,10 +6,11 @@
66
"""
77
88
import logging
99
import os
1010
import shutil
11
+from pathlib import Path
1112
1213
from utils.theme_finder import find_hugo_theme, find_raw_html_files
1314
from utils.hugoify import hugoify_html
1415
from utils.decapify import decapify
1516
from utils.theme_patcher import patch_theme, patch_config
@@ -46,11 +47,11 @@
4647
return _assemble_hugo_site(info, output_dir, branding)
4748
else:
4849
# Raw HTML path
4950
html_files = find_raw_html_files(input_path)
5051
if not html_files:
51
- return f"No Hugo theme or HTML files found in {input_path}"
52
+ raise ValueError(f"No Hugo theme or HTML files found in {input_path}")
5253
return _convert_raw_html(input_path, html_files, output_dir, branding)
5354
5455
5556
# ---------------------------------------------------------------------------
5657
# Hugo theme path
@@ -60,15 +61,11 @@
6061
theme_dir = info['theme_dir']
6162
example_site = info['example_site']
6263
theme_name = info['theme_name']
6364
6465
if output_dir is None:
65
- output_dir = os.path.join(
66
- os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
67
- 'output',
68
- theme_name,
69
- )
66
+ output_dir = str(Path(__file__).parents[2] / 'output' / theme_name)
7067
7168
logging.info(f"Building site at {output_dir} ...")
7269
os.makedirs(output_dir, exist_ok=True)
7370
7471
# 1. Copy theme files → themes/{theme_name}/
@@ -122,15 +119,11 @@
122119
123120
def _convert_raw_html(input_path: str, html_files: list, output_dir: str = None, branding: dict = None) -> str:
124121
theme_name = os.path.basename(os.path.abspath(input_path))
125122
126123
if output_dir is None:
127
- output_dir = os.path.join(
128
- os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
129
- 'output',
130
- theme_name,
131
- )
124
+ output_dir = str(Path(__file__).parents[2] / 'output' / theme_name)
132125
133126
logging.info(f"Converting raw HTML theme: {theme_name}")
134127
135128
# Use AI to convert the main HTML file to Hugo layouts
136129
main_html = _pick_main_html(html_files)
@@ -209,13 +202,10 @@
209202
"""Copy source config to hugo.toml, ensuring theme = theme_name and modern key names."""
210203
import re
211204
with open(source_config, 'r') as f:
212205
content = f.read()
213206
214
- # Fix deprecated keys for Hugo >= v0.128
215
- content = re.sub(r'^paginate\s*=\s*(\d+)', r'[pagination]\n pagerSize = \1', content, flags=re.MULTILINE)
216
-
217207
# Suppress noisy but harmless warnings from example content
218208
if 'ignoreLogs' not in content:
219209
content += "\nignorelogs = ['warning-goldmark-raw-html']\n"
220210
221211
# Ensure theme is set correctly
@@ -231,15 +221,17 @@
231221
logging.info("Wrote hugo.toml")
232222
233223
234224
def _write_minimal_hugo_toml(output_dir: str, theme_name: str):
235225
dest = os.path.join(output_dir, 'hugo.toml')
226
+ safe_name = theme_name.replace('"', '')
227
+ title = safe_name.replace('-', ' ').title()
236228
with open(dest, 'w') as f:
237229
f.write(f'''baseURL = "http://localhost:1313/"
238230
languageCode = "en-us"
239
-title = "{theme_name.replace('-', ' ').title()}"
240
-theme = "{theme_name}"
231
+title = "{title}"
232
+theme = "{safe_name}"
241233
''')
242234
logging.info("Wrote minimal hugo.toml")
243235
244236
245237
def _pick_main_html(html_files: list) -> str:
246238
--- src/utils/complete.py
+++ src/utils/complete.py
@@ -6,10 +6,11 @@
6 """
7
8 import logging
9 import os
10 import shutil
 
11
12 from utils.theme_finder import find_hugo_theme, find_raw_html_files
13 from utils.hugoify import hugoify_html
14 from utils.decapify import decapify
15 from utils.theme_patcher import patch_theme, patch_config
@@ -46,11 +47,11 @@
46 return _assemble_hugo_site(info, output_dir, branding)
47 else:
48 # Raw HTML path
49 html_files = find_raw_html_files(input_path)
50 if not html_files:
51 return f"No Hugo theme or HTML files found in {input_path}"
52 return _convert_raw_html(input_path, html_files, output_dir, branding)
53
54
55 # ---------------------------------------------------------------------------
56 # Hugo theme path
@@ -60,15 +61,11 @@
60 theme_dir = info['theme_dir']
61 example_site = info['example_site']
62 theme_name = info['theme_name']
63
64 if output_dir is None:
65 output_dir = os.path.join(
66 os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
67 'output',
68 theme_name,
69 )
70
71 logging.info(f"Building site at {output_dir} ...")
72 os.makedirs(output_dir, exist_ok=True)
73
74 # 1. Copy theme files → themes/{theme_name}/
@@ -122,15 +119,11 @@
122
123 def _convert_raw_html(input_path: str, html_files: list, output_dir: str = None, branding: dict = None) -> str:
124 theme_name = os.path.basename(os.path.abspath(input_path))
125
126 if output_dir is None:
127 output_dir = os.path.join(
128 os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
129 'output',
130 theme_name,
131 )
132
133 logging.info(f"Converting raw HTML theme: {theme_name}")
134
135 # Use AI to convert the main HTML file to Hugo layouts
136 main_html = _pick_main_html(html_files)
@@ -209,13 +202,10 @@
209 """Copy source config to hugo.toml, ensuring theme = theme_name and modern key names."""
210 import re
211 with open(source_config, 'r') as f:
212 content = f.read()
213
214 # Fix deprecated keys for Hugo >= v0.128
215 content = re.sub(r'^paginate\s*=\s*(\d+)', r'[pagination]\n pagerSize = \1', content, flags=re.MULTILINE)
216
217 # Suppress noisy but harmless warnings from example content
218 if 'ignoreLogs' not in content:
219 content += "\nignorelogs = ['warning-goldmark-raw-html']\n"
220
221 # Ensure theme is set correctly
@@ -231,15 +221,17 @@
231 logging.info("Wrote hugo.toml")
232
233
234 def _write_minimal_hugo_toml(output_dir: str, theme_name: str):
235 dest = os.path.join(output_dir, 'hugo.toml')
 
 
236 with open(dest, 'w') as f:
237 f.write(f'''baseURL = "http://localhost:1313/"
238 languageCode = "en-us"
239 title = "{theme_name.replace('-', ' ').title()}"
240 theme = "{theme_name}"
241 ''')
242 logging.info("Wrote minimal hugo.toml")
243
244
245 def _pick_main_html(html_files: list) -> str:
246
--- src/utils/complete.py
+++ src/utils/complete.py
@@ -6,10 +6,11 @@
6 """
7
8 import logging
9 import os
10 import shutil
11 from pathlib import Path
12
13 from utils.theme_finder import find_hugo_theme, find_raw_html_files
14 from utils.hugoify import hugoify_html
15 from utils.decapify import decapify
16 from utils.theme_patcher import patch_theme, patch_config
@@ -46,11 +47,11 @@
47 return _assemble_hugo_site(info, output_dir, branding)
48 else:
49 # Raw HTML path
50 html_files = find_raw_html_files(input_path)
51 if not html_files:
52 raise ValueError(f"No Hugo theme or HTML files found in {input_path}")
53 return _convert_raw_html(input_path, html_files, output_dir, branding)
54
55
56 # ---------------------------------------------------------------------------
57 # Hugo theme path
@@ -60,15 +61,11 @@
61 theme_dir = info['theme_dir']
62 example_site = info['example_site']
63 theme_name = info['theme_name']
64
65 if output_dir is None:
66 output_dir = str(Path(__file__).parents[2] / 'output' / theme_name)
 
 
 
 
67
68 logging.info(f"Building site at {output_dir} ...")
69 os.makedirs(output_dir, exist_ok=True)
70
71 # 1. Copy theme files → themes/{theme_name}/
@@ -122,15 +119,11 @@
119
120 def _convert_raw_html(input_path: str, html_files: list, output_dir: str = None, branding: dict = None) -> str:
121 theme_name = os.path.basename(os.path.abspath(input_path))
122
123 if output_dir is None:
124 output_dir = str(Path(__file__).parents[2] / 'output' / theme_name)
 
 
 
 
125
126 logging.info(f"Converting raw HTML theme: {theme_name}")
127
128 # Use AI to convert the main HTML file to Hugo layouts
129 main_html = _pick_main_html(html_files)
@@ -209,13 +202,10 @@
202 """Copy source config to hugo.toml, ensuring theme = theme_name and modern key names."""
203 import re
204 with open(source_config, 'r') as f:
205 content = f.read()
206
 
 
 
207 # Suppress noisy but harmless warnings from example content
208 if 'ignoreLogs' not in content:
209 content += "\nignorelogs = ['warning-goldmark-raw-html']\n"
210
211 # Ensure theme is set correctly
@@ -231,15 +221,17 @@
221 logging.info("Wrote hugo.toml")
222
223
224 def _write_minimal_hugo_toml(output_dir: str, theme_name: str):
225 dest = os.path.join(output_dir, 'hugo.toml')
226 safe_name = theme_name.replace('"', '')
227 title = safe_name.replace('-', ' ').title()
228 with open(dest, 'w') as f:
229 f.write(f'''baseURL = "http://localhost:1313/"
230 languageCode = "en-us"
231 title = "{title}"
232 theme = "{safe_name}"
233 ''')
234 logging.info("Wrote minimal hugo.toml")
235
236
237 def _pick_main_html(html_files: list) -> str:
238

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button