ctucx.git: gallery

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

commit 0ca925ad12ea0eee6fe63fe35039777d3813df7b
parent 82479e4961d7058374454b3e4d962653d196d625
Author: Leah (ctucx) <leah@ctu.cx>
Date: Sun, 14 Mar 2021 12:38:06 +0100

gallery.nim improve code
1 file changed, 70 insertions(+), 54 deletions(-)
M
src/gallery.nim
|
124
+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
diff --git a/src/gallery.nim b/src/gallery.nim
@@ -43,15 +43,17 @@ type
         filetype*:    string
         filesize*:    BiggestInt
 
-const asset_exif_js             = staticRead"./assets/exif.js"
-const asset_justified_layout_js = staticRead"./assets/justified-layout.min.js"
-const asset_albums_js           = staticRead"./assets/albums.js"
-const asset_style_css           = staticRead"./assets/style.css"
-const asset_noimages_svg        = staticRead"./assets/no_images.svg"
-const asset_iconic_svg          = staticRead"./assets/iconic.svg"
-const asset_album_html          = staticRead"./assets/album.html"
-const asset_picture_html        = staticRead"./assets/picture.html"
-var   config* {.threadvar.}: Config
+const
+  asset_exif_js             = staticRead"./assets/exif.js"
+  asset_justified_layout_js = staticRead"./assets/justified-layout.min.js"
+  asset_albums_js           = staticRead"./assets/albums.js"
+  asset_style_css           = staticRead"./assets/style.css"
+  asset_noimages_svg        = staticRead"./assets/no_images.svg"
+  asset_iconic_svg          = staticRead"./assets/iconic.svg"
+  asset_album_html          = staticRead"./assets/album.html"
+  asset_picture_html        = staticRead"./assets/picture.html"
+
+var config* {.threadvar.}: Config
 
 ###
 #

@@ -78,6 +80,7 @@ proc sortPictures(x, y: Picture): int =
     else:
         return cmp(y.name, x.name)
 
+
 proc isaRound* [T: float32|float64](value: T, places: int = 0): float = 
   if places == 0:
     result = round(value)

@@ -88,6 +91,59 @@ proc isaRound* [T: float32|float64](value: T, places: int = 0): float =
 
 
 
+proc placeAssets(targetDir: string, enableJS: bool) =
+    echo "============"
+    echo "Create Assets in target dir"
+
+    discard existsOrCreateDir(targetDir)
+    writeFile(joinPath(targetDir, "style.css"), asset_style_css)
+    writeFile(joinPath(targetDir, "no_images.svg"), asset_noimages_svg)
+    writeFile(joinPath(targetDir, "iconic.svg"), asset_iconic_svg)
+    if enableJS:
+        writeFile(joinPath(targetDir, "exif.js"), asset_exif_js)
+        writeFile(joinPath(targetDir, "justified-layout.min.js"), asset_justified_layout_js)
+        writeFile(joinPath(targetDir, "albums.js"), asset_albums_js)
+
+
+proc removeOrphans (targetDir: string) = 
+    echo "Checking for orphaned files and folders..."
+    let sourceDir = joinPath(config.sourceDir, targetDir.replace(config.targetDir, ""))
+
+    #Albums
+    for album in walkDir(targetDir):
+        if album.kind != pcDir: continue
+
+        var targetDir = targetDir
+        normalizePath(targetDir)
+
+        var dirname = album.path.replace(targetDir & "/", "")
+        
+        if dirname == "thumbnails": continue
+        if dirname == "medium":     continue 
+
+        if not dirExists(joinPath(sourceDir, dirname)):
+            echo "Removing orphaned folder(" & joinPath(sourceDir, dirname) & ") from gallery!"
+            removeDir(joinPath(targetDir, dirname))
+
+    #Photos
+    for photo in walkDir(joinPath(targetDir, "/thumbnails/medium")):
+        let (dir, name, ext) = splitFile(photo.path)
+        let filename         = name & ext
+
+        if not fileExists(joinPath(sourceDir, filename)):
+            echo "Removing orphaned file(" & joinPath(sourceDir, filename) & ") from gallery!"
+            removeFile(joinPath(targetDir, name & ".html"))
+            removeFile(joinPath(targetDir, "thumbnails/small", name & ".jpg"))
+            removeFile(joinPath(targetDir, "thumbnails/medium", filename))
+
+
+
+###
+#
+# create 
+#
+###
+
 proc createPicture(path: string): Picture = 
     let
       allowedExtensions = @[".jpg", ".jpeg", ".JPG", ".JPEG"]

@@ -136,51 +192,12 @@ proc createAlbum(path: string, isRoot: bool): Album =
     result.pictures.sort(sortPictures)
 
 
-proc placeAssets(targetDir: string, enableJS: bool) =
-    echo "============"
-    echo "Create Assets in target dir"
-
-    discard existsOrCreateDir(targetDir)
-    writeFile(joinPath(targetDir, "style.css"), asset_style_css)
-    writeFile(joinPath(targetDir, "no_images.svg"), asset_noimages_svg)
-    writeFile(joinPath(targetDir, "iconic.svg"), asset_iconic_svg)
-    if enableJS:
-        writeFile(joinPath(targetDir, "exif.js"), asset_exif_js)
-        writeFile(joinPath(targetDir, "justified-layout.min.js"), asset_justified_layout_js)
-        writeFile(joinPath(targetDir, "albums.js"), asset_albums_js)
-
-
-proc removeOrphans (targetDir: string) = 
-    echo "Checking for orphaned files and folders..."
-    let sourceDir = joinPath(config.sourceDir, targetDir.replace(config.targetDir, ""))
-
-    #Albums
-    for album in walkDir(targetDir):
-        if album.kind != pcDir: continue
-
-        var targetDir = targetDir
-        normalizePath(targetDir)
-
-        var dirname = album.path.replace(targetDir & "/", "")
-        
-        if dirname == "thumbnails": continue
-        if dirname == "medium":     continue 
-
-        if not dirExists(joinPath(sourceDir, dirname)):
-            echo "Removing orphaned folder(" & joinPath(sourceDir, dirname) & ") from gallery!"
-            removeDir(joinPath(targetDir, dirname))
-
-    #Photos
-    for photo in walkDir(joinPath(targetDir, "/thumbnails/medium")):
-        let (dir, name, ext) = splitFile(photo.path)
-        let filename         = name & ext
-
-        if not fileExists(joinPath(sourceDir, filename)):
-            echo "Removing orphaned file(" & joinPath(sourceDir, filename) & ") from gallery!"
-            removeFile(joinPath(targetDir, name & ".html"))
-            removeFile(joinPath(targetDir, "thumbnails/small", name & ".jpg"))
-            removeFile(joinPath(targetDir, "thumbnails/medium", filename))
 
+###
+#
+# generate
+#
+###
 
 proc generateWebsite(targetDir: string, album: Album) =
     echo "============"

@@ -432,7 +449,6 @@ proc main =
             createSymlink(config.sourceDir, joinPath(config.targetDir, "originals"))
  
     placeAssets(config.targetDir, config.enableJS)
-
     generateWebsite(config.targetDir, mainAlbum)