-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgallery_generator.py
144 lines (108 loc) · 4.88 KB
/
gallery_generator.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Point to a folder. [$FOLDER_NAME]
# Find all the images [.PNG, .JPG, .JPEG] and copy them to images/galleries/[$FOLDER_NAME]
# If not already present, create a new "page" in the _pages folder with the same name as the foldername
# layout: gallery
# title: $FOLDER_NAME
# permalink: "/$FOLDER_NAME/"
# Create a new file called $FOLDER_NAME.yml in_galleries:
# ---
# name: "illustration"
# arts:
# - image: "images/path/to/your/images.jpg"
# order: "1"
# title: ""
# artist: ""
# description: ""
# argument = folder path
import os
import glob
import shutil
from handle_files import copy_images, EXTENSIONS
def main(jekyll_site_path, images_path, category):
if not os.path.exists(jekyll_site_path):
print(f"Jekyll site path {jekyll_site_path} does not exist :(")
return False
if not os.path.exists(images_path):
print(f"Images path {images_path} does not exist :(")
return False
# Get gallery name from end of images_path
gallery_name = get_gallery_name(images_path)
# Create page .md file for gallery
create_page(jekyll_site_path, gallery_name, category)
# Create gallery folder
gallery_path = create_gallery(jekyll_site_path, gallery_name)
# Copy images to gallery
# TODO: Add pillow image resize in here.
copy_images(images_path, gallery_path)
# Populate gallery .yml with images in gallery_path
populate_gallery(jekyll_site_path, gallery_name, gallery_path)
def create_page(jekyll_site_path, gallery_name, category):
page_path = os.path.join(jekyll_site_path, '_pages')
if not os.path.exists(page_path):
print(f"Page path {page_path} does not exist :(")
return False
# make new file in ./_pages/$FOLDER_NAME.md
f = open(os.path.join(page_path, gallery_name + '.md') ,"w+")
f.write("---\n")
f.write('layout: gallery\n')
f.write('title: ' + gallery_name + '\n')
f.write('permalink: "' + '/' + category + '/' + gallery_name + '/"' + '\n')
f.write("---\n")
f.close()
print(f"Created page {page_path}")
def create_gallery(jekyll_site_path, gallery_name):
"""Create a gallery, and return the gallery path to add images to it.
Args:
jekyll_site_path (str): Root path to the jekyll site.
gallery_name(str): Name of the gallery.
Returns:
str: Full path to created gallery
"""
gallery_path = os.path.join(jekyll_site_path, 'images', 'galleries', gallery_name)
if not os.path.exists(gallery_path):
os.makedirs(gallery_path)
print(f"Created gallery path {gallery_path}")
return gallery_path
def populate_gallery(jekyll_site_path, gallery_name, gallery_path):
image_files = []
for extension in EXTENSIONS:
image_files.extend(glob.glob(os.path.join(gallery_path, '*' + extension), recursive=False))
# make new file in ./_galleries/$FOLDER_NAME.yml
f = open(os.path.join(jekyll_site_path, '_galleries', gallery_name + '.yml') ,"w+")
# contents:
f.write('---\n')
f.write(f'name: "{gallery_name}"\n')
f.write('arts:\n')
artist = 'Sander Lootus'
description = ''
for i in range(len(image_files)):
image_file = image_files[i]
image_number = i + 1
image_title = os.path.basename(image_file).split('.')[0]
# Jekyll wants relative paths
image_relative_path = image_file.split(jekyll_site_path + "\\")[1]
# Jekyll wants UNIX style paths.
image_relative_path = image_relative_path.replace(os.sep, '/')
f.write(f'- image: "{image_relative_path}"\n')
f.write(f' order: "{image_number}"\n')
f.write(f' title: "{image_title}"\n')
# f.write(f' artist: "{artist}"\n')
# f.write(f' description: "{description}"\n\n')
print(f' Wrote image {image_relative_path} with title {image_title} and order {image_number}')
f.write('---\n')
f.close()
def get_gallery_name(images_path):
gallery_name = os.path.basename(os.path.normpath(images_path)).lower()
print(f"Got gallery name {gallery_name}")
return gallery_name
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Automatically create galleries for a jekyll site')
parser.add_argument('--jekyll-site-path', metavar='path', required=True,
help='the path to the jekyll site')
parser.add_argument('--images-path', metavar='path', required=True,
help='path to folder of new images')
parser.add_argument('--category', metavar='path', required=True,
help='Category of new images')
args = parser.parse_args()
main(jekyll_site_path=args.jekyll_site_path, images_path=args.images_path, category=args.category)