This is a gentle fork from https://framagit.org/marienfressinaud/photos.marienfressinaud.fr with a responsive and optimized mindset. https://media.larlet.fr/
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

boop.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env python3
  2. import os
  3. import shutil
  4. import re
  5. from operator import itemgetter
  6. from jinja2 import Environment, PackageLoader, select_autoescape
  7. from configuration import SITE_TITLE, THEME
  8. PICTURES_DIR_NAME = "photos"
  9. OUTPUT_DIR_NAME = "output"
  10. def list_galleries_in(path):
  11. gallery_dirs = [f for f in os.scandir(path) if f.is_dir()]
  12. for gallery_dir in gallery_dirs:
  13. photos = list(list_photos_in(gallery_dir))
  14. if len(photos) == 0:
  15. continue
  16. photos.sort(key=itemgetter("name"))
  17. gallery = {
  18. "name": gallery_dir.name,
  19. "path": gallery_dir.path,
  20. "url": gallery_dir.name,
  21. "num_photos": len(photos),
  22. "photos": photos,
  23. "cover_photo": photos[0],
  24. }
  25. yield gallery
  26. def list_photos_in(gallery_dir):
  27. photo_files = [
  28. f for f in os.scandir(gallery_dir) if re.match(".+\.jpg", f.name, re.I)
  29. ]
  30. for photo_file in photo_files:
  31. url = os.path.join(gallery_dir.name, photo_file.name)
  32. thumb_url = os.path.join(gallery_dir.name, f"thumb_{photo_file.name}")
  33. photo = {
  34. "name": photo_file.name,
  35. "path": photo_file.path,
  36. "url": url,
  37. "thumb_url": thumb_url,
  38. }
  39. yield photo
  40. def generate_output_dir():
  41. output_path = os.path.join(os.curdir, OUTPUT_DIR_NAME)
  42. if not os.path.isdir(output_path):
  43. os.mkdir(output_path)
  44. return output_path
  45. def generate_style(output_path):
  46. style_path = os.path.join(os.curdir, THEME, "style")
  47. style_output_path = os.path.join(output_path, "style")
  48. if os.path.isdir(style_output_path):
  49. shutil.rmtree(style_output_path)
  50. shutil.copytree(style_path, style_output_path)
  51. def generate_index(output_path, galleries):
  52. index_path = os.path.join(output_path, "index.html")
  53. theme_path = os.path.join(os.curdir, THEME)
  54. jinja_env = Environment(
  55. loader=PackageLoader("boop", theme_path), autoescape=select_autoescape(["html"])
  56. )
  57. index_template = jinja_env.get_template("index.html.j2")
  58. with open(index_path, "w") as index_file:
  59. index_file.write(
  60. index_template.render(galleries=galleries, site_title=SITE_TITLE)
  61. )
  62. def generate_gallery(output_path, gallery):
  63. generate_gallery_index(output_path, gallery)
  64. generate_gallery_dir(output_path, gallery)
  65. def generate_gallery_index(output_path, gallery):
  66. gallery_index_path = os.path.join(output_path, f"{gallery['name']}.html")
  67. theme_path = os.path.join(os.curdir, THEME)
  68. jinja_env = Environment(
  69. loader=PackageLoader("boop", theme_path), autoescape=select_autoescape(["html"])
  70. )
  71. gallery_template = jinja_env.get_template("gallery.html.j2")
  72. with open(gallery_index_path, "w") as gallery_file:
  73. gallery_file.write(
  74. gallery_template.render(gallery=gallery, site_title=SITE_TITLE)
  75. )
  76. def generate_gallery_dir(output_path, gallery):
  77. gallery_output_path = os.path.join(output_path, gallery["name"])
  78. if not os.path.isdir(gallery_output_path):
  79. os.mkdir(gallery_output_path)
  80. for photo in gallery["photos"]:
  81. photo_output_path = os.path.join(output_path, photo["url"])
  82. shutil.copyfile(photo["path"], photo_output_path)
  83. thumb_output_path = os.path.join(output_path, photo["thumb_url"])
  84. shutil.copyfile(photo["path"], thumb_output_path)
  85. def main():
  86. pictures_folder = os.path.join(os.curdir, PICTURES_DIR_NAME)
  87. galleries = list(list_galleries_in(pictures_folder))
  88. if len(galleries) == 0:
  89. return
  90. galleries.sort(key=itemgetter("name"))
  91. output_path = generate_output_dir()
  92. generate_style(output_path)
  93. generate_index(output_path, galleries)
  94. for gallery in galleries:
  95. generate_gallery(output_path, gallery)
  96. if __name__ == "__main__":
  97. main()