#!/usr/bin/env python3 import os import re import sys from pathlib import Path # DIR="/Volumes/web/files/books/Nancy-McWilliams/psychoanalytic-diagnosis/text" DIR="./text" meta_tag=r'' existing_tag=r'' def insert_viewport_meta(file_path:str): """Insert the viewport meta tag if it does not exist.""" try: with open(file_path, 'r', encoding='utf-8') as file: content = file.read() print(f"Read {len(content) / 1024:.2f} kb from {file_path}") if meta_tag in content: print(f"Tag already exists in {file_path}") return modified_content = re.sub( existing_tag, f"{existing_tag}\n {meta_tag}\n", content ) with open(file_path, 'w', encoding='utf-8') as file: file.write(modified_content) print(f"Inserted the {meta_tag} in {file_path}") except Exception as e: print(f"Error processing {file_path}: {e}", file=sys.stderr) return if __name__ == "__main__": html_files = list(Path(DIR).glob("**/*.html")) print(f"Found {len(html_files)} html files in {DIR}") for file in html_files: print(f"------------\nTo process: {file}") insert_viewport_meta(file)