#!/bin/python3
import argparse
import os
from utils import *
from website_config import configs_sites
parser = argparse.ArgumentParser(description="Générer un site Web à partir de fichiers HTML.")
parser.add_argument("blog_name", help="Le chemin vers le dossier contenant les fichiers HTML.")
parser.add_argument("--style", default="templates/style_general.css", help="Le chemin vers le fichier de style CSS.")
args = parser.parse_args()
# Style CSS minimaliste
style_file = args.style
blog_name = args.blog_name.replace('html-websites/', '')
html_pages = 'html-websites/'+blog_name
source_blog = f"sources/{blog_name}"
header_content_path = f"{source_blog}/templates/converted/header_page.html"
footer_content_path = f"{source_blog}/templates/converted/footer_page.html"
static_page_path = f"{source_blog}/templates/html/static.html"
enable_header=True
# print('---------- blog name ', blog_name)
template_content = configs_sites[blog_name]
after_article = ''
inline_the_css = False
# inline_the_css=True
def enrich_one_file(html_content, partials={"header_page": "", "footer_content": ""}):
# remove some parts
html_content = remove_properties_section(html_content)
html_content = remove_article_head_properties_orgmode(html_content)
html_content = remove_hint_html(html_content)
html_content = extract_body_content(html_content)
if inline_the_css is True:
print(' ----------- enrich_html: include css inline in each html page')
with open(os.path.join(root_path, file), "r") as f:
css_content = f.read()
css_content = ""
template_content["CSS_INLINE_CONTENT"] = css_content
template_content["PAGE_SLUG"] = "la_page"
# remplir le template
html_content = f"""
{template_content['TITLE']}
{template_content['BLOG_TITLE']}
{template_content['BLOG_SUBTITLE']}
{html_content}
{after_article}
"""
# {partials['footer_page']}
# {partials['header_page']}
return html_content
def ouvrir_fichier(chemin_fichier):
if os.path.exists(chemin_fichier):
with open(chemin_fichier, 'r') as fichier:
# Faire quelque chose avec le fichier ouvert
contenu = fichier.read()
return contenu
else:
raise FileNotFoundError(f"Le fichier {chemin_fichier} n'existe pas.")
liste_fichiers_du_blog_convertis = os.walk(html_pages)
print('fichiers à enrichir:', liste_fichiers_du_blog_convertis)
# Parcourir tous les fichiers HTML dans le dossier du blog donné
for root_path, dirs, files in liste_fichiers_du_blog_convertis :
# Prendre les templates partiaux pour chaque site web
partials = {
"header_content": "",
"footer_content": "",
}
print(len(files))
partials["header_content"] = ouvrir_fichier(os.path.join('sources',blog_name, 'templates', 'header_page.org'))
partials["footer_content"] = ouvrir_fichier(os.path.join('sources',blog_name, 'templates', 'footer_page.org'))
for file in files:
print(file)
if file is "index.html":
template_content['no_header']
if file.endswith(".html"):
print(' ----------- enrich_html: file:', os.path.join(root_path, file))
print(' ----------- enrich_html: CSS inline: ', inline_the_css)
# Ouvrir le fichier HTML en mode lecture
with open(os.path.join(root_path, file), "r") as f:
html_content = f.read()
html_content = enrich_one_file(html_content, partials)
html_path_enriched = os.path.join(root_path, file)
print(' ----------- enrich_html: html_path_enriched ============> ', html_path_enriched)
# Écrire le contenu modifié dans le fichier HTML
with open(html_path_enriched, "w") as f:
f.write(html_content)
print('\n ----------- enrich_html: html écrit ', html_path_enriched)