|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import yaml |
| 4 | +import os |
| 5 | + |
| 6 | +# Define file paths |
| 7 | +yaml_file_path = 'public/index.yaml' |
| 8 | +template_html_path = 'index.html' # Assuming this script is run from the repository root |
| 9 | +output_html_path = 'public/index.html' |
| 10 | +placeholder_comment = '<!-- Chart list will be populated here by the build script -->' |
| 11 | + |
| 12 | +def generate_chart_html(data): |
| 13 | + """Generates HTML list for charts from parsed YAML data.""" |
| 14 | + if not data or 'entries' not in data: |
| 15 | + print("No 'entries' found in YAML data or data is empty.") |
| 16 | + return "<p>No chart entries found in index.yaml.</p>" |
| 17 | + |
| 18 | + entries = data['entries'] |
| 19 | + if not entries: |
| 20 | + return "<p>No charts listed in 'entries'.</p>" |
| 21 | + |
| 22 | + html_parts = ['<ul class="chart-list">'] |
| 23 | + for chart_name, versions in sorted(entries.items()): |
| 24 | + html_parts.append(f' <li><h2>{chart_name}</h2>') |
| 25 | + if versions: |
| 26 | + html_parts.append(' <ul>') |
| 27 | + for version_details in sorted(versions, key=lambda x: x.get('version'), reverse=True): |
| 28 | + version = version_details.get('version', 'N/A') |
| 29 | + description = version_details.get('description', '') |
| 30 | + appVersion = version_details.get('appVersion', '') |
| 31 | + |
| 32 | + display_text = f"Version: {version}" |
| 33 | + if appVersion: |
| 34 | + display_text += f" (App: {appVersion})" |
| 35 | + if description: |
| 36 | + display_text += f" - <em>{description}</em>" |
| 37 | + html_parts.append(f' <li>{display_text}</li>') |
| 38 | + html_parts.append(' </ul>') |
| 39 | + else: |
| 40 | + html_parts.append(' <p>No versions listed for this chart.</p>') |
| 41 | + html_parts.append(' </li>') |
| 42 | + html_parts.append('</ul>') |
| 43 | + return '\n'.join(html_parts) |
| 44 | + |
| 45 | +def main(): |
| 46 | + print(f"Starting static HTML generation: {template_html_path} + {yaml_file_path} -> {output_html_path}") |
| 47 | + |
| 48 | + # 1. Read index.yaml |
| 49 | + try: |
| 50 | + with open(yaml_file_path, 'r', encoding='utf-8') as f: |
| 51 | + yaml_data = yaml.safe_load(f) |
| 52 | + except FileNotFoundError: |
| 53 | + print(f"Error: YAML file not found at {yaml_file_path}") |
| 54 | + # Create a minimal public/index.html indicating the issue |
| 55 | + try: |
| 56 | + with open(template_html_path, 'r', encoding='utf-8') as tpl_f: |
| 57 | + template_html = tpl_f.read() |
| 58 | + error_message_html = "<p><strong>Error: Chart index (index.yaml) not found. Cannot display charts.</strong></p>" |
| 59 | + output_html = template_html.replace(placeholder_comment, error_message_html) |
| 60 | + os.makedirs(os.path.dirname(output_html_path), exist_ok=True) |
| 61 | + with open(output_html_path, 'w', encoding='utf-8') as out_f: |
| 62 | + out_f.write(output_html) |
| 63 | + print(f"Wrote minimal HTML to {output_html_path} indicating missing YAML.") |
| 64 | + except Exception as e_tpl: |
| 65 | + print(f"Additionally, could not process template HTML: {e_tpl}") |
| 66 | + return # Exit if YAML is missing |
| 67 | + except yaml.YAMLError as e: |
| 68 | + print(f"Error parsing YAML file: {e}") |
| 69 | + # Similar error handling for YAML parse error |
| 70 | + try: |
| 71 | + with open(template_html_path, 'r', encoding='utf-8') as tpl_f: |
| 72 | + template_html = tpl_f.read() |
| 73 | + error_message_html = f"<p><strong>Error: Could not parse chart index (index.yaml). Details: {e}</strong></p>" |
| 74 | + output_html = template_html.replace(placeholder_comment, error_message_html) |
| 75 | + os.makedirs(os.path.dirname(output_html_path), exist_ok=True) |
| 76 | + with open(output_html_path, 'w', encoding='utf-8') as out_f: |
| 77 | + out_f.write(output_html) |
| 78 | + print(f"Wrote minimal HTML to {output_html_path} indicating YAML parsing error.") |
| 79 | + except Exception as e_tpl: |
| 80 | + print(f"Additionally, could not process template HTML: {e_tpl}") |
| 81 | + return # Exit on YAML parse error |
| 82 | + |
| 83 | + # 2. Generate HTML for charts |
| 84 | + print("Generating HTML content for charts...") |
| 85 | + charts_html = generate_chart_html(yaml_data) |
| 86 | + |
| 87 | + # 3. Read the template index.html |
| 88 | + try: |
| 89 | + with open(template_html_path, 'r', encoding='utf-8') as f: |
| 90 | + template_html = f.read() |
| 91 | + except FileNotFoundError: |
| 92 | + print(f"Error: Template HTML file not found at {template_html_path}") |
| 93 | + # If template is missing, we can't really proceed to create a useful public/index.html |
| 94 | + # However, the script's primary job is to process index.yaml, so this is a critical error. |
| 95 | + return |
| 96 | + |
| 97 | + # 4. Inject generated HTML into the template |
| 98 | + if placeholder_comment in template_html: |
| 99 | + output_html = template_html.replace(placeholder_comment, charts_html) |
| 100 | + print(f"Successfully injected chart HTML into template using placeholder.") |
| 101 | + else: |
| 102 | + # Fallback or error if placeholder is missing. |
| 103 | + # For now, let's try to append to a known div if it exists, or just log an error. |
| 104 | + container_div_end_tag = '</div>' # from <div id="chart-list-container"> |
| 105 | + container_div_full_tag = '<div id="chart-list-container">' |
| 106 | + |
| 107 | + # Try to find the specific container |
| 108 | + container_start_index = template_html.find(container_div_full_tag) |
| 109 | + if container_start_index != -1: |
| 110 | + # Find where this div ends |
| 111 | + end_tag_index = template_html.find(container_div_end_tag, container_start_index + len(container_div_full_tag)) |
| 112 | + if end_tag_index != -1: |
| 113 | + output_html = template_html[:end_tag_index] + charts_html + template_html[end_tag_index:] |
| 114 | + print("Warning: Placeholder comment not found. Appended chart HTML inside the 'chart-list-container' div.") |
| 115 | + else: # No end tag for the container div |
| 116 | + output_html = template_html + charts_html # Append at the end as a last resort |
| 117 | + print("Error: Placeholder comment not found and could not properly locate end of 'chart-list-container' div. Appended to end of file.") |
| 118 | + else: # Container div itself not found |
| 119 | + output_html = template_html + charts_html # Append at the end as a last resort |
| 120 | + print(f"Error: Placeholder comment '{placeholder_comment}' not found in template. Appended chart HTML to the end of the template as a fallback.") |
| 121 | + |
| 122 | + |
| 123 | + # 5. Write the output HTML |
| 124 | + try: |
| 125 | + os.makedirs(os.path.dirname(output_html_path), exist_ok=True) # Ensure 'public/' directory exists |
| 126 | + with open(output_html_path, 'w', encoding='utf-8') as f: |
| 127 | + f.write(output_html) |
| 128 | + print(f"Successfully generated and wrote static HTML to {output_html_path}") |
| 129 | + except IOError as e: |
| 130 | + print(f"Error writing output HTML file: {e}") |
| 131 | + |
| 132 | +if __name__ == '__main__': |
| 133 | + main() |
0 commit comments