|
1
|
""" |
|
2
|
Analyzes a Hugo theme or raw HTML theme and reports structure + recommendations. |
|
3
|
""" |
|
4
|
|
|
5
|
import logging |
|
6
|
import os |
|
7
|
|
|
8
|
from ..config import call_ai |
|
9
|
from .theme_finder import find_hugo_theme, find_raw_html_files |
|
10
|
|
|
11
|
SYSTEM = "You are an expert Hugo theme developer analyzing themes for conversion." |
|
12
|
|
|
13
|
|
|
14
|
def analyze(path: str) -> str: |
|
15
|
logging.info(f"Analyzing {path} ...") |
|
16
|
|
|
17
|
info = find_hugo_theme(path) |
|
18
|
|
|
19
|
if info: |
|
20
|
return _analyze_hugo_theme(info) |
|
21
|
else: |
|
22
|
return _analyze_raw_html(path) |
|
23
|
|
|
24
|
|
|
25
|
def _analyze_hugo_theme(info: dict) -> str: |
|
26
|
theme_dir = info['theme_dir'] |
|
27
|
theme_name = info['theme_name'] |
|
28
|
example_site = info['example_site'] |
|
29
|
|
|
30
|
# Collect layout files |
|
31
|
layouts = [] |
|
32
|
for root, dirs, files in os.walk(os.path.join(theme_dir, 'layouts')): |
|
33
|
for f in files: |
|
34
|
if f.endswith('.html'): |
|
35
|
rel = os.path.relpath(os.path.join(root, f), theme_dir) |
|
36
|
layouts.append(rel) |
|
37
|
|
|
38
|
# Collect content types from exampleSite |
|
39
|
content_types = [] |
|
40
|
if example_site: |
|
41
|
content_dir = os.path.join(example_site, 'content') |
|
42
|
if os.path.isdir(content_dir): |
|
43
|
content_types = [ |
|
44
|
d for d in os.listdir(content_dir) |
|
45
|
if os.path.isdir(os.path.join(content_dir, d)) |
|
46
|
] |
|
47
|
|
|
48
|
report = [ |
|
49
|
f"Theme: {theme_name}", |
|
50
|
f"Theme dir: {theme_dir}", |
|
51
|
f"Layouts ({len(layouts)}):", |
|
52
|
*[f" {layout}" for layout in sorted(layouts)], |
|
53
|
f"Content types: {content_types}", |
|
54
|
f"ExampleSite: {example_site or 'none'}", |
|
55
|
"", |
|
56
|
"Status: Already a Hugo theme. Use 'complete' to assemble a working site.", |
|
57
|
] |
|
58
|
return "\n".join(report) |
|
59
|
|
|
60
|
|
|
61
|
def _analyze_raw_html(path: str) -> str: |
|
62
|
html_files = find_raw_html_files(path) |
|
63
|
if not html_files: |
|
64
|
return f"No HTML files found at {path}" |
|
65
|
|
|
66
|
# Read main HTML file for AI analysis |
|
67
|
main = next( |
|
68
|
(f for f in html_files if os.path.basename(f).lower() == 'index.html'), |
|
69
|
html_files[0], |
|
70
|
) |
|
71
|
with open(main, 'r', errors='replace') as f: |
|
72
|
html = f.read()[:20000] |
|
73
|
|
|
74
|
prompt = f"""Analyze this HTML theme file and provide: |
|
75
|
1. Identified reusable components (header, footer, nav, sidebar, etc.) |
|
76
|
2. Recommended Hugo template tags for dynamic content |
|
77
|
3. Suggested partial splits |
|
78
|
4. Content sections that should be data/ YAML files for Decap CMS |
|
79
|
|
|
80
|
HTML: |
|
81
|
{html}""" |
|
82
|
|
|
83
|
try: |
|
84
|
return call_ai(prompt, SYSTEM) |
|
85
|
except Exception as e: |
|
86
|
logging.error(f"AI analysis failed: {e}") |
|
87
|
return f"HTML theme with {len(html_files)} files. AI analysis failed: {e}" |
|
88
|
|