From 8e77c43d328fe06dc9083be314676eee41da1b8c Mon Sep 17 00:00:00 2001 From: David Todd Date: Wed, 10 Apr 2019 00:00:33 -0500 Subject: [PATCH] Modularize into template files, add root gen Removes most of the inline HTML to their own files Uses the most basic setup to create links for all the existing index files created with GenTemplate --- .gitignore | 1 + GenGallery.py | 30 +++++++ GenTemplate.py | 160 ++---------------------------------- GenThumb.py | 1 + templates/card.html | 8 ++ templates/root_gallery.html | 123 +++++++++++++++++++++++++++ templates/sub_gallery.html | 137 ++++++++++++++++++++++++++++++ 7 files changed, 307 insertions(+), 153 deletions(-) create mode 100755 GenGallery.py create mode 100644 templates/card.html create mode 100644 templates/root_gallery.html create mode 100644 templates/sub_gallery.html diff --git a/.gitignore b/.gitignore index a505071..9857ee0 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ thumbs/* GenThumb.log index*.html .vscode +gallery.html diff --git a/GenGallery.py b/GenGallery.py new file mode 100755 index 0000000..f24fa1e --- /dev/null +++ b/GenGallery.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +# GenThumb.py - Part of the simple s3 gallery +# Usage: ./GenThumb.py + +from pathlib import Path +import getopt +import sys +import os + +# Set these to override +baseuri = os.environ.get('BASEURI',"https://s3.wasabisys.com/c0de-photography/") +thumb_path = os.environ.get('THUMBNAILS', "./thumbs") + +# Load templates into memory +gallery_template = open('templates/root_gallery.html', 'r').read() +card_template = open('templates/card.html', 'r').read() + +# Get all the index files in local directory +indexlist = list(Path(".").rglob("index_*.html")) + +with open('gallery.html', 'w') as gallery: + thumbrow = "" + for index in indexlist: + the_template = card_template # Don't mutate the source + # basic string replacements + the_template = the_template.replace("{{FULLLINK}}", index.name) + the_template = the_template.replace("{{TITLE}}", index.name.strip("index_").strip(".html").replace(":", "/")) + thumbrow += the_template.replace("{{THUMBNAIL}}", "thumbs/folder.png") + gallery.write(gallery_template.replace("{{THUMBROW}}", thumbrow)) + diff --git a/GenTemplate.py b/GenTemplate.py index 6586966..a3838e2 100755 --- a/GenTemplate.py +++ b/GenTemplate.py @@ -23,159 +23,12 @@ for opt, arg in opts: elif opt in ("-p", "--pathlist"): pathlist_file = arg # Index file created by GenThumb.py -template = """ - - - Simple S3 Gallery - - - - - - - - - - - - -
- - -
- -
- - - - -
-
-
- {{THUMBROW}} -
-
-
-
- - - - - - - - -""" - -template2 = """ -
- -
-
{{TITLE}}
- -
-
-
-""" - +# Load templates into memory +gallery_template = open('templates/root_gallery.html', 'r').read() +card_template = open('templates/card.html', 'r').read() crumb_template = """""" +# Get the paths of all JPGs in the thumbnail directory thumblist = list(Path(thumb_path).rglob("*.[jJ][pP][gG]")) with open(pathlist_file, 'r') as pathlist: @@ -185,10 +38,11 @@ with open(pathlist_file, 'r') as pathlist: pathlist.sort() thumbrow = "" for image in thumblist: + # Search the pathlist for any lines with a matching filename indices = [i for i, s in enumerate(pathlist) if image.name in s] if len(indices) > 0: imagename = "%s" % image - the_template = template2 + the_template = card_template # Don't mutate the source the_template = the_template.replace("{{FULLLINK}}", pathlist[indices[0]]) the_template = the_template.replace("{{TITLE}}", imagename.strip("thumbs/")) thumbrow += the_template.replace("{{THUMBNAIL}}", "%s" % image) @@ -199,5 +53,5 @@ with open(pathlist_file, 'r') as pathlist: the_template = crumb_template crumblist += the_template.replace("{{PATH}}", crumb) - index.write(template.replace("{{THUMBROW}}", thumbrow).replace("{{BREADCRUMBS}}", crumblist)) + index.write(gallery_template.replace("{{THUMBROW}}", thumbrow).replace("{{BREADCRUMBS}}", crumblist)) diff --git a/GenThumb.py b/GenThumb.py index 1913827..3c6b8d3 100755 --- a/GenThumb.py +++ b/GenThumb.py @@ -52,6 +52,7 @@ def gen_thumb(image, imagepath): logging.info("Saved Thumbnail as %s" % thumbname) +logging.debug("Creating pathlist file as: pathlist_%s.txt" % browse_path.replace('/', ':')) with open('pathlist_%s.txt' % browse_path.replace('/', ':'), 'w') as pathlist: for image in filelist: imagepath = "%s" % image.parent diff --git a/templates/card.html b/templates/card.html new file mode 100644 index 0000000..9f3a464 --- /dev/null +++ b/templates/card.html @@ -0,0 +1,8 @@ +
+ +
+
{{TITLE}}
+ +
+
+
diff --git a/templates/root_gallery.html b/templates/root_gallery.html new file mode 100644 index 0000000..78ddfc2 --- /dev/null +++ b/templates/root_gallery.html @@ -0,0 +1,123 @@ + + + Simple S3 Gallery + + + + + + + + + + + + +
+ + +
+ +
+ + + + +
+
+
+ {{THUMBROW}} +
+
+
+
+ + + + + + + + diff --git a/templates/sub_gallery.html b/templates/sub_gallery.html new file mode 100644 index 0000000..7d80010 --- /dev/null +++ b/templates/sub_gallery.html @@ -0,0 +1,137 @@ + + + Simple S3 Gallery + + + + + + + + + + + + +
+ + +
+ +
+ + + + +
+
+
+ {{THUMBROW}} +
+
+
+
+ + + + + + + +