templating gmi

This commit is contained in:
Tykayn 2025-02-28 00:26:19 +01:00 committed by tykayn
parent 9569dd56b4
commit 6767f8fe50
3 changed files with 176 additions and 130 deletions

View file

@ -26,8 +26,8 @@ parser.add_argument('--run_pandoc', type=str, help='Activer ou non la générati
parser.add_argument('--enable_roam_id_rewrite', type=str, help='Activer ou non la réécriture des liens roam', default=False)
parser.add_argument('--generate_html_pages', type=str, help='Activer ou non la génération des pages html', default=True)
parser.add_argument('--generate_linkings_json', type=str, help='Activer ou non la génération du json des liens entre articles', default=True)
parser.add_argument('--force_html_regen', action='store_true', help='Forcer la régénération des fichiers HTML même s\'ils existent déjà')
parser.add_argument('--rebuild_articles_info_json', type=str, help='Chemin du fichier JSON des articles', default=False)
parser.add_argument('--force_html_regen', action='store_true', help='Forcer la régénération des fichiers HTML même s\'ils existent déjà', default=False)
parser.add_argument('--rebuild_articles_info_json', type=str, help='Chemin du fichier JSON des articles', default=True)
args = parser.parse_args()
run_gemini = args.run_gemini
@ -53,7 +53,7 @@ directories_to_scan = [directory_pages]
destination_json = f'sources/{args.blog}/build'
destination_html = f'html-websites/{args.blog}/'
destination_gmi = f'gemini-capsules/{args.blog}/'
json_file = destination_json + '/articles_info.json'
# Si rebuild_articles_info_json est True, supprimer le fichier JSON existant
if rebuild_articles_info_json:
print(f"Suppression du fichier JSON existant: {json_file}")
@ -66,10 +66,10 @@ if rebuild_articles_info_json:
# Dictionnaire pour stocker les informations des fichiers
# Vérifier si le fichier JSON existe déjà
json_file = destination_json + '/articles_info.json'
files_dict = {}
if os.path.exists(json_file):
# ----- print(f"Chargement du fichier JSON existant: {json_file}")
#print(f"Chargement du fichier JSON existant: {json_file}")
try:
with open(json_file, 'r', encoding='utf-8') as f:
files_dict = json.load(f)
@ -93,9 +93,9 @@ lang_folder = global_config.get('lang_default', 'fr')
if generate_linkings_json :
# ----- print(f"Génération des liens entre articles pour {count_articles} articles")
# ----- print(f"run_pandoc: {run_pandoc}")
# ----- print(f"run_gemini: {run_gemini}")
#print(f"Génération des liens entre articles pour {count_articles} articles")
#print(f"run_pandoc: {run_pandoc}")
#print(f"run_gemini: {run_gemini}")
article_type = "article"
# Parcourir les fichiers du dossier
@ -117,28 +117,39 @@ if generate_linkings_json :
print(f"directory: {subdir}, {article_type}, {file_name}, {index}/{len(os.listdir(f'{directory}/{subdir}'))}")
# Vérifier si le fichier se termine par une extension supportée
if not (file_name.endswith('.org') or file_name.endswith('.md') or file_name.endswith('.gmi')):
print(f"Fichier {file_name} non supporté")
continue
if file_name.endswith('.org'):
counter+=1
print(f"Traitement de l'article {counter}/{count_articles} {file_name}")
# print(f"Traitement de l'article {counter}/{count_articles} {file_name}")
file_path = os.path.join(directory, subdir, file_name)
if force_html_regen and counter % 10 == 0:
print(f"{time.strftime('%H:%M:%S')} : Articles traités : {counter}/{count_articles}")
# on ouvre chacun des fichiers interprétables
# pour déterminer les informations qu'il contient
# afin de les stocker dans un json pour la génération des pages html et gemini
with open(file_path, "r", encoding="utf-8") as f:
print(f"----- Traitement de l'article {counter}/{count_articles} {file_name}")
content = f.read()
# Convertir le contenu Org en HTML
title = find_first_level1_title(content)
date_modified = time.ctime(os.path.getmtime(file_path))
rebuild_this_article_gemini = False
rebuild_this_article_html = False
basename = get_basename(file_name)
date_str, annee, slug = find_year_and_slug_on_filename(basename)
slug = slugify_title(title)
tags = extract_tags_from_file(file_path, global_config['excluded_tags'])
# Convertir les tags en liste si c'est un set
if isinstance(tags, set):
tags = list(tags)
boom = basename.split('__')
# Convertir le contenu Org en HTML
title = find_first_level1_title(content)
# Désactiver les warning d'identifiant dupliqué dans la conversion pandoc
content_without_h1 = re.sub(r'^\*.*?$', '', content, count=1, flags=re.MULTILINE)
@ -148,21 +159,20 @@ if generate_linkings_json :
html_content_without_h1 = ''
# Vérifier l'existence du fichier HTML pour déterminer last_html_build
html_path = f"html-websites/{args.blog}/{annee}/{slug}/index.html"
print(f"html_path existe il? : {html_path}")
last_html_build_time = None
if os.path.exists(html_path):
# Obtenir la date de création du fichier HTML
last_html_build_time = os.path.getctime(html_path)
# # ----- print(f"last_html_build: {last_html_build_time} : {html_path}")
print(f"----- last_html_build EXISTE: {last_html_build_time} : {html_path}")
else:
# ----- print(f"----------- last_html_build html_path: {html_path} n'existe pas")
print(f"html_path n'existe pas: on va le créer")
#print(f"----------- last_html_build html_path: {html_path} n'existe pas")
# Vérifier l'existence du fichier Gemini pour déterminer last_gemini_build
os.makedirs(os.path.dirname(f"./html_websites/{args.blog}/{annee}"), exist_ok=True)
os.makedirs(os.path.dirname(f"./gemini-capsules/{args.blog}/{annee}"), exist_ok=True)
gemini_path = f"./gemini-capsules/{args.blog}/{annee}/{slug}.gmi"
last_gemini_build = None
rebuild_this_article_gemini = False
if os.path.exists(gemini_path):
last_gemini_build = time.ctime(os.path.getmtime(gemini_path))
# Vérifier si l'article doit être reconstruit en comparant les dates de modification
@ -171,22 +181,26 @@ if generate_linkings_json :
last_build_time = time.mktime(time.strptime(last_gemini_build))
rebuild_this_article_gemini = file_modified_time > last_build_time
else:
rebuild_this_article_gemini = True
print(f"rebuild_this_article_gemini: {rebuild_this_article_gemini}")
# print(f"rebuild_this_article_gemini: {rebuild_this_article_gemini}")
# Vérifier si l'article doit être reconstruit en comparant les dates de modification
rebuild_this_article_html = False
if last_html_build_time:
file_modified_time = os.path.getmtime(file_path)
# # ----- print(f"--------- file_modified_time: {file_path} : {file_modified_time}")
print(f"--------- file_modified_time: {file_path} : {file_modified_time}")
print(f"--------- last_html_build_time: {last_html_build_time}")
# Obtenir l'heure de dernière modification du fichier HTML
rebuild_this_article_html = file_modified_time > last_html_build_time
# # ----- print(f"--------- article modifié après le build de son rendu html: {file_path}, {rebuild_this_article_html}")
if rebuild_this_article_html:
print(f"--------- article modifié après le build de son rendu html: {file_path}, {rebuild_this_article_html}")
else:
print(f"--------- article non modifié après le build de son rendu html: {file_path}, {rebuild_this_article_html}, on ne recrée pas")
rebuild_this_article_html = False
else:
# si il n'y a pas de fichier html, on le construit pour la première fois
# ----- print('on reconstruit le html de l\'article', file_name)
print('on reconstruit le html de l\'article', file_name)
rebuild_this_article_html = True
if rebuild_this_article_html:
@ -195,7 +209,7 @@ if generate_linkings_json :
# Garder le contenu HTML existant si déjà présent
if f"{annee}/{slug}" in files_dict and 'html_content' in files_dict[f"{annee}/{slug}"]:
# ----- print('on reprend le contenu html existant')
#print('on reprend le contenu html existant')
if len(files_dict[f"{annee}/{slug}"]['html_content']) > 0:
html_content = files_dict[f"{annee}/{slug}"]['html_content']
if len(files_dict[f"{annee}/{slug}"]['html_content_without_h1']) > 0:
@ -207,8 +221,8 @@ if generate_linkings_json :
if run_pandoc and rebuild_this_article_html or force_html_regen:
# convertir le contenu d'article org vers html
# # ----- print(f"\033[91mBRRRRRRRRRRRRR pandoc time {time.strftime('%H:%M:%S')} : Conversion de {file_name} en html\033[0m")
# ----- print(f"\033[91m.\033[0m", end='', flush=True)
print(f"\033[91m {time.strftime('%H:%M:%S')} BRRRRRRRRRRRRR pandoc : {title} en html\033[0m")
# print(f"\033[91m.\033[0m", end='', flush=True)
html_content = pypandoc.convert_text(content_without_h1, 'html', format='org')
pandoc_runs_counter += 1
@ -216,13 +230,14 @@ if generate_linkings_json :
html_content = content_without_h1
# if run_gemini and rebuild_this_article_gemini:
# # ----- print('-----------on régénère le gemini')
# #print('-----------on régénère le gemini')
# # convertir le contenu d'article org vers gmi pour la capsule gemini
# gemini_content = org_to_gmi(content_without_h1)
# # ----- print('len(gemini_content)', len(gemini_content))
# #print('len(gemini_content)', len(gemini_content))
# else:
# print('-----------on ne régénère pas le gemini')
if rebuild_this_article_gemini:
print(f"\033[91m {time.strftime('%H:%M:%S')} BRRRRRRRRRRRRR gemini : {title} en gmi\033[0m")
gemini_content = org_to_gmi(content_without_h1)
files_dict[f"{annee}/{slug}"] = {
@ -252,14 +267,14 @@ if generate_linkings_json :
# Contenu gemini
}
# ----- print(f"======= Nombre d'articles reconstruits: {rebuild_counter}")
# ----- print(f"======= Nombre de runs de pandoc: {pandoc_runs_counter}")
print(f"======= Nombre d'articles reconstruits: {rebuild_counter}")
print(f"======= Nombre de runs de pandoc: {pandoc_runs_counter}")
else:
print(f"Pas de génération des liens entre articles")
# Trier les basenames par ordre décroissant
sorted_basenames = sorted(files_dict.keys(), reverse=True)
# ----- print(len(sorted_basenames), 'articles trouvés')
#print(len(sorted_basenames), 'articles trouvés')
template_content = get_blog_template_conf(args.blog)
@ -273,7 +288,7 @@ if enable_roam_id_rewrite:
rewritten_html_content = html_content
for roam_id, slug in articles_roam_id_to_slugs.items():
if roam_id is not None and isinstance(rewritten_html_content, str) and roam_id in rewritten_html_content:
# ----- print(f'{roam_id} -> {slug}')
#print(f'{roam_id} -> {slug}')
rewritten_html_content = rewritten_html_content.replace(f'href="#{roam_id}"', f'href="{template_content["NDD"]}/{slug}"')
info['rewritten_roam_links_html'] = rewritten_html_content
@ -294,15 +309,15 @@ os.makedirs(destination_json, exist_ok=True)
# sauver le json de tous les articles et pages
if pandoc_runs_counter > 0 or not os.path.exists(json_file):
# ----- print(f"\033[91m Les articles ont changé, Génération du json {json_file} \033[0m")
if pandoc_runs_counter > 0 or not os.path.exists(json_file) or rebuild_articles_info_json:
print(f"\033[91m Les articles ont changé, Génération du json {json_file} \033[0m")
with open( json_file, 'w', encoding='utf-8') as json_file:
files_dict_serialized = json.dumps(files_dict, ensure_ascii=False, indent=4)
json_file.write(files_dict_serialized)
# ----- print(f"Nombre d'articles trouvés : {len(sorted_basenames)}")
#print(f"Nombre d'articles trouvés : {len(sorted_basenames)}")
count_articles_updated = 0
current_year = datetime.now().year
for basename, info in files_dict.items():
@ -311,7 +326,7 @@ for basename, info in files_dict.items():
if date_str > f'{current_year}0101':
count_articles_updated += 1
# ----- print(f"Nombre d'articles mis à jour après le 01 01 {current_year} : {count_articles_updated}")
#print(f"Nombre d'articles mis à jour après le 01 01 {current_year} : {count_articles_updated}")
def generate_blog_index(json_file, template_file, output_file):
@ -391,23 +406,23 @@ Pages:
# Écrire le fichier de sortie en html et en gmi
with open(output_file, 'w', encoding='utf-8') as f:
f.write(output_index_html)
# ----- print(f"Page d'index générée dans {output_file}")
#print(f"Page d'index générée dans {output_file}")
os.makedirs(os.path.dirname(gmi_index_file), exist_ok=True)
# ----- print('gmi_index_file', gmi_index_file)
#print('gmi_index_file', gmi_index_file)
with open(gmi_index_file, 'w', encoding='utf-8') as f:
f.write(output_index_gmi)
# ----- print(f"Page d'index gemini générée dans {gmi_index_file}")
#print(f"Page d'index gemini générée dans {gmi_index_file}")
# Générer la page d'index seulement si des articles ont été convertis
# if pandoc_runs_counter > 0:
if pandoc_runs_counter > 0 or run_gemini:
# Appel de la fonction pour générer la page d'index
generate_blog_index(destination_json + '/articles_info.json', 'templates/html/index.html.jinja', destination_html + 'index.html')
generate_blog_index(destination_json + '/articles_info.json', 'templates/html/index.html.jinja', destination_html + 'index.html')
# ----- print(f"\033[91m index régénéré {destination_html}index.html \033[0m")
print(f"\033[91m index régénéré {destination_html}index.html \033[0m")
# else:
# # ----- print("Aucun article n'a été converti, la page d'index n'est pas régénérée")
# #print("Aucun article n'a été converti, la page d'index n'est pas régénérée")
def generate_article_pages(json_file, template_file, output_dir):
@ -419,69 +434,60 @@ def generate_article_pages(json_file, template_file, output_dir):
:param output_dir: Répertoire de sortie pour les fichiers HTML.
"""
counter_gemini = 0
# ----- print('generate_article_pages: ouverture du json')
# Charger les données JSON
try:
print(f"generate_article_pages: ouverture du json {json_file}")
# Charger les données JSON
with open(json_file, 'r', encoding='utf-8') as f:
print('----------------------- yay json chargé')
articles_info = json.load(f)
# Configurer Jinja2
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template(template_file)
template_content = get_blog_template_conf(args.blog)
# Générer les pages pour chaque article
for article in articles_info.values():
print('----------------------- article', article['title'])
if article['first_picture_url']:
template_content['OG_IMAGE'] = article['first_picture_url']
else:
template_content['OG_IMAGE'] = template_content['SITE_ICON']
output_html = template.render(
template_content=template_content,
article=article,
all_articles=articles_info
)
slug_to_use = article['slug_with_year']
# Déterminer le slug à utiliser selon le type d'article
if 'article_type' in article and article['article_type'] == 'article':
slug_to_use = article['slug_with_year']
else:
slug_to_use = article['slug']
# Construire le chemin de sortie html en fonction du slug avec l'année
output_subdir = os.path.join(output_dir, slug_to_use)
os.makedirs(output_subdir, exist_ok=True)
output_file = os.path.join(output_subdir ,"index.html")
# Écrire le fichier de sortie en HTML pour un article
with open(output_file, 'w', encoding='utf-8') as f:
f.write(output_html)
print(f"Génération de la page gemini pour {article['title']}")
if 'gemini_content' in article and len(article['gemini_content']) > 0:
# Construire le chemin de sortie gmi en fonction du slug avec l'année
save_gemini_file(args.blog, article)
counter_gemini += 1
else:
print(f"----------- on ne génère pas le gemini pour {article['slug']}")
# ----- print('generate_article_pages: fin de génération de l index')
# ----- print(f"Nombre d'articles générés en gemini: {counter_gemini}")
except FileNotFoundError:
print(f"Erreur : Le fichier JSON {json_file} n'a pas été trouvé")
return
except json.JSONDecodeError:
print(f"Erreur : Le fichier JSON {json_file} est mal formaté")
return
except Exception as e:
print(f"Erreur lors de la lecture du fichier JSON : {json_file} {str(e)}")
return
with open(json_file, 'r', encoding='utf-8') as f:
print('----------------------- yay json chargé')
articles_info = json.load(f)
# Configurer Jinja2
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template(template_file)
template_content = get_blog_template_conf(args.blog)
print(f"articles count: {len(articles_info.values())}")
# Générer les pages pour chaque article
for article in articles_info.values():
print('----------------------- article', article['title'])
if article['first_picture_url']:
template_content['OG_IMAGE'] = article['first_picture_url']
else:
template_content['OG_IMAGE'] = template_content['SITE_ICON']
output_html = template.render(
template_content=template_content,
article=article,
all_articles=articles_info
)
slug_to_use = article['slug_with_year']
# Déterminer le slug à utiliser selon le type d'article
if 'article_type' in article and article['article_type'] == 'article':
slug_to_use = article['slug_with_year']
else:
slug_to_use = article['slug']
# Construire le chemin de sortie html en fonction du slug avec l'année
output_subdir = os.path.join(output_dir, slug_to_use)
print(f"output_subdir: {output_subdir}")
os.makedirs(output_subdir, exist_ok=True)
output_file = os.path.join(output_subdir ,"index.html")
print(f"output_file: {output_file}")
# Écrire le fichier de sortie en HTML pour un article
with open(output_file, 'w', encoding='utf-8') as f:
f.write(output_html)
print(f"Génération de la page gemini pour {article['title']}")
if 'gemini_content' in article and len(article['gemini_content']) > 0:
# Construire le chemin de sortie gmi en fonction du slug avec l'année
save_gemini_file(args.blog, article, articles_info, template_content)
counter_gemini += 1
else:
print(f"----------- on ne génère pas le gemini pour {article['slug']}")
return
# if pandoc_runs_counter or run_gemini:
@ -490,7 +496,7 @@ generate_article_pages(destination_json + '/articles_info.json', 'templates/html
# À la fin du script, calculer et afficher le temps d'exécution
execution_time = time.time() - start_time
# ----- print(f"Temps d'exécution : {execution_time:.2f} secondes")
#print(f"Temps d'exécution : {execution_time:.2f} secondes")