Skip to content

Instantly share code, notes, and snippets.

@aligusnet
Last active December 27, 2015 22:59
Show Gist options
  • Save aligusnet/7402316 to your computer and use it in GitHub Desktop.
Save aligusnet/7402316 to your computer and use it in GitHub Desktop.
The script creates a web library of images
#!/usr/bin/env bash
IFS="$(printf '\n\t')" # eliminate whitespace in pathnames
# function real_path (path)
function real_path () {
[[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
}
# function build_href (caption, url)
function build_href () {
local caption="$1"
local url="$2"
printf '<a href="%s">%s</a>' "$url" "$caption"
}
# function build_page (image_path, prev_url, next_url)
function build_page () {
local image_path="$1"
local prev_url="$(basename ${2%.*}.html)"
if [ "$prev_url" = ".html" ]; then
prev_url=""
fi
local next_url="$(basename ${3%.*}.html)"
if [ "$next_url" = ".html" ]; then
next_url=""
fi
echo '<html>'
echo '<head>'
echo '<title> Picture: ' $(basename "$image_path") '</title>'
echo '<meta charset="UTF-8">'
echo '</head>'
echo '<body>'
echo '<h3> Picture: ' $(basename "$image_path") '</h3>'
echo '<p>' "$image_path" '<p>'
echo '<p>'
build_href first __first.html
echo ' '
build_href previous "$prev_url"
echo ' '
build_href next "$next_url"
echo ' '
build_href last __last.html
echo '</p>'
printf '<img src="%s"/>' "$image_path"
echo '</body>'
echo '</html>'
}
# function main (images)
function main () {
local output_path="html"
if [ -e "$output_path" ]; then
rm -rf "$output_path"
fi
mkdir "$output_path"
local prev_img=""
local curr_img=""
local next_img=""
for img in $@; do
if [ -n "$curr_img" ]; then
build_page "$curr_img" "$prev_img" "$next_img" > "$output_path"/$(basename "${curr_img%.*}.html")
if [ -z $prev_img ]; then
ln -s $(basename "${curr_img%.*}.html") "$output_path/__first.html"
fi
fi
prev_img="$curr_img"
curr_img="$next_img"
next_img="$img"
done
build_page "$curr_img" "$prev_img" "$next_img" > "$output_path"/$(basename "${curr_img%.*}.html")
prev_img="$curr_img"
curr_img="$next_img"
next_img=""
build_page "$curr_img" "$prev_img" "$next_img" > "$output_path"/$(basename "${curr_img%.*}.html")
ln -s $(basename "${curr_img%.*}.html") "$output_path/__last.html"
}
if (( $# != 1 )); then
echo Usage: $(basename "$0") path/to/img/folder
exit 2
fi
IMG_PATH=$(real_path $1)
main $(find "$IMG_PATH" \( -iname "*.jpg" -o -iname "*.png" \) -print )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment