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
25 function bpgallery_default_head() {
28 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">
29 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
31 <title>${TITLE}</title>
32 <link rel="stylesheet" href="style.css" type="text/css" />
38 declare -rf bpgallery_default_head
40 function bpgallery_default_description() {
47 declare -rf bpgallery_default_description
49 function bpgallery_default_thumbsline() {
50 if [[ -z $caption ]]; then
56 <div class="thumbnail"><a href="$link"><img src="icons/$filename" alt="$caption_alt" /></a><div class="caption">$caption</div></div>
60 declare -rf bpgallery_default_thumbsline
62 function bpgallery_default_tail() {
69 declare -rf bpgallery_default_tail
71 function bpgallery_default_stylesheet() {
76 font-family: sans-serif;
85 height: ${MAXHEIGHT}px;
105 div.navigation ul li {
120 declare -rf bpgallery_default_stylesheet
122 function bpgallery_default_page() {
123 $BPGALLERY_HEAD_FUNCTION
128 if [[ ! -z $linklocation ]]; then
129 linkstart="<a href='$linklocation'>"
135 <div class="navigation">
137 <li><a href='${INDEXDOCUMENT}'>Thumbnails</a></li>
139 if [[ ! -z $previouspage ]]; then
140 $ECHOCOMMAND "<li><a href='${previouspage}'>Previous</a></li>"
142 $ECHOCOMMAND "<li>Previous</li>"
145 if [[ ! -z $nextpage ]]; then
146 $ECHOCOMMAND "<li><a href='${nextpage}'>Next</a></li>"
148 $ECHOCOMMAND "<li>Next</li>"
153 <div class="mainimage">
154 ${linkstart}<img src="${filename}" alt="${caption}" />${linkend}
157 $BPGALLERY_TAIL_FUNCTION
160 declare -rf bpgallery_default_page
162 function bpgallery_escape_url() {
180 declare -rf bpgallery_escape_url
182 if [[ ! -z ${BPGALLERY_THEME} ]] ; then
183 declare -r BPGALLERY_THEME
186 if [[ ! -z ${TITLE} ]] ; then
190 if [[ -e /usr/local/etc/bpgallery/config ]] ; then
191 . /usr/local/etc/bpgallery/config 2>/dev/null
194 if [[ -e /etc/bpgallery/config ]] ; then
195 . /etc/bpgallery/config 2>/dev/null
198 if [[ -e $HOME/.bpgallery.rc ]]; then
199 . $HOME/.bpgallery.rc 2>/dev/null
202 if [[ -e $1 && -d $1 && -e $1/.bpgallery.rc ]]; then
203 . $1/.bpgallery.rc 2>/dev/null
206 if [[ -z ${BPGALLERY_THEME} ]]; then
207 BPGALLERY_THEME=default
210 if [[ ! -z ${BPGALLERY_THEME_DIR} ]] && \
211 [[ -e ${BPGALLERY_THEME_DIR}/${BPGALLERY_THEME} ]]; then
212 . ${BPGALLERY_THEME_DIR}/${BPGALLERY_THEME} 2>/dev/null
213 elif [[ -e $HOME/.bpgallery.themes/${BPGALLERY_THEME} ]]; then
214 . $HOME/.bpgallery.themes/${BPGALLERY_THEME} 2>/dev/null
215 elif [[ -e /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
216 . /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
217 elif [[ -e /etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
218 . /etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
221 if [[ -z $TITLE ]]; then
222 TITLE="Photo Gallery"
225 if [[ -z $CONVERTTOOL ]]; then
229 if [[ -z $FINDCOMMAND ]]; then
233 if [[ -z $XARGSCOMMAND ]]; then
237 if [[ -z $ECHOCOMMAND ]]; then
241 if [[ -z $SORTCOMMAND ]]; then
245 if [[ -z $IDENTIFYCOMMAND ]]; then
246 IDENTIFYCOMMAND=identify
249 if [[ -z $HEADCOMMAND ]]; then
253 if [[ -z $GREPCOMMAND ]]; then
257 if [[ -z $SEDCOMMAND ]]; then
261 if [[ -z $WIDTH ]]; then
265 if [[ -z $IMAGEEXTENSIONS ]]; then
266 IMAGEEXTENSIONS="jpeg jpg gif png";
269 if [[ -z $WCCOMMAND ]]; then
273 if [[ -z $CAPTIONHEIGHT ]]; then
277 if [[ -z $OUTPUTHTML ]]; then
281 if [[ -z $GENERATEPAGESFORFULLSIZE ]]; then
282 GENERATEPAGESFORFULLSIZE=0
285 if [[ -z $GENERATEPAGESFORMEDIUMSIZE ]]; then
286 GENERATEPAGESFORMEDIUMSIZE=1
289 if [[ -z $MEDIUMWIDTH ]]; then
293 if [[ -z $INDEXDOCUMENT ]]; then
294 INDEXDOCUMENT=index.html
297 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
298 if declare -F "bpgallery_${BPGALLERY_THEME}_page" > /dev/null ; then
299 BPGALLERY_PAGE_FUNCTION="bpgallery_${BPGALLERY_THEME}_page"
301 BPGALLERY_PAGE_FUNCTION="bpgallery_default_page"
305 if declare -F "bpgallery_${BPGALLERY_THEME}_head" > /dev/null ; then
306 BPGALLERY_HEAD_FUNCTION="bpgallery_${BPGALLERY_THEME}_head"
308 BPGALLERY_HEAD_FUNCTION="bpgallery_default_head"
311 if declare -F "bpgallery_${BPGALLERY_THEME}_description" > /dev/null ; then
312 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_${BPGALLERY_THEME}_description"
314 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_default_description"
317 if declare -F "bpgallery_${BPGALLERY_THEME}_thumbsline" > /dev/null ; then
318 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_${BPGALLERY_THEME}_thumbsline"
320 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_default_thumbsline"
323 if declare -F "bpgallery_${BPGALLERY_THEME}_tail" > /dev/null ; then
324 BPGALLERY_TAIL_FUNCTION="bpgallery_${BPGALLERY_THEME}_tail"
326 BPGALLERY_TAIL_FUNCTION="bpgallery_default_tail"
329 if declare -F "bpgallery_${BPGALLERY_THEME}_stylesheet" > /dev/null ; then
330 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_${BPGALLERY_THEME}_stylesheet"
332 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_default_stylesheet"
337 for imageext in $IMAGEEXTENSIONS; do
338 FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -iname '*.$imageext' -print0'
341 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
342 FINDIMAGESOPTIONS='-maxdepth 1 '${FINDIMAGESOPTIONS}
347 $0 [--help|--version|<path to images>]
350 displays this help screen
352 displays the version and exits
354 This can also takes some environment variables, these will use defaults if not
358 the title of the gallery
361 the width to make the icons
364 set the width of images in the medium size pages
367 set the theme to use (described below)
370 set an extra location to look for themes
373 sets the script output to be wrapped in a <pre> block
375 GENERATEPAGESFORMEDIUMSIZE
376 generate medium sized images and pages
378 GENERATEPAGESFORFULLSIZE
379 decide wether to generate pages for the full size images or not
382 name of the index page (e.g. index.html)
385 TITLE="My Funky Gallery" WIDTH=200 INDEXDOCUMENT="welcome.html" GENERATEPAGESFORFULLSIZE=1 GENERATEPAGESFORMEDIUMSIZE=1 MEDIUMWIDTH=400 $0 /path/to/image/folder
395 function generate_resized_images() {
396 $ECHOCOMMAND "Generating $2"
398 totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | $WCCOMMAND);
399 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)
401 if [ $totalimages -eq 0 ]; then
402 # might as well exit at this point - there are no pictures
403 $ECHOCOMMAND "No images for the gallery - exiting."
406 # check if the directory exists and create it other wise
412 $ECHOCOMMAND "Can't write to $2 directory, exiting"
417 if [ $totalimages -eq $imagestoupdate ]; then
418 $ECHOCOMMAND "Regenerating all $2"
419 elif [ $imagestoupdate -eq 0 ]; then
420 $ECHOCOMMAND "No Updated $2, not regenerating"
423 $ECHOCOMMAND "Generating $imagestoupdate of $totalimages $2"
426 $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
427 $ECHOCOMMAND done: $imagestoupdate/$imagestoupdate images
428 $ECHOCOMMAND "Completed generating $2 for $totalimages images"
431 function generate_thumbs() {
432 generate_resized_images $WIDTH icons
435 function generate_medium() {
436 generate_resized_images $MEDIUMWIDTH medium
439 function generate_pages() {
440 $ECHOCOMMAND "Generating Pages"
460 if [ -z $2 ] && [[ $GENERATEPAGESFORMEDIUMSIZE != 0 ]]; then
464 $FINDCOMMAND . $FINDIMAGESOPTIONS | \
465 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
467 while read imagefilename; do
468 previousimage=$currentimage
469 currentimage=$nextimage
470 nextimage=${imagefilename#./}
473 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
474 addlink=${currentimage}${extra}.html
476 addlink=${currentimage}
479 if [[ $addlinks == 1 ]]; then
480 addlink=${currentimage}.html
482 addlink=${currentimage}${extra}.html
485 previouspage=$currentpage
486 currentpage=$nextpage
487 if [[ $addlinks == 1 ]]; then
488 nextpage=${nextimage}${extra}.html
490 nextpage=${nextimage}.html
492 filename=${extradir}${currentimage}
493 generate_general_page "$previouspage" "$currentpage" "$nextpage" $addlink
494 $ECHOCOMMAND $nextimage
500 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
501 addlink=${currentpage}.html
503 addlink=${currentpage}
506 if [[ $addlinks == 1 ]]; then
507 addlink=${currentpage}.html
509 addlink=${currentpage}${extra}.html
512 filename=${extradir}${currentpage}
513 if [[ $addlinks == 1 ]]; then
514 currentpage=${currentpage}${extra}.html
515 previouspage=${previouspage}${extra}.html
517 currentpage=${currentpage}.html
518 previouspage=${previouspage}.html
521 generate_general_page "$previouspage" "$currentpage" "" $addlink
525 function generate_medium_pages() {
526 generate_pages __medium medium
529 function generate_general_page() {
544 if [[ ! -z $4 ]]; then
548 if [ -r captions.txt ]; then
549 caption=$($GREPCOMMAND -E "^${filename} " captions.txt); caption=${caption#* }
554 $BPGALLERY_PAGE_FUNCTION > "$currentpage"
557 if [[ $OUTPUTHTML != 0 ]]; then
562 $ECHOCOMMAND "No path given"
567 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
572 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
577 if [[ ! -d $1 ]]; then
578 $ECHOCOMMAND "$1 is not a directory"
585 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
586 $ECHOCOMMAND "$1 does not contain any images. Quitting."
591 $ECHOCOMMAND "Can't write to images directory, exiting"
595 if [ -e ${INDEXDOCUMENT} ] && [ ! -w ${INDEXDOCUMENT} ]; then
596 $ECHOCOMMAND "Can't write ${INDEXDOCUMENT}, exiting"
600 if [ -e style.css ] && [ ! -w style.css ]; then
601 $ECHOCOMMAND "Can't write style.css, exiting"
607 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
609 generate_medium_pages
612 if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then
613 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
614 generate_pages __medium
620 if [ -r description.txt ] ; then
621 DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
627 $ECHOCOMMAND "Starting to generate page"
629 $BPGALLERY_HEAD_FUNCTION > ${INDEXDOCUMENT}
630 $BPGALLERY_DESCRIPTION_FUNCTION >> ${INDEXDOCUMENT}
632 $ECHOCOMMAND "Adding Captions"
635 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
639 $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}
641 $BPGALLERY_TAIL_FUNCTION >> ${INDEXDOCUMENT}
643 $ECHOCOMMAND "Finished generating the page"
644 $ECHOCOMMAND "Generating stylesheet"
647 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
648 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
652 # add a bit to the maxheight for the size of the caption
653 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
655 $BPGALLERY_STYLESHEET_FUNCTION > style.css
657 $ECHOCOMMAND "All done"
659 if [[ $OUTPUTHTML != 0 ]]; then
660 $ECHOCOMMAND "</pre>"