This is a gentle fork from https://framagit.org/marienfressinaud/photos.marienfressinaud.fr with a responsive and optimized mindset. https://media.larlet.fr/
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

boop.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. photo = {"name": photo_file.name, "url": url}
  33. yield photo
  34. def generate_output_dir():
  35. output_path = os.path.join(os.curdir, OUTPUT_DIR_NAME)
  36. shutil.rmtree(output_path)
  37. if not os.path.isdir(output_path):
  38. os.mkdir(output_path)
  39. return output_path
  40. def generate_style(output_path):
  41. style_path = os.path.join(os.curdir, THEME, "style")
  42. style_output_path = os.path.join(output_path, "style")
  43. shutil.copytree(style_path, style_output_path)
  44. def generate_index(output_path, galleries):
  45. index_path = os.path.join(output_path, "index.html")
  46. theme_path = os.path.join(os.curdir, THEME)
  47. jinja_env = Environment(
  48. loader=PackageLoader("boop", theme_path), autoescape=select_autoescape(["html"])
  49. )
  50. index_template = jinja_env.get_template("index.html.j2")
  51. with open(index_path, "w") as index_file:
  52. index_file.write(
  53. index_template.render(galleries=galleries, site_title=SITE_TITLE)
  54. )
  55. def generate_gallery(output_path, gallery):
  56. generate_gallery_index(output_path, gallery)
  57. generate_gallery_dir(output_path, gallery)
  58. def generate_gallery_index(output_path, gallery):
  59. gallery_index_path = os.path.join(output_path, f"{gallery['name']}.html")
  60. theme_path = os.path.join(os.curdir, THEME)
  61. jinja_env = Environment(
  62. loader=PackageLoader("boop", theme_path), autoescape=select_autoescape(["html"])
  63. )
  64. gallery_template = jinja_env.get_template("gallery.html.j2")
  65. with open(gallery_index_path, "w") as gallery_file:
  66. gallery_file.write(
  67. gallery_template.render(gallery=gallery, site_title=SITE_TITLE)
  68. )
  69. def generate_gallery_dir(output_path, gallery):
  70. gallery_output_path = os.path.join(output_path, gallery["name"])
  71. if os.path.isdir(gallery_output_path):
  72. os.mkdir(gallery_output_path)
  73. shutil.copytree(gallery["path"], gallery_output_path)
  74. def main():
  75. pictures_folder = os.path.join(os.curdir, PICTURES_DIR_NAME)
  76. galleries = list(list_galleries_in(pictures_folder))
  77. if len(galleries) == 0:
  78. return
  79. galleries.sort(key=itemgetter("name"))
  80. output_path = generate_output_dir()
  81. generate_style(output_path)
  82. generate_index(output_path, galleries)
  83. for gallery in galleries:
  84. generate_gallery(output_path, gallery)
  85. if __name__ == "__main__":
  86. main()