diff options
author | Julian T <julian@jtle.dk> | 2021-04-05 18:14:01 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2021-04-05 18:14:01 +0200 |
commit | 5d4049f7f9de2507c18702fd3892548850eae6d5 (patch) | |
tree | 9a845f4fedcd2f0ebfcbb0b8ce273a8df0d6d64a | |
parent | ce4983421e2d4bad3a4e563920f46f210aad7334 (diff) |
Reworked the config structure
-rwxr-xr-x | build.py | 79 | ||||
-rw-r--r-- | imginfo.yml | 40 | ||||
-rw-r--r-- | index.html.j2 | 24 |
3 files changed, 81 insertions, 62 deletions
@@ -1,11 +1,12 @@ #!/usr/bin/env python3 import jinja2 -from PIL import Image +import PIL.Image import argparse import yaml import os import re import hashlib +import glob imagereg = re.compile("([a-z0-9]*)\\..*") resreg = re.compile("([0-9]*)x([0-9]*)") @@ -77,8 +78,8 @@ class ImageLocation: fname = f"{thash}.{settings['ext']}" print(f"Converting file {imgname} to {fname}") - im = Image.open(imgname) - im = im.resize(settings["res"], Image.ANTIALIAS) + im = PIL.Image.open(imgname) + im = im.resize(settings["res"], PIL.Image.ANTIALIAS) tmpname = f"{self.tmpname}.{settings['ext']}" im.save(tmpname) @@ -109,17 +110,23 @@ 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.fullurl = "" + + + +class Loader: + def __init__(self, config_file, loadpath): + self.config = Loader.__load_config(config_file) -class Renderer: - def __init__(self, config_file, loadpath, resolution, extension): - self.config = Renderer.__load_config(config_file) self.loadpath = loadpath - self.scaled = None - self.settings = { - "res": parse_res(resolution), - "ext": extension, - } + self.images = [] def __load_config(config_file): config = {} @@ -129,28 +136,59 @@ class Renderer: print(f"Loaded config: {config}") return config - def imgfetch_gen(self, loc, dest): - def fn(img): - return loc.fetch(img, self.settings, relto=dest) + def load(self, imagehook=None, clear=False): + # Clear if neccesary + if clear: + self.images = [] + + # First find all images from glob + for image in glob.iglob(os.path.join(self.loadpath, self.config["imageglob"])): + img = Image(image) + img.description = self.config["info"].get(img.name, "") - return fn + if imagehook is not None: + img = imagehook(img) + + self.images.append(img) + + +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']) ) - jenv.globals.update( - imgfetch=self.imgfetch_gen(loc, dest) - ) tmpl = jenv.get_template(template) with open(os.path.join(dest, "index.html"), "w") as f: - f.write(tmpl.render({**self.config, **context})) + f.write(tmpl.render({ + "ctx": context, + "images": self.loader.images + })) if clean: loc.clean() @@ -174,5 +212,6 @@ context = { "git": args.commit } -rend = Renderer(args.config, args.load, args.size, args.ext) +loader = Loader(args.config, args.load) +rend = Renderer(loader, args.size, args.ext) rend.build_to(args.dest, args.template, context, clean=args.clean) diff --git a/imginfo.yml b/imginfo.yml index fbcc628..4230883 100644 --- a/imginfo.yml +++ b/imginfo.yml @@ -1,30 +1,10 @@ -posts: - - imgs: - - kirke.jpg - text: Kirke i Århus - - imgs: - - bi.jpg - - imgs: - - olie.jpg - text: Boreplatform i Grenå - - imgs: - - bakker.jpg - - imgs: - - vindmøller.jpg - - imgs: - - vinge.jpg - - imgs: - - blomster2.jpg - - blomster.jpg - - blomster3.jpg - - imgs: - - maskiner.jpg - text: Taget ved Tangeværket. - - imgs: - - Struktur.jpg - text: Taget ved Dwingeloo Radiotelescoop. - - imgs: - - bænk.jpg - text: Taget ved fyrtårn på Helgenæs. - - imgs: - - saks.jpg +imageglob: "*.jpg" +info: + kirke.jpg: + desc: Kirke i Århus + olie.jpg: + desc: Boreplatform i Grenå + maskiner.jpg: + desc: Gammel generator ved Tangeværket + Struktur.jpg: + desc: Restaureret radiotelescoop i Dwingeloo diff --git a/index.html.j2 b/index.html.j2 index fde5f4b..cc7dc28 100644 --- a/index.html.j2 +++ b/index.html.j2 @@ -19,29 +19,29 @@ <body> <p>Billeder taget på forskellige ferier etc, som jeg bruger til baggrund.</p> - {% if cgit is not none %} + {% 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 post in posts %} + + {% for img in images %} <div class="post"> - <p>{{ post.text }}</p> - {% for img in post.imgs %} - {% if cgit is not none %} - <a href="{{cgit}}/plain/{{img}}"> - <img src="{{ imgfetch(img) }}" /> + {% 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="{{ imgfetch(img) }}" /> + <img src="{{ img.scaledpath }}" /> {% endif %} - - {% endfor %} </div> {% endfor %} - {% if git is not none %} - <p>Bygget fra commit {{ git }}</p> + {% if ctx.git is not none %} + <p>Bygget fra commit {{ ctx.git }}</p> {% endif %} </body> </html> |