ctucx.git: gallery

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

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

gallery.nim: code improvements
1 file changed, 20 insertions(+), 17 deletions(-)
M
src/gallery.nim
|
37
++++++++++++++++++++-----------------
diff --git a/src/gallery.nim b/src/gallery.nim
@@ -13,8 +13,8 @@ type
         showOriginalsButton*: bool
         symlinkOriginals*:    bool
         enableJS*:            bool
-        sortAlbums*:          SortOrder
-        sortPictures*:        SortOrder
+        albumSortOrder*:      SortOrder
+        pictureSortOrder*:    SortOrder
         thumbMediumWidth*:    int
         thumbMediumHeight*:   int
         thumbSmallHeight*:    int

@@ -64,14 +64,14 @@ proc mergeJson(a: JsonNode, b: JsonNode): JsonNode =
 
 
 proc sortAlbums(x, y: Album): int =
-    if config.sortAlbums != Descending:
+    if config.albumSortOrder != Descending:
         return cmp(x.name, y.name)
     else:
         return cmp(y.name, x.name)
 
 
 proc sortPictures(x, y: Picture): int =
-    if config.sortPictures != Descending:
+    if config.pictureSortOrder != Descending:
         return cmp(x.name, y.name)
     else:
         return cmp(y.name, x.name)

@@ -87,15 +87,17 @@ proc isaRound* [T: float32|float64](value: T, places: int = 0): float =
 
 
 proc createPicture(path: string): Picture = 
-    let allowedExtensions = @[".jpg", ".jpeg", ".JPG", ".JPEG"]
-    let (dir, name, ext) = splitFile(path)
+    let
+      allowedExtensions = @[".jpg", ".jpeg", ".JPG", ".JPEG"]
+      (dir, name, ext)  = splitFile(path)
 
     if not allowedExtensions.contains(ext): return
 
     if fileExists(joinPath(dir, name, ".txt")): result.desc = some(readFile(joinPath(dir, name,".txt")))
 
-    let file        = open(path)
-    let jpgMetadata = collect_jpg(file)
+    let
+      file        = open(path)
+      jpgMetadata = collect_jpg(file)
 
     if jpgMetadata.exifData.isSome():
         result.exif     = jpgMetadata.exifData.get

@@ -197,9 +199,10 @@ proc generateWebsite(targetDir: string, album: Album) =
         "pictures":     []
     }, %config)
 
-    var smallThumbnails  = newSeq[string]()
-    var mediumThumbnails = newSeq[string]()
-    var bigThumbnails    = newSeq[string]()
+    var
+      smallThumbnails  = newSeq[string]()
+      mediumThumbnails = newSeq[string]()
+      bigThumbnails    = newSeq[string]()
 
     if album.path == "":         templateContext["isSubalbum"]   = %false
     if album.subalbums.len != 0: templateContext["hasSubalbums"] = %true

@@ -208,9 +211,10 @@ proc generateWebsite(targetDir: string, album: Album) =
     for subalbum in album.subalbums:
         generateWebsite(joinPath(targetDir, subalbum.name), subalbum)
 
-        var thumbnail1      = "/no_images.svg"
-        var thumbnail2      = "/no_images.svg"
-        var thumbnail3      = "/no_images.svg"
+        var
+          thumbnail1      = "/no_images.svg"
+          thumbnail2      = "/no_images.svg"
+          thumbnail3      = "/no_images.svg"
 
         if subalbum.pictures.len > 0:
             thumbnail1  = subalbum.name & "/thumbnails/small/" & subalbum.pictures[rand(0..subalbum.pictures.len-1)].name & ".jpg"

@@ -373,11 +377,10 @@ proc main =
             showOriginalsButton: configFile.getSectionValue("Site",                          "ShowOriginalsButton", "true").parseBool,
             symlinkOriginals:    configFile.getSectionValue("Site",                          "SymlinkOriginals",    "true").parseBool,
             enableJS:            configFile.getSectionValue("Site",                          "EnableJS",            "true").parseBool,
-            sortAlbums:          parseEnum[SortOrder](configFile.getSectionValue("Album",    "SortOrder",           "Descending")),
-            sortPictures:        parseEnum[SortOrder](configFile.getSectionValue("Picture",  "SortOrder",           "Ascending")),
+            albumSortOrder:      parseEnum[SortOrder](configFile.getSectionValue("Album",    "SortOrder",           "Descending")),
+            pictureSortOrder:    parseEnum[SortOrder](configFile.getSectionValue("Picture",  "SortOrder",           "Ascending")),
             thumbMediumWidth:    configFile.getSectionValue("Thumbnails",                    "MediumWidth",         "0").parseInt,
             thumbMediumHeight:   configFile.getSectionValue("Thumbnails",                    "MediumHeight",        "0").parseInt,
-            thumbSmallWidth:     configFile.getSectionValue("Thumbnails",                    "SmallWidth",          "0").parseInt,
             thumbSmallHeight:    configFile.getSectionValue("Thumbnails",                    "SmallHeight",         "0").parseInt,
             thumbSmallQuality:   configFile.getSectionValue("Thumbnails",                    "SmallQuality",        "90").parseInt
         )