ctucx.git: gallery

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

commit 8fbed97307bc8632d73549806d8e8a0432ffd9dd
parent 4a23d597f57114e87e451e12b701e1a782e56a47
Author: Leah (ctucx) <leah@ctu.cx>
Date: Sun, 14 Mar 2021 11:41:43 +0100

gallery.nim: SortOrder for albums and pictures is now a config-option
1 file changed, 28 insertions(+), 20 deletions(-)
M
src/gallery.nim
|
48
++++++++++++++++++++++++++++--------------------
diff --git a/src/gallery.nim b/src/gallery.nim
@@ -13,6 +13,8 @@ type
         showOriginalsButton*: bool
         symlinkOriginals*:    bool
         enableJS*:            bool
+        sortAlbums*:          SortOrder
+        sortPictures*:        SortOrder
         thumbMediumWidth*:    int
         thumbMediumHeight*:   int
         thumbSmallWidth*:     int

@@ -63,15 +65,17 @@ proc mergeJson(a: JsonNode, b: JsonNode): JsonNode =
 
 
 proc sortAlbums(x, y: Album): int =
-  if x.name < y.name: -1
-  elif x.name == y.name: 0
-  else: 1
+    if config.sortAlbums != Descending:
+        return cmp(x.name, y.name)
+    else:
+        return cmp(y.name, x.name)
 
 
 proc sortPictures(x, y: Picture): int =
-  if x.name < y.name: -1
-  elif x.name == y.name: 0
-  else: 1
+    if config.sortPictures != Descending:
+        return cmp(x.name, y.name)
+    else:
+        return cmp(y.name, x.name)
 
 proc isaRound* [T: float32|float64](value: T, places: int = 0): float = 
   if places == 0:

@@ -354,6 +358,8 @@ proc main =
             config.setSectionKey("Site",        "ShowOriginalsButton",    "true")
             config.setSectionKey("Site",        "SymlinkOriginals",       "false")
             config.setSectionKey("Site",        "EnableJS",               "true")
+            config.setSectionKey("Album",       "SortOrder",              "Descending")
+            config.setSectionKey("Picture",     "SortOrder",              "Ascending")
 
             config.setSectionKey("Thumbnails",  "SmallWidth",             "200")
             config.setSectionKey("Thumbnails",  "SmallHeight",            "200")

@@ -369,20 +375,22 @@ proc main =
     try:
         let configFile = loadConfig(paramStr(1))
         config = Config(
-            sourceDir:           normalizedPath(configFile.getSectionValue("", "SourceDir",           "")),
-            targetDir:           normalizedPath(configFile.getSectionValue("", "TargetDir",           "")),
-            siteName:            configFile.getSectionValue("Site",            "Name",                "default title - change me plese"),
-            siteAuthor:          configFile.getSectionValue("Site",            "Author",              "Max Mustermann"),
-            siteTags:            configFile.getSectionValue("Site",            "Tags",                ""),
-            siteDescription:     configFile.getSectionValue("Site",            "Description",         ""),
-            showOriginalsButton: configFile.getSectionValue("Site",            "ShowOriginalsButton", "true").parseBool,
-            symlinkOriginals:    configFile.getSectionValue("Site",            "SymlinkOriginals",    "true").parseBool,
-            enableJS:            configFile.getSectionValue("Site",            "EnableJS",            "true").parseBool,
-            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
+            sourceDir:           normalizedPath(configFile.getSectionValue("",               "SourceDir",           "")),
+            targetDir:           normalizedPath(configFile.getSectionValue("",               "TargetDir",           "")),
+            siteName:            configFile.getSectionValue("Site",                          "Name",                "default title - change me plese"),
+            siteAuthor:          configFile.getSectionValue("Site",                          "Author",              "Max Mustermann"),
+            siteTags:            configFile.getSectionValue("Site",                          "Tags",                ""),
+            siteDescription:     configFile.getSectionValue("Site",                          "Description",         ""),
+            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")),
+            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
         )
 
         if config.sourceDir == "":