Spaces:
Runtime error
Runtime error
File size: 6,520 Bytes
ee84cd3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
import os
import subprocess
import re
import shutil
from datetime import datetime
from dotenv import load_dotenv
class MindMapService:
def __init__(self):
load_dotenv()
self.project_name = os.getenv('PROJECT_NAME', 'Project')
self.templates_directory = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "templates")
self.markdowns_directory = os.path.join(os.path.dirname(
os.path.abspath(__file__)), "markdowns")
self.css_path = 'static/css/styles.css'
def convert_markdown_to_html(self):
try:
result = subprocess.run(['node', 'convert_markdown_to_html.js'],
check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(result.stdout.decode())
except subprocess.CalledProcessError as e:
print(f"Error converting markdown to HTML: {e.stderr.decode()}")
def style_htmls(self):
css_link = f'<link rel="stylesheet" type="text/css" href="/{self.css_path}">'
# Define el directorio de respaldo con marca de tiempo
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
"""
backup_directory = os.path.join(
self.templates_directory, f"backup_{timestamp}")
# Crea el directorio de respaldo si no existe
if not os.path.exists(backup_directory):
os.makedirs(backup_directory)
"""
# Copia los archivos originales al directorio de respaldo
files = [f for f in os.listdir(
self.templates_directory) if f.endswith('.html')]
"""
for file in files:
shutil.copy2(os.path.join(
self.templates_directory, file), backup_directory)
"""
# Itera sobre cada archivo y realiza la sustitución
for file in files:
file_path = os.path.join(self.templates_directory, file)
# Lee el contenido del archivo
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Agrega el enlace al CSS si no existe
if css_link not in content:
if '<head>' in content:
new_content = content.replace(
'<head>', f'<head>\n {css_link}')
elif '<head ' in content: # Manejo de variaciones de la etiqueta <head>
new_content = re.sub(
r'(<head\s*.*?>)', r'\1\n ' + css_link, content, flags=re.IGNORECASE)
else:
new_content = re.sub(r'(<html\s*.*?>)', r'\1\n<head>\n ' +
css_link + '\n</head>', content, flags=re.IGNORECASE)
else:
new_content = content
# Inserta el logo al final del body
if '<img id="logo" src="static/assets/logo.png"' not in new_content:
new_content = new_content.replace(
"</body>", '<img id="logo" src="/static/assets/logo.png" alt="Logo">\n</body>')
# Guarda el contenido modificado en el archivo
with open(file_path, 'w', encoding='utf-8') as f:
f.write(new_content)
print(
f"Enlace al CSS actualizado y logo añadido en todos los archivos HTML. Respaldo creado en el directorio 'backup_{timestamp}'.")
def generate_menu(self):
# Define el template del sidebar con comillas consistentes
sidebar_template = """
<div id="sidebar">
<h2>{0}</h2>
{1}
</div>
"""
# Define el directorio de respaldo con marca de tiempo
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
backup_directory = os.path.join(
self.templates_directory, f"backup_{timestamp}")
# Crea el directorio de respaldo si no existe
"""
if not os.path.exists(backup_directory):
os.makedirs(backup_directory)
"""
# Copia los archivos originales al directorio de respaldo
files = [f for f in os.listdir(
self.templates_directory) if f.endswith('.html')]
"""
for file in files:
shutil.copy2(os.path.join(
self.templates_directory, file), backup_directory)
"""
# Construye el sidebar con los enlaces a todos los archivos HTML
sidebar_links = ""
for file in files:
file_path = os.path.join(self.templates_directory, file)
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
match = re.search(r"<title>(.*?)</title>", content)
if match:
title = match.group(1)
else:
# Usa el nombre del archivo si no se encuentra <title>
title = os.path.splitext(file)[0]
# Ajusta la ruta para Flask
sidebar_links += f'<a href="/templates/{file}">{title}</a>\n'
# Itera sobre cada archivo y realiza la modificación del título y la inserción del sidebar
for file in files:
file_path = os.path.join(self.templates_directory, file)
# Lee el contenido del archivo
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Define el nuevo contenido con el sidebar
new_sidebar_links = re.sub(
f'<a href="/templates/{file}"', f'<a href="/templates/{file}" class="active"', sidebar_links)
sidebar = sidebar_template.format(
self.project_name, new_sidebar_links)
# Reemplaza o inserta el sidebar de manera robusta
if '<div id="sidebar">' in content:
new_content = re.sub(
r'<div id="sidebar">.*?</div>', sidebar, content, flags=re.DOTALL)
else:
new_content = re.sub(
r'(<body.*?>)', r'\1\n' + sidebar, content, flags=re.IGNORECASE)
# Guarda el contenido modificado en el archivo
with open(file_path, 'w', encoding='utf-8') as f:
f.write(new_content)
print(
f"Título y menú actualizados en todos los archivos HTML. Respaldo creado en el directorio 'backup_{timestamp}'.")
# Ejemplo de uso
if __name__ == '__main__':
service = MindMapService()
service.convert_markdown_to_html()
service.style_htmls()
service.generate_menu()
|