ctucx.git: gallery

static-site-generator for image-galleries [used @ photos.ctu.cx]

commit f3e5dfcedf2cdddbcce62b79486e2a16d714d523
parent 117fc8930a360e0632073fd439b1c90f2ef44070
Author: Leah (ctucx) <leah@ctu.cx>
Date: Sun, 14 Mar 2021 12:15:11 +0100

gallery.nim: read formatstring for picture-takestamp from config
3 files changed, 17 insertions(+), 16 deletions(-)
M
src/assets/album.html
|
6
+++---
M
src/assets/picture.html
|
2
+-
M
src/gallery.nim
|
25
+++++++++++++------------
diff --git a/src/assets/album.html b/src/assets/album.html
@@ -120,9 +120,9 @@
 					<img src="thumbnails/small/{{name}}.jpg" alt="Photo thumbnail">
 					<span class="overlay">
 						<h1>{{name}}</h1>
-						{{#takedate}}
-						<p><span title="Camera Date"><svg class="iconic "><use xlink:href="/iconic.svg#camera-slr"></use></svg>{{takedate}}</span></p>
-						{{/takedate}}
+						{{#takestamp}}
+						<p><span title="Camera Date"><svg class="iconic "><use xlink:href="/iconic.svg#camera-slr"></use></svg>{{takestamp}}</span></p>
+						{{/takestamp}}
 					</span>
 				</a>
 				{{/pictures}}
diff --git a/src/assets/picture.html b/src/assets/picture.html
@@ -100,7 +100,7 @@
 							<tbody>
 								<tr>
 									<td>Captured</td>
-									<td>{{takedate}}{{#taketime}} {{taketime}}{{/taketime}}</td>
+									<td>{{takestamp}}</td>
 								</tr>
 
 								<tr>
diff --git a/src/gallery.nim b/src/gallery.nim
@@ -1,4 +1,4 @@
-import os, osproc, options, json, strutils, random, algorithm, parsecfg, tables, math
+import os, osproc, options, json, strutils, random, algorithm, parsecfg, tables, math, times
 import moustachu
 import nimjpg
 

@@ -15,6 +15,7 @@ type
         enableJS*:            bool
         albumSortOrder*:      SortOrder
         pictureSortOrder*:    SortOrder
+        pictureFormatStr*:    string
         thumbMediumWidth*:    int
         thumbMediumHeight*:   int
         thumbSmallHeight*:    int

@@ -231,11 +232,10 @@ proc generateWebsite(targetDir: string, album: Album) =
         })
 
     for index, picture in album.pictures:
-        let takestamp  = picture.exif.getOrDefault("DateTimeOriginal", "").split(" ")
-        var takedate   = "-"
+        var takestamp: DateTime
 
-        if takestamp.len > 0:
-            takedate = takestamp[0].replace(":", "-")
+        if picture.exif.hasKey("DateTimeOriginal"):
+            takestamp = picture.exif["DateTimeOriginal"].parse("yyyy:MM:dd HH:mm:ss")
 
         var pictureTemplateContext = mergeJson(%* {
             "name":             picture.name,

@@ -263,11 +263,10 @@ proc generateWebsite(targetDir: string, album: Album) =
         if picture.exif.hasKey("FocalLengthIn35mmFilm"):
             pictureTemplateContext["focalLength35mm"] = %(picture.exif["FocalLengthIn35mmFilm"])
 
-        if takestamp.len > 0:
-            pictureTemplateContext["takedate"] = %takedate
-
-        if takestamp.len > 1:
-            pictureTemplateContext["taketime"] = %takestamp[1]
+        if $takestamp != "Uninitialized DateTime":
+            pictureTemplateContext["takestamp"] = %takestamp.format("yyyy-MM-dd HH:mm:ss")
+        else:
+            pictureTemplateContext["takestamp"] = %"-"
 
         if index > 0:
             pictureTemplateContext["hasPrev"]   = %true

@@ -307,8 +306,8 @@ proc generateWebsite(targetDir: string, album: Album) =
             "height":     picture.thumbHeight,
         }
 
-        if takestamp.len > 0:
-            picAlbumTemplateContext["takedate"] = %takedate
+        if $takestamp != "Uninitialized DateTime":
+            picAlbumTemplateContext["takestamp"] = %takestamp.format(config.pictureFormatStr)
 
         templateContext["pictures"].add(picAlbumTemplateContext)
 

@@ -353,6 +352,7 @@ proc main =
             config.setSectionKey("Site",        "EnableJS",               "true")
             config.setSectionKey("Album",       "SortOrder",              "Descending")
             config.setSectionKey("Picture",     "SortOrder",              "Ascending")
+            config.setSectionKey("Picture",     "DateTimeFormatStr",      "yyyy-MM-dd")
 
             config.setSectionKey("Thumbnails",  "SmallWidth",             "200")
             config.setSectionKey("Thumbnails",  "SmallHeight",            "200")

@@ -379,6 +379,7 @@ proc main =
             enableJS:            configFile.getSectionValue("Site",                          "EnableJS",            "true").parseBool,
             albumSortOrder:      parseEnum[SortOrder](configFile.getSectionValue("Album",    "SortOrder",           "Descending")),
             pictureSortOrder:    parseEnum[SortOrder](configFile.getSectionValue("Picture",  "SortOrder",           "Ascending")),
+            pictureFormatStr:    configFile.getSectionValue("Picture",                       "DateTimeFormatStr",   "yyyy-MM-dd"),
             thumbMediumWidth:    configFile.getSectionValue("Thumbnails",                    "MediumWidth",         "0").parseInt,
             thumbMediumHeight:   configFile.getSectionValue("Thumbnails",                    "MediumHeight",        "0").parseInt,
             thumbSmallHeight:    configFile.getSectionValue("Thumbnails",                    "SmallHeight",         "0").parseInt,