Hugoifier
fix(complete): duplicate paginate patch, fragile path construction, TOML injection, error return vs raise Closes #2
Commit
1f1280be7f5e7c20c01598b36449476187141c24379e2e54315107ded0409a09
Parent
5641b02026e6083…
1 file changed
+8
-16
+8
-16
| --- src/utils/complete.py | ||
| +++ src/utils/complete.py | ||
| @@ -6,10 +6,11 @@ | ||
| 6 | 6 | """ |
| 7 | 7 | |
| 8 | 8 | import logging |
| 9 | 9 | import os |
| 10 | 10 | import shutil |
| 11 | +from pathlib import Path | |
| 11 | 12 | |
| 12 | 13 | from utils.theme_finder import find_hugo_theme, find_raw_html_files |
| 13 | 14 | from utils.hugoify import hugoify_html |
| 14 | 15 | from utils.decapify import decapify |
| 15 | 16 | from utils.theme_patcher import patch_theme, patch_config |
| @@ -46,11 +47,11 @@ | ||
| 46 | 47 | return _assemble_hugo_site(info, output_dir, branding) |
| 47 | 48 | else: |
| 48 | 49 | # Raw HTML path |
| 49 | 50 | html_files = find_raw_html_files(input_path) |
| 50 | 51 | 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}") | |
| 52 | 53 | return _convert_raw_html(input_path, html_files, output_dir, branding) |
| 53 | 54 | |
| 54 | 55 | |
| 55 | 56 | # --------------------------------------------------------------------------- |
| 56 | 57 | # Hugo theme path |
| @@ -60,15 +61,11 @@ | ||
| 60 | 61 | theme_dir = info['theme_dir'] |
| 61 | 62 | example_site = info['example_site'] |
| 62 | 63 | theme_name = info['theme_name'] |
| 63 | 64 | |
| 64 | 65 | 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) | |
| 70 | 67 | |
| 71 | 68 | logging.info(f"Building site at {output_dir} ...") |
| 72 | 69 | os.makedirs(output_dir, exist_ok=True) |
| 73 | 70 | |
| 74 | 71 | # 1. Copy theme files → themes/{theme_name}/ |
| @@ -122,15 +119,11 @@ | ||
| 122 | 119 | |
| 123 | 120 | def _convert_raw_html(input_path: str, html_files: list, output_dir: str = None, branding: dict = None) -> str: |
| 124 | 121 | theme_name = os.path.basename(os.path.abspath(input_path)) |
| 125 | 122 | |
| 126 | 123 | 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) | |
| 132 | 125 | |
| 133 | 126 | logging.info(f"Converting raw HTML theme: {theme_name}") |
| 134 | 127 | |
| 135 | 128 | # Use AI to convert the main HTML file to Hugo layouts |
| 136 | 129 | main_html = _pick_main_html(html_files) |
| @@ -209,13 +202,10 @@ | ||
| 209 | 202 | """Copy source config to hugo.toml, ensuring theme = theme_name and modern key names.""" |
| 210 | 203 | import re |
| 211 | 204 | with open(source_config, 'r') as f: |
| 212 | 205 | content = f.read() |
| 213 | 206 | |
| 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 | 207 | # Suppress noisy but harmless warnings from example content |
| 218 | 208 | if 'ignoreLogs' not in content: |
| 219 | 209 | content += "\nignorelogs = ['warning-goldmark-raw-html']\n" |
| 220 | 210 | |
| 221 | 211 | # Ensure theme is set correctly |
| @@ -231,15 +221,17 @@ | ||
| 231 | 221 | logging.info("Wrote hugo.toml") |
| 232 | 222 | |
| 233 | 223 | |
| 234 | 224 | def _write_minimal_hugo_toml(output_dir: str, theme_name: str): |
| 235 | 225 | dest = os.path.join(output_dir, 'hugo.toml') |
| 226 | + safe_name = theme_name.replace('"', '') | |
| 227 | + title = safe_name.replace('-', ' ').title() | |
| 236 | 228 | with open(dest, 'w') as f: |
| 237 | 229 | f.write(f'''baseURL = "http://localhost:1313/" |
| 238 | 230 | languageCode = "en-us" |
| 239 | -title = "{theme_name.replace('-', ' ').title()}" | |
| 240 | -theme = "{theme_name}" | |
| 231 | +title = "{title}" | |
| 232 | +theme = "{safe_name}" | |
| 241 | 233 | ''') |
| 242 | 234 | logging.info("Wrote minimal hugo.toml") |
| 243 | 235 | |
| 244 | 236 | |
| 245 | 237 | def _pick_main_html(html_files: list) -> str: |
| 246 | 238 |
| --- 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 |