2024-11-03 12:32:08 +01:00
#!/usr/bin/env python3
2025-02-22 00:06:03 +01:00
# Création de nouvel article de blog
# Exemple de commande :
# python new_article.py [blog_dir] [lang] "article_title"
2024-11-11 23:20:04 +01:00
import os
2024-11-03 12:32:08 +01:00
from datetime import datetime
2024-11-16 00:21:38 +01:00
import argparse
2024-11-03 12:32:08 +01:00
# Configuration des arguments de la ligne de commande
parser = argparse . ArgumentParser ( description = " Générer un nouvel article en mode orgmode. " )
2025-02-22 18:55:01 +01:00
parser . add_argument ( " --title " , nargs = " ? " , help = " Le titre de l ' article. " )
parser . add_argument ( " --lang " , nargs = " ? " , default = " fr " , help = " La langue de l ' article (par défaut : fr pour Français ou en pour English). " )
parser . add_argument ( " --blog_dir " , nargs = " ? " , default = None , help = " Le nom du dossier de blog (sous source/). Si non spécifié, une liste de dossiers disponibles sera proposée. " )
2025-02-23 19:46:10 +01:00
parser . add_argument ( " --year_prefix_in_slug " , nargs = " ? " , default = True , help = " Ajouter l ' année au début du slug. " )
2024-11-03 12:32:08 +01:00
args = parser . parse_args ( )
2025-02-21 23:40:35 +01:00
2025-02-22 00:06:03 +01:00
# Définition du dossier de base pour les blogs
base_blog_dir = " sources/ "
2025-02-23 19:46:10 +01:00
year_prefix_in_slug = args . year_prefix_in_slug
2025-02-22 00:06:03 +01:00
# Si aucun dossier de blog n'est spécifié, proposer une sélection
if args . blog_dir is None :
available_dirs = [ d for d in os . listdir ( base_blog_dir ) if os . path . isdir ( os . path . join ( base_blog_dir , d ) ) ]
if not available_dirs :
print ( " Aucun dossier de blog trouvé sous " , base_blog_dir )
exit ( 1 )
print ( " Sélectionnez un dossier de blog : " )
for i , dir_name in enumerate ( available_dirs , start = 1 ) :
print ( f " { i } . { dir_name } " )
choice = input ( " Entrez le numéro de votre choix : " )
try :
choice = int ( choice )
if choice < 1 or choice > len ( available_dirs ) :
raise ValueError
args . blog_dir = available_dirs [ choice - 1 ]
except ValueError :
print ( " Choix invalide. Annulation. " )
exit ( 1 )
2025-02-22 18:55:01 +01:00
if args . title is None or not len ( args . title ) :
2025-02-22 00:06:03 +01:00
args . title = input ( " Entrez le titre de votre nouvel article: " )
if args . title is None :
print ( " Vous n ' avez pas choisi de titre d ' article " )
exit ( 1 )
# Vérification de l'existence du dossier de blog
blog_path = os . path . join ( base_blog_dir , args . blog_dir )
if not os . path . exists ( blog_path ) :
print ( f " Le dossier de blog ' { args . blog_dir } ' n ' existe pas sous ' { base_blog_dir } ' . " )
create_dir = input ( " Créer le dossier? (o/n) : " )
if create_dir . lower ( ) == ' o ' :
os . makedirs ( blog_path )
print ( f " Dossier ' { args . blog_dir } ' créé sous ' { base_blog_dir } ' . " )
else :
print ( " Annulation de la création de l ' article. " )
exit ( 1 )
2024-11-03 12:32:08 +01:00
# Génération du nom de fichier org avec la date et le slug
now = datetime . now ( )
2024-11-08 17:40:06 +01:00
# date_string = now.strftime("%Y-%m-%d")
date_string = now . strftime ( " % Y % m %d % H % M % S " )
2024-11-03 12:42:10 +01:00
date_string_full = now . strftime ( " % Y- % m- %d % H: % M: % S " )
2024-11-08 17:40:06 +01:00
# date_string_full = now.strftime("%Y%m%d%H%M%S")
2025-02-22 00:06:03 +01:00
slug = ' '
if args . title :
slug = args . title . lower ( ) . replace ( " " , " - " )
2024-11-28 23:24:55 +01:00
slug = slug . replace ( " -- " , " - " )
slug = slug . replace ( " -- " , " - " )
2025-02-23 19:46:10 +01:00
if year_prefix_in_slug :
schema_slug = f " { now . year } / { slug } "
else :
schema_slug = slug
2024-11-11 23:20:04 +01:00
file_abs_path = os . path . abspath ( os . path . dirname ( __file__ ) )
2025-02-22 00:06:03 +01:00
if args . lang == ' fr ' or args . lang == ' en ' :
filename = f " { file_abs_path } /sources/ { args . blog_dir } /lang_ { args . lang } / { date_string } __ { slug } .org "
else :
print ( ' langue invalide, disponible: fr ou en seulement ' )
exit ( 1 )
2024-11-03 12:32:08 +01:00
2024-11-03 12:42:10 +01:00
import uuid
def create_uuid_property ( ) :
uuid_value = uuid . uuid4 ( )
2025-02-23 16:59:59 +01:00
return uuid_value
2024-11-03 12:42:10 +01:00
2024-11-03 12:32:08 +01:00
# Écriture du fichier org
2024-11-26 12:48:25 +01:00
with open ( filename , " w " ) as f :
2025-02-23 16:59:59 +01:00
uuid = create_uuid_property ( )
2024-11-26 12:48:25 +01:00
f . write ( f """
2025-02-23 16:59:59 +01:00
: PROPERTIES :
: ID : { uuid }
: END :
#+title: {args.title}
2024-11-26 12:48:25 +01:00
#+post_ID:
2025-02-23 19:46:10 +01:00
#+post_slug: {slug}
#+post_url: https://www.ciperbliss.com/{schema_slug}
2024-11-26 12:48:25 +01:00
#+post_title: {args.title}
#+post_tags:
2025-02-23 16:59:59 +01:00
#+post_series:
2024-11-18 16:01:34 +01:00
#+post_type: post
#+post_status: publish
2025-02-23 19:46:10 +01:00
#+post_picture:
2024-11-26 12:48:25 +01:00
#+post_date_published: <{date_string_full}>
#+post_date_modified: <{date_string_full}>
2025-02-23 16:59:59 +01:00
#+post_index_page_roam_id: {uuid}
#+BLOG: {args.blog_dir}
2024-11-26 12:48:25 +01:00
* { args . title }
""" )
2024-11-18 16:01:34 +01:00
2024-11-11 23:20:04 +01:00
print ( f " Le fichier ' { filename } ' a été créé avec succès. " )