summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2021-06-08 20:20:31 +0200
committerJulian T <julian@jtle.dk>2021-06-08 20:20:31 +0200
commite7dc10f1bd0ad3a1ddcaed37bb53e82a5e6e934d (patch)
tree6333192002bc47cc8eb38952340c587847d4e21f
parentb4bea04224d5d418c148ea9f7badc8ad489e7a17 (diff)
Output to index.json instead of html template
-rwxr-xr-xbuild.py98
-rw-r--r--index.html.j250
-rw-r--r--shell.nix2
3 files changed, 43 insertions, 107 deletions
diff --git a/build.py b/build.py
index f018b94..9dccb8e 100755
--- a/build.py
+++ b/build.py
@@ -3,6 +3,7 @@ import jinja2
import PIL.Image
import PIL.ImageOps
import argparse
+import json
import yaml
import os
import re
@@ -11,6 +12,7 @@ import glob
from datetime import datetime
imagereg = re.compile("([a-z0-9]*)\\..*")
+thumbsize = 480
def hashfile(fname):
@@ -38,9 +40,10 @@ class FileLoader(jinja2.BaseLoader):
class ImageLocation:
- def __init__(self, folder):
+ def __init__(self, folder, extension):
self.folder = folder
self.stuff = {}
+ self.ext = extension
self.unused = set()
self.existing = {}
@@ -61,11 +64,11 @@ class ImageLocation:
self.unused.add(fhash)
self.existing[fhash] = name
- def convert(self, imgname, settings):
+ def convert(self, imgname, resolution):
# First determine the hash from settings
thash = hashlib.md5(
bytes(
- f"{settings['res']}+{settings['ext']}" + imgname,
+ f"{resolution}+{self.ext}" + imgname,
encoding="utf8")
).hexdigest()
@@ -75,12 +78,12 @@ class ImageLocation:
return self.existing[thash], thash
# Okay convert it
- fname = f"{thash}.{settings['ext']}"
+ fname = f"{thash}.{self.ext}"
print(f"Converting file {imgname} to {fname}")
- tmpname = f"{self.tmpname}.{settings['ext']}"
+ tmpname = f"{self.tmpname}.{self.ext}"
with PIL.Image.open(imgname) as im:
- target_height = settings["res"]
+ target_height = resolution
im = PIL.ImageOps.exif_transpose(im)
if im.size[0] > target_height:
res = (int((im.size[0] / im.size[1]) * target_height), target_height)
@@ -95,9 +98,9 @@ class ImageLocation:
return fname, thash
- def fetch(self, imgname, settings, relto=None):
+ def fetch(self, imgname, resolution, relto=None):
# Check if we already have it
- fname, hash = self.convert(imgname, settings)
+ fname, hash = self.convert(imgname, resolution)
# Mark the hash as used
self.unused.remove(hash)
@@ -115,13 +118,16 @@ class ImageLocation:
print(f"Removing file {fname}")
os.remove(fname)
+
class Image:
def __init__(self, filename):
self.name = os.path.basename(filename)
self.filename = filename
self.description = ""
- self.scaledpath = ""
+ self.url = ""
+ self.thumburl = ""
self.fullurl = ""
+ self.file = filename
self.taken = None
self.load_metadata()
@@ -179,67 +185,47 @@ class Loader:
datetime.min),
reverse=True)
-
-class Renderer:
- def __init__(self, loader, resolution, extension):
- self.loader = loader
- self.scaled = None
-
- self.settings = {
- "res": parse_res(resolution),
- "ext": extension,
- }
-
- def build_to(self, dest, template, context, loc=None, clean=False):
- # Load images
- if loc is None:
- loc = ImageLocation(os.path.join(dest, "imgs"))
-
- def imgproc(img):
- if context["cgit"] is not None:
- img.fullurl = os.path.join(context["cgit"], "plain", img.name)
-
- img.scaledpath = loc.fetch(img.filename, self.settings, relto=dest)
-
- return img
-
- self.loader.load(imgproc, clear=True)
-
- jenv = jinja2.Environment(
- loader=FileLoader(),
- autoescape=jinja2.select_autoescape(['html', 'xml'])
- )
-
- tmpl = jenv.get_template(template)
-
- with open(os.path.join(dest, "index.html"), "w") as f:
- f.write(tmpl.render({
- "ctx": context,
- "images": self.loader.images
- }))
-
- if clean:
- loc.clean()
+ def to_json(self):
+ jsonable = [img.__dict__ for img in self.images]
+ return json.dumps(jsonable)
parser = argparse.ArgumentParser()
-parser.add_argument("--dest", "-d", default="build", help="where to put resulting files")
-parser.add_argument("--size", "-s", default="1080", help="size to scale web images to")
+parser.add_argument("--dest", "-d", default="dist", help="where to put resulting files")
+parser.add_argument("--size", "-s", type=int, default="1080", help="size to scale web images to")
parser.add_argument("--commit", "-g", help="git commit hash to announce")
parser.add_argument("--clean", help="clean unused image files", action="store_true")
-parser.add_argument("--config", "-c", default="imginfo.yml", help="where to load image definitions from")
+parser.add_argument("--config", "-c", default=None, help="where to load image definitions from")
parser.add_argument("--template", "-t", default="index.html.j2", help="html template to use")
parser.add_argument("--cgit", "-w", help="cgit repo base url")
-parser.add_argument("--load", "-l", default=".", help="where to load full size images from")
+parser.add_argument("load", help="where to load full size images from")
parser.add_argument("--ext", "-e", default="png", help="image extension to use")
args = parser.parse_args()
+if args.config is None:
+ args.config = os.path.join(args.load, "imginfo.yml")
+
context = {
"cgit": args.cgit,
"git": args.commit
}
loader = Loader(args.config, args.load)
-rend = Renderer(loader, args.size, args.ext)
-rend.build_to(args.dest, args.template, context, clean=args.clean)
+loc = ImageLocation(os.path.join(args.dest, "imgs"), args.ext)
+
+
+def imgload(img):
+ if context["cgit"] is not None:
+ img.fullurl = os.path.join(context["cgit"], "plain", img.name)
+
+ img.url = loc.fetch(img.file, args.size, relto=args.dest)
+ img.thumburl = loc.fetch(img.file, thumbsize, relto=args.dest)
+
+ return img
+
+
+loader.load(imgload, clear=True)
+
+with open(os.path.join(args.dest, "imgs", "index.json"), "w") as f:
+ f.write(loader.to_json())
diff --git a/index.html.j2 b/index.html.j2
deleted file mode 100644
index ba58b70..0000000
--- a/index.html.j2
+++ /dev/null
@@ -1,50 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
- <head>
- <meta charset="utf-8">
- <title>Jtle images</title>
- <link rel="icon" href="/favicon.png">
- <meta name="robots" content="noindex,nofollow">
- <style>
- img {
- width: 50%;
- }
- .post {
- background-color: lightgray;
- margin-bottom: 20px;
- padding: 10px;
- }
- </style>
- </head>
- <body>
- <p>Billeder taget på forskellige ferier etc, som jeg bruger til baggrund.</p>
-
- {% if ctx.cgit is not none %}
- <p>Klik på et billede for at få fuld opløsning.
- Højere opløsning samt licens kan også findes her <a href="{{ cgit }}/tree">her</a>.</p>
- {% endif %}
-
- {% for img in images %}
- <div class="post">
- {% if img.taken is not none %}
- <i>Taget den {{ img.taken }}</i>
- {% endif %}
- {% if img.description is not none %}
- <p>{{ img.description }}</p>
- {% endif %}
- {% if img.fullurl is not none %}
- <a href="{{ img.fullurl }}">
- <img src="{{ img.scaledpath }}" />
- </a>
- {% else %}
- <img src="{{ img.scaledpath }}" />
- {% endif %}
- </div>
- {% endfor %}
-
-
- {% if ctx.git is not none %}
- <p>Bygget fra commit {{ ctx.git }}</p>
- {% endif %}
- </body>
-</html>
diff --git a/shell.nix b/shell.nix
index d452969..c3dd58f 100644
--- a/shell.nix
+++ b/shell.nix
@@ -2,6 +2,6 @@
pkgs.mkShell {
buildInputs = with pythonPackages; [
- jinja2 schema pillow pyyaml
+ pillow pyyaml
];
}