|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Final fix: Add ctx definition exactly where it should be, matching exemplar structure. |
| 4 | +""" |
| 5 | + |
| 6 | +import re |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +def fix_blank_charts(file_path): |
| 10 | + """Add ctx and DPI fix in exact exemplar structure.""" |
| 11 | + |
| 12 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 13 | + content = f.read() |
| 14 | + |
| 15 | + # Find: const canvas = document.getElementById('severityChart'); |
| 16 | + # followed by blank lines, and replace with the correct structure |
| 17 | + |
| 18 | + # Pattern to find the canvas line followed by whitespace |
| 19 | + pattern = r"(// Create chart with custom plugin for colored squares\s*const canvas = document\.getElementById\('severityChart'\);)\s*\n\s*\n\s*\n\s*(let hoveredExpertIndex)" |
| 20 | + |
| 21 | + # Replacement with ctx definition |
| 22 | + replacement = r'''\1 |
| 23 | + const ctx = document.getElementById('severityChart').getContext('2d'); |
| 24 | +
|
| 25 | + \2''' |
| 26 | + |
| 27 | + content = re.sub(pattern, replacement, content) |
| 28 | + |
| 29 | + # Now add the DPI fix after medianValuesPlugin closes |
| 30 | + # Find the closing of medianValuesPlugin |
| 31 | + dpi_fix = ''' |
| 32 | + // Fix blurry canvas on high-DPI displays |
| 33 | + const dpr = window.devicePixelRatio || 1; |
| 34 | + const canvas = document.getElementById('severityChart'); |
| 35 | + const rect = canvas.getBoundingClientRect(); |
| 36 | + canvas.width = rect.width * dpr; |
| 37 | + canvas.height = rect.height * dpr; |
| 38 | + ctx.scale(dpr, dpr); |
| 39 | +''' |
| 40 | + |
| 41 | + # Pattern: end of medianValuesPlugin, before any // Track hover or chartCanvas references |
| 42 | + # Look for the plugin closing followed by whitespace |
| 43 | + plugin_pattern = r'(const medianValuesPlugin = \{[^}]*afterDatasetsDraw: \(chart\) => \{(?:[^{}]|\{[^{}]*\})*\}\s*\};)\s*\n(\s*const chartCanvas|// Track|let hoveredExpertIndex)' |
| 44 | + |
| 45 | + # Check if DPI fix is already there |
| 46 | + if '// Fix blurry canvas on high-DPI displays' not in content: |
| 47 | + # Insert after the plugin |
| 48 | + plugin_replacement = r'\1' + dpi_fix + '\n\2' |
| 49 | + content = re.sub(plugin_pattern, plugin_replacement, content, flags=re.DOTALL) |
| 50 | + |
| 51 | + # Write back |
| 52 | + with open(file_path, 'w', encoding='utf-8') as f: |
| 53 | + f.write(content) |
| 54 | + |
| 55 | + print(f"Updated: {file_path.name}") |
| 56 | + |
| 57 | + |
| 58 | +def main(): |
| 59 | + """Process all severity chart files.""" |
| 60 | + |
| 61 | + # Find all BAU and PM severity charts |
| 62 | + bau_charts = sorted(Path('.').glob('risk*_bau_chart.html')) |
| 63 | + pm_charts = sorted(Path('.').glob('risk*_pm_chart.html')) |
| 64 | + |
| 65 | + all_charts = bau_charts + pm_charts |
| 66 | + |
| 67 | + if not all_charts: |
| 68 | + print("No severity charts found!") |
| 69 | + return |
| 70 | + |
| 71 | + print(f"Found {len(all_charts)} severity charts") |
| 72 | + print(f"Adding ctx definition and DPI fix...\n") |
| 73 | + |
| 74 | + for chart_file in all_charts: |
| 75 | + fix_blank_charts(chart_file) |
| 76 | + |
| 77 | + print(f"\nCompleted! Updated {len(all_charts)} files.") |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == '__main__': |
| 81 | + main() |
0 commit comments