From 56c3046d7fe94f19c0ff682364722738bae22119 Mon Sep 17 00:00:00 2001 From: Leonard Jonathan Oh Date: Fri, 28 Oct 2022 23:02:58 +0000 Subject: [PATCH] Feature: Add `-s|--scoped` flag to support scoping `index.htm` to each descendent folder --- README.md | 21 +- doc/assets/index.htm | 850 +++++++++++++++++++++++++++++++++++++++++++ webize | 130 ++++--- webize.bats | 37 +- 4 files changed, 968 insertions(+), 70 deletions(-) create mode 100644 doc/assets/index.htm diff --git a/README.md b/README.md index afc1c35..3e4c04f 100644 --- a/README.md +++ b/README.md @@ -32,14 +32,21 @@ At present, gallery is kept as minimalistic as possible. But it might already be ![](doc/assets/gallery.gif) ```sh -# Generate a `index.htm` for every descendent folder of /path/to with image files, and opens it in firefox -webize gallery /path/to/pictures | xargs firefox +# Linux: Generate index.htm in /path/to for all descendent image files, and opens it in firefox +webize gallery /path/to | xargs firefox -# Or, open in default web browser on a mac -webize gallery /path/to/pictures | xargs open +# Cleanup index.htm +webize clean /path/to -# Cleanup once you are done -webize clean /path/to/pictures +# Linux: Generate a scoped index.htm in each descendent folder of /path/to, and opens it in firefox +webize gallery -s /path/to | xargs firefox + +# Cleanup index.htm +webize clean -s /path/to + +# Mac: Generate index.htm in /path/to for all descendent image files, and opens it in firefox +webize gallery /path/to | xargs open +webize clean /path/to ``` Keyboard shortcuts: @@ -77,7 +84,7 @@ Available as [vscode tasks](.vscode/tasks.json). ./test.sh # To test the gallery, make some pictures available -cp -rf /path/to/pictures .pictures +cp -rf /path/to .pictures # Debug (Local browser) ./webize gallery . | xargs firefox diff --git a/doc/assets/index.htm b/doc/assets/index.htm new file mode 100644 index 0000000..0d309f1 --- /dev/null +++ b/doc/assets/index.htm @@ -0,0 +1,850 @@ + + + + + + + + 💻 + ↔️ + ↕️ + 🐜 + 🦕 + 🏷 + + 📌 + + + + + + + diff --git a/webize b/webize index 21dd0ae..4afdab0 100755 --- a/webize +++ b/webize @@ -5,17 +5,22 @@ set -u usage() { echo 'Usage: ./webize [OPTIONS]' echo 'Subcommands:' - echo ' gallery Generate a gallery index.htm' - echo ' clean Removes index.htm' + echo ' gallery [--scoped] Generate a gallery index.htm' + echo " -s|--scoped Scope index.htm to each descendent directory" + echo ' If omitted, image search is recursive' + echo ' clean [--scoped] Cleanup index.htm' + echo " -s|--scoped Scope index.htm to each descendent directory" echo 'Examples: ' - echo ' # Generate a gallery index.htm for current directory and all descendent folders which contain images' - echo ' ./webize gallery .' + echo ' # Linux: Generate index.htm in /path/to for all descendent image files, and opens it in firefox' + echo ' ./webize gallery /path/to | xargs firefox' + echo ' # Cleanup index.htm' + echo ' ./webize clean /path/to' echo - echo ' # Generate a gallery index.htm for /path/to/camera/roll and all descendent folders which contain images, and open them in firefox' - echo ' ./webize gallery /path/to/camera/roll | xargs firefox' + echo ' # Linux: Generate a scoped index.htm in each descendent folder of /path/to, and opens it in firefox' + echo ' ./webize gallery -s /path/to | xargs firefox' + echo ' # Cleanup index.htm' + echo ' ./webize clean -s /path/to' echo - echo ' # Clean index.htm in current directory' - echo ' ./webize clean .' } # Exit if we got no options if [ $# -eq 0 ]; then usage; exit 1; fi @@ -30,42 +35,37 @@ while test $# -gt 0; do gallery) GALLERY=1 shift - if test $# -gt 0; then - DIR="$1" - shift - else - echo "Please specify a directory as a third argument. E.g. /path/to, or . for current directory" 1>&2 - exit 1 - fi ;; clean) CLEAN=1 shift - if test $# -gt 0; then - DIR="$1" - shift - else - echo "Please specify a directory as a third argument. E.g. /path/to, or . for current directory" 1>&2 - exit 1 - fi + ;; + -s|--scoped) + SCOPED=1 + shift ;; *) - echo "Invalid option '$1'" 1>&2 - usage - exit 1 + DIR="$1" + shift ;; esac done -# Normalize, compile, and validation configuration +# Configuration DIR=${DIR:-} GALLERY=${GALLERY:-} CLEAN=${CLEAN:-} +SCOPED=${SCOPED:-} + +# Normalization +DIR=$( echo "$DIR" | sed 's@/$@@' ) # Trim trailing '/' +DIR=$( cd "$DIR" && pwd ) + +# Validation if [ ! -d "$DIR" ]; then echo "Directory does not exist: $DIR" 1>&2 exit 1 fi -DIR=$( echo "$DIR" | sed 's@/$@@' ) # Trim trailing '/' PLATFORM=linux if echo "${OSTYPE:-}" | grep -iE 'darwin' > /dev/null; then @@ -75,29 +75,46 @@ if ! ls -al --time-style='+%Y' > /dev/null 2>&1; then PLATFORM=busybox fi +if [ -n "$SCOPED" ]; then + DIRS=$( find "$DIR" -type d ) +else + DIRS="$DIR" +fi + # Generate a index.htm in current directory if [ -n "$GALLERY" ]; then - # Get images files, then parse file attributes into a single CSV - # E.g.: - # -rwxr-xr-x 1 user user 22681 2021-08-27T07:36:45+0000 /path/to/my.png - # Becomes: - # /path/to/my.png,22681,2021-08-27T07:36:45+0000 - IMAGES_REGEX='(\.gif|\.jpe?g|\.png|\.svg|\.webp)$' - IMAGES_CSV=$( - if [ "$PLATFORM" = 'darwin' ]; then - find "$DIR" -type f | grep -iE "$IMAGES_REGEX" | while read -r f; do stat -l -t '%Y-%m-%dT%H:%M:%S%z' "$f" | while read -r a b c d size dateiso filename; do echo "$filename,$size,$dateiso"; done; done - elif [ "$PLATFORM" = 'busybox' ]; then - find "$DIR" -type f | grep -iE "$IMAGES_REGEX" | while read -r f; do ls -al --full-time "$f" | while read -r a b c d size date time timezone filename; do echo "$filename,$size,${date}T${time}${timezone}"; done; done - else - find "$DIR" -type f | grep -iE "$IMAGES_REGEX" | while read -r f; do ls -al --time-style='+%Y-%m-%dT%H:%M:%S%z' "$f" | while read -r a b c d size dateiso filename; do echo "$filename,$size,$dateiso"; done; done + echo "$DIRS" | while read -r d; do + IMAGES_REGEX='(\.gif|\.jpe?g|\.png|\.svg|\.webp)$' + IMAGES=$( + if [ -n "$SCOPED" ]; then + find "$d" -type f -maxdepth 1 | grep -iE "$IMAGES_REGEX" + else + find "$d" -type f | grep -iE "$IMAGES_REGEX" + fi + ) + if [ -n "$SCOPED" ] && [ -z "$IMAGES" ]; then + continue + fi + if [ -z "$SCOPED" ] && [ -z "$IMAGES" ]; then + echo "No images found in: $d" >&2 + exit 1 fi - ) - if [ -z "$IMAGES_CSV" ]; then - echo "No images found in: $DIR" - exit 1 - fi - INDEX_HTM="$DIR/index.htm" - cat - > "$INDEX_HTM" <<'EOF' + IMAGES_CSV=$( + # Get images files, then parse file attributes into a single CSV + # E.g.: + # -rwxr-xr-x 1 user user 22681 2021-08-27T07:36:45+0000 /path/to/my.png + # Becomes: + # /path/to/my.png,22681,2021-08-27T07:36:45+0000 + if [ "$PLATFORM" = 'darwin' ]; then + echo "$IMAGES" | while read -r f; do stat -l -t '%Y-%m-%dT%H:%M:%S%z' "$f" | while read -r a b c d size dateiso filename; do echo "$filename,$size,$dateiso"; done; done + elif [ "$PLATFORM" = 'busybox' ]; then + echo "$IMAGES" | while read -r f; do ls -al --full-time "$f" | while read -r a b c d size date time timezone filename; do echo "$filename,$size,${date}T${time}${timezone}"; done; done + else + echo "$IMAGES" | while read -r f; do ls -al --time-style='+%Y-%m-%dT%H:%M:%S%z' "$f" | while read -r a b c d size dateiso filename; do echo "$filename,$size,$dateiso"; done; done + fi + ) + INDEX_HTM="$d/index.htm" + cat - > "$INDEX_HTM" <<'EOF' @@ -134,8 +151,8 @@ if [ -n "$GALLERY" ]; then // Images const IMAGES_CSV = ` EOF - echo "$IMAGES_CSV" >> "$INDEX_HTM" -cat - >> "$INDEX_HTM" <<'EOF' + echo "$IMAGES_CSV" >> "$INDEX_HTM" + cat - >> "$INDEX_HTM" <<'EOF' `; // Parse into image objects const imageObjects = (function(IMAGES_CSV) { @@ -951,14 +968,17 @@ cat - >> "$INDEX_HTM" <<'EOF' EOF - echo "$INDEX_HTM" + echo "$INDEX_HTM" + done fi # Remove index.htm in each directory if [ -n "$CLEAN" ]; then - INDEX_HTM="$DIR/index.htm" - if [ -f "$INDEX_HTM" ]; then - rm -f "$INDEX_HTM" - echo "$INDEX_HTM" - fi + echo "$DIRS" | while read -r d; do + INDEX_HTM="$d/index.htm" + if [ -f "$INDEX_HTM" ]; then + rm -f "$INDEX_HTM" + echo "$INDEX_HTM" + fi + done fi diff --git a/webize.bats b/webize.bats index 1acd23e..a76f000 100644 --- a/webize.bats +++ b/webize.bats @@ -1,17 +1,18 @@ setup() { mkdir -p "$BATS_FILE_TMPDIR/pictures" + mkdir -p "$BATS_FILE_TMPDIR/pictures/child" touch "$BATS_FILE_TMPDIR/pictures/01.gif" touch "$BATS_FILE_TMPDIR/pictures/02.GIF" touch "$BATS_FILE_TMPDIR/pictures/03.jpg" touch "$BATS_FILE_TMPDIR/pictures/04.jpeg" touch "$BATS_FILE_TMPDIR/pictures/05.JPG" touch "$BATS_FILE_TMPDIR/pictures/06.JPEG" - touch "$BATS_FILE_TMPDIR/pictures/07.png" - touch "$BATS_FILE_TMPDIR/pictures/08.PNG" - touch "$BATS_FILE_TMPDIR/pictures/09.svg" - touch "$BATS_FILE_TMPDIR/pictures/10.SVG" - touch "$BATS_FILE_TMPDIR/pictures/11.webp" - touch "$BATS_FILE_TMPDIR/pictures/12.WEBP" + touch "$BATS_FILE_TMPDIR/pictures/child/07.png" + touch "$BATS_FILE_TMPDIR/pictures/child/08.PNG" + touch "$BATS_FILE_TMPDIR/pictures/child/09.svg" + touch "$BATS_FILE_TMPDIR/pictures/child/10.SVG" + touch "$BATS_FILE_TMPDIR/pictures/child/11.webp" + touch "$BATS_FILE_TMPDIR/pictures/child/12.WEBP" } teardown() { @@ -30,7 +31,7 @@ teardown() { [ "$status" = 1 ] } -@test "gallery - Generates index.htm with correct number of images" { +@test "webize gallery - Generates index.htm" { "$BATS_TEST_DIRNAME/webize" gallery "$BATS_FILE_TMPDIR/pictures" | grep index.htm [ -f "$BATS_FILE_TMPDIR/pictures/index.htm" ] @@ -38,9 +39,29 @@ teardown() { [ $( cat "$BATS_FILE_TMPDIR/pictures/index.htm" | grep 'const IMAGES_CSV' -A12 | grep -Ei '(\.gif|\.jpe?g|\.png|\.svg|\.webp)' | wc -l ) = 12 ] } -@test "clean - Cleans index.htm" { +@test "webize clean - Cleans index.htm" { touch "$BATS_FILE_TMPDIR/pictures/index.htm" "$BATS_TEST_DIRNAME/webize" clean "$BATS_FILE_TMPDIR/pictures" | grep index.htm [ ! -f "$BATS_FILE_TMPDIR/pictures/index.htm" ] } + +@test "webize gallery --scoped - Generates index.htm in each descendent folder" { + "$BATS_TEST_DIRNAME/webize" gallery "$BATS_FILE_TMPDIR/pictures" --scoped | grep index.htm + + [ -f "$BATS_FILE_TMPDIR/pictures/index.htm" ] + [ -s "$BATS_FILE_TMPDIR/pictures/index.htm" ] + [ $( cat "$BATS_FILE_TMPDIR/pictures/index.htm" | grep 'const IMAGES_CSV' -A6 | grep -Ei '(\.gif|\.jpe?g|\.png|\.svg|\.webp)' | wc -l ) = 6 ] + [ -f "$BATS_FILE_TMPDIR/pictures/child/index.htm" ] + [ -s "$BATS_FILE_TMPDIR/pictures/child/index.htm" ] + [ $( cat "$BATS_FILE_TMPDIR/pictures/index.htm" | grep 'const IMAGES_CSV' -A6 | grep -Ei '(\.gif|\.jpe?g|\.png|\.svg|\.webp)' | wc -l ) = 6 ] +} + +@test "webize clean --scoped - Cleans index.htm in each descendent folder" { + touch "$BATS_FILE_TMPDIR/pictures/index.htm" + touch "$BATS_FILE_TMPDIR/pictures/child/index.htm" + "$BATS_TEST_DIRNAME/webize" clean "$BATS_FILE_TMPDIR/pictures" --scoped | grep index.htm + + [ ! -f "$BATS_FILE_TMPDIR/pictures/index.htm" ] + [ ! -f "$BATS_FILE_TMPDIR/pictures/child/index.htm" ] +}