|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Apply the EXACT structure from exemplar: ctx first, then DPI fix without redefining ctx. |
| 4 | +""" |
| 5 | + |
| 6 | +import re |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +def fix_dpi_correct(file_path): |
| 10 | + """Fix DPI with correct structure from exemplar.""" |
| 11 | + |
| 12 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 13 | + content = f.read() |
| 14 | + |
| 15 | + # The correct pattern from exemplar: |
| 16 | + # 1. const ctx = getContext |
| 17 | + # 2. medianValuesPlugin |
| 18 | + # 3. DPI fix (that uses ctx, doesn't redefine it) |
| 19 | + |
| 20 | + # Remove the broken DPI fix that redefines ctx |
| 21 | + content = re.sub( |
| 22 | + r'// Fix blurry canvas on high-DPI displays\s*const ctx = document\.getElementById.*?ctx\.scale\(dpr, dpr\);', |
| 23 | + '', |
| 24 | + content, |
| 25 | + flags=re.DOTALL |
| 26 | + ) |
| 27 | + |
| 28 | + # Now find where medianValuesPlugin ends (look for the closing };) |
| 29 | + # and insert the DPI fix RIGHT AFTER it |
| 30 | + |
| 31 | + dpi_fix_correct = ''' |
| 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 | + # Find the end of medianValuesPlugin and insert DPI fix after it |
| 42 | + # Pattern: closing of medianValuesPlugin followed by whitespace then either // Track hover or something else |
| 43 | + pattern = r'(const medianValuesPlugin = \{[^}]+afterDatasetsDraw:[^}]+\}\s*\};)\s*\n' |
| 44 | + |
| 45 | + replacement = r'\1' + dpi_fix_correct + '\n' |
| 46 | + |
| 47 | + content = re.sub(pattern, replacement, content, flags=re.DOTALL) |
| 48 | + |
| 49 | + # Write back |
| 50 | + with open(file_path, 'w', encoding='utf-8') as f: |
| 51 | + f.write(content) |
| 52 | + |
| 53 | + print(f"Updated: {file_path.name}") |
| 54 | + |
| 55 | + |
| 56 | +def main(): |
| 57 | + """Process all severity chart files.""" |
| 58 | + |
| 59 | + # Find all BAU and PM severity charts |
| 60 | + bau_charts = sorted(Path('.').glob('risk*_bau_chart.html')) |
| 61 | + pm_charts = sorted(Path('.').glob('risk*_pm_chart.html')) |
| 62 | + |
| 63 | + all_charts = bau_charts + pm_charts |
| 64 | + |
| 65 | + if not all_charts: |
| 66 | + print("No severity charts found!") |
| 67 | + return |
| 68 | + |
| 69 | + print(f"Found {len(all_charts)} severity charts") |
| 70 | + print(f"Applying correct DPI fix structure from exemplar...\n") |
| 71 | + |
| 72 | + for chart_file in all_charts: |
| 73 | + fix_dpi_correct(chart_file) |
| 74 | + |
| 75 | + print(f"\nCompleted! Updated {len(all_charts)} files.") |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + main() |
0 commit comments