Browse Source

Generate basic galleries

master
Marien Fressinaud 5 years ago
parent
commit
1f001efecc
4 changed files with 88 additions and 11 deletions
  1. 16
    0
      Herisson/gallery.html.j2
  2. 8
    5
      Herisson/index.html.j2
  3. 4
    0
      Herisson/style/main.css
  4. 60
    6
      boop.py

+ 16
- 0
Herisson/gallery.html.j2 View File

@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0" />
<link rel="stylesheet" href="style/main.css">
<title>{{ gallery.name }} · Boop</title>
</head>
<body>
<h1>{{ gallery.name }}</h1>
{% for photo in gallery.photos %}
<img src="{{ photo.url }}" alt="" />
<p>{{ photo.name }}</p>
{% endfor %}
</body>
</html>

+ 8
- 5
Herisson/index.html.j2 View File

@@ -7,10 +7,13 @@
<title>Boop</title>
</head>
<body>
<ul>
{% for gallery in galleries -%}
<li>{{ gallery.name }}</li>
{% endfor %}
</ul>
{% for gallery in galleries -%}
<div class="gallery">
<a href="{{ gallery.url }}.html">
<img src="{{ gallery.cover_photo.url }}" alt="" />
{{ gallery.name }}
</a>
</div>
{% endfor %}
</body>
</html>

+ 4
- 0
Herisson/style/main.css View File

@@ -3,3 +3,7 @@ body {

background-color: #222;
}

a {
color: #fff;
}

+ 60
- 6
boop.py View File

@@ -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__":

Loading…
Cancel
Save