import os, csv, inspect, json, textwrap from datetime import datetime from pathlib import Path from lib import GalleryTable GALLERY_ID = "gallery" # e.g.
will turn into the gallery. GALLERY_CONFIG_PATH = Path(__file__, "..", "nanogallery2-config.json").resolve() JAVASCRIPT_OUTPUT_PATH = Path(__file__, "..", "gallery", "gallery.js").resolve() HTML_OUTPUT_PATH = Path(__file__, "..", "gallery", "index.html").resolve() TABLE_PATH = Path(__file__, "..", "gallery-items.csv").resolve() MEDIA_FOLDER = Path(__file__, "..", "media").resolve() THUMBNAIL_FOLDER = Path(__file__, "..", "thumbs").resolve() def format_tags(tag_string): # Nanogallery2 expects space-separated tags, # which isn't intuitive in cases where you # want a tag to contain a space. # # So instead, this script (and the table) expects comma-separated tags, # then trims any whitespace from the start and end, # replaces all normal spaces with non-breaking spaces (" ") # automatically, and finally puts normal spaces # inbetween each tag, the way Nanogallery2 expects them. tags = [] for tag in tag_string.split(","): tags.append(tag.strip().replace(" ", " ")) return " ".join(tags) # If you want to use *commas* in a tag, I'm sorry. # Please let me know. I'll find a solution. # In the meantime, you can write "," whereever you need one. # I just don't want to get stuck overengineering # this any harder than I already am BEFORE I have # something finished that most people can put to use. # # The setup for all this is already going to be a tall ask. # Installing Python and FFmpeg isn't something I'd ask of a parent. # Unless they're on Linux in which case it's like, super trivial to do. # But that's not most parents. I think? I think. # # I mean, the reason this is all in Python to begin with # is purely so I only have to write it once, and people can # use it regardless of whether they use Windows, Linux, or Mac, # but also still readily look inside or tweak stuff easily. # And FFmpeg is required for the thumbnail generation stuff. # So there *is* a point to it all, but every extra step of setup # I ask people to do gets me worried that it'll be The step # that makes someone give up on the whole thing... # # Anyway. That's why I reformat the tags here. # It's so you can have spaces in them easystyle. # You're welcome. Sorry again if you needed commas and stuff. if __name__ == "__main__": # Only do this when the script is run directly table = GalleryTable.parse_table(TABLE_PATH) print("Found {} gallery items in {}.".format(len(table), TABLE_PATH)) try: with open(GALLERY_CONFIG_PATH, "r") as file: gallery_config = json.load(file) except Exception as error: print("Could not find/load {}:".format(GALLERY_CONFIG_PATH)) print(error) print("Continuing with default gallery configuration.") gallery_config = {} gallery_items = [] print("Creating gallery javascript file...") for name, entry in table.items(): gallery_items.append({ "src": "/media/" + entry["File"], "srct": "/thumbs/" + entry["Thumbnail"], "title": entry["Title"], "description": entry["Description"], "tags": format_tags(entry["Tags (with commas in-between)"]) }) gallery_config["items"] = gallery_items output_js = inspect.cleandoc( """ jQuery(document).ready(function () {{ jQuery("#{gallery_id}").nanogallery2( {gallery_config} ) }}) """ ).format( gallery_id=GALLERY_ID, gallery_config=textwrap.indent( json.dumps( gallery_config, indent=2 ), " ") ) try: with open(JAVASCRIPT_OUTPUT_PATH, "w", encoding="utf-8") as js_output_file: js_output_file.write(output_js) print("All done!") except Exception as error: print("Could not write the javascript file:") print(error) input("Press Enter key to exit...")