mirror of
https://forge.chapril.org/tykayn/orgmode-to-gemini-blog
synced 2025-06-20 09:04:42 +02:00
515 lines
18 KiB
Python
515 lines
18 KiB
Python
#!/bin/python3
|
|
import os
|
|
import re
|
|
import shutil
|
|
from datetime import datetime
|
|
import unicodedata
|
|
import pypandoc
|
|
import subprocess
|
|
import tempfile
|
|
from md2gemini import md2gemini
|
|
|
|
from website_config import *
|
|
|
|
# this path should be customized
|
|
org_roam_dir: str = '/home/tykayn/Nextcloud/textes/orgmode/org-roam/'
|
|
|
|
# Trouver l'identifiant OrgROAM
|
|
pattern_roam_id_search = r':ID:(?:\s+)?([a-zA-Z0-9-]+)'
|
|
# Expression régulière pour extraire la date et le slug du nom de fichier org
|
|
regex = r"^([a-zA-Z0-9_-]+)\_\_(-[a-zA-Z0-9_-]+)\.org$"
|
|
# Recherche de date de création du fichier org-roam dans un article gemini
|
|
regex_orgroam = regex
|
|
|
|
# show_logs=True
|
|
show_logs = global_config["show_logs"]
|
|
|
|
|
|
def mylog(*content):
|
|
"""Fonction qui imprime tous les arguments passés selon le niveau de debug souhaité."""
|
|
if show_logs:
|
|
for c in content:
|
|
print(' ',c)
|
|
|
|
|
|
def trouver_nom_article(fichier_org, blog_name, format="html"):
|
|
# mylog('fichier_org, ', fichier_org)
|
|
with open(fichier_org, 'r') as file:
|
|
lignes = file.readlines()
|
|
|
|
nom_article = ''
|
|
|
|
# mylog('trouver_nom_article format', format)
|
|
# Expressions régulières pour trouver les titres de niveau 1 et 2
|
|
if format == 'html':
|
|
titre_niveau_1 = r'<h1\s+(?:id|data-created)="[^"]*">(.*?)</h1>'
|
|
titre_niveau_2 = r'^\<h2.*?\>(.+)\<\/h2\>$'
|
|
else:
|
|
titre_niveau_1 = r'^\*+ (.+)$'
|
|
titre_niveau_2 = r'^\*\*+ (.+)$'
|
|
|
|
# Itérer sur les lignes du fichier
|
|
for ligne in lignes:
|
|
# Rechercher un titre de niveau 1
|
|
titre_niveau_1_match = re.match(titre_niveau_1, ligne)
|
|
if titre_niveau_1_match:
|
|
titre_niveau_1_texte = titre_niveau_1_match.group(1)
|
|
if titre_niveau_1_texte.lower() != "article" and titre_niveau_1_texte.lower() != "liens":
|
|
nom_article = titre_niveau_1_texte
|
|
break
|
|
else:
|
|
# Si le premier titre de niveau 1 est "Article", rechercher le premier titre de niveau 2
|
|
titre_niveau_2_match = re.match(titre_niveau_2, ligne)
|
|
if titre_niveau_2_match:
|
|
nom_article = titre_niveau_2_match.group(1)
|
|
break
|
|
# mylog(f"Nom de l'article : {nom_article}")
|
|
|
|
return nom_article.replace(blog_name + '_', '').replace('_', ' ')
|
|
|
|
def find_org_roam_id(content):
|
|
match = re.search(pattern_roam_id_search, content)
|
|
if match:
|
|
return match.group(1)
|
|
return None
|
|
|
|
def get_blog_template_conf(blogname) -> dict:
|
|
"""
|
|
Retourne la configuration du blog spécifié.
|
|
|
|
:param blogname: Nom du blog (str).
|
|
:return: Configuration du blog (dict).
|
|
"""
|
|
if blogname not in configs_sites:
|
|
return default_config
|
|
else:
|
|
return configs_sites[blogname]
|
|
|
|
def find_year_and_slug_on_filename(filename):
|
|
print(f"Traitement du fichier: {filename}") # Debug
|
|
try:
|
|
# Supposons que le format attendu est "YYYYMMDDHHMMSS-slug.org"
|
|
date_str = filename[:14] # Prend les 14 premiers caractères pour la date
|
|
annee = date_str[:4] # Prend les 4 premiers caractères pour l'année
|
|
|
|
# Gestion plus robuste du slug
|
|
if '-' in filename:
|
|
slug = filename.split('-', 1)[1].replace('.org', '')
|
|
else:
|
|
slug = filename.replace('.org', '')
|
|
|
|
return date_str, annee, slug
|
|
except Exception as e:
|
|
print(f"Format de fichier non standard: {filename}")
|
|
return None, None, filename.replace('.org', '')
|
|
|
|
|
|
def enlever_premier_tiret_ou_underscore(chaîne):
|
|
if chaîne.startswith('-') or chaîne.startswith('_'):
|
|
chaîne = chaîne[1:]
|
|
return chaîne
|
|
|
|
|
|
# création des dossiers intermédiaires s'il y en a
|
|
# déplace le fichier dans le dossier spécifié
|
|
def create_path_folders_and_move_file(path, file):
|
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
|
|
shutil.move(file, path)
|
|
|
|
|
|
def get_files_list_of_folder(folder_path):
|
|
# Vérifie si le dossier existe
|
|
if not os.path.exists(folder_path):
|
|
print(f" ------------ build_indexes: Erreur : Le dossier '{folder_path}' n'existe pas.")
|
|
return
|
|
mylog('----------- get_files_list_of_folder: folder_path : ', folder_path)
|
|
# Liste les fichiers articles, trie par nom décroissant
|
|
try:
|
|
fichiers_md = sorted(
|
|
[f.replace('.' + global_config['source_files_extension'], '.gmi') for f in os.listdir(folder_path) if
|
|
f.endswith(global_config['source_files_extension'])], reverse=True)
|
|
print('fichiers trouvés:', len(fichiers_md))
|
|
return fichiers_md
|
|
except OSError as e:
|
|
print(f" ------------ build_indexes: Erreur lors de la lecture du dossier : {e}")
|
|
return
|
|
|
|
|
|
def get_id_of_roam_note_content(content):
|
|
match = re.search(pattern_roam_id_search, content)
|
|
if match:
|
|
return match.group(1)
|
|
return None
|
|
|
|
|
|
def find_first_level1_title(content):
|
|
pattern = r'^\* (.+)$'
|
|
match = re.search(pattern, content, re.MULTILINE)
|
|
if match:
|
|
if match.group(1) != 'Article':
|
|
return match.group(1)
|
|
else:
|
|
pattern = r'^\*\* (.+)$'
|
|
match = re.search(pattern, content, re.MULTILINE)
|
|
if match:
|
|
return match.group(1)
|
|
return None
|
|
|
|
def find_extract_in_content_org(org_content):
|
|
# Supprimer les lignes qui commencent par #+
|
|
org_content = re.sub(r'^\s*#\+.*\n', '', org_content, flags=re.MULTILINE)
|
|
|
|
# Supprimer les sections de logbook
|
|
org_content = re.sub(r'^\*\* Logbook\n.*?(?=\*\* |\Z)', '', org_content, flags=re.DOTALL | re.MULTILINE)
|
|
|
|
# Supprimer les propriétés
|
|
org_content = re.sub(r'^:PROPERTIES:\n.*?:END:\n', '', org_content, flags=re.DOTALL | re.MULTILINE)
|
|
|
|
# Supprimer les lignes vides supplémentaires
|
|
org_content = re.sub(r'\n\s*\n+', '\n', org_content)
|
|
|
|
# Supprimer les espaces en début et fin de chaque ligne
|
|
org_content = '\n'.join(line.strip() for line in org_content.splitlines())
|
|
|
|
# Supprimer les espaces en début et fin du contenu final
|
|
return org_content.strip()
|
|
|
|
def extract_body_content(html_content):
|
|
pattern = r'<body.*?>(.*?)</body>'
|
|
match = re.search(pattern, html_content, re.DOTALL)
|
|
if match:
|
|
return match.group(1)
|
|
else:
|
|
print('---- extract_body_content : no body found in this html')
|
|
return html_content
|
|
|
|
def add_tags_from_content(tags=None, file_content="", words_to_check=None):
|
|
"""
|
|
Ajoute des tags à l'ensemble `tags` si les mots correspondants sont trouvés dans le contenu du fichier.
|
|
|
|
:param tags: Ensemble de tags (set). Si None, un nouvel ensemble est créé (type set, optionnel).
|
|
:param file_content: Contenu du fichier (str).
|
|
:param words_to_check: Liste de mots à repérer (list). Si None, une liste vide est utilisée (type list, optionnel).
|
|
:return: Ensemble de tags mis à jour (set).
|
|
"""
|
|
# Initialiser l'ensemble tags s'il est None
|
|
if tags is None:
|
|
tags = set()
|
|
|
|
# Initialiser la liste words_to_check s'il est None
|
|
if words_to_check is None:
|
|
words_to_check = []
|
|
|
|
# Convertir le contenu du fichier en minuscules pour une recherche insensible à la casse
|
|
file_content_lower = file_content.lower()
|
|
|
|
# Parcourir chaque mot à vérifier
|
|
for word in words_to_check:
|
|
# Vérifier si le mot est présent dans le contenu du fichier
|
|
# Chercher une correspondance sans mettre en lowercase si le tag est en majuscule, c'est sans doute un acronyme/sigle.
|
|
if word.isupper():
|
|
if word in file_content:
|
|
tags.add(word)
|
|
else:
|
|
if word.lower() in file_content_lower:
|
|
tags.add(word)
|
|
|
|
return tags
|
|
# Variable globale pour stocker les fichiers sans tags
|
|
untagged_files = []
|
|
|
|
def save_untagged_files(output_file="sources/site_web/build/articles_without_tags.json"):
|
|
"""
|
|
Sauvegarde la liste des fichiers sans tags dans un fichier JSON.
|
|
|
|
:param output_file: Chemin du fichier JSON de sortie
|
|
"""
|
|
import json
|
|
import os
|
|
|
|
# Créer le dossier de sortie si nécessaire
|
|
os.makedirs(os.path.dirname(output_file), exist_ok=True)
|
|
|
|
print('save_untagged_files', len(untagged_files))
|
|
# Sauvegarder la liste dans le fichier JSON
|
|
with open(output_file, 'w', encoding='utf-8') as f:
|
|
json.dump(untagged_files, f, ensure_ascii=False, indent=4)
|
|
|
|
|
|
def extract_tags_from_file(file_path, excluded_tags, auto_detected_tags_list=global_config['auto_tag_terms']):
|
|
tags = set()
|
|
with open(file_path, 'r', encoding='utf-8') as file_content:
|
|
tag_found = False
|
|
for line in file_content:
|
|
if global_config['automatic_tagging_enabled'] and len(auto_detected_tags_list) > 0:
|
|
tags = add_tags_from_content(tags, line, auto_detected_tags_list)
|
|
# Check for orgmode tags :tag1:tag2:
|
|
if global_config.get('automatic_tagging_org_files', True):
|
|
if ':' in line:
|
|
for word in line.split():
|
|
if len(word) > 1 and word.startswith(':') and word.endswith(':'):
|
|
tag = word[1:-1]
|
|
if tag not in excluded_tags:
|
|
tags.add(tag)
|
|
tag_found = True
|
|
# Check for #+tags: tag1,tag2
|
|
if line.startswith('#+tags:'):
|
|
for tag in line[len('#+tags:'):].split(','):
|
|
tag = tag.strip()
|
|
if tag and tag not in excluded_tags:
|
|
tags.add(tag)
|
|
tag_found = True
|
|
|
|
if not tag_found:
|
|
untagged_files.append(file_path)
|
|
# print('no tag in the article', file_path)
|
|
return tags
|
|
|
|
def remove_properties_section(text):
|
|
pattern = r"<h1 id=\"article\">Article</h1>.+?</ul>"
|
|
replacement = ""
|
|
return re.sub(pattern, replacement, text, flags=re.DOTALL)
|
|
|
|
|
|
def remove_article_head_properties_orgmode(text):
|
|
pattern = r":PROPERTIES:.+?:END:"
|
|
replacement = ""
|
|
return re.sub(pattern, replacement, text, flags=re.DOTALL)
|
|
|
|
|
|
def remove_hint_html(text):
|
|
pattern = r"<p>ceci<sub>estduhtml</sub></p>"
|
|
replacement = ""
|
|
return re.sub(pattern, replacement, text, flags=re.DOTALL)
|
|
|
|
|
|
def slugify_title(title_text):
|
|
"""
|
|
Transforme un titre en un slug valide.
|
|
|
|
:param title_text: Titre en texte (str).
|
|
:return: Slug en minuscules avec des tirets (str).
|
|
"""
|
|
title_text = unicodedata.normalize('NFKD', title_text).encode('ascii', 'ignore').decode('ascii')
|
|
title_text = title_text.lower()
|
|
title_text = re.sub(r'[^a-z0-9\s-]', '', title_text)
|
|
title_text = re.sub(r'\s+', '-', title_text)
|
|
title_text = re.sub(r'-+', '-', title_text)
|
|
title_text = title_text.strip('-')
|
|
return title_text
|
|
|
|
def find_slug_in_file_basename(file_basename) -> str:
|
|
"""
|
|
Extrait l'année et le slug du nom de fichier selon le format spécifié.
|
|
|
|
:param file_basename: Nom de fichier (str).
|
|
:return: Tuple contenant l'année et le slug (année, slug) ou None si non trouvé.
|
|
"""
|
|
pattern = regex_orgroam
|
|
match = re.match(pattern, file_basename)
|
|
if match:
|
|
year = match.group(1)
|
|
slug = match.group(2)
|
|
# prendre la partie finale du nom du fichier
|
|
splitted = slug.split('_')
|
|
# print('len(splitted)', len(splitted), splitted)
|
|
if len(splitted) > 1:
|
|
slug = splitted[len(splitted)-1]
|
|
|
|
slug=enlever_premier_tiret_ou_underscore(slug)
|
|
|
|
slug = f"{year}/{slug}"
|
|
|
|
|
|
|
|
return slug
|
|
return None
|
|
|
|
def get_stats_on_all_websites():
|
|
"""
|
|
Retourne des statistiques sur tous les sites web dans le dossier sources/.
|
|
Pour chaque site, compte le nombre d'articles .org et trouve l'article le plus récent.
|
|
|
|
:return: Dictionnaire avec les stats par site
|
|
"""
|
|
stats = {}
|
|
base_dir = "sources"
|
|
|
|
# Parcourir tous les dossiers de sites dans sources/
|
|
for site in os.listdir(base_dir):
|
|
site_path = os.path.join(base_dir, site)
|
|
|
|
if not os.path.isdir(site_path):
|
|
continue
|
|
|
|
# Initialiser les stats pour ce site
|
|
stats[site] = {
|
|
'nb_articles': 0,
|
|
'nb_mots': 0,
|
|
'dernier_article': None,
|
|
'date_dernier_article': None
|
|
}
|
|
|
|
# Chercher les articles .org dans lang_fr et lang_en
|
|
for lang in ['lang_fr', 'lang_en']:
|
|
lang_path = os.path.join(site_path, lang)
|
|
|
|
if not os.path.exists(lang_path):
|
|
continue
|
|
|
|
# Lister tous les fichiers .org
|
|
org_files = [f for f in os.listdir(lang_path) if f.endswith('.org')]
|
|
stats[site]['nb_articles'] += len(org_files)
|
|
# Calculer le nombre total de mots pour ce dossier de langue
|
|
total_mots = 0
|
|
for org_file in org_files:
|
|
file_path = os.path.join(lang_path, org_file)
|
|
try:
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
contenu = f.read()
|
|
# Compter les mots en divisant par les espaces
|
|
total_mots += len(contenu.split())
|
|
except Exception as e:
|
|
print(f"Erreur lors de la lecture de {file_path}: {e}")
|
|
|
|
# Ajouter ou incrémenter le compteur de mots dans les stats
|
|
stats[site]['nb_mots'] += total_mots
|
|
|
|
# Trouver le fichier le plus récent
|
|
for org_file in org_files:
|
|
file_path = os.path.join(lang_path, org_file)
|
|
mod_time = os.path.getmtime(file_path)
|
|
|
|
if (stats[site]['date_dernier_article'] is None or
|
|
mod_time > stats[site]['date_dernier_article']):
|
|
stats[site]['date_dernier_article'] = mod_time
|
|
stats[site]['dernier_article'] = file_path
|
|
|
|
# Convertir le timestamp en date lisible
|
|
if stats[site]['date_dernier_article']:
|
|
stats[site]['date_dernier_article'] = datetime.fromtimestamp(
|
|
stats[site]['date_dernier_article']
|
|
).strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
return stats
|
|
|
|
def convert_org_to_html(org_file, output_html_file):
|
|
"""
|
|
Convertit un fichier Org en HTML en utilisant pypandoc.
|
|
|
|
:param org_file: Chemin du fichier Org à convertir.
|
|
:param output_html_file: Chemin du fichier HTML de sortie.
|
|
"""
|
|
try:
|
|
pypandoc.convert_file(org_file, 'html', outputfile=output_html_file)
|
|
print(f"Conversion réussie : {org_file} -> {output_html_file}")
|
|
except Exception as e:
|
|
print(f"Erreur lors de la conversion de {org_file} : {e}")
|
|
|
|
|
|
|
|
def get_first_picture_url(content):
|
|
# Utiliser une expression régulière pour
|
|
# trouver la première URL d'image dans le contenu
|
|
pattern = r'\[\[(.*?)\]\]'
|
|
match = re.search(pattern, content)
|
|
if match:
|
|
return match.group(1)
|
|
else:
|
|
return None
|
|
|
|
|
|
def org_to_gmi(org_text: str, output_filename_slug: str) -> str:
|
|
"""
|
|
Convertit un texte au format Org en un fichier au format GMI (Gemini)
|
|
en utilisant pypandoc.
|
|
|
|
Args:
|
|
- org_text (str): Le texte au format Org à convertir.
|
|
- output_file (str): Chemin du fichier de sortie au format GMI, sans avoir à préciser l'extension.
|
|
"""
|
|
output = """
|
|
# mock land output
|
|
===========
|
|
|
|
blah blah blah
|
|
|
|
-----------------
|
|
Tykayn blog mock content
|
|
-----------------
|
|
|
|
Navigation:
|
|
|
|
=> accueil.gmi Accueil
|
|
=> a-propos.gmi à propos
|
|
"""
|
|
# Conversion du texte Org en GMI via Pandoc
|
|
try:
|
|
output = pypandoc.convert_text(org_text, 'markdown', format='org')
|
|
except RuntimeError as e:
|
|
print(f"Erreur de conversion : {e}")
|
|
return
|
|
|
|
# Sauvegarde du contenu GMI dans un fichier
|
|
try:
|
|
with open(destination_gmi+'/'+output_filename_slug+'.gmi', 'w', encoding='utf-8') as f:
|
|
f.write(output)
|
|
print(f"Fichier GMI sauvegardé avec succès : {output_filename_slug}")
|
|
except OSError as e:
|
|
print(f"Erreur lors de la sauvegarde du fichier : {e}")
|
|
return output
|
|
|
|
def count_files_in_directories(directories):
|
|
total_count = 0
|
|
for directory in directories:
|
|
for root, dirs, files in os.walk(directory):
|
|
total_count += len(files)
|
|
return total_count
|
|
|
|
|
|
def convert_org_to_gemini(org_content):
|
|
"""
|
|
Convertit un contenu org en gemini en utilisant pandoc et md2gemini
|
|
|
|
Args:
|
|
org_content (str): Contenu au format org
|
|
|
|
Returns:
|
|
str: Contenu converti en format gemini
|
|
"""
|
|
try:
|
|
# Créer un fichier temporaire avec le contenu org
|
|
with tempfile.NamedTemporaryFile(mode='w', suffix='.org', encoding='utf-8') as temp_org:
|
|
temp_org.write(org_content)
|
|
temp_org.flush()
|
|
|
|
# Première étape : conversion org vers markdown avec pandoc
|
|
pandoc_cmd = [
|
|
'pandoc',
|
|
'-f', 'org',
|
|
'-t', 'markdown',
|
|
temp_org.name
|
|
]
|
|
|
|
markdown_content = subprocess.check_output(
|
|
pandoc_cmd,
|
|
text=True,
|
|
stderr=subprocess.PIPE
|
|
)
|
|
|
|
# Deuxième étape : conversion markdown vers gemini avec md2gemini
|
|
gemini_content = md2gemini(
|
|
markdown_content,
|
|
frontmatter=True,
|
|
links='inline',
|
|
)
|
|
|
|
return gemini_content.strip()
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
print(f"Erreur lors de la conversion avec pandoc: {e.stderr}")
|
|
raise
|
|
except Exception as e:
|
|
print(f"Erreur lors de la conversion: {str(e)}")
|
|
raise
|