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="${BASEURL}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='${BASEURL}${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 $PAGESDIRECTORY ]]; then
291 temp=${PAGESDIRECTORY:${#PAGESDIRECTORY}-1:1}
292 if [[ "x$temp" != "x/" ]] && [[ "x$temp" != "x\\" ]]; then
293 PAGESDIRECTORY=${PAGESDIRECTORY}/
297 if [[ -z $MEDIUMWIDTH ]]; then
301 if [[ -z $INDEXDOCUMENT ]]; then
302 INDEXDOCUMENT=index.html
305 if declare -F "bpgallery_${BPGALLERY_THEME}_page" > /dev/null ; then
306 BPGALLERY_PAGE_FUNCTION="bpgallery_${BPGALLERY_THEME}_page"
308 BPGALLERY_PAGE_FUNCTION="bpgallery_default_page"
311 if declare -F "bpgallery_${BPGALLERY_THEME}_head" > /dev/null ; then
312 BPGALLERY_HEAD_FUNCTION="bpgallery_${BPGALLERY_THEME}_head"
314 BPGALLERY_HEAD_FUNCTION="bpgallery_default_head"
317 if declare -F "bpgallery_${BPGALLERY_THEME}_description" > /dev/null ; then
318 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_${BPGALLERY_THEME}_description"
320 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_default_description"
323 if declare -F "bpgallery_${BPGALLERY_THEME}_thumbsline" > /dev/null ; then
324 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_${BPGALLERY_THEME}_thumbsline"
326 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_default_thumbsline"
329 if declare -F "bpgallery_${BPGALLERY_THEME}_tail" > /dev/null ; then
330 BPGALLERY_TAIL_FUNCTION="bpgallery_${BPGALLERY_THEME}_tail"
332 BPGALLERY_TAIL_FUNCTION="bpgallery_default_tail"
335 if declare -F "bpgallery_${BPGALLERY_THEME}_stylesheet" > /dev/null ; then
336 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_${BPGALLERY_THEME}_stylesheet"
338 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_default_stylesheet"
343 for imageext in $IMAGEEXTENSIONS; do
344 FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -iname '*.$imageext' -print0'
347 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
348 FINDIMAGESOPTIONS='-maxdepth 1 '${FINDIMAGESOPTIONS}
353 $0 [--help|--version|<path to images>]
356 displays this help screen
358 displays the version and exits
360 This can also takes some environment variables, these will use defaults if not
364 the title of the gallery
367 the width to make the icons
370 set the width of images in the medium size pages
373 set the theme to use (described below)
376 set an extra location to look for themes
379 sets the script output to be wrapped in a <pre> block
381 GENERATEPAGESFORMEDIUMSIZE
382 generate medium sized images and pages
384 GENERATEPAGESFORFULLSIZE
385 decide wether to generate pages for the full size images or not
388 name of the index page (e.g. index.html)
391 TITLE="My Funky Gallery" WIDTH=200 INDEXDOCUMENT="welcome.html" GENERATEPAGESFORFULLSIZE=1 GENERATEPAGESFORMEDIUMSIZE=1 MEDIUMWIDTH=400 $0 /path/to/image/folder
401 function generate_resized_images() {
402 $ECHOCOMMAND "Generating $2"
404 totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | $WCCOMMAND);
405 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)
407 if [ $totalimages -eq 0 ]; then
408 # might as well exit at this point - there are no pictures
409 $ECHOCOMMAND "No images for the gallery - exiting."
412 # check if the directory exists and create it other wise
418 $ECHOCOMMAND "Can't write to $2 directory, exiting"
423 if [ $totalimages -eq $imagestoupdate ]; then
424 $ECHOCOMMAND "Regenerating all $2"
425 elif [ $imagestoupdate -eq 0 ]; then
426 $ECHOCOMMAND "No Updated $2, not regenerating"
429 $ECHOCOMMAND "Generating $imagestoupdate of $totalimages $2"
432 $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
433 $ECHOCOMMAND done: $imagestoupdate/$imagestoupdate images
434 $ECHOCOMMAND "Completed generating $2 for $totalimages images"
437 function generate_thumbs() {
438 generate_resized_images $WIDTH icons
441 function generate_medium() {
442 generate_resized_images $MEDIUMWIDTH medium
445 function generate_pages() {
446 $ECHOCOMMAND "Generating Pages"
466 if [ -z $2 ] && [[ $GENERATEPAGESFORMEDIUMSIZE != 0 ]]; then
470 $FINDCOMMAND . $FINDIMAGESOPTIONS | \
471 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
473 while read imagefilename; do
474 previousimage=$currentimage
475 currentimage=$nextimage
476 nextimage=${imagefilename#./}
479 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
480 if [[ $addlinks == 1 ]]; then
481 addlink=${currentimage}.html
483 addlink=${currentimage}${extra}.html
486 addlink=${currentimage}
489 previouspage=$currentpage
490 currentpage=$nextpage
491 if [[ $addlinks == 1 ]]; then
492 nextpage=${nextimage}${extra}.html
494 nextpage=${nextimage}.html
497 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
498 filename=../${extradir}${currentimage}
500 filename=${extradir}${currentimage}
502 if [ "x$currentpage" != "x" ]; then
503 generate_general_page "$previouspage" "$currentpage" "$nextpage" $addlink
505 $ECHOCOMMAND $nextimage
511 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
512 if [[ $addlinks == 1 ]]; then
513 addlink=${currentpage}.html
515 addlink=${currentpage}${extra}.html
518 addlink=${currentpage}
521 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
522 filename=../${extradir}${currentpage}
524 filename=${extradir}${currentpage}
526 if [[ $addlinks == 1 ]]; then
527 currentpage=${currentpage}${extra}.html
528 previouspage=${previouspage}${extra}.html
530 currentpage=${currentpage}.html
531 previouspage=${previouspage}.html
534 generate_general_page "$previouspage" "$currentpage" "" $addlink
538 function generate_medium_pages() {
539 generate_pages __medium medium
542 function generate_general_page() {
557 if [[ ! -z $4 ]]; then
561 if [ -r captions.txt ]; then
562 imagefilename=${filename##*/}
563 caption=$($GREPCOMMAND -E "^${imagefilename} " captions.txt); caption=${caption#* }
568 $BPGALLERY_PAGE_FUNCTION > "${PAGESDIRECTORY}$currentpage"
571 if [[ $OUTPUTHTML != 0 ]]; then
576 $ECHOCOMMAND "No path given"
581 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
586 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
591 if [[ ! -d $1 ]]; then
592 $ECHOCOMMAND "$1 is not a directory"
599 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
600 $ECHOCOMMAND "$1 does not contain any images. Quitting."
605 $ECHOCOMMAND "Can't write to images directory, exiting"
609 if [ -e ${INDEXDOCUMENT} ] && [ ! -w ${INDEXDOCUMENT} ]; then
610 $ECHOCOMMAND "Can't write ${INDEXDOCUMENT}, exiting"
614 if [ -e style.css ] && [ ! -w style.css ]; then
615 $ECHOCOMMAND "Can't write style.css, exiting"
621 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
622 if [ ! -d $PAGESDIRECTORY ]; then
623 mkdir -p $PAGESDIRECTORY
626 if [ ! -w $PAGESDIRECTORY ]; then
627 $ECHOCOMMAND "Can't write to $PAGESDIRECTORY directory, exiting"
634 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
636 generate_medium_pages
639 if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then
640 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
641 generate_pages __medium
647 if [ -r description.txt ] ; then
648 DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
655 $ECHOCOMMAND "Starting to generate page"
657 $BPGALLERY_HEAD_FUNCTION > ${INDEXDOCUMENT}
658 $BPGALLERY_DESCRIPTION_FUNCTION >> ${INDEXDOCUMENT}
660 $ECHOCOMMAND "Adding Captions"
663 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
667 $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=${PAGESDIRECTORY}$(bpgallery_escape_url "${filename}${extra}.html"); elif [ $GENERATEPAGESFORFULLSIZE != 0 ]; then link=${PAGESDIRECTORY}$(bpgallery_escape_url "${filename}.html"); else link=$(bpgallery_escape_url "$filename"); fi; filename=$(bpgallery_escape_url "$filename"); $BPGALLERY_THUMBSLINE_FUNCTION; done >> ${INDEXDOCUMENT}
669 $BPGALLERY_TAIL_FUNCTION >> ${INDEXDOCUMENT}
671 $ECHOCOMMAND "Finished generating the page"
672 $ECHOCOMMAND "Generating stylesheet"
675 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
676 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
680 # add a bit to the maxheight for the size of the caption
681 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
683 $BPGALLERY_STYLESHEET_FUNCTION > style.css
685 $ECHOCOMMAND "All done"
687 if [[ $OUTPUTHTML != 0 ]]; then
688 $ECHOCOMMAND "</pre>"