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;
105 div.navigation ul li {
120 declare -rf bpgallery_default_stylesheet
122 function bpgallery_default_page() {
123 $BPGALLERY_HEAD_FUNCTION
126 <div class="navigation">
128 <li><a href='index.html'>Thumbnails</a></li>
130 if [[ ! -z $previouspage ]]; then
131 echo "<li><a href='${previouspage}'>Previous</a></li>"
133 echo "<li>Previous</li>"
136 if [[ ! -z $nextpage ]]; then
137 echo "<li><a href='${nextpage}'>Next</a></li>"
144 <div class="mainimage">
145 <img src="${filename}" alt="${caption}" />
148 $BPGALLERY_TAIL_FUNCTION
151 declare -rf bpgallery_default_page
153 function bpgallery_escape_url() {
171 declare -rf bpgallery_escape_url
173 if [[ ! -z ${BPGALLERY_THEME} ]] ; then
174 declare -r BPGALLERY_THEME
177 if [[ ! -z ${TITLE} ]] ; then
181 if [[ -e /usr/local/etc/bpgallery/config ]] ; then
182 . /usr/local/etc/bpgallery/config 2>/dev/null
185 if [[ -e /etc/bpgallery/config ]] ; then
186 . /etc/bpgallery/config 2>/dev/null
189 if [[ -e $HOME/.bpgallery.rc ]]; then
190 . $HOME/.bpgallery.rc 2>/dev/null
193 if [[ -e $1 && -d $1 && -e $1/.bpgallery.rc ]]; then
194 . $1/.bpgallery.rc 2>/dev/null
197 if [[ -z ${BPGALLERY_THEME} ]]; then
198 BPGALLERY_THEME=default
201 if [[ ! -z ${BPGALLERY_THEME_DIR} ]] && \
202 [[ -e ${BPGALLERY_THEME_DIR}/${BPGALLERY_THEME} ]]; then
203 . ${BPGALLERY_THEME_DIR}/${BPGALLERY_THEME} 2>/dev/null
204 elif [[ -e $HOME/.bpgallery.themes/${BPGALLERY_THEME} ]]; then
205 . $HOME/.bpgallery.themes/${BPGALLERY_THEME} 2>/dev/null
206 elif [[ -e /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
207 . /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
208 elif [[ -e /etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
209 . /etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
212 if [[ -z $TITLE ]]; then
213 TITLE="Photo Gallery"
216 if [[ -z $CONVERTTOOL ]]; then
220 if [[ -z $FINDCOMMAND ]]; then
224 if [[ -z $XARGSCOMMAND ]]; then
228 if [[ -z $ECHOCOMMAND ]]; then
232 if [[ -z $SORTCOMMAND ]]; then
236 if [[ -z $IDENTIFYCOMMAND ]]; then
237 IDENTIFYCOMMAND=identify
240 if [[ -z $HEADCOMMAND ]]; then
244 if [[ -z $GREPCOMMAND ]]; then
248 if [[ -z $SEDCOMMAND ]]; then
252 if [[ -z $WIDTH ]]; then
256 if [[ -z $IMAGEEXTENSIONS ]]; then
257 IMAGEEXTENSIONS="jpeg jpg gif png";
260 if [[ -z $WCCOMMAND ]]; then
264 if [[ -z $CAPTIONHEIGHT ]]; then
268 if [[ -z $OUTPUTHTML ]]; then
272 if [[ -z $GENERATEPAGESFORFULLSIZE ]]; then
273 GENERATEPAGESFORFULLSIZE=0
276 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
277 if declare -F "bpgallery_${BPGALLERY_THEME}_page" > /dev/null ; then
278 BPGALLERY_PAGE_FUNCTION="bpgallery_${BPGALLERY_THEME}_page"
280 BPGALLERY_PAGE_FUNCTION="bpgallery_default_page"
284 if declare -F "bpgallery_${BPGALLERY_THEME}_head" > /dev/null ; then
285 BPGALLERY_HEAD_FUNCTION="bpgallery_${BPGALLERY_THEME}_head"
287 BPGALLERY_HEAD_FUNCTION="bpgallery_default_head"
290 if declare -F "bpgallery_${BPGALLERY_THEME}_description" > /dev/null ; then
291 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_${BPGALLERY_THEME}_description"
293 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_default_description"
296 if declare -F "bpgallery_${BPGALLERY_THEME}_thumbsline" > /dev/null ; then
297 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_${BPGALLERY_THEME}_thumbsline"
299 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_default_thumbsline"
302 if declare -F "bpgallery_${BPGALLERY_THEME}_tail" > /dev/null ; then
303 BPGALLERY_TAIL_FUNCTION="bpgallery_${BPGALLERY_THEME}_tail"
305 BPGALLERY_TAIL_FUNCTION="bpgallery_default_tail"
308 if declare -F "bpgallery_${BPGALLERY_THEME}_stylesheet" > /dev/null ; then
309 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_${BPGALLERY_THEME}_stylesheet"
311 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_default_stylesheet"
316 for imageext in $IMAGEEXTENSIONS; do
317 FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -iname '*.$imageext' -print0'
320 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
321 FINDIMAGESOPTIONS='-maxdepth 1 '${FINDIMAGESOPTIONS}
326 $0 [--help|--version|<path to images>]
329 displays this help screen
331 displays the version and exits
342 function generate_thumbs() {
343 $ECHOCOMMAND "Generating Thumbnails"
345 totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} | $WCCOMMAND);
346 imagestoupdate=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} | while read filename; do filename=${filename//\"/\\\"}; if [ ! -r "icons/$filename" ] || [ "$filename" -nt "icons/$filename" ] || [ $($IDENTIFYCOMMAND -format "%w" "icons/$filename") -ne $WIDTH ]; then $ECHOCOMMAND $filename; fi; done | $WCCOMMAND)
348 if [ $totalimages -eq $imagestoupdate ]; then
349 $ECHOCOMMAND "Regenerating all thumbnails"
350 elif [ $imagestoupdate -eq 0 ]; then
351 $ECHOCOMMAND "No Updated Thumbs, not regenerating"
354 $ECHOCOMMAND "Generating $imagestoupdate of $totalimages thumbnails"
357 $FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} | while read filename; do tempfilename=${filename//\"/\\\"/}; if [ ! -r "icons/$tempfilename" ] || [ "$tempfilename" -nt "icons/$tempfilename" ] || [ $($IDENTIFYCOMMAND -format "%w" "icons/$filename") -ne $WIDTH ]; then $ECHOCOMMAND -n $filename; $ECHOCOMMAND -n -e "\000"; fi; done | $XARGSCOMMAND -0 --verbose --max-procs=4 --replace $CONVERTTOOL -resize $WIDTH '{}' 'icons/{}' 2>&1 | while read throwout; do $ECHOCOMMAND done: $currentimage/$imagestoupdate images; currentimage=$((currentimage+1)); done
358 $ECHOCOMMAND done: $imagestoupdate/$imagestoupdate images
359 $ECHOCOMMAND "Completed generating thumbs for $totalimages images"
362 function generate_pages() {
363 $ECHOCOMMAND "Generating Pages"
371 $FINDCOMMAND . $FINDIMAGESOPTIONS | \
372 $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} | \
374 while read imagefilename; do
375 previousimage=$currentimage
376 currentimage=$nextimage
377 nextimage=$imagefilename
378 previouspage=$currentpage
379 currentpage=$nextpage
380 nextpage=${imagefilename}.html
381 filename=${currentimage}
382 generate_general_page "$previouspage" "$currentpage" "$nextpage"
387 filename=${currentpage}
388 currentpage=${currentpage}.html
389 previouspage=${previouspage}.html
391 generate_general_page "$previouspage" "$currentpage" ""
397 function generate_general_page() {
412 if [ -r captions.txt ]; then
413 caption=$($GREPCOMMAND -E "^${filename} " captions.txt); caption=${caption#* }
418 $BPGALLERY_PAGE_FUNCTION > $currentpage
421 if [[ $OUTPUTHTML != 0 ]]; then
426 $ECHOCOMMAND "No path given"
431 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
436 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
441 if [[ ! -d $1 ]]; then
442 $ECHOCOMMAND "$1 is not a directory"
449 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
450 $ECHOCOMMAND "$1 does not contain any images. Quitting."
455 $ECHOCOMMAND "Can't write to images directory, exiting"
459 if [ -e index.html ] && [ ! -w index.html ]; then
460 $ECHOCOMMAND "Can't write index.html, exiting"
464 if [ -e style.css ] && [ ! -w style.css ]; then
465 $ECHOCOMMAND "Can't write style.css, exiting"
469 if [ ! -d icons ]; then
473 if [ ! -w icons ]; then
474 $ECHOCOMMAND "Can't write to icons directory, exiting"
480 if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then
484 if [ -r description.txt ] ; then
485 DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
491 $ECHOCOMMAND "Starting to generate page"
493 $BPGALLERY_HEAD_FUNCTION > index.html
494 $BPGALLERY_DESCRIPTION_FUNCTION >> index.html
496 $ECHOCOMMAND "Adding Captions"
498 $FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $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}.html"); else link=$(bpgallery_escape_url "$filename"); fi; filename=$(bpgallery_escape_url "$filename"); $BPGALLERY_THUMBSLINE_FUNCTION; done >> index.html
500 $BPGALLERY_TAIL_FUNCTION >> index.html
502 $ECHOCOMMAND "Finished generating the page"
503 $ECHOCOMMAND "Generating stylesheet"
506 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
507 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
511 # add a bit to the maxheight for the size of the caption
512 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
514 $BPGALLERY_STYLESHEET_FUNCTION > style.css
516 $ECHOCOMMAND "All done"
518 if [[ $OUTPUTHTML != 0 ]]; then
519 $ECHOCOMMAND "</pre>"