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 declare -F "bpgallery_${BPGALLERY_THEME}_page" > /dev/null ; then
298 BPGALLERY_PAGE_FUNCTION="bpgallery_${BPGALLERY_THEME}_page"
300 BPGALLERY_PAGE_FUNCTION="bpgallery_default_page"
303 if declare -F "bpgallery_${BPGALLERY_THEME}_head" > /dev/null ; then
304 BPGALLERY_HEAD_FUNCTION="bpgallery_${BPGALLERY_THEME}_head"
306 BPGALLERY_HEAD_FUNCTION="bpgallery_default_head"
309 if declare -F "bpgallery_${BPGALLERY_THEME}_description" > /dev/null ; then
310 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_${BPGALLERY_THEME}_description"
312 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_default_description"
315 if declare -F "bpgallery_${BPGALLERY_THEME}_thumbsline" > /dev/null ; then
316 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_${BPGALLERY_THEME}_thumbsline"
318 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_default_thumbsline"
321 if declare -F "bpgallery_${BPGALLERY_THEME}_tail" > /dev/null ; then
322 BPGALLERY_TAIL_FUNCTION="bpgallery_${BPGALLERY_THEME}_tail"
324 BPGALLERY_TAIL_FUNCTION="bpgallery_default_tail"
327 if declare -F "bpgallery_${BPGALLERY_THEME}_stylesheet" > /dev/null ; then
328 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_${BPGALLERY_THEME}_stylesheet"
330 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_default_stylesheet"
335 for imageext in $IMAGEEXTENSIONS; do
336 FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -iname '*.$imageext' -print0'
339 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
340 FINDIMAGESOPTIONS='-maxdepth 1 '${FINDIMAGESOPTIONS}
345 $0 [--help|--version|<path to images>]
348 displays this help screen
350 displays the version and exits
352 This can also takes some environment variables, these will use defaults if not
356 the title of the gallery
359 the width to make the icons
362 set the width of images in the medium size pages
365 set the theme to use (described below)
368 set an extra location to look for themes
371 sets the script output to be wrapped in a <pre> block
373 GENERATEPAGESFORMEDIUMSIZE
374 generate medium sized images and pages
376 GENERATEPAGESFORFULLSIZE
377 decide wether to generate pages for the full size images or not
380 name of the index page (e.g. index.html)
383 TITLE="My Funky Gallery" WIDTH=200 INDEXDOCUMENT="welcome.html" GENERATEPAGESFORFULLSIZE=1 GENERATEPAGESFORMEDIUMSIZE=1 MEDIUMWIDTH=400 $0 /path/to/image/folder
393 function generate_resized_images() {
394 $ECHOCOMMAND "Generating $2"
396 totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | $WCCOMMAND);
397 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)
399 if [ $totalimages -eq 0 ]; then
400 # might as well exit at this point - there are no pictures
401 $ECHOCOMMAND "No images for the gallery - exiting."
404 # check if the directory exists and create it other wise
410 $ECHOCOMMAND "Can't write to $2 directory, exiting"
415 if [ $totalimages -eq $imagestoupdate ]; then
416 $ECHOCOMMAND "Regenerating all $2"
417 elif [ $imagestoupdate -eq 0 ]; then
418 $ECHOCOMMAND "No Updated $2, not regenerating"
421 $ECHOCOMMAND "Generating $imagestoupdate of $totalimages $2"
424 $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
425 $ECHOCOMMAND done: $imagestoupdate/$imagestoupdate images
426 $ECHOCOMMAND "Completed generating $2 for $totalimages images"
429 function generate_thumbs() {
430 generate_resized_images $WIDTH icons
433 function generate_medium() {
434 generate_resized_images $MEDIUMWIDTH medium
437 function generate_pages() {
438 $ECHOCOMMAND "Generating Pages"
458 if [ -z $2 ] && [[ $GENERATEPAGESFORMEDIUMSIZE != 0 ]]; then
462 $FINDCOMMAND . $FINDIMAGESOPTIONS | \
463 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
465 while read imagefilename; do
466 previousimage=$currentimage
467 currentimage=$nextimage
468 nextimage=${imagefilename#./}
471 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
472 if [[ $addlinks == 1 ]]; then
473 addlink=${currentimage}.html
475 addlink=${currentimage}${extra}.html
478 addlink=${currentimage}
481 previouspage=$currentpage
482 currentpage=$nextpage
483 if [[ $addlinks == 1 ]]; then
484 nextpage=${nextimage}${extra}.html
486 nextpage=${nextimage}.html
488 filename=${extradir}${currentimage}
489 echo "'$previouspage' '$currentpage' '$nextpage' '$addlink'" >> /tmp/bpgallery-tmp.log
490 if [ "x$currentpage" != "x" ]; then
491 generate_general_page "$previouspage" "$currentpage" "$nextpage" $addlink
493 $ECHOCOMMAND $nextimage
499 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
500 if [[ $addlinks == 1 ]]; then
501 addlink=${currentpage}.html
503 addlink=${currentpage}${extra}.html
506 addlink=${currentpage}
509 filename=${extradir}${currentpage}
510 if [[ $addlinks == 1 ]]; then
511 currentpage=${currentpage}${extra}.html
512 previouspage=${previouspage}${extra}.html
514 currentpage=${currentpage}.html
515 previouspage=${previouspage}.html
518 generate_general_page "$previouspage" "$currentpage" "" $addlink
522 function generate_medium_pages() {
523 generate_pages __medium medium
526 function generate_general_page() {
528 echo 'Generating the page...' >> /tmp/bpgallery-tmp.log
543 if [[ ! -z $4 ]]; then
547 if [ -r captions.txt ]; then
548 caption=$($GREPCOMMAND -E "^${filename} " captions.txt); caption=${caption#* }
553 $BPGALLERY_PAGE_FUNCTION > "$currentpage"
556 if [[ $OUTPUTHTML != 0 ]]; then
561 $ECHOCOMMAND "No path given"
566 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
571 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
576 if [[ ! -d $1 ]]; then
577 $ECHOCOMMAND "$1 is not a directory"
584 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
585 $ECHOCOMMAND "$1 does not contain any images. Quitting."
590 $ECHOCOMMAND "Can't write to images directory, exiting"
594 if [ -e ${INDEXDOCUMENT} ] && [ ! -w ${INDEXDOCUMENT} ]; then
595 $ECHOCOMMAND "Can't write ${INDEXDOCUMENT}, exiting"
599 if [ -e style.css ] && [ ! -w style.css ]; then
600 $ECHOCOMMAND "Can't write style.css, exiting"
606 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
608 generate_medium_pages
611 if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then
612 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
613 generate_pages __medium
619 if [ -r description.txt ] ; then
620 DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
626 $ECHOCOMMAND "Starting to generate page"
628 $BPGALLERY_HEAD_FUNCTION > ${INDEXDOCUMENT}
629 $BPGALLERY_DESCRIPTION_FUNCTION >> ${INDEXDOCUMENT}
631 $ECHOCOMMAND "Adding Captions"
634 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
638 $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}
640 $BPGALLERY_TAIL_FUNCTION >> ${INDEXDOCUMENT}
642 $ECHOCOMMAND "Finished generating the page"
643 $ECHOCOMMAND "Generating stylesheet"
646 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
647 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
651 # add a bit to the maxheight for the size of the caption
652 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
654 $BPGALLERY_STYLESHEET_FUNCTION > style.css
656 $ECHOCOMMAND "All done"
658 if [[ $OUTPUTHTML != 0 ]]; then
659 $ECHOCOMMAND "</pre>"