Repository navigation
Expand file tree
/
Copy pathinject_analysis.py
More file actions
63 lines (49 loc) · 2.17 KB
/
Copy pathinject_analysis.py
File metadata and controls
63 lines (49 loc) · 2.17 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
inject_analysis.py
Injects AI-generated HTML analysis into a Banana Persona/Audience HTML report.
Usage:
python inject_analysis.py <report.html> <analysis.html>
python inject_analysis.py <report.html> --stdin (reads from stdin)
"""
import sys
import io
import argparse
from pathlib import Path
if sys.platform == 'win32':
sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8', errors='replace')
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
PLACEHOLDER_START = '<div class="ai-placeholder"'
PLACEHOLDER_END = '<!-- /AI_ANALYSIS_PLACEHOLDER -->'
def inject(report_path: str, analysis_html: str) -> str:
report = Path(report_path)
html = report.read_text(encoding='utf-8')
# Find and replace the placeholder region
start_idx = html.find(PLACEHOLDER_START)
end_idx = html.find(PLACEHOLDER_END)
if start_idx == -1 or end_idx == -1:
print("[inject] WARNING: placeholder not found, appending before </div></body>", file=sys.stderr)
html = html.replace('</div>\n</body>', f'{analysis_html}\n</div>\n</body>')
else:
# Replace everything from placeholder start to end marker
html = html[:start_idx] + analysis_html + '\n' + html[end_idx:]
report.write_text(html, encoding='utf-8')
print(f"[inject] ✅ Analysis injected into {report_path}", file=sys.stderr)
return str(report)
def main():
parser = argparse.ArgumentParser(description='Inject AI analysis into Banana report')
parser.add_argument('report', help='Path to the HTML report file')
parser.add_argument('analysis', nargs='?', default=None, help='Path to HTML analysis file (or --stdin)')
parser.add_argument('--stdin', action='store_true', help='Read analysis HTML from stdin')
args = parser.parse_args()
if args.stdin:
analysis_html = sys.stdin.read()
elif args.analysis:
analysis_html = Path(args.analysis).read_text(encoding='utf-8')
else:
print("Error: provide analysis file path or --stdin", file=sys.stderr)
sys.exit(1)
inject(args.report, analysis_html)
if __name__ == '__main__':
main()