#!/usr/bin/env python3 """ Script to create a self-contained HTML guide for AWS access key rotation. This script takes the template HTML file and embeds all referenced images as base64 data URLs. This makes the guide fully portable and shareable without external dependencies. """ import base64 import os import re from pathlib import Path def embed_images_in_html(html_path, output_path): """ Reads an HTML file, embeds all referenced images as base64 data URLs, and writes the result to a new file. """ # Read the HTML file with open(html_path, 'r', encoding='utf-8') as f: html_content = f.read() # Find all image references img_tags = re.findall(r']*src=["\']([^"\'>]+)["\'][^>]*>', html_content) # Get the directory containing the HTML file html_dir = os.path.dirname(html_path) # Replace each image reference with a base64 data URL for img_src in img_tags: # Get the full path to the image img_filename = os.path.basename(img_src) img_path = os.path.join(html_dir, 'images', img_filename) if os.path.exists(img_path): # Read the image file and encode it as base64 with open(img_path, 'rb') as img_file: img_data = img_file.read() img_base64 = base64.b64encode(img_data).decode('utf-8') # Determine the MIME type based on file extension mime_type = 'image/png' # Default to PNG if img_filename.lower().endswith('.jpg') or img_filename.lower().endswith('.jpeg'): mime_type = 'image/jpeg' elif img_filename.lower().endswith('.gif'): mime_type = 'image/gif' # Create the data URL data_url = f'data:{mime_type};base64,{img_base64}' # Replace the image reference with the data URL html_content = html_content.replace(f'src="{img_src}"', f'src="{data_url}"') # Write the modified HTML to the output file with open(output_path, 'w', encoding='utf-8') as f: f.write(html_content) print(f"Created self-contained guide: {output_path}") print("This file contains all images embedded as data URLs and can be shared directly.") def main(): """Main function to create the self-contained HTML guide.""" # Get the base directory of the project base_dir = Path(__file__).parent.parent # Path to the template HTML file input_html = base_dir / 'src' / 'templates' / 'aws_key_rotation_guide.html' # Path to the output self-contained HTML file output_html = base_dir / 'guide' / 'AWS_Access_Key_Rotation_Guide_Self_Contained.html' # Create the output directory if it doesn't exist output_html.parent.mkdir(parents=True, exist_ok=True) # Create the self-contained HTML file embed_images_in_html(input_html, output_html) if __name__ == "__main__": main()