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 echo "<li><a href='${previouspage}'>Previous</a></li>"
141 echo "<li>Previous</li>"
144 if [[ ! -z $nextpage ]]; then
145 echo "<li><a href='${nextpage}'>Next</a></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 [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
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"
304 if declare -F "bpgallery_${BPGALLERY_THEME}_head" > /dev/null ; then
305 BPGALLERY_HEAD_FUNCTION="bpgallery_${BPGALLERY_THEME}_head"
307 BPGALLERY_HEAD_FUNCTION="bpgallery_default_head"
310 if declare -F "bpgallery_${BPGALLERY_THEME}_description" > /dev/null ; then
311 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_${BPGALLERY_THEME}_description"
313 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_default_description"
316 if declare -F "bpgallery_${BPGALLERY_THEME}_thumbsline" > /dev/null ; then
317 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_${BPGALLERY_THEME}_thumbsline"
319 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_default_thumbsline"
322 if declare -F "bpgallery_${BPGALLERY_THEME}_tail" > /dev/null ; then
323 BPGALLERY_TAIL_FUNCTION="bpgallery_${BPGALLERY_THEME}_tail"
325 BPGALLERY_TAIL_FUNCTION="bpgallery_default_tail"
328 if declare -F "bpgallery_${BPGALLERY_THEME}_stylesheet" > /dev/null ; then
329 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_${BPGALLERY_THEME}_stylesheet"
331 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_default_stylesheet"
336 for imageext in $IMAGEEXTENSIONS; do
337 FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -iname '*.$imageext' -print0'
340 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
341 FINDIMAGESOPTIONS='-maxdepth 1 '${FINDIMAGESOPTIONS}
346 $0 [--help|--version|<path to images>]
349 displays this help screen
351 displays the version and exits
362 function generate_resized_images() {
363 $ECHOCOMMAND "Generating $2"
365 totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | $WCCOMMAND);
366 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)
368 if [ $totalimages -eq $imagestoupdate ]; then
369 $ECHOCOMMAND "Regenerating all $2"
370 elif [ $imagestoupdate -eq 0 ]; then
371 $ECHOCOMMAND "No Updated $2, not regenerating"
374 $ECHOCOMMAND "Generating $imagestoupdate of $totalimages $2"
377 $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
378 $ECHOCOMMAND done: $imagestoupdate/$imagestoupdate images
379 $ECHOCOMMAND "Completed generating $2 for $totalimages images"
382 function generate_thumbs() {
383 if [ ! -d icons ]; then
387 if [ ! -w icons ]; then
388 $ECHOCOMMAND "Can't write to icons directory, exiting"
392 generate_resized_images $WIDTH icons
395 function generate_medium() {
396 if [ ! -d medium ]; then
400 if [ ! -w medium ]; then
401 $ECHOCOMMAND "Can't write to medium directory, exiting"
404 generate_resized_images $MEDIUMWIDTH medium
407 function generate_pages() {
408 $ECHOCOMMAND "Generating Pages"
428 if [ -z $1 ] && [ -z $2 ] && [[ $GENERATEPAGESFORMEDIUMSIZE != 0 ]]; then
432 $FINDCOMMAND . $FINDIMAGESOPTIONS | \
433 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
435 while read imagefilename; do
436 previousimage=$currentimage
437 currentimage=$nextimage
438 nextimage=${imagefilename#./}
441 if [[ $addlinks == 1 ]]; then
442 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
443 addlink=${currentimage}.html
445 addlink=${currentimage}
449 if [[ $addlinks == 2 ]]; then
450 addlink=${currentimage}__medium.html
453 previouspage=$currentpage
454 currentpage=$nextpage
455 nextpage=${imagefilename}${extra}.html
456 filename=${extradir}${currentimage}
457 generate_general_page "$previouspage" "$currentpage" "$nextpage" $addlink
464 if [[ $addlinks == 1 ]]; then
465 if [[ $GENERATEPAGESFORFULLSIZE ]]; then
466 addlink=${currentpage}.html
468 addlink=${currentpage}
472 if [[ $addlinks == 2 ]]; then
473 addlink=${nextimage}__medium.html
476 filename=${extradir}${currentpage}
477 currentpage=${currentpage}${extra}.html
478 previouspage=${previouspage}${extra}.html
480 generate_general_page "$previouspage" "$currentpage" "" $addlink
484 function generate_medium_pages() {
485 generate_pages __medium medium
488 function generate_general_page() {
503 if [[ ! -z $4 ]]; then
507 if [ -r captions.txt ]; then
508 caption=$($GREPCOMMAND -E "^${filename} " captions.txt); caption=${caption#* }
513 $BPGALLERY_PAGE_FUNCTION > "$currentpage"
516 if [[ $OUTPUTHTML != 0 ]]; then
521 $ECHOCOMMAND "No path given"
526 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
531 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
536 if [[ ! -d $1 ]]; then
537 $ECHOCOMMAND "$1 is not a directory"
544 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
545 $ECHOCOMMAND "$1 does not contain any images. Quitting."
550 $ECHOCOMMAND "Can't write to images directory, exiting"
554 if [ -e ${INDEXDOCUMENT} ] && [ ! -w ${INDEXDOCUMENT} ]; then
555 $ECHOCOMMAND "Can't write ${INDEXDOCUMENT}, exiting"
559 if [ -e style.css ] && [ ! -w style.css ]; then
560 $ECHOCOMMAND "Can't write style.css, exiting"
566 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
568 generate_medium_pages
571 if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then
575 if [ -r description.txt ] ; then
576 DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
582 $ECHOCOMMAND "Starting to generate page"
584 $BPGALLERY_HEAD_FUNCTION > ${INDEXDOCUMENT}
585 $BPGALLERY_DESCRIPTION_FUNCTION >> ${INDEXDOCUMENT}
587 $ECHOCOMMAND "Adding Captions"
590 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
594 $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 [ $GENERATEPAGESFORFULLSIZE != 0 ]; then link=$(bpgallery_escape_url "${filename}${extra}.html"); else link=$(bpgallery_escape_url "$filename"); fi; filename=$(bpgallery_escape_url "$filename"); $BPGALLERY_THUMBSLINE_FUNCTION; done >> ${INDEXDOCUMENT}
596 $BPGALLERY_TAIL_FUNCTION >> ${INDEXDOCUMENT}
598 $ECHOCOMMAND "Finished generating the page"
599 $ECHOCOMMAND "Generating stylesheet"
602 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
603 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
607 # add a bit to the maxheight for the size of the caption
608 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
610 $BPGALLERY_STYLESHEET_FUNCTION > style.css
612 $ECHOCOMMAND "All done"
614 if [[ $OUTPUTHTML != 0 ]]; then
615 $ECHOCOMMAND "</pre>"