-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
53 lines (44 loc) · 1.77 KB
/
Copy pathmain.py
File metadata and controls
53 lines (44 loc) · 1.77 KB
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
# main.py
import os
import re # Import the regular expression module
def define_env(env):
"Hook function"
def list_images_in_dir(full_path, directory):
"""Helper function to list images with natural sorting."""
def natural_sort_key(filename):
"""
A key for sorting strings in a 'natural' order.
e.g., 'Slide2.jpg' comes before 'Slide10.jpg'
"""
# Find all sequences of digits in the filename
parts = re.split(r'(\d+)', filename)
# Convert the digit parts to integers for proper numeric sorting
parts[1::2] = map(int, parts[1::2])
return parts
image_files = []
if os.path.exists(full_path):
# Use the new sorting key when sorting the directory listing
for filename in sorted(os.listdir(full_path), key=natural_sort_key):
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):
relative_path = os.path.join(directory, filename).replace("\\", "/")
image_files.append(relative_path)
return image_files
@env.macro
def slideshow_for_page():
"""
Generates a list of images for a slideshow based on the page's
frontmatter variable 'slides_folder'.
"""
page = env.variables.page
folder_name = page.meta.get('slides_folder')
if not folder_name:
return []
directory = f"assets/slides/{folder_name}"
docs_dir = 'docs'
full_path = os.path.join(docs_dir, directory)
# Get the base_url from config (includes /winter-school/)
base_url = env.conf.get('site_url', '').rstrip('/')
site_path = base_url.split('/')[-1] if base_url else ''
# Return paths with the site prefix
images = list_images_in_dir(full_path, directory)
return [f"/{site_path}/{img}" if site_path else f"/{img}" for img in images]