mirror of
https://forge.chapril.org/tykayn/orgmode-to-gemini-blog
synced 2025-06-20 09:04:42 +02:00
up gen gemini
This commit is contained in:
parent
bba1df0377
commit
6134f677fa
622 changed files with 165 additions and 544 deletions
94
utils.py
94
utils.py
|
@ -86,7 +86,7 @@ def get_blog_template_conf(blogname) -> dict:
|
|||
return configs_sites[blogname]
|
||||
|
||||
def find_year_and_slug_on_filename(filename):
|
||||
print(f"Traitement du fichier: {filename}") # Debug
|
||||
# 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
|
||||
|
@ -129,7 +129,7 @@ def get_files_list_of_folder(folder_path):
|
|||
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))
|
||||
# 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}")
|
||||
|
@ -144,13 +144,13 @@ def get_id_of_roam_note_content(content):
|
|||
|
||||
|
||||
def find_first_level1_title(content):
|
||||
pattern = r'^\* (.+)$'
|
||||
pattern = r'^\*\s(.+)$'
|
||||
match = re.search(pattern, content, re.MULTILINE)
|
||||
if match:
|
||||
if match.group(1) != 'Article':
|
||||
return match.group(1)
|
||||
else:
|
||||
pattern = r'^\*\* (.+)$'
|
||||
pattern = r'^\*\*\s(.+)$'
|
||||
match = re.search(pattern, content, re.MULTILINE)
|
||||
if match:
|
||||
return match.group(1)
|
||||
|
@ -420,51 +420,50 @@ def get_first_picture_url(content):
|
|||
return None
|
||||
|
||||
|
||||
def org_to_gmi(org_text: str, output_filename_slug: str) -> str:
|
||||
def org_to_gmi(org_text: 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
|
||||
"""
|
||||
# Nettoyer le contenu org en retirant les lignes commençant par #+post
|
||||
cleaned_content = '\n'.join( line for line in org_text.splitlines() if not line.strip().startswith('#+') )
|
||||
org_content = cleaned_content
|
||||
# print('org to gmi org_content', org_content)
|
||||
# Conversion du texte Org en GMI via Pandoc
|
||||
try:
|
||||
output = pypandoc.convert_text(org_text, 'markdown', format='org')
|
||||
converted_text = pypandoc.convert_text(org_content, 'markdown', format='org')
|
||||
|
||||
output = f"""
|
||||
-------------------
|
||||
|
||||
{converted_text}
|
||||
|
||||
-------------------
|
||||
"""
|
||||
|
||||
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
|
||||
# Exclure le dossier ".." du comptage
|
||||
|
||||
for directory in directories:
|
||||
if directory == "..":
|
||||
continue
|
||||
|
||||
for root, dirs, files in os.walk(directory):
|
||||
total_count += len(files)
|
||||
|
||||
print('files', files)
|
||||
total_count += sum(1 for f in files if f.endswith(('.org', '.md', '.gmi')))
|
||||
continue
|
||||
return total_count
|
||||
|
||||
|
||||
|
@ -478,6 +477,11 @@ def convert_org_to_gemini(org_content):
|
|||
Returns:
|
||||
str: Contenu converti en format gemini
|
||||
"""
|
||||
|
||||
# Nettoyer le contenu org en retirant les lignes commençant par #+post
|
||||
cleaned_content = '\n'.join( line for line in org_content.splitlines() if not line.strip().startswith('#+') )
|
||||
org_content = cleaned_content
|
||||
|
||||
try:
|
||||
# Créer un fichier temporaire avec le contenu org
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.org', encoding='utf-8') as temp_org:
|
||||
|
@ -513,3 +517,33 @@ def convert_org_to_gemini(org_content):
|
|||
except Exception as e:
|
||||
print(f"Erreur lors de la conversion: {str(e)}")
|
||||
raise
|
||||
|
||||
|
||||
def save_gemini_file(blog, article):
|
||||
|
||||
annee = article['annee']
|
||||
slug = article['slug']
|
||||
title = article['title']
|
||||
gemini_content = article['gemini_content']
|
||||
gemini_file_path = article['gemini_file_path']
|
||||
|
||||
print(f"_________ Sauver l'article {slug} en gemini dans {gemini_file_path}")
|
||||
year_gemini = f'gemini-capsules/{blog}/{annee}'
|
||||
file_gemini=f'{year_gemini}/{slug}.gmi'
|
||||
|
||||
print(f"_________ créer le dossier : {year_gemini}")
|
||||
os.makedirs(os.path.dirname(file_gemini), exist_ok=True)
|
||||
gemini_file_path = file_gemini
|
||||
website_title = configs_sites[blog]['BLOG_TITLE']
|
||||
|
||||
# Sauvegarde du contenu GMI dans un fichier
|
||||
try:
|
||||
# Créer le dossier parent s'il n'existe pas
|
||||
os.makedirs(os.path.dirname(gemini_file_path), exist_ok=True)
|
||||
with open(gemini_file_path, 'w', encoding='utf-8') as f:
|
||||
f.write(
|
||||
f'# {website_title} \n ## {title} \n----------------\n {gemini_content} \n ----------------\n ## Liens\n=> index.gmi')
|
||||
print(f"\033[92mFichier GMI sauvegardé avec succès : {gemini_file_path}\033[0m")
|
||||
|
||||
except OSError as e:
|
||||
print(f"\033[92mErreur lors de la sauvegarde du fichier : {e}\033[0m")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue