commit bddc360359e1002641619ebbd0538b61f35b3276
parent 99bc64f18ef3ab1d2689d718121770e519246ec7
Author: sej <sej@sejdt.localhost>
Date: Wed, 30 Oct 2024 15:28:20 +0100
Streamlined paths. Paths in generated HTML are now relative by default, can be easily changed to absolute.
Diffstat:
5 files changed, 49 insertions(+), 24 deletions(-)
diff --git a/jinjarender.py b/jinjarender.py
@@ -33,6 +33,7 @@ if __name__ == "__main__":
autoescape=select_autoescape()
)
+
index = env.get_template( "index.html" )
viewer = env.get_template( "viewer.html" )
album = env.get_template( "album.html" )
@@ -61,25 +62,40 @@ if __name__ == "__main__":
nalbums = len( allalbums )
+ # set rootpath to enable absolute paths
+ # rootpath = "/mysite/something/blabla" # rememeber no trailing slash
+ rootpath = None
+ # rootpath = ""
+
#generate album files
for i in range( nalbums ):
json = jsonarray[i];
nextalbum = f"{ jsonarray[ (i+1)%nalbums ]['name'] }.html"
prevalbum = f"{ jsonarray[ (i-1)%nalbums ]['name'] }.html"
+ if rootpath == None:
+ rootdir = "."
+ else:
+ rootdir = rootpath
+
with open( f"{ args.output_path }/{ json['name'] }.html", "x" ) as f:
- f.write( album.render( nextalbum = nextalbum, prevalbum = prevalbum, title = json["name"], imgs = json["imgs"], csspath = "css/stylesheet.css", navbar = True ) )
+ f.write( album.render( rootdir = rootdir, nextalbum = nextalbum, prevalbum = prevalbum, title = json["name"], imgs = json["imgs"], navbar = True ) )
#generate viewer files for this album
- os.makedirs( f"{ args.output_path }/img/viewer/{ json['name'] }" )
+ os.makedirs( f"{ args.output_path }/img/viewer/{ json['name'] }", exist_ok = True )
n = len( json["imgs"] )
for i in range( n ):
img = json["imgs"][i]
exif = m[img]
- nexti = f"{ str( json['imgs'][ (i+1)%n ] ) }.html"
- previ = f"{ str( json['imgs'][ (i-1)%n ] ) }.html"
+ album_name = json['name']
+ nexti = f"{ album_name }/{ str( json['imgs'][ (i+1)%n ] ) }.html"
+ previ = f"{ album_name }/{ str( json['imgs'][ (i-1)%n ] ) }.html"
+ if rootpath == None:
+ rootdir = "../../.."
+ else:
+ rootdir = rootpath
with open( f"{ args.output_path }/img/viewer/{ json['name'] }/{ img }.html", "x" ) as f:
- f.write( viewer.render( navbar = True, fullrespath = "/img/fullres/", previewpath = "/img/preview/", albumpath = f"/{ json['name'] }.html", img = img, exif = exifstring( exif ), nexti = nexti, previ = previ ) )
+ f.write( viewer.render( rootdir = rootdir, navbar = True, album_name = album_name, img = img, exif = exifstring( exif ), nexti = nexti, previ = previ ) )
#generate index file
albums = []
@@ -90,8 +106,13 @@ if __name__ == "__main__":
imgs.append( json["imgs"][0] )
desc.append( json["name"] )
+ if rootpath == None:
+ rootdir = "."
+ else:
+ rootdir = rootpath
+
with open( f"{ args.output_path }/index.html", "x" ) as f:
- f.write( index.render( title = "index", imgs = imgs, hrefs = albums, desc = desc, bodyclass = "index" ) )
+ f.write( index.render( rootdir = rootdir, title = "index", imgs = imgs, hrefs = albums, desc = desc, bodyclass = "index" ) )
#generate viewer files for all imgs
n = len( allimgs )
@@ -100,5 +121,9 @@ if __name__ == "__main__":
exif = m[img]
nexti = f"{ str( allimgs[ (i+1)%n ] ) }.html"
previ = f"{ str( allimgs[ (i-1)%n ] ) }.html"
+ if rootpath == None:
+ rootdir = "../.."
+ else:
+ rootdir = rootpath
with open( f"{ args.output_path}/img/viewer/{ img }.html", "x" ) as f:
- f.write( viewer.render( navbar = True, fullrespath = "/img/fullres/", previewpath = "/img/preview/", img = img, exif = exifstring( exif ), path = "/img/", nexti = nexti, previ = previ) )
+ f.write( viewer.render( rootdir = rootdir, navbar = True, img = img, exif = exifstring( exif ), nexti = nexti, previ = previ, album_name = "index" ) )
diff --git a/templates/album.html b/templates/album.html
@@ -1,15 +1,15 @@
{% extends "base.html" %}
{% block title %}{{ title }}{% endblock %}
-{% block navbar %}
-<a class="half" href="index.html">
+{% block navbar %}
+<a class="half" href="{{ rootdir }}/index.html">
<p>Return to all albums</p>
</a>
-<a class="quarter" href="{{ prevalbum }}">
+<a class="quarter" href="{{ rootdir }}/{{ prevalbum }}">
<p><== previous album</p>
</a>
-<a class="right quarter" href="{{ nextalbum }}">
+<a class="right quarter" href="{{ rootdir }}/{{ nextalbum }}">
<p>next album ==></p>
</a>
{% endblock %}
@@ -21,8 +21,8 @@
{% for img in imgs %}
<div class="box">
- <a href="img/viewer/{{ title }}/{{ img }}.html">
- <img src="img/thumb/{{ img }}">
+ <a href="{{ rootdir }}/img/viewer/{{ title }}/{{ img }}.html">
+ <img src="{{ rootdir }}/img/thumb/{{ img }}">
</a>
</div>
{% endfor %}
diff --git a/templates/base.html b/templates/base.html
@@ -5,7 +5,7 @@
<title>{% block title %}{% endblock %}</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="stylesheet" href="/css/stylesheet.css">
+ <link rel="stylesheet" href="{{ rootdir }}/css/stylesheet.css">
</head>
<body {% block bodyclass %}class="{{ bodyclass }}"{% endblock %}>
{% if navbar %}
diff --git a/templates/index.html b/templates/index.html
@@ -8,8 +8,8 @@
<div class="flex-container wrap">
{% for img in imgs %}
<div class="box">
- <a href="{{ hrefs[loop.index0] }}">
- <img class="preview" src="img/thumb/{{ img }}">
+ <a href="{{ rootdir }}/{{ hrefs[loop.index0] }}">
+ <img class="preview" src="{{ rootdir }}/img/thumb/{{ img }}">
</a>
<div class="desc">
<p class="desc">{{ desc[loop.index0] }}</p>
diff --git a/templates/viewer.html b/templates/viewer.html
@@ -3,32 +3,32 @@
{% block title %}{{ img }}{% endblock %}
{% block navbar %}
-<a href="/index.html">
+<a href="{{ rootdir }}/index.html">
<p>Back to index view</p>
</a>
-<a href="{{ albumpath }}">
+<a href="{{ rootdir }}/{{ album_name }}.html">
<p>Back to albums view</p>
</a>
-<a href="{{ fullrespath + img }}">
+<a href="{{ rootdir }}/img/fullres/{{ img }}">
<p>Open original</p>
</a>
-<a href="../{{ img }}.html">
+<a href="{{ rootdir }}/img/viewer/{{ img }}.html">
<p>Open in all‑viewer</p>
</a>
-<a class="np half" href="{{ previ }}">
+<a class="np half" href="{{ rootdir }}/img/viewer/{{ previ }}">
</a>
-<a class="right np half" href="{{ nexti }}">
+<a class="right np half" href="{{ rootdir }}/img/viewer/{{ nexti }}">
</a>
{% endblock %}
{% block content %}
<div class="view-container">
- <a href="{{ fullrespath + img }}">
- <img class="viewer" src="{{ previewpath + img }}">
+ <a href="{{ rootdir }}/img/fullres/{{ img }}">
+ <img class="viewer" src="{{ rootdir }}/img/preview/{{ img }}">
</a>
</div>
<p id="metadata" class="imgdesc">{{ exif }}</p>