|
|
@@ -2,6 +2,7 @@ |
|
|
|
|
|
|
|
import os |
|
|
|
import shutil |
|
|
|
import re |
|
|
|
|
|
|
|
from jinja2 import Environment, PackageLoader, select_autoescape |
|
|
|
|
|
|
@@ -13,7 +14,31 @@ OUTPUT_DIR_NAME = "output" |
|
|
|
|
|
|
|
|
|
|
|
def list_galleries_in(path): |
|
|
|
return [f for f in os.scandir(path) if f.is_dir()] |
|
|
|
gallery_dirs = [f for f in os.scandir(path) if f.is_dir()] |
|
|
|
for gallery_dir in gallery_dirs: |
|
|
|
photos = list(list_photos_in(gallery_dir)) |
|
|
|
if len(photos) == 0: |
|
|
|
continue |
|
|
|
|
|
|
|
gallery = { |
|
|
|
"name": gallery_dir.name, |
|
|
|
"path": gallery_dir.path, |
|
|
|
"url": gallery_dir.name, |
|
|
|
"num_photos": len(photos), |
|
|
|
"photos": photos, |
|
|
|
"cover_photo": photos[0], |
|
|
|
} |
|
|
|
yield gallery |
|
|
|
|
|
|
|
|
|
|
|
def list_photos_in(gallery_dir): |
|
|
|
photo_files = [ |
|
|
|
f for f in os.scandir(gallery_dir) if re.match(".+\.jpg", f.name, re.I) |
|
|
|
] |
|
|
|
for photo_file in photo_files: |
|
|
|
url = os.path.join(gallery_dir.name, photo_file.name) |
|
|
|
photo = {"name": photo_file.name, "url": url} |
|
|
|
yield photo |
|
|
|
|
|
|
|
|
|
|
|
def generate_output_dir(): |
|
|
@@ -43,14 +68,43 @@ def generate_index(output_path, galleries): |
|
|
|
index_file.write(index_template.render(galleries=galleries)) |
|
|
|
|
|
|
|
|
|
|
|
def generate_gallery(output_path, gallery): |
|
|
|
generate_gallery_index(output_path, gallery) |
|
|
|
generate_gallery_dir(output_path, gallery) |
|
|
|
|
|
|
|
|
|
|
|
def generate_gallery_index(output_path, gallery): |
|
|
|
gallery_index_path = os.path.join(output_path, f"{gallery['name']}.html") |
|
|
|
|
|
|
|
theme_path = os.path.join(os.curdir, THEME) |
|
|
|
jinja_env = Environment( |
|
|
|
loader=PackageLoader("boop", theme_path), autoescape=select_autoescape(["html"]) |
|
|
|
) |
|
|
|
gallery_template = jinja_env.get_template("gallery.html.j2") |
|
|
|
|
|
|
|
with open(gallery_index_path, "w") as gallery_file: |
|
|
|
gallery_file.write(gallery_template.render(gallery=gallery)) |
|
|
|
|
|
|
|
|
|
|
|
def generate_gallery_dir(output_path, gallery): |
|
|
|
gallery_output_path = os.path.join(output_path, gallery["name"]) |
|
|
|
if os.path.isdir(gallery_output_path): |
|
|
|
os.mkdir(gallery_output_path) |
|
|
|
shutil.copytree(gallery["path"], gallery_output_path) |
|
|
|
|
|
|
|
|
|
|
|
def main(): |
|
|
|
pictures_folder = os.path.join(os.curdir, PICTURES_DIR_NAME) |
|
|
|
galleries = list_galleries_in(pictures_folder) |
|
|
|
galleries = list(list_galleries_in(pictures_folder)) |
|
|
|
|
|
|
|
if len(galleries) == 0: |
|
|
|
return |
|
|
|
|
|
|
|
if len(galleries) > 0: |
|
|
|
output_path = generate_output_dir() |
|
|
|
generate_style(output_path) |
|
|
|
generate_index(output_path, galleries) |
|
|
|
output_path = generate_output_dir() |
|
|
|
generate_style(output_path) |
|
|
|
generate_index(output_path, galleries) |
|
|
|
for gallery in galleries: |
|
|
|
generate_gallery(output_path, gallery) |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |