Fix template rendering issue and improve export functionality

- Fix template rendering by storing rendered HTML in variable before writing
  This resolves subdirectories not appearing in navigation despite being
  correctly detected and included in template variables
- Add missing template variables (paths.work_dir) to gallery config
- Add CGI refresh handler for web-based gallery regeneration
- Improve PDF export functionality with better error handling
- Add ESC key shortcut documentation for exiting selection mode
- Enhanced export manager with proper dependency checking

Fixes issue where new subdirectories were detected but not displayed
in browser navigation due to incomplete template rendering.
This commit is contained in:
Kylian Schmidt
2025-07-20 08:59:10 +02:00
parent 2d98ed0e7a
commit dbd67f6039
5 changed files with 347 additions and 29 deletions
+29 -3
View File
@@ -15,6 +15,8 @@ Features:
import subprocess
import shutil
import os
import sys
from pathlib import Path
from typing import Dict, Any, Optional
from jinja2 import Environment, FileSystemLoader
@@ -114,6 +116,7 @@ def build_gallery(source_dir: Path, web_dir: Path,
current_metadata = merge_metadata(inherited_metadata, folder_metadata)
pdf_files = list(source_dir.glob("*.pdf"))
subdirs = [d for d in source_dir.iterdir() if d.is_dir()]
items = []
@@ -188,16 +191,18 @@ def build_gallery(source_dir: Path, web_dir: Path,
assets_path = "../" * (depth + 1) + "assets"
with output_html.open("w") as f:
f.write(template.render(
rendered_html = template.render(
title=title,
items=items,
subdirs=subdir_names,
relpath=str(relative_path),
paths=CONFIG.paths,
ui=CONFIG.ui,
stats=stats,
folder_metadata=current_metadata,
assets_path=assets_path
))
)
f.write(rendered_html)
print(f"Generated {output_html}")
@@ -262,6 +267,21 @@ def format_file_size(size_bytes: int) -> str:
return f"{size:.1f} {size_names[i]}"
def refresh_gallery_cgi():
"""
CGI handler to refresh the gallery from a web request.
Outputs a minimal HTTP response and triggers gallery regeneration.
"""
import traceback
print("Content-Type: text/plain\n")
try:
main()
print("Gallery refreshed successfully.")
except Exception as e:
print(f"Error refreshing gallery: {e}")
traceback.print_exc(file=sys.stdout)
def main() -> None:
"""
Main entry point for gallery generation.
@@ -356,6 +376,7 @@ def main() -> None:
items=items,
subdirs=[],
relpath=source.name,
paths=CONFIG.paths,
ui=CONFIG.ui,
stats=stats,
folder_metadata={},
@@ -375,7 +396,12 @@ def main() -> None:
f"directory nor a PDF file")
print("Done")
# No copying of this script to CGI location
if __name__ == "__main__":
main()
# If run as CGI, call the CGI handler
if 'GATEWAY_INTERFACE' in os.environ:
refresh_gallery_cgi()
else:
main()