mirror of
https://forge.chapril.org/tykayn/orgmode-to-gemini-blog
synced 2025-06-20 09:04:42 +02:00
refacto
This commit is contained in:
parent
9267636687
commit
086287fad3
48 changed files with 536 additions and 1681 deletions
109
enrich_html.py
109
enrich_html.py
|
@ -1,36 +1,34 @@
|
|||
#!/bin/python3
|
||||
import os
|
||||
import argparse
|
||||
import os
|
||||
import re
|
||||
|
||||
from website_config import configs_sites
|
||||
|
||||
parser = argparse.ArgumentParser(description="Générer un site Web à partir de fichiers HTML.")
|
||||
parser.add_argument("blog_name", help="Le chemin vers le dossier contenant les fichiers HTML.")
|
||||
parser.add_argument("--title", "-t", default="Mon site Web", help="Le titre du site Web.")
|
||||
|
||||
parser.add_argument("--style", default="templates/style_general.css", help="Le chemin vers le fichier de style CSS.")
|
||||
args = parser.parse_args()
|
||||
|
||||
# Style CSS minimaliste
|
||||
style_file = args.style
|
||||
|
||||
blog_name = args.blog_name.replace('html-websites/','')
|
||||
blog_name = args.blog_name.replace('html-websites/', '')
|
||||
source_blog = f"sources/{blog_name}"
|
||||
header_content_path = f"{source_blog}/templates/converted/header_page.html"
|
||||
footer_content_path = f"{source_blog}/templates/converted/footer_page.html"
|
||||
static_page_path = f"{source_blog}/templates/html/static.html"
|
||||
|
||||
|
||||
print('---------- blog name ', blog_name)
|
||||
template_content = configs_sites[blog_name]
|
||||
template_content = configs_sites[blog_name]
|
||||
|
||||
footer_content=''
|
||||
after_article=''
|
||||
# TODO add footer on every article
|
||||
# with open(footer_content_path, "r") as f:
|
||||
# footer_content = f.read()
|
||||
after_article = ''
|
||||
|
||||
# TODO make these variables overrided by configuration of a source website
|
||||
# variables du template de page
|
||||
inline_the_css = False
|
||||
|
||||
|
||||
# inline_the_css=True
|
||||
|
||||
def extract_body_content(html_content):
|
||||
pattern = r'<body[^>]*?>(.*?)</body>'
|
||||
|
@ -40,41 +38,26 @@ def extract_body_content(html_content):
|
|||
else:
|
||||
return None
|
||||
|
||||
|
||||
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 enrich_one_file(file, root_path):
|
||||
|
||||
|
||||
print(' ----------- enrich_html: file:',os.path.join(root_path, file))
|
||||
css_content = ""
|
||||
|
||||
inline_the_css=False
|
||||
# inline_the_css=True
|
||||
|
||||
print(' ----------- enrich_html: CSS inline: ',inline_the_css)
|
||||
# Trouver le fichier entête
|
||||
header_content=''
|
||||
with open(os.path.join(root_path, file), "r") as f:
|
||||
header_content = f.read()
|
||||
# Ouvrir le fichier HTML en mode lecture
|
||||
with open(os.path.join(root_path, file), "r") as f:
|
||||
html_content = f.read()
|
||||
|
||||
def enrich_one_file(html_content, partials={"header_page": "", "footer_content": ""}):
|
||||
# remove some parts
|
||||
html_content = remove_properties_section(html_content)
|
||||
html_content = remove_article_head_properties_orgmode(html_content)
|
||||
|
@ -126,13 +109,15 @@ def enrich_one_file(file, root_path):
|
|||
</a>
|
||||
<h1 class="blog-title">{template_content['BLOG_TITLE']}</h1>
|
||||
<p class="blog-subtitle">{template_content['BLOG_SUBTITLE']}</p>
|
||||
<div class="template-header">
|
||||
|
||||
</div>
|
||||
</div>
|
||||
<nav class="navbar is-fixed-top is-dark" role="navigation" aria-label="main navigation">
|
||||
<div class="navbar-brand">
|
||||
<a class="navbar-item" href="{template_content['NDD']}">
|
||||
|
||||
Accueil
|
||||
</a>
|
||||
|
||||
</div>
|
||||
|
||||
<div id="navbarBasicExample" class="navbar-menu">
|
||||
|
@ -168,11 +153,12 @@ def enrich_one_file(file, root_path):
|
|||
<footer class="site-footer has-top-divider">
|
||||
<div class="container">
|
||||
<div class="site-footer-inner">
|
||||
|
||||
|
||||
|
||||
{template_content['NAVIGATION']}
|
||||
|
||||
<div class="site-foot">
|
||||
|
||||
</div>
|
||||
<nav class="footer-nav">
|
||||
{template_content['NAVIGATION']}
|
||||
</nav>
|
||||
</div>
|
||||
</div>
|
||||
</footer>
|
||||
|
@ -182,17 +168,46 @@ def enrich_one_file(file, root_path):
|
|||
</html>
|
||||
|
||||
"""
|
||||
# {partials['footer_page']}
|
||||
# {partials['header_page']}
|
||||
return html_content
|
||||
|
||||
|
||||
def ouvrir_fichier(chemin_fichier):
|
||||
if os.path.exists(chemin_fichier):
|
||||
with open(chemin_fichier, 'r') as fichier:
|
||||
# Faire quelque chose avec le fichier ouvert
|
||||
contenu = fichier.read()
|
||||
return contenu
|
||||
else:
|
||||
raise FileNotFoundError(f"Le fichier {chemin_fichier} n'existe pas.")
|
||||
|
||||
html_path_enriched=os.path.join(root_path, file)
|
||||
print(' ----------- enrich_html: html_path_enriched ============> ',html_path_enriched)
|
||||
# Écrire le contenu modifié dans le fichier HTML
|
||||
with open(html_path_enriched, "w") as f:
|
||||
f.write(html_content)
|
||||
print('\n ----------- enrich_html: html écrit ', html_path_enriched)
|
||||
|
||||
# Parcourir tous les fichiers HTML dans le dossier
|
||||
for root, _, files in os.walk(blog_name):
|
||||
# print(files)
|
||||
for root_path, _, files in os.walk(blog_name):
|
||||
# Prendre les templates partiaux pour chaque site web
|
||||
partials = {
|
||||
"header_content": "",
|
||||
"footer_content": "",
|
||||
}
|
||||
|
||||
partials["header_content"] = ouvrir_fichier(os.path.join(root_path, 'templates', 'header_page.org'))
|
||||
partials["footer_content"] = ouvrir_fichier(os.path.join(root_path, 'templates', 'footer_page.org'))
|
||||
|
||||
for file in files:
|
||||
if file.endswith(".html"):
|
||||
enrich_one_file(file, root)
|
||||
print(' ----------- enrich_html: file:', os.path.join(root_path, file))
|
||||
|
||||
print(' ----------- enrich_html: CSS inline: ', inline_the_css)
|
||||
|
||||
# Ouvrir le fichier HTML en mode lecture
|
||||
with open(os.path.join(root_path, file), "r") as f:
|
||||
html_content = f.read()
|
||||
|
||||
html_content = enrich_one_file(html_content, partials)
|
||||
html_path_enriched = os.path.join(root_path, file)
|
||||
print(' ----------- enrich_html: html_path_enriched ============> ', html_path_enriched)
|
||||
# Écrire le contenu modifié dans le fichier HTML
|
||||
with open(html_path_enriched, "w") as f:
|
||||
f.write(html_content)
|
||||
print('\n ----------- enrich_html: html écrit ', html_path_enriched)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue