def render_gallery_index(
self, template_name, output_name, context, index_dst_path, img_name_list, thumbs, file_dep, kw
):
"""Build the gallery index."""
# The photo array needs to be created here, because
# it relies on thumbnails already being created on
# output
photo_array = []
d_name = os.path.dirname(output_name)
for name, thumb_name in zip(img_name_list, thumbs):
im = Image.open(os.path.join(d_name, thumb_name))
w, h = im.size
title = ""
if kw["use_filename_as_title"]:
title = utils.unslugify(os.path.splitext(name)[0])
photo_array.append({"url": name, "url_thumb": thumb_name, "title": title, "size": {"w": w, "h": h}})
context["photo_array_json"] = json.dumps(photo_array)
context["photo_array"] = photo_array
if os.path.exists(index_dst_path):
with codecs.open(index_dst_path, "rb", "utf8") as fd:
context["text"] = fd.read()
file_dep.append(index_dst_path)
else:
context["text"] = ""
self.site.render_template(template_name, output_name, context)
def render_gallery_index(self, template_name, output_name, context, img_list, thumbs, file_dep):
"""Build the gallery index."""
# The photo array needs to be created here, because
# it relies on thumbnails already being created on
# output
def url_from_path(p):
url = "/".join(os.path.relpath(p, os.path.dirname(output_name) + os.sep).split(os.sep))
return url
photo_array = []
for img, thumb in zip(img_list, thumbs):
im = Image.open(thumb)
w, h = im.size
title = ""
if self.kw["use_filename_as_title"]:
title = utils.unslugify(os.path.splitext(img)[0])
# Thumbs are files in output, we need URLs
photo_array.append(
{"url": url_from_path(img), "url_thumb": url_from_path(thumb), "title": title, "size": {"w": w, "h": h}}
)
context["photo_array_json"] = json.dumps(photo_array)
context["photo_array"] = photo_array
self.site.render_template(template_name, output_name, context)
def render_gallery_index(
self,
template_name,
output_name,
context,
index_dst_path,
img_name_list,
thumbs,
file_dep,
kw):
"""Build the gallery index."""
# The photo array needs to be created here, because
# it relies on thumbnails already being created on
# output
photo_array = []
d_name = os.path.dirname(output_name)
for name, thumb_name in zip(img_name_list, thumbs):
im = Image.open(os.path.join(d_name, thumb_name))
w, h = im.size
title = ''
if kw['use_filename_as_title']:
title = utils.unslugify(os.path.splitext(name)[0])
photo_array.append({
'url': name,
'url_thumb': thumb_name,
'title': title,
'size': {
'w': w,
'h': h
},
})
context['photo_array_json'] = json.dumps(photo_array)
context['photo_array'] = photo_array
if os.path.exists(index_dst_path):
with codecs.open(index_dst_path, "rb", "utf8") as fd:
context['text'] = fd.read()
file_dep.append(index_dst_path)
else:
context['text'] = ''
self.site.render_template(template_name, output_name, context)
def extract_filename(self, filename: str, lang: str) -> dict:
"""Try to read the metadata from the filename based on the given re.
This requires to use symbolic group names in the pattern.
The part to read the metadata from the filename based on a regular
expression is taken from Pelican - pelican/readers.py
"""
match = re.match(self.site.config['FILE_METADATA_REGEXP'], filename)
meta = {}
if match:
for key, value in match.groupdict().items():
k = key.lower().strip() # metadata must be lowercase
if k == 'title' and self.site.config['FILE_METADATA_UNSLUGIFY_TITLES']:
meta[k] = unslugify(value, lang, discard_numbers=False)
else:
meta[k] = value
return meta
def render_gallery_index(
self,
template_name,
output_name,
context,
img_list,
thumbs,
file_dep):
"""Build the gallery index."""
# The photo array needs to be created here, because
# it relies on thumbnails already being created on
# output
def url_from_path(p):
url = '/'.join(os.path.relpath(p, os.path.dirname(output_name) + os.sep).split(os.sep))
return url
photo_array = []
for img, thumb in zip(img_list, thumbs):
im = Image.open(thumb)
w, h = im.size
title = ''
if self.kw['use_filename_as_title']:
title = utils.unslugify(os.path.splitext(img)[0])
# Thumbs are files in output, we need URLs
photo_array.append({
'url': url_from_path(img),
'url_thumb': url_from_path(thumb),
'title': title,
'size': {
'w': w,
'h': h
},
})
context['photo_array_json'] = json.dumps(photo_array)
context['photo_array'] = photo_array
self.site.render_template(template_name, output_name, context)
def gen_tasks(self):
"""Render image galleries."""
if Image is None:
req_missing(['pillow'], 'render galleries')
self.logger = utils.get_logger('render_galleries', self.site.loghandlers)
self.image_ext_list = ['.jpg', '.png', '.jpeg', '.gif', '.svg', '.bmp', '.tiff']
self.image_ext_list.extend(self.site.config.get('EXTRA_IMAGE_EXTENSIONS', []))
self.kw = {
'thumbnail_size': self.site.config['THUMBNAIL_SIZE'],
'max_image_size': self.site.config['MAX_IMAGE_SIZE'],
'output_folder': self.site.config['OUTPUT_FOLDER'],
'cache_folder': self.site.config['CACHE_FOLDER'],
'default_lang': self.site.config['DEFAULT_LANG'],
'use_filename_as_title': self.site.config['USE_FILENAME_AS_TITLE'],
'gallery_path': self.site.config['GALLERY_PATH'],
'sort_by_date': self.site.config['GALLERY_SORT_BY_DATE'],
'filters': self.site.config['FILTERS'],
'translations': self.site.config['TRANSLATIONS'],
'global_context': self.site.GLOBAL_CONTEXT,
"feed_length": self.site.config['FEED_LENGTH'],
}
yield self.group_task()
template_name = "gallery.tmpl"
# Find all galleries we need to process
self.find_galleries()
# Create all output folders
for task in self.create_galleries():
yield task
# For each gallery:
for gallery in self.gallery_list:
# Create subfolder list
folder_list = [(x, x.split(os.sep)[-2]) for x in
glob.glob(os.path.join(gallery, '*') + os.sep)]
# Parse index into a post (with translations)
post = self.parse_index(gallery)
# Create image list, filter exclusions
image_list = self.get_image_list(gallery)
# Sort as needed
# Sort by date
if self.kw['sort_by_date']:
image_list.sort(key=lambda a: self.image_date(a))
else: # Sort by name
image_list.sort()
# Create thumbnails and large images in destination
for image in image_list:
for task in self.create_target_images(image):
yield task
# Remove excluded images
for image in self.get_excluded_images(gallery):
for task in self.remove_excluded_image(image):
yield task
crumbs = utils.get_crumbs(gallery, index_folder=self)
# Create index.html for each language
for lang in self.kw['translations']:
dst = os.path.join(
self.kw['output_folder'],
self.site.path(
"gallery",
os.path.relpath(gallery, self.kw['gallery_path']), lang))
dst = os.path.normpath(dst)
context = {}
context["lang"] = lang
if post:
context["title"] = post.title(lang)
else:
context["title"] = os.path.basename(gallery)
context["description"] = None
image_name_list = [os.path.basename(p) for p in image_list]
if self.kw['use_filename_as_title']:
img_titles = []
for fn in image_name_list:
name_without_ext = os.path.splitext(fn)[0]
img_titles.append(
'id="{0}" alt="{1}" title="{2}"'.format(
name_without_ext,
name_without_ext,
utils.unslugify(name_without_ext)))
else:
img_titles = [''] * len(image_name_list)
thumbs = ['.thumbnail'.join(os.path.splitext(p)) for p in image_list]
#.........这里部分代码省略.........
def gen_tasks(self):
"""Render image galleries."""
self.image_ext_list = self.image_ext_list_builtin
self.image_ext_list.extend(self.site.config.get('EXTRA_IMAGE_EXTENSIONS', []))
for k, v in self.site.GLOBAL_CONTEXT['template_hooks'].items():
self.kw['||template_hooks|{0}||'.format(k)] = v._items
yield self.group_task()
template_name = "gallery.tmpl"
# Create all output folders
for task in self.create_galleries():
yield task
# For each gallery:
for gallery, input_folder, output_folder in self.gallery_list:
# Create subfolder list
folder_list = [(x, x.split(os.sep)[-2]) for x in
glob.glob(os.path.join(gallery, '*') + os.sep)]
# Parse index into a post (with translations)
post = self.parse_index(gallery, input_folder, output_folder)
# Create image list, filter exclusions
image_list = self.get_image_list(gallery)
# Create thumbnails and large images in destination
for image in image_list:
for task in self.create_target_images(image, input_folder):
yield task
# Remove excluded images
for image in self.get_excluded_images(gallery):
for task in self.remove_excluded_image(image, input_folder):
yield task
for lang in self.kw['translations']:
# save navigation links as dependencies
self.kw['navigation_links|{0}'.format(lang)] = self.kw['global_context']['navigation_links'](lang)
# Create index.html for each language
for lang in self.kw['translations']:
dst = os.path.join(
self.kw['output_folder'],
self.site.path("gallery", gallery, lang))
dst = os.path.normpath(dst)
for k in self.site._GLOBAL_CONTEXT_TRANSLATABLE:
self.kw[k] = self.site.GLOBAL_CONTEXT[k](lang)
context = {}
context["lang"] = lang
if post:
context["title"] = post.title(lang)
else:
context["title"] = os.path.basename(gallery)
context["description"] = None
image_name_list = [os.path.basename(p) for p in image_list]
if self.kw['use_filename_as_title']:
img_titles = []
for fn in image_name_list:
name_without_ext = os.path.splitext(os.path.basename(fn))[0]
img_titles.append(utils.unslugify(name_without_ext))
else:
img_titles = [''] * len(image_name_list)
thumbs = ['.thumbnail'.join(os.path.splitext(p)) for p in image_list]
thumbs = [os.path.join(self.kw['output_folder'], output_folder, os.path.relpath(t, input_folder)) for t in thumbs]
dst_img_list = [os.path.join(output_folder, os.path.relpath(t, input_folder)) for t in image_list]
dest_img_list = [os.path.join(self.kw['output_folder'], t) for t in dst_img_list]
folders = []
# Generate friendly gallery names
for path, folder in folder_list:
fpost = self.parse_index(path, input_folder, output_folder)
if fpost:
ft = fpost.title(lang) or folder
else:
ft = folder
if not folder.endswith('/'):
folder += '/'
folders.append((folder, ft))
context["folders"] = natsort.natsorted(
folders, alg=natsort.ns.F | natsort.ns.IC)
context["crumbs"] = utils.get_crumbs(gallery, index_folder=self, lang=lang)
context["permalink"] = self.site.link("gallery", gallery, lang)
context["enable_comments"] = self.kw['comments_in_galleries']
context["thumbnail_size"] = self.kw["thumbnail_size"]
context["pagekind"] = ["gallery_front"]
if post:
yield {
#.........这里部分代码省略.........
def gen_tasks(self):
"""Render image galleries."""
self.logger = utils.get_logger("render_galleries", self.site.loghandlers)
self.image_ext_list = [".jpg", ".png", ".jpeg", ".gif", ".svg", ".bmp", ".tiff"]
self.image_ext_list.extend(self.site.config.get("EXTRA_IMAGE_EXTENSIONS", []))
self.kw = {
"thumbnail_size": self.site.config["THUMBNAIL_SIZE"],
"max_image_size": self.site.config["MAX_IMAGE_SIZE"],
"output_folder": self.site.config["OUTPUT_FOLDER"],
"cache_folder": self.site.config["CACHE_FOLDER"],
"default_lang": self.site.config["DEFAULT_LANG"],
"use_filename_as_title": self.site.config["USE_FILENAME_AS_TITLE"],
"gallery_path": self.site.config["GALLERY_PATH"],
"sort_by_date": self.site.config["GALLERY_SORT_BY_DATE"],
"filters": self.site.config["FILTERS"],
"translations": self.site.config["TRANSLATIONS"],
"global_context": self.site.GLOBAL_CONTEXT,
"feed_length": self.site.config["FEED_LENGTH"],
}
yield self.group_task()
template_name = "gallery.tmpl"
# Find all galleries we need to process
self.find_galleries()
# Create all output folders
for task in self.create_galleries():
yield task
# For each gallery:
for gallery in self.gallery_list:
# Create subfolder list
folder_list = [(x, x.split(os.sep)[-2]) for x in glob.glob(os.path.join(gallery, "*") + os.sep)]
# Parse index into a post (with translations)
post = self.parse_index(gallery)
# Create image list, filter exclusions
image_list = self.get_image_list(gallery)
# Sort as needed
# Sort by date
if self.kw["sort_by_date"]:
image_list.sort(key=lambda a: self.image_date(a))
else: # Sort by name
image_list.sort()
# Create thumbnails and large images in destination
for image in image_list:
for task in self.create_target_images(image):
yield task
# Remove excluded images
for image in self.get_excluded_images(gallery):
for task in self.remove_excluded_image(image):
yield task
crumbs = utils.get_crumbs(gallery)
# Create index.html for each language
for lang in self.kw["translations"]:
dst = os.path.join(
self.kw["output_folder"],
self.site.path("gallery", os.path.relpath(gallery, self.kw["gallery_path"]), lang),
)
dst = os.path.normpath(dst)
context = {}
context["lang"] = lang
if post:
context["title"] = post.title(lang)
else:
context["title"] = os.path.basename(gallery)
context["description"] = None
image_name_list = [os.path.basename(p) for p in image_list]
if self.kw["use_filename_as_title"]:
img_titles = []
for fn in image_name_list:
name_without_ext = os.path.splitext(fn)[0]
img_titles.append(
'id="{0}" alt="{1}" title="{2}"'.format(
name_without_ext, name_without_ext, utils.unslugify(name_without_ext)
)
)
else:
img_titles = [""] * len(image_name_list)
thumbs = [".thumbnail".join(os.path.splitext(p)) for p in image_list]
thumbs = [os.path.join(self.kw["output_folder"], t) for t in thumbs]
folders = []
# Generate friendly gallery names
#.........这里部分代码省略.........
def gen_tasks(self):
"""Render image galleries."""
if Image is None:
req_missing(["pillow"], "render galleries")
self.image_ext_list = self.image_ext_list_builtin
self.image_ext_list.extend(self.site.config.get("EXTRA_IMAGE_EXTENSIONS", []))
for k, v in self.site.GLOBAL_CONTEXT["template_hooks"].items():
self.kw["||template_hooks|{0}||".format(k)] = v._items
yield self.group_task()
template_name = "gallery.tmpl"
# Create all output folders
for task in self.create_galleries():
yield task
# For each gallery:
for gallery, input_folder, output_folder in self.gallery_list:
# Create subfolder list
folder_list = [(x, x.split(os.sep)[-2]) for x in glob.glob(os.path.join(gallery, "*") + os.sep)]
# Parse index into a post (with translations)
post = self.parse_index(gallery, input_folder, output_folder)
# Create image list, filter exclusions
image_list = self.get_image_list(gallery)
# Sort as needed
# Sort by date
if self.kw["sort_by_date"]:
image_list.sort(key=lambda a: self.image_date(a))
else: # Sort by name
image_list.sort()
# Create thumbnails and large images in destination
for image in image_list:
for task in self.create_target_images(image, input_folder):
yield task
# Remove excluded images
for image in self.get_excluded_images(gallery):
for task in self.remove_excluded_image(image, input_folder):
yield task
crumbs = utils.get_crumbs(gallery, index_folder=self)
# Create index.html for each language
for lang in self.kw["translations"]:
# save navigation links as dependencies
self.kw["navigation_links|{0}".format(lang)] = self.kw["global_context"]["navigation_links"](lang)
dst = os.path.join(self.kw["output_folder"], self.site.path("gallery", gallery, lang))
dst = os.path.normpath(dst)
for k in self.site._GLOBAL_CONTEXT_TRANSLATABLE:
self.kw[k] = self.site.GLOBAL_CONTEXT[k](lang)
context = {}
context["lang"] = lang
if post:
context["title"] = post.title(lang)
else:
context["title"] = os.path.basename(gallery)
context["description"] = None
image_name_list = [os.path.basename(p) for p in image_list]
if self.kw["use_filename_as_title"]:
img_titles = []
for fn in image_name_list:
name_without_ext = os.path.splitext(os.path.basename(fn))[0]
img_titles.append(utils.unslugify(name_without_ext))
else:
img_titles = [""] * len(image_name_list)
thumbs = [".thumbnail".join(os.path.splitext(p)) for p in image_list]
thumbs = [
os.path.join(self.kw["output_folder"], output_folder, os.path.relpath(t, input_folder))
for t in thumbs
]
dst_img_list = [os.path.join(output_folder, os.path.relpath(t, input_folder)) for t in image_list]
dest_img_list = [os.path.join(self.kw["output_folder"], t) for t in dst_img_list]
folders = []
# Generate friendly gallery names
for path, folder in folder_list:
fpost = self.parse_index(path, input_folder, output_folder)
if fpost:
ft = fpost.title(lang) or folder
else:
ft = folder
if not folder.endswith("/"):
folder += "/"
folders.append((folder, ft))
#.........这里部分代码省略.........
请发表评论