mirror of
https://forge.chapril.org/tykayn/orgmode-to-gemini-blog
synced 2025-06-20 09:04:42 +02:00
auto detect tags and move pictures after resizing
This commit is contained in:
parent
9c72473913
commit
618c029c62
8 changed files with 165 additions and 154 deletions
121
build_indexes.py
121
build_indexes.py
|
@ -1,9 +1,9 @@
|
|||
import argparse
|
||||
import datetime
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
|
||||
from utils import *
|
||||
from enrich_html import enrich_one_file
|
||||
from website_config import configs_sites, global_config
|
||||
|
||||
|
@ -22,120 +22,11 @@ source_files_extension = "org"
|
|||
|
||||
config_title = configs_sites[args.source]['BLOG_TITLE']
|
||||
|
||||
# Expression régulière pour extraire la date et le slug du nom de fichier org
|
||||
regex = r"^(\d{14})(-[a-zA-Z0-9_-]+)\.gmi$"
|
||||
regex_orgroam = r"^(\d{14})_([a-zA-Z0-9_-]+)\.gmi$"
|
||||
|
||||
use_article_file_for_name = (not global_config["slug_with_year"])
|
||||
website_name = args.source
|
||||
|
||||
|
||||
def trouver_nom_article(fichier_org, format="html"):
|
||||
# print('fichier_org, ', fichier_org)
|
||||
with open(fichier_org, 'r') as file:
|
||||
lignes = file.readlines()
|
||||
|
||||
nom_article = ''
|
||||
|
||||
# print('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
|
||||
# print(f"Nom de l'article : {nom_article}")
|
||||
|
||||
return nom_article.replace(args.source + '_', '').replace('_', ' ')
|
||||
|
||||
|
||||
def find_year_and_slug(fichier):
|
||||
fichier = fichier.replace('..', '.')
|
||||
# print(f" ------------ build_indexes: find in {fichier} -------------")
|
||||
slug = fichier.replace('.gmi', '')
|
||||
annee = '2024'
|
||||
date_str = '2024-00-00'
|
||||
date = '2024-00-00'
|
||||
match = re.match(regex_orgroam, fichier)
|
||||
|
||||
if match:
|
||||
date_str = match.group(1)
|
||||
annee = date_str[:4]
|
||||
slug = match.group(2)
|
||||
|
||||
match = re.match(regex, fichier)
|
||||
if match:
|
||||
date_str = match.group(1)
|
||||
# Convertir la date en objet datetime
|
||||
if "-" in date_str:
|
||||
date = datetime.datetime.strptime(date_str, "%Y-%m-%d")
|
||||
else:
|
||||
date = datetime.datetime.strptime(date_str, "%Y%m%d%H%M%S")
|
||||
date_string_replaced = str(date).replace(' 00:00:00', '')
|
||||
slug = fichier.replace('.gmi', '')
|
||||
slug = slug.replace(date_string_replaced, '')
|
||||
slug = enlever_premier_tiret_ou_underscore(slug)
|
||||
|
||||
annee = str(date.year).replace(' 00:00:00', '')
|
||||
# else:
|
||||
# print('ERREUR aucun slug trouvé')
|
||||
|
||||
# print(f" ------------ build_indexes: ")
|
||||
# print(f" ------------ build_indexes: Fichier: {fichier}")
|
||||
# print(f" ------------ build_indexes: année: {annee}")
|
||||
# print(f" ------------ build_indexes: str(date): {str(date)}")
|
||||
# print(f" ------------ build_indexes: slug: {slug}")
|
||||
# print(f" ------------ build_indexes: chemin: {annee}/{slug}/")
|
||||
return [date_str, annee, slug]
|
||||
|
||||
|
||||
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
|
||||
# print('----------- get_files_list_of_folder: folder_path : ', folder_path)
|
||||
# Liste les fichiers articles, trie par nom décroissant
|
||||
try:
|
||||
fichiers_md = sorted([f.replace('.' + source_files_extension, '.gmi') for f in os.listdir(folder_path) if
|
||||
f.endswith(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
|
||||
|
||||
|
||||
# transformer le nom du fichier orgmode en une création de dossier de l'année, et un sous dossier du nom du slug dans le nom de fichier, contenant un seul fichier d'index afin de faire de l'url rewrite en dur.
|
||||
# le nom de fichier org commence par une date YYYY-MM-DD ou bien YYYYMMDDHHmmss, est suivie d'un slug, et finit par l'extension .org
|
||||
|
@ -179,7 +70,7 @@ def generer_index(dossier_source, fichier_index):
|
|||
|
||||
link_org = fichier.replace('.gmi', '.org')
|
||||
file_path_org = os.path.join(dossier_parent, "sources", website_name, lang_folder, link_org)
|
||||
article_name = trouver_nom_article(file_path_org, 'org')
|
||||
article_name = trouver_nom_article(file_path_org,args.source, 'org')
|
||||
|
||||
if not article_name:
|
||||
article_name = slug.replace('-', ' ')
|
||||
|
@ -200,11 +91,11 @@ def generer_index(dossier_source, fichier_index):
|
|||
contenu_index_html += "<hr/>"
|
||||
contenu_index_html += "<h1>Navigation</h1>"
|
||||
for fichier in files_static:
|
||||
# print(" -------- fichier ", fichier)
|
||||
mylog(" -------- fichier ", fichier)
|
||||
link_html = fichier.replace('.gmi', '.html')
|
||||
link_org = fichier.replace('.gmi', '.org')
|
||||
file_path_org = os.path.join(dossier_parent, "sources", website_name, link_org)
|
||||
article_name = trouver_nom_article(file_path_org, 'org')
|
||||
article_name = trouver_nom_article(file_path_org,args.source, 'org')
|
||||
|
||||
if article_name:
|
||||
contenu_index_gmi += f"=> {fichier} {article_name}\n"
|
||||
|
@ -212,11 +103,11 @@ def generer_index(dossier_source, fichier_index):
|
|||
contenu_index_gmi += f"=> {fichier}\n"
|
||||
|
||||
if fichier != "index.gmi":
|
||||
# print(' -------- rechercher le nom de l article dans le fichier ')
|
||||
mylog(' -------- rechercher le nom de l article dans le fichier ')
|
||||
if use_article_file_for_name:
|
||||
article_name = link_html
|
||||
else:
|
||||
article_name = trouver_nom_article(file_path_org, 'org')
|
||||
article_name = trouver_nom_article(file_path_org,args.source, 'org')
|
||||
|
||||
if not article_name:
|
||||
article_name = link_html
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue