#!/usr/bin/env python3
"""
Modify announcement banner for development preview deployment.
This script modifies the Quarto announcement banner in HTML files to:
1. Make it non-dismissible
2. Add a prominent "DEVELOPMENT PREVIEW" banner at the top
3. Style it with appropriate warning colors
Used by the deploy-preview GitHub Actions workflow.
"""
import os
import sys
import re
from pathlib import Path
from typing import List
import argparse
from datetime import datetime
from zoneinfo import ZoneInfo
def find_html_files_with_announcement(directory: Path) -> List[Path]:
"""Find all HTML files that contain quarto-announcement."""
html_files = []
for html_file in directory.rglob("*.html"):
try:
with open(html_file, 'r', encoding='utf-8') as f:
content = f.read()
if 'quarto-announcement' in content:
html_files.append(html_file)
except Exception as e:
print(f"⚠️ Warning: Could not read {html_file}: {e}")
return html_files
def modify_announcement_banner(file_path: Path, commit_hash: str = None, commit_short: str = None) -> bool:
"""
Modify the announcement banner in an HTML file for development preview.
Returns True if modifications were made, False otherwise.
"""
try:
# Read the file
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
original_content = content
# 1. Make announcement non-dismissible by hiding the close button
content = re.sub(
r']*>.*?',
'',
content,
flags=re.DOTALL
)
# 2. Remove dismissible functionality
content = re.sub(r'data-bs-dismiss="alert"', '', content)
# 3. Add development preview text at the beginning of the existing content
# Get current Eastern Time (EST/EDT)
try:
# Use timezone-aware datetime with Eastern Time
eastern = ZoneInfo("America/New_York")
eastern_now = datetime.now(eastern)
timestamp = eastern_now.strftime("%Y-%m-%d %H:%M %Z")
except Exception:
# Fallback to UTC if timezone fails
utc_now = datetime.utcnow()
timestamp = utc_now.strftime("%Y-%m-%d %H:%M UTC")
commit_info = ""
if commit_hash and commit_short:
commit_info = f''' Built from dev@{commit_short} • {timestamp}'''
elif commit_short:
commit_info = f''' Built from commit {commit_short} • {timestamp}'''
else:
commit_info = f''' • {timestamp}'''
dev_text = f'''
🚧 DEVELOPMENT PREVIEW - Built from dev@{commit_short or "unknown"} • {timestamp} • Stable version →