3 # bpgallery.sh - builds a simple 'gallery' from a collection of images
4 # Copyright (C) 2004 Brett Parker <iDunno@sommitrealweird.co.uk>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 function bpgallery_default_head() {
27 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">
28 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
30 <title>${TITLE}</title>
31 <link rel="stylesheet" href="style.css" type="text/css" />
37 declare -rf bpgallery_default_head
39 function bpgallery_default_description() {
46 declare -rf bpgallery_default_description
48 function bpgallery_default_thumbsline() {
49 if [[ -z $caption ]]; then
55 <div class="thumbnail"><a href="$link"><img src="icons/$filename" alt="$caption_alt" /></a><div class="caption">$caption</div></div>
59 declare -rf bpgallery_default_thumbsline
61 function bpgallery_default_tail() {
68 declare -rf bpgallery_default_tail
70 function bpgallery_default_stylesheet() {
75 font-family: sans-serif;
84 height: ${MAXHEIGHT}px;
104 div.navigation ul li {
119 declare -rf bpgallery_default_stylesheet
121 function bpgallery_default_page() {
122 $BPGALLERY_HEAD_FUNCTION
127 if [[ ! -z $linklocation ]]; then
128 linkstart="<a href='$linklocation'>"
134 <div class="navigation">
136 <li><a href='${INDEXDOCUMENT}'>Thumbnails</a></li>
138 if [[ ! -z $previouspage ]]; then
139 $ECHOCOMMAND "<li><a href='${previouspage}'>Previous</a></li>"
141 $ECHOCOMMAND "<li>Previous</li>"
144 if [[ ! -z $nextpage ]]; then
145 $ECHOCOMMAND "<li><a href='${nextpage}'>Next</a></li>"
147 $ECHOCOMMAND "<li>Next</li>"
152 <div class="mainimage">
153 ${linkstart}<img src="${filename}" alt="${caption}" />${linkend}
156 $BPGALLERY_TAIL_FUNCTION
159 declare -rf bpgallery_default_page
161 function bpgallery_escape_url() {
179 declare -rf bpgallery_escape_url
181 if [[ ! -z ${BPGALLERY_THEME} ]] ; then
182 declare -r BPGALLERY_THEME
185 if [[ ! -z ${TITLE} ]] ; then
189 if [[ -e /usr/local/etc/bpgallery/config ]] ; then
190 . /usr/local/etc/bpgallery/config 2>/dev/null
193 if [[ -e /etc/bpgallery/config ]] ; then
194 . /etc/bpgallery/config 2>/dev/null
197 if [[ -e $HOME/.bpgallery.rc ]]; then
198 . $HOME/.bpgallery.rc 2>/dev/null
201 if [[ -e $1 && -d $1 && -e $1/.bpgallery.rc ]]; then
202 . $1/.bpgallery.rc 2>/dev/null
205 if [[ -z ${BPGALLERY_THEME} ]]; then
206 BPGALLERY_THEME=default
209 if [[ ! -z ${BPGALLERY_THEME_DIR} ]] && \
210 [[ -e ${BPGALLERY_THEME_DIR}/${BPGALLERY_THEME} ]]; then
211 . ${BPGALLERY_THEME_DIR}/${BPGALLERY_THEME} 2>/dev/null
212 elif [[ -e $HOME/.bpgallery.themes/${BPGALLERY_THEME} ]]; then
213 . $HOME/.bpgallery.themes/${BPGALLERY_THEME} 2>/dev/null
214 elif [[ -e /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
215 . /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
216 elif [[ -e /etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
217 . /etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
220 if [[ -z $TITLE ]]; then
221 TITLE="Photo Gallery"
224 if [[ -z $CONVERTTOOL ]]; then
228 if [[ -z $FINDCOMMAND ]]; then
232 if [[ -z $XARGSCOMMAND ]]; then
236 if [[ -z $ECHOCOMMAND ]]; then
240 if [[ -z $SORTCOMMAND ]]; then
244 if [[ -z $IDENTIFYCOMMAND ]]; then
245 IDENTIFYCOMMAND=identify
248 if [[ -z $HEADCOMMAND ]]; then
252 if [[ -z $GREPCOMMAND ]]; then
256 if [[ -z $SEDCOMMAND ]]; then
260 if [[ -z $WIDTH ]]; then
264 if [[ -z $IMAGEEXTENSIONS ]]; then
265 IMAGEEXTENSIONS="jpeg jpg gif png";
268 if [[ -z $WCCOMMAND ]]; then
272 if [[ -z $CAPTIONHEIGHT ]]; then
276 if [[ -z $OUTPUTHTML ]]; then
280 if [[ -z $GENERATEPAGESFORFULLSIZE ]]; then
281 GENERATEPAGESFORFULLSIZE=0
284 if [[ -z $GENERATEPAGESFORMEDIUMSIZE ]]; then
285 GENERATEPAGESFORMEDIUMSIZE=1
288 if [[ -z $MEDIUMWIDTH ]]; then
292 if [[ -z $INDEXDOCUMENT ]]; then
293 INDEXDOCUMENT=index.html
296 if declare -F "bpgallery_${BPGALLERY_THEME}_page" > /dev/null ; then
297 BPGALLERY_PAGE_FUNCTION="bpgallery_${BPGALLERY_THEME}_page"
299 BPGALLERY_PAGE_FUNCTION="bpgallery_default_page"
302 if declare -F "bpgallery_${BPGALLERY_THEME}_head" > /dev/null ; then
303 BPGALLERY_HEAD_FUNCTION="bpgallery_${BPGALLERY_THEME}_head"
305 BPGALLERY_HEAD_FUNCTION="bpgallery_default_head"
308 if declare -F "bpgallery_${BPGALLERY_THEME}_description" > /dev/null ; then
309 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_${BPGALLERY_THEME}_description"
311 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_default_description"
314 if declare -F "bpgallery_${BPGALLERY_THEME}_thumbsline" > /dev/null ; then
315 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_${BPGALLERY_THEME}_thumbsline"
317 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_default_thumbsline"
320 if declare -F "bpgallery_${BPGALLERY_THEME}_tail" > /dev/null ; then
321 BPGALLERY_TAIL_FUNCTION="bpgallery_${BPGALLERY_THEME}_tail"
323 BPGALLERY_TAIL_FUNCTION="bpgallery_default_tail"
326 if declare -F "bpgallery_${BPGALLERY_THEME}_stylesheet" > /dev/null ; then
327 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_${BPGALLERY_THEME}_stylesheet"
329 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_default_stylesheet"
334 for imageext in $IMAGEEXTENSIONS; do
335 FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -iname '*.$imageext' -print0'
338 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
339 FINDIMAGESOPTIONS='-maxdepth 1 '${FINDIMAGESOPTIONS}
344 $0 [--help|--version|<path to images>]
347 displays this help screen
349 displays the version and exits
351 This can also takes some environment variables, these will use defaults if not
355 the title of the gallery
358 the width to make the icons
361 set the width of images in the medium size pages
364 set the theme to use (described below)
367 set an extra location to look for themes
370 sets the script output to be wrapped in a <pre> block
372 GENERATEPAGESFORMEDIUMSIZE
373 generate medium sized images and pages
375 GENERATEPAGESFORFULLSIZE
376 decide wether to generate pages for the full size images or not
379 name of the index page (e.g. index.html)
382 TITLE="My Funky Gallery" WIDTH=200 INDEXDOCUMENT="welcome.html" GENERATEPAGESFORFULLSIZE=1 GENERATEPAGESFORMEDIUMSIZE=1 MEDIUMWIDTH=400 $0 /path/to/image/folder
392 function generate_resized_images() {
393 $ECHOCOMMAND "Generating $2"
395 totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | $WCCOMMAND);
396 imagestoupdate=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | while read filename; do filename=${filename//\"/\\\"}; if [ ! -r "$2/$filename" ] || [ "$filename" -nt "$2/$filename" ] || [ $($IDENTIFYCOMMAND -format "%w" "$2/$filename") -ne $1 ]; then $ECHOCOMMAND $filename; fi; done | $WCCOMMAND)
398 if [ $totalimages -eq 0 ]; then
399 # might as well exit at this point - there are no pictures
400 $ECHOCOMMAND "No images for the gallery - exiting."
403 # check if the directory exists and create it other wise
409 $ECHOCOMMAND "Can't write to $2 directory, exiting"
414 if [ $totalimages -eq $imagestoupdate ]; then
415 $ECHOCOMMAND "Regenerating all $2"
416 elif [ $imagestoupdate -eq 0 ]; then
417 $ECHOCOMMAND "No Updated $2, not regenerating"
420 $ECHOCOMMAND "Generating $imagestoupdate of $totalimages $2"
423 $FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | while read filename; do tempfilename=${filename//\"/\\\"/}; if [ ! -r "$2/$tempfilename" ] || [ "$tempfilename" -nt "$2/$tempfilename" ] || [ $($IDENTIFYCOMMAND -format "%w" "$2/$filename") -ne $1 ]; then $ECHOCOMMAND -n $filename; $ECHOCOMMAND -n -e "\000"; fi; done | $XARGSCOMMAND -0 --verbose --max-procs=4 -I {} $CONVERTTOOL -resize $1 '{}' $2/'{}' 2>&1 | while read throwout; do $ECHOCOMMAND done: $currentimage/$imagestoupdate images; currentimage=$((currentimage+1)); done
424 $ECHOCOMMAND done: $imagestoupdate/$imagestoupdate images
425 $ECHOCOMMAND "Completed generating $2 for $totalimages images"
428 function generate_thumbs() {
429 generate_resized_images $WIDTH icons
432 function generate_medium() {
433 generate_resized_images $MEDIUMWIDTH medium
436 function generate_pages() {
437 $ECHOCOMMAND "Generating Pages"
457 if [ -z $2 ] && [[ $GENERATEPAGESFORMEDIUMSIZE != 0 ]]; then
461 $FINDCOMMAND . $FINDIMAGESOPTIONS | \
462 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
464 while read imagefilename; do
465 previousimage=$currentimage
466 currentimage=$nextimage
467 nextimage=${imagefilename#./}
470 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
471 if [[ $addlinks == 1 ]]; then
472 addlink=${currentimage}.html
474 addlink=${currentimage}${extra}.html
477 addlink=${currentimage}
480 previouspage=$currentpage
481 currentpage=$nextpage
482 if [[ $addlinks == 1 ]]; then
483 nextpage=${nextimage}${extra}.html
485 nextpage=${nextimage}.html
487 filename=${extradir}${currentimage}
488 if [ "x$currentpage" != "x" ]; then
489 generate_general_page "$previouspage" "$currentpage" "$nextpage" $addlink
491 $ECHOCOMMAND $nextimage
497 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
498 if [[ $addlinks == 1 ]]; then
499 addlink=${currentpage}.html
501 addlink=${currentpage}${extra}.html
504 addlink=${currentpage}
507 filename=${extradir}${currentpage}
508 if [[ $addlinks == 1 ]]; then
509 currentpage=${currentpage}${extra}.html
510 previouspage=${previouspage}${extra}.html
512 currentpage=${currentpage}.html
513 previouspage=${previouspage}.html
516 generate_general_page "$previouspage" "$currentpage" "" $addlink
520 function generate_medium_pages() {
521 generate_pages __medium medium
524 function generate_general_page() {
539 if [[ ! -z $4 ]]; then
543 if [ -r captions.txt ]; then
544 imagefilename=${filename##*/}
545 caption=$($GREPCOMMAND -E "^${imagefilename} " captions.txt); caption=${caption#* }
550 $BPGALLERY_PAGE_FUNCTION > "$currentpage"
553 if [[ $OUTPUTHTML != 0 ]]; then
558 $ECHOCOMMAND "No path given"
563 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
568 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
573 if [[ ! -d $1 ]]; then
574 $ECHOCOMMAND "$1 is not a directory"
581 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
582 $ECHOCOMMAND "$1 does not contain any images. Quitting."
587 $ECHOCOMMAND "Can't write to images directory, exiting"
591 if [ -e ${INDEXDOCUMENT} ] && [ ! -w ${INDEXDOCUMENT} ]; then
592 $ECHOCOMMAND "Can't write ${INDEXDOCUMENT}, exiting"
596 if [ -e style.css ] && [ ! -w style.css ]; then
597 $ECHOCOMMAND "Can't write style.css, exiting"
603 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
605 generate_medium_pages
608 if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then
609 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
610 generate_pages __medium
616 if [ -r description.txt ] ; then
617 DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
623 $ECHOCOMMAND "Starting to generate page"
625 $BPGALLERY_HEAD_FUNCTION > ${INDEXDOCUMENT}
626 $BPGALLERY_DESCRIPTION_FUNCTION >> ${INDEXDOCUMENT}
628 $ECHOCOMMAND "Adding Captions"
631 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
635 $FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | $SORTCOMMAND -g | while read filename; do filename=${filename#./}; if [ -r captions.txt ]; then caption=$($GREPCOMMAND -E "^$filename " captions.txt); caption=${caption#* }; else caption=""; fi; if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then link=$(bpgallery_escape_url "${filename}${extra}.html"); elif [ $GENERATEPAGESFORFULLSIZE != 0 ]; then link=$(bpgallery_escape_url "${filename}.html"); else link=$(bpgallery_escape_url "$filename"); fi; filename=$(bpgallery_escape_url "$filename"); $BPGALLERY_THUMBSLINE_FUNCTION; done >> ${INDEXDOCUMENT}
637 $BPGALLERY_TAIL_FUNCTION >> ${INDEXDOCUMENT}
639 $ECHOCOMMAND "Finished generating the page"
640 $ECHOCOMMAND "Generating stylesheet"
643 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
644 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
648 # add a bit to the maxheight for the size of the caption
649 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
651 $BPGALLERY_STYLESHEET_FUNCTION > style.css
653 $ECHOCOMMAND "All done"
655 if [[ $OUTPUTHTML != 0 ]]; then
656 $ECHOCOMMAND "</pre>"