91 lines
4.1 KiB
Python
91 lines
4.1 KiB
Python
import os
|
|
import re
|
|
|
|
def update_all_footers_from_template():
|
|
"""
|
|
Robustna skripta, ki vsebino iz 'layouts/footer.html' prebere in z njo
|
|
prepiše VSE <footer>...</footer> sekcije v vseh ostalih .html datotekah v projektu.
|
|
Eksplicitno izključi datoteki header.html in footer.html.
|
|
"""
|
|
project_root = '.'
|
|
footer_template_path = os.path.join(project_root, 'layouts', 'footer.html')
|
|
|
|
# --- KORAK 1: Preberi vsebino nove noge iz datoteke s predlogo ---
|
|
try:
|
|
with open(footer_template_path, 'r', encoding='utf-8') as f:
|
|
new_footer_content = f.read().strip()
|
|
print(f"Uspešno prebrana vsebina noge iz: {footer_template_path}")
|
|
except FileNotFoundError:
|
|
print(f"NAPAKA: Datoteka s predlogo noge ni bila najdena na poti: {footer_template_path}")
|
|
print("Prepričajte se, da datoteka obstaja in da skripto zaganjate iz korenske mape projekta ('hermina-merc').")
|
|
return
|
|
except Exception as e:
|
|
print(f"NAPAKA pri branju datoteke s predlogo noge: {e}")
|
|
return
|
|
|
|
# --- KORAK 2: Pripravi seznam datotek za izključitev ---
|
|
# Uporabimo os.path.normpath za zanesljivo primerjavo poti ne glede na OS (npr. / vs \)
|
|
excluded_files = [
|
|
os.path.normpath(footer_template_path),
|
|
os.path.normpath(os.path.join(project_root, 'layouts', 'header.html'))
|
|
]
|
|
print(f"\nDatoteki, ki bosta preskočeni (ker sta predlogi): {excluded_files}")
|
|
|
|
# --- KORAK 3: Pripravi robusten regularni izraz ---
|
|
# Ta vzorec bo našel katerokoli <footer> oznako, ne glede na atribute, in vso vsebino do </footer>
|
|
footer_pattern = re.compile(r'<footer.*?</footer', re.DOTALL | re.IGNORECASE)
|
|
|
|
# --- KORAK 4: Pojdi skozi vse datoteke in posodobi noge ---
|
|
print("\nZačenjam posodabljanje nog v HTML datotekah...")
|
|
|
|
updated_files_count = 0
|
|
skipped_files_count = 0
|
|
|
|
for root, dirs, files in os.walk(project_root):
|
|
# Izključimo mape, ki jih ne želimo pregledovati
|
|
dirs[:] = [d for d in dirs if d not in ['.git', '.vscode', '__pycache__']]
|
|
|
|
for filename in files:
|
|
if filename.endswith(".html"):
|
|
filepath = os.path.normpath(os.path.join(root, filename))
|
|
|
|
# PREVERI, ALI JE DATOTEKA NA SEZNAMU ZA IZKLJUČITEV
|
|
if filepath in excluded_files:
|
|
continue # Tiho preskoči predloge
|
|
|
|
try:
|
|
with open(filepath, 'r', encoding='utf-8') as f:
|
|
content = f.read()
|
|
|
|
original_content = content
|
|
|
|
# Zamenjaj obstoječo nogo z novo
|
|
updated_content, num_replacements = footer_pattern.subn(new_footer_content, content)
|
|
|
|
if num_replacements > 0:
|
|
# Preverimo, ali je dejansko prišlo do spremembe v vsebini
|
|
if updated_content != content:
|
|
with open(filepath, 'w', encoding='utf-8') as f:
|
|
f.write(updated_content)
|
|
|
|
print(f" [OK] Uspešno posodobljena noga v: {filepath}")
|
|
updated_files_count += 1
|
|
else:
|
|
print(f" [INFO] Noga najdena, vendar je že posodobljena v: {filepath}")
|
|
skipped_files_count += 1
|
|
else:
|
|
print(f" [POZOR] Noga za zamenjavo ni bila najdena v (preskočeno): {filepath}")
|
|
skipped_files_count += 1
|
|
|
|
except Exception as e:
|
|
print(f" [NAPAKA] Pri obdelavi datoteke {filepath}: {e}")
|
|
|
|
print("\n-------------------------------------------")
|
|
print("Posodabljanje je končano.")
|
|
print(f"Dejansko posodobljenih datotek: {updated_files_count}")
|
|
print(f"Preskočenih, že posodobljenih ali datotek brez noge: {skipped_files_count}")
|
|
print("-------------------------------------------")
|
|
|
|
# Zaženemo glavno funkcijo
|
|
if __name__ == "__main__":
|
|
update_all_footers_from_template() |