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"
405 generate_resized_images $MEDIUMWIDTH medium
408 function generate_pages() {
409 $ECHOCOMMAND "Generating Pages"
429 if [ -z $1 ] && [ -z $2 ] && [[ $GENERATEPAGESFORMEDIUMSIZE != 0 ]]; then
433 $FINDCOMMAND . $FINDIMAGESOPTIONS | \
434 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
436 while read imagefilename; do
437 previousimage=$currentimage
438 currentimage=$nextimage
439 nextimage=${imagefilename#./}
442 if [[ $addlinks == 1 ]]; then
443 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
444 addlink=${currentimage}.html
446 addlink=${currentimage}
450 if [[ $addlinks == 2 ]]; then
451 addlink=${currentimage}__medium.html
454 previouspage=$currentpage
455 currentpage=$nextpage
456 nextpage=${imagefilename}${extra}.html
457 filename=${extradir}${currentimage}
458 generate_general_page "$previouspage" "$currentpage" "$nextpage" $addlink
465 if [[ $addlinks == 1 ]]; then
466 if [[ $GENERATEPAGESFORFULLSIZE ]]; then
467 addlink=${currentpage}.html
469 addlink=${currentpage}
473 if [[ $addlinks == 2 ]]; then
474 addlink=${nextimage}__medium.html
477 filename=${extradir}${currentpage}
478 currentpage=${currentpage}${extra}.html
479 previouspage=${previouspage}${extra}.html
481 generate_general_page "$previouspage" "$currentpage" "" $addlink
485 function generate_medium_pages() {
486 generate_pages __medium medium
489 function generate_general_page() {
504 if [[ ! -z $4 ]]; then
508 if [ -r captions.txt ]; then
509 caption=$($GREPCOMMAND -E "^${filename} " captions.txt); caption=${caption#* }
514 $BPGALLERY_PAGE_FUNCTION > "$currentpage"
517 if [[ $OUTPUTHTML != 0 ]]; then
522 $ECHOCOMMAND "No path given"
527 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
532 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
537 if [[ ! -d $1 ]]; then
538 $ECHOCOMMAND "$1 is not a directory"
545 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
546 $ECHOCOMMAND "$1 does not contain any images. Quitting."
551 $ECHOCOMMAND "Can't write to images directory, exiting"
555 if [ -e ${INDEXDOCUMENT} ] && [ ! -w ${INDEXDOCUMENT} ]; then
556 $ECHOCOMMAND "Can't write ${INDEXDOCUMENT}, exiting"
560 if [ -e style.css ] && [ ! -w style.css ]; then
561 $ECHOCOMMAND "Can't write style.css, exiting"
567 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
569 generate_medium_pages
572 if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then
576 if [ -r description.txt ] ; then
577 DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
583 $ECHOCOMMAND "Starting to generate page"
585 $BPGALLERY_HEAD_FUNCTION > ${INDEXDOCUMENT}
586 $BPGALLERY_DESCRIPTION_FUNCTION >> ${INDEXDOCUMENT}
588 $ECHOCOMMAND "Adding Captions"
591 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
595 $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}
597 $BPGALLERY_TAIL_FUNCTION >> ${INDEXDOCUMENT}
599 $ECHOCOMMAND "Finished generating the page"
600 $ECHOCOMMAND "Generating stylesheet"
603 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
604 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
608 # add a bit to the maxheight for the size of the caption
609 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
611 $BPGALLERY_STYLESHEET_FUNCTION > style.css
613 $ECHOCOMMAND "All done"
615 if [[ $OUTPUTHTML != 0 ]]; then
616 $ECHOCOMMAND "</pre>"