2024-11-03 11:42:44 +01:00
|
|
|
#!/bin/python3
|
2024-11-03 10:29:30 +01:00
|
|
|
import os
|
2024-11-03 11:42:44 +01:00
|
|
|
import argparse
|
2024-11-03 10:29:30 +01:00
|
|
|
|
2024-11-04 00:13:58 +01:00
|
|
|
|
2024-11-03 11:42:44 +01:00
|
|
|
parser = argparse.ArgumentParser(description="Générer un site Web à partir de fichiers HTML.")
|
|
|
|
parser.add_argument("html_dir", help="Le chemin vers le dossier contenant les fichiers HTML.")
|
|
|
|
parser.add_argument("--title", "-t", default="Mon site Web", help="Le titre du site Web.")
|
2024-11-08 18:16:36 +01:00
|
|
|
parser.add_argument("--style", default="templates/style_general.css", help="Le chemin vers le fichier de style CSS.")
|
2024-11-03 11:42:44 +01:00
|
|
|
args = parser.parse_args()
|
2024-11-03 10:29:30 +01:00
|
|
|
|
2024-11-04 00:13:58 +01:00
|
|
|
# Style CSS minimaliste
|
2024-11-03 11:42:44 +01:00
|
|
|
style_file = args.style
|
|
|
|
css_content = ""
|
2024-11-08 18:05:08 +01:00
|
|
|
|
2024-11-08 23:19:39 +01:00
|
|
|
inline_the_css=False
|
|
|
|
# inline_the_css=True
|
2024-11-08 18:05:08 +01:00
|
|
|
|
2024-11-03 10:29:30 +01:00
|
|
|
|
2024-11-03 11:42:44 +01:00
|
|
|
html_dir = args.html_dir
|
|
|
|
|
2024-11-08 18:05:08 +01:00
|
|
|
|
2024-11-08 23:19:39 +01:00
|
|
|
def enrich_one_file(file, root_path):
|
|
|
|
print('enrich html file:',os.path.join(root_path, file))
|
2024-11-08 18:05:08 +01:00
|
|
|
# Ouvrir le fichier HTML en mode lecture
|
2024-11-08 23:19:39 +01:00
|
|
|
with open(os.path.join(root_path, file), "r") as f:
|
2024-11-08 18:05:08 +01:00
|
|
|
html_content = f.read()
|
|
|
|
|
2024-11-08 23:19:39 +01:00
|
|
|
if inline_the_css:
|
|
|
|
print('include css inline in each html page')
|
|
|
|
with open(os.path.join(root_path, file), "r") as f:
|
|
|
|
css_content = f.read()
|
|
|
|
css_content = "<style type='text/css'>{css_content}</style>"
|
|
|
|
|
2024-11-08 18:05:08 +01:00
|
|
|
# Ajouter la déclaration de charset UTF-8, le doctype HTML et le titre du site Web
|
|
|
|
html_content = f"<!DOCTYPE html>\n<html lang=\"fr\">\n<head>\n<meta charset=\"UTF-8\">\n<title>{args.title}</title>\n{css_content}<link type='stylesheet/css' href='/style.css'></link></head>\n<body>\n<a href='/'>Retour à l'Accueil</a><hr/>{html_content}\n<footer><hr/><a href='/'>Retour à l'Accueil</a></footer></body>\n</html>"
|
|
|
|
|
|
|
|
# Écrire le contenu modifié dans le fichier HTML
|
2024-11-08 23:19:39 +01:00
|
|
|
with open(os.path.join(root_path, file), "w") as f:
|
2024-11-08 18:05:08 +01:00
|
|
|
f.write(html_content)
|
2024-11-03 10:29:30 +01:00
|
|
|
# Parcourir tous les fichiers HTML dans le dossier
|
|
|
|
for root, _, files in os.walk(html_dir):
|
2024-11-08 18:05:08 +01:00
|
|
|
print(files)
|
2024-11-03 10:29:30 +01:00
|
|
|
for file in files:
|
|
|
|
if file.endswith(".html"):
|
2024-11-08 23:19:39 +01:00
|
|
|
enrich_one_file(file, root)
|