import os import json import markdown from wand.image import Image import subprocess from datetime import datetime # Directory paths garden_path = 'website/contents/garden/' misc_path = 'website/contents/misc/' compressed_path = os.path.join(garden_path, 'compressed') # Ensure the compressed folder exists os.makedirs(compressed_path, exist_ok=True) # Load configuration settings from config.json with open('config.json', 'r', encoding='utf-8') as config_file: config = json.load(config_file) website_title = config['name_website'] website_description = config['description'] background_color = config['background_color'] text_color = config['text_color'] background_color_header_footer = config['background_color_header_footer'] text_color_header_footer = config['text_color_header_footer'] # Helper function to read file content def read_file(file_path): with open(file_path, 'r', encoding='utf-8') as file: return file.read() # Function to compress images using ImageMagick def compress_image(input_path, output_path): with Image(filename=input_path) as img: img.compression_quality = 50 # Adjust quality as needed # img.dither() img.save(filename=output_path) # Function to compress videos using ffmpeg def compress_video(input_path, output_path): subprocess.run(['ffmpeg', '-i', input_path, '-vcodec', 'libx264', '-crf', '28', output_path]) # Function to generate HTML content def generate_html(): html_content = "" # Read header content header_content = read_file(os.path.join(misc_path, 'header.md')) header_html = markdown.markdown(header_content) html_content += f"""
{header_html}
""" # Get the files in the garden directory sorted by modification time files = sorted( os.listdir(garden_path), key=lambda x: os.path.getmtime(os.path.join(garden_path, x)) ) # Keep track of compressed files to manage deletions compressed_files_to_keep = set() # Process each file in the garden directory for filename in files: file_path = os.path.join(garden_path, filename) file_extension = filename.split('.')[-1].lower() if file_extension == 'txt': text_content = read_file(file_path) html_content += f"
{text_content}
" elif file_extension == 'md': md_content = read_file(file_path) md_html = markdown.markdown(md_content) html_content += f"
{md_html}
" elif file_extension == 'pdf': html_content += f"
" elif file_extension in ['png', 'jpg', 'jpeg', 'gif']: compressed_image_path = os.path.join(compressed_path, f"compressed_{filename}") compress_image(file_path, compressed_image_path) html_content += f"
" compressed_files_to_keep.add(compressed_image_path) elif file_extension in ['mp4', 'avi', 'mov', 'mkv']: compressed_video_path = os.path.join(compressed_path, f"compressed_{filename}") compress_video(file_path, compressed_video_path) html_content += f"
" compressed_files_to_keep.add(compressed_video_path) # Remove compressed files that no longer have a source file in the garden directory for compressed_file in os.listdir(compressed_path): compressed_file_path = os.path.join(compressed_path, compressed_file) if compressed_file_path not in compressed_files_to_keep: os.remove(compressed_file_path) # Read footer content footer_content = read_file(os.path.join(misc_path, 'footer.md')) footer_html = markdown.markdown(footer_content) html_content += f"" return html_content # Generate the final HTML final_html = f""" {website_title} {generate_html()} """ # Save the final HTML to a file with open('website/index.html', 'w', encoding='utf-8') as file: file.write(final_html) print("Website generated successfully!")