move back files
This commit is contained in:
parent
b1c8937fc9
commit
d885085861
@ -15,6 +15,10 @@ import time # Importer le module time
|
||||
|
||||
# Démarrer le chronomètre
|
||||
start_time = time.time()
|
||||
# pour tester
|
||||
generate_linkings_json = True
|
||||
generate_articles = True
|
||||
run_pandoc = True # le plus long quand on a beaucoup d'articles
|
||||
|
||||
# Configurer argparse pour prendre le blog en argument
|
||||
parser = argparse.ArgumentParser(description='Générer une liste des derniers articles de blog.')
|
||||
@ -41,51 +45,56 @@ def get_first_picture_url(content):
|
||||
else:
|
||||
return None
|
||||
|
||||
if generate_linkings_json :
|
||||
|
||||
# Parcourir les fichiers du dossier
|
||||
for file_name in os.listdir(directory):
|
||||
if file_name.endswith('.org'):
|
||||
file_path = os.path.join(directory, file_name)
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
content = f.read()
|
||||
date_modified = time.ctime(os.path.getmtime(file_path))
|
||||
# Parcourir les fichiers du dossier
|
||||
for file_name in os.listdir(directory):
|
||||
if file_name.endswith('.org'):
|
||||
file_path = os.path.join(directory, file_name)
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
content = f.read()
|
||||
date_modified = time.ctime(os.path.getmtime(file_path))
|
||||
|
||||
basename = get_basename(file_name)
|
||||
date_str, annee, slug = find_year_and_slug_on_filename(basename)
|
||||
tags = extract_tags_from_file(file_path, global_config['excluded_tags'])
|
||||
basename = get_basename(file_name)
|
||||
date_str, annee, slug = find_year_and_slug_on_filename(basename)
|
||||
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)
|
||||
# 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)
|
||||
# Désactiver les warning d'identifiant dupliqué dans la conversion pandoc
|
||||
content_without_h1 = re.sub(r'^\*.*?$', '', content, count=1, flags=re.MULTILINE)
|
||||
|
||||
html_content = pypandoc.convert_text(content_without_h1, 'html', format='org')
|
||||
if run_pandoc:
|
||||
|
||||
# html_content = pypandoc.convert_text(content, 'html', format='org')
|
||||
html_content = pypandoc.convert_text(content_without_h1, 'html', format='org')
|
||||
else:
|
||||
html_content = content_without_h1
|
||||
|
||||
files_dict[f"{annee}/{slug}"] = {
|
||||
'path': file_path,
|
||||
'basename': basename,
|
||||
'slug': f"{slug}/",
|
||||
'slug_with_year': f"{annee}/{slug}",
|
||||
'date': boom[0],
|
||||
'date_modified' : date_modified,
|
||||
'first_picture_url' : get_first_picture_url(content),
|
||||
'date_formattee': datetime.strptime(date_str, '%Y%m%d%H%M%S').strftime('%d %B %Y à %H:%M:%S') if len(date_str) == 14 else datetime.strptime(date_str, '%Y%m%dT%H%M%S').strftime('%d %B %Y à %H:%M:%S') if len(date_str) == 15 else datetime.strptime(date_str, '%Y-%m-%d').strftime('%d %B %Y'),
|
||||
'annee': annee,
|
||||
'tags': tags, # Assurez-vous que c'est une liste
|
||||
'title': title,
|
||||
'next': None,
|
||||
'previous': None,
|
||||
'org_content': content, # Contenu Org original
|
||||
'html_content_without_h1': re.sub(r'<h1>.*?</h1>', '', html_content), # Contenu HTML converti sans le titre de premier niveau
|
||||
'html_content': html_content # Contenu HTML converti
|
||||
}
|
||||
# html_content = pypandoc.convert_text(content, 'html', format='org')
|
||||
|
||||
files_dict[f"{annee}/{slug}"] = {
|
||||
'path': file_path,
|
||||
'basename': basename,
|
||||
'slug': f"{slug}/",
|
||||
'slug_with_year': f"{annee}/{slug}",
|
||||
'date': boom[0],
|
||||
'date_modified' : date_modified,
|
||||
'first_picture_url' : get_first_picture_url(content),
|
||||
'date_formattee': datetime.strptime(date_str, '%Y%m%d%H%M%S').strftime('%d %B %Y à %H:%M:%S') if len(date_str) == 14 else datetime.strptime(date_str, '%Y%m%dT%H%M%S').strftime('%d %B %Y à %H:%M:%S') if len(date_str) == 15 else datetime.strptime(date_str, '%Y-%m-%d').strftime('%d %B %Y'),
|
||||
'annee': annee,
|
||||
'tags': tags,
|
||||
'title': title,
|
||||
'next': None,
|
||||
'previous': None,
|
||||
'org_content': content, # Contenu Org original
|
||||
'html_content_without_h1': re.sub(r'<h1>.*?</h1>', '', html_content), # Contenu HTML converti sans le titre de premier niveau
|
||||
'html_content': html_content # Contenu HTML converti
|
||||
}
|
||||
|
||||
# Trier les basenames par ordre décroissant
|
||||
sorted_basenames = sorted(files_dict.keys(), reverse=True)
|
||||
@ -132,17 +141,18 @@ def generate_blog_index(json_file, template_file, output_file):
|
||||
articles_info = json.load(f)
|
||||
|
||||
# Trier les articles par date (ou par slug) et prendre les 10 derniers
|
||||
sorted_articles = sorted(articles_info.values(), key=lambda x: x['date'], reverse=True)[:10]
|
||||
sorted_articles = sorted(articles_info.values(), key=lambda x: x['date'], reverse=True)[:global_config['posts_per_page']]
|
||||
|
||||
# Configurer Jinja2
|
||||
env = Environment(loader=FileSystemLoader('.'))
|
||||
template = env.get_template(template_file)
|
||||
|
||||
articles_others = sorted(articles_info.values(), key=lambda x: x['date'], reverse=True)[10:]
|
||||
# Rendre le template avec les données
|
||||
output_html = template.render(
|
||||
template_content=configs_sites[args.blog],
|
||||
articles=sorted_articles[:global_config['posts_per_page']],
|
||||
articles_others=sorted_articles
|
||||
articles_others=articles_others
|
||||
)
|
||||
|
||||
# Écrire le fichier de sortie
|
||||
@ -163,6 +173,8 @@ def generate_article_pages(json_file, template_file, output_dir):
|
||||
:param template_file: Chemin du fichier template Jinja2.
|
||||
:param output_dir: Répertoire de sortie pour les fichiers HTML.
|
||||
"""
|
||||
|
||||
print('generate_article_pages: ouverture du json')
|
||||
# Charger les données JSON
|
||||
with open(json_file, 'r', encoding='utf-8') as f:
|
||||
articles_info = json.load(f)
|
||||
@ -182,7 +194,7 @@ def generate_article_pages(json_file, template_file, output_dir):
|
||||
# Construire le chemin de sortie en fonction du slug avec l'année
|
||||
output_subdir = os.path.join(output_dir, article['slug_with_year'])
|
||||
|
||||
print('make subdir: ',output_subdir)
|
||||
# print('make subdir: ',output_subdir)
|
||||
|
||||
|
||||
|
||||
@ -195,9 +207,11 @@ def generate_article_pages(json_file, template_file, output_dir):
|
||||
# Écrire le fichier de sortie
|
||||
with open(output_file, 'w', encoding='utf-8') as f:
|
||||
f.write(output_html)
|
||||
print('generate_article_pages: fin de génération de l index')
|
||||
|
||||
if generate_articles:
|
||||
# Appel de la fonction pour générer les pages des articles
|
||||
generate_article_pages(destination_json + '/articles_info.json', 'templates/html/article.html.jinja', destination_html)
|
||||
generate_article_pages(destination_json + '/articles_info.json', 'templates/html/article.html.jinja', destination_html)
|
||||
|
||||
# À la fin du script, calculer et afficher le temps d'exécution
|
||||
execution_time = time.time() - start_time
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user