This is a gentle fork from https://framagit.org/marienfressinaud/photos.marienfressinaud.fr with a responsive and optimized mindset. https://media.larlet.fr/
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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