You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
5.2 KiB
Python

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>{header_html}</header>"""
# 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"<div class='draggable text'>{text_content}</div>"
elif file_extension == 'md':
md_content = read_file(file_path)
md_html = markdown.markdown(md_content)
html_content += f"<div class='draggable text'>{md_html}</div>"
elif file_extension == 'pdf':
html_content += f"<div class='draggable iframe'><iframe src='./contents/garden/{filename}'></iframe></div>"
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"<div class='draggable'><img src='./contents/garden/compressed/compressed_{filename}'></div>"
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"<div class='draggable video'><video controls src='./contents/garden/compressed/compressed_{filename}'></video></div>"
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"<footer>{footer_html}</footer>"
return html_content
# Generate the final HTML
final_html = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{website_title}</title>
<link rel="stylesheet" href="style.css">
<meta name="description" content="{website_description}">
<style>
body {{
background-color: {background_color};
color: {text_color};
}}
.text{{
border: solid {text_color};
}}
a{{color: {text_color};}}
footer a, header a{{color: {text_color_header_footer};}}
header, footer{{
background-color: {background_color_header_footer};
color: {text_color_header_footer};
}}
.draggable {{
position: absolute;
cursor: move;
}}
</style>
</head>
<body>
{generate_html()}
<script src="scripts/script.js"></script>
</body>
</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!")