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="${ICONSDIRECTORY}/$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 function bpgallery_cleanup_directory_name() {
197 declare -rf bpgallery_escape_url
199 if [[ ! -z ${BPGALLERY_THEME} ]] ; then
200 declare -r BPGALLERY_THEME
203 if [[ ! -z ${TITLE} ]] ; then
207 if [[ -e /usr/local/etc/bpgallery/config ]] ; then
208 . /usr/local/etc/bpgallery/config 2>/dev/null
211 if [[ -e /etc/bpgallery/config ]] ; then
212 . /etc/bpgallery/config 2>/dev/null
215 if [[ -e $HOME/.bpgallery.rc ]]; then
216 . $HOME/.bpgallery.rc 2>/dev/null
219 if [[ -e $1 && -d $1 && -e $1/.bpgallery.rc ]]; then
220 . $1/.bpgallery.rc 2>/dev/null
223 if [[ -z ${BPGALLERY_THEME} ]]; then
224 BPGALLERY_THEME=default
227 if [[ ! -z ${BPGALLERY_THEME_DIR} ]] && \
228 [[ -e ${BPGALLERY_THEME_DIR}/${BPGALLERY_THEME} ]]; then
229 . ${BPGALLERY_THEME_DIR}/${BPGALLERY_THEME} 2>/dev/null
230 elif [[ -e $HOME/.bpgallery.themes/${BPGALLERY_THEME} ]]; then
231 . $HOME/.bpgallery.themes/${BPGALLERY_THEME} 2>/dev/null
232 elif [[ -e /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
233 . /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
234 elif [[ -e /etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
235 . /etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
238 if [[ -z $TITLE ]]; then
239 TITLE="Photo Gallery"
242 if [[ -z $CONVERTTOOL ]]; then
246 if [[ -z $FINDCOMMAND ]]; then
250 if [[ -z $XARGSCOMMAND ]]; then
254 if [[ -z $ECHOCOMMAND ]]; then
258 if [[ -z $SORTCOMMAND ]]; then
262 if [[ -z $IDENTIFYCOMMAND ]]; then
263 IDENTIFYCOMMAND=identify
266 if [[ -z $HEADCOMMAND ]]; then
270 if [[ -z $GREPCOMMAND ]]; then
274 if [[ -z $SEDCOMMAND ]]; then
278 if [[ -z $WIDTH ]]; then
282 if [[ -z $IMAGEEXTENSIONS ]]; then
283 IMAGEEXTENSIONS="jpeg jpg gif png";
286 if [[ -z $WCCOMMAND ]]; then
290 if [[ -z $CAPTIONHEIGHT ]]; then
294 if [[ -z $OUTPUTHTML ]]; then
298 if [[ -z $GENERATEPAGESFORFULLSIZE ]]; then
299 GENERATEPAGESFORFULLSIZE=0
302 if [[ -z $GENERATEPAGESFORMEDIUMSIZE ]]; then
303 GENERATEPAGESFORMEDIUMSIZE=1
306 if [[ -z $PAGESDIRECTORY ]]; then
309 removetrailingslashes=${PAGESDIRECTORY%/}
310 temp=$removetrailingslashes
311 while [[ $removetrailingslashes != $temp ]]; do
312 temp=$removetrailingslashes
313 removetrailingslashes=${temp%/}
315 PAGESDIRECTORY=$(bpgallery_escape_url "${PAGESDIRECTORY}")
318 if [[ -z $MEDIUMWIDTH ]]; then
322 if [[ -z $INDEXDOCUMENT ]]; then
323 INDEXDOCUMENT=index.html
326 if [[ -z $MEDIUMDIRECTORY ]]; then
327 MEDIUMDIRECTORY=medium
329 # Strip out anything that might be broken
330 MEDIUMDIRECTORY=$(bpgallery_cleanup_directory_name "$MEDIUMDIRECTORY")
333 if [[ -z $ICONSDIRECTORY ]]; then
336 # Strip out anything that might be broken
337 ICONSDIRECTORY=$(bpgallery_cleanup_directory_name "$ICONSDIRECTORY")
340 if declare -F "bpgallery_${BPGALLERY_THEME}_page" > /dev/null ; then
341 BPGALLERY_PAGE_FUNCTION="bpgallery_${BPGALLERY_THEME}_page"
343 BPGALLERY_PAGE_FUNCTION="bpgallery_default_page"
346 if declare -F "bpgallery_${BPGALLERY_THEME}_head" > /dev/null ; then
347 BPGALLERY_HEAD_FUNCTION="bpgallery_${BPGALLERY_THEME}_head"
349 BPGALLERY_HEAD_FUNCTION="bpgallery_default_head"
352 if declare -F "bpgallery_${BPGALLERY_THEME}_description" > /dev/null ; then
353 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_${BPGALLERY_THEME}_description"
355 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_default_description"
358 if declare -F "bpgallery_${BPGALLERY_THEME}_thumbsline" > /dev/null ; then
359 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_${BPGALLERY_THEME}_thumbsline"
361 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_default_thumbsline"
364 if declare -F "bpgallery_${BPGALLERY_THEME}_tail" > /dev/null ; then
365 BPGALLERY_TAIL_FUNCTION="bpgallery_${BPGALLERY_THEME}_tail"
367 BPGALLERY_TAIL_FUNCTION="bpgallery_default_tail"
370 if declare -F "bpgallery_${BPGALLERY_THEME}_stylesheet" > /dev/null ; then
371 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_${BPGALLERY_THEME}_stylesheet"
373 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_default_stylesheet"
378 for imageext in $IMAGEEXTENSIONS; do
379 FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -iname '*.$imageext' -print0'
382 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
383 FINDIMAGESOPTIONS='-maxdepth 1 '${FINDIMAGESOPTIONS}
388 $0 [--help|--version|<path to images>]
391 displays this help screen
393 displays the version and exits
395 This can also takes some environment variables, these will use defaults if not
399 the title of the gallery
402 the width to make the icons
405 set the width of images in the medium size pages
408 set the theme to use (described below)
411 set an extra location to look for themes
414 sets the script output to be wrapped in a <pre> block
416 GENERATEPAGESFORMEDIUMSIZE
417 generate medium sized images and pages
419 GENERATEPAGESFORFULLSIZE
420 decide wether to generate pages for the full size images or not
423 name of the index page (e.g. index.html)
426 TITLE="My Funky Gallery" WIDTH=200 INDEXDOCUMENT="welcome.html" GENERATEPAGESFORFULLSIZE=1 GENERATEPAGESFORMEDIUMSIZE=1 MEDIUMWIDTH=400 $0 /path/to/image/folder
436 function generate_resized_images() {
437 $ECHOCOMMAND "Generating $2"
439 totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | $WCCOMMAND);
440 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)
442 if [ $totalimages -eq 0 ]; then
443 # might as well exit at this point - there are no pictures
444 $ECHOCOMMAND "No images for the gallery - exiting."
447 # check if the directory exists and create it other wise
453 $ECHOCOMMAND "Can't write to $2 directory, exiting"
458 if [ $totalimages -eq $imagestoupdate ]; then
459 $ECHOCOMMAND "Regenerating all $2"
460 elif [ $imagestoupdate -eq 0 ]; then
461 $ECHOCOMMAND "No Updated $2, not regenerating"
464 $ECHOCOMMAND "Generating $imagestoupdate of $totalimages $2"
467 $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
468 $ECHOCOMMAND done: $imagestoupdate/$imagestoupdate images
469 $ECHOCOMMAND "Completed generating $2 for $totalimages images"
472 function generate_thumbs() {
473 generate_resized_images $WIDTH $ICONSDIRECTORY
476 function generate_medium() {
477 generate_resized_images $MEDIUMWIDTH $MEDIUMDIRECTORY
480 function generate_pages() {
481 $ECHOCOMMAND "Generating Pages"
501 if [ -z $2 ] && [[ $GENERATEPAGESFORMEDIUMSIZE != 0 ]]; then
505 $FINDCOMMAND . $FINDIMAGESOPTIONS | \
506 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
508 while read imagefilename; do
509 previousimage=$currentimage
510 currentimage=$nextimage
511 nextimage=${imagefilename#./}
514 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
515 if [[ $addlinks == 1 ]]; then
516 addlink=${currentimage}.html
518 addlink=${currentimage}${extra}.html
521 addlink=${currentimage}
524 previouspage=$currentpage
525 currentpage=$nextpage
526 if [[ $addlinks == 1 ]]; then
527 nextpage=${nextimage}${extra}.html
529 nextpage=${nextimage}.html
532 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
533 filename=../${extradir}${currentimage}
535 filename=${extradir}${currentimage}
537 if [ "x$currentpage" != "x" ]; then
538 generate_general_page "$previouspage" "$currentpage" "$nextpage" $addlink
540 $ECHOCOMMAND $nextimage
546 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
547 if [[ $addlinks == 1 ]]; then
548 addlink=${currentpage}.html
550 addlink=${currentpage}${extra}.html
553 addlink=${currentpage}
556 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
557 filename=../${extradir}${currentpage}
559 filename=${extradir}${currentpage}
561 if [[ $addlinks == 1 ]]; then
562 currentpage=${currentpage}${extra}.html
563 previouspage=${previouspage}${extra}.html
565 currentpage=${currentpage}.html
566 previouspage=${previouspage}.html
569 generate_general_page "$previouspage" "$currentpage" "" $addlink
573 function generate_medium_pages() {
574 generate_pages __${MEDIUMDIRECTORY} ${MEDIUMDIRECTORY}
577 function generate_general_page() {
592 if [[ ! -z $4 ]]; then
596 if [ -r captions.txt ]; then
597 imagefilename=${filename##*/}
598 caption=$($GREPCOMMAND -E "^${imagefilename} " captions.txt); caption=${caption#* }
603 $BPGALLERY_PAGE_FUNCTION > "${UNIXPAGESDIRECTORY}$currentpage"
606 if [[ $OUTPUTHTML != 0 ]]; then
611 $ECHOCOMMAND "No path given"
616 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
621 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
626 if [[ ! -d $1 ]]; then
627 $ECHOCOMMAND "$1 is not a directory"
634 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
635 $ECHOCOMMAND "$1 does not contain any images. Quitting."
640 $ECHOCOMMAND "Can't write to images directory, exiting"
644 if [ -e ${INDEXDOCUMENT} ] && [ ! -w ${INDEXDOCUMENT} ]; then
645 $ECHOCOMMAND "Can't write ${INDEXDOCUMENT}, exiting"
649 if [ -e style.css ] && [ ! -w style.css ]; then
650 $ECHOCOMMAND "Can't write style.css, exiting"
656 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
657 if [ ! -d $PAGESDIRECTORY ]; then
658 mkdir -p $PAGESDIRECTORY
661 if [ ! -w $PAGESDIRECTORY ]; then
662 $ECHOCOMMAND "Can't write to $PAGESDIRECTORY directory, exiting"
665 UNIXPAGESDIRECTORY=$PAGESDIRECTORY/
666 PAGESDIRECTORY=$(bpgallery_escape_url $PAGESDIRECTORY)/
671 UNIXPAGESDIRECTORY=""
674 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
676 generate_medium_pages
679 if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then
680 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
681 generate_pages __${MEDIUMDIRECTORY}
687 if [ -r description.txt ] ; then
688 DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
695 $ECHOCOMMAND "Starting to generate page"
697 $BPGALLERY_HEAD_FUNCTION > ${INDEXDOCUMENT}
698 $BPGALLERY_DESCRIPTION_FUNCTION >> ${INDEXDOCUMENT}
700 $ECHOCOMMAND "Adding Captions"
703 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
704 extra=__${MEDIUMDIRECTORY}
707 $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}
709 $BPGALLERY_TAIL_FUNCTION >> ${INDEXDOCUMENT}
711 $ECHOCOMMAND "Finished generating the page"
712 $ECHOCOMMAND "Generating stylesheet"
715 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
716 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
720 # add a bit to the maxheight for the size of the caption
721 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
723 $BPGALLERY_STYLESHEET_FUNCTION > style.css
725 $ECHOCOMMAND "All done"
727 if [[ $OUTPUTHTML != 0 ]]; then
728 $ECHOCOMMAND "</pre>"