Newer
Older
webtls / img2www
@yuuji yuuji on 23 Dec 2002 10 KB -te
#!/bin/sh
# Compose from Image files to www page HTML sets
# $Id$
# (c)1997-2002 by HIROSE Yuuji [yuuji@gentei.org]

SCALE=0.2
SCALE="-xsize 60 -ysize 45"
INDEX=index.html
noindex=0
nomini=0
tojis=/usr/local/bin/nkf
toeuc='/usr/local/bin/nkf -e'
# bg='bgcolor="white"'
bg=white
back=none
fg=black
TABLE=''
TB=0
TW=200
TW2=300
CNT=0
CJPEG=cjpeg
DJPEG=djpeg
CONVERT=convert
cssfile=img2www.css
paragraphmargin=0.5ex
formhead=''
formtail=''
perl=`which perl`
cgi2args="./cgi2args.cgi"
timesort=''
annotate=img2www.txt
anntmp=./img2www-$$.tmp

finalize () {
  rm -f $anntmp > /dev/null 2>&1
  echo Terminated. 1>&2
  exit 0
}

while [ $# -ne 0 ];
do
    case "$1" in
	-s)     shift;  SCALE=$1 ;;
	-4)	SCALE="-xsize 48 -ysize 36" ;;	# これをデフォルトにした
	-6)	SCALE="-xsize 60 -ysize 45" ;;
	-1)	SCALE="-xsize 120 -ysize 90" ;;
	-ix)	shift;  INDEX=$1 ;;
	-ni)	noindex=1 ;;			# index.htmlを作らない
	-nm)	nomini=1 ;;			# mini作成をスキップ
	-t)	TABLE='   ' ;;			# <table>モード
	-tb)	shift; TB=$1 ;;			# <table>の border
	-tw)	shift; TW=$1 ;;			# <table>左側のWIDTH
	-tw2)	shift; TW2=$1 ;;		# <table>右側のWIDTH
	-T)	TABLE='   '
		SCALE="-xsize 120 -ysize 90" ;; # table御気楽バージョン
	-time)	timesort=1 ;;			# 時刻順ソート
	-bg)	shift; bg="$1" ;;
	-back)	shift; back="url($1)" ;;
	-fg)	shift; fg="$1" ;;
	-h)	shift; heading="$1" ;;		# htmlの最初のH1
	-css)	shift; cssfile="$1" ;;
	-pm)	shift; paragraphmargin="$1" ;;
	-an)	shift; annotate="$1"	;;	# annotateファイル名
	-na)	annotate=''		;;	# annotateなし
	-tj)	shift; tojis="$1"	;;
	-te)	shift; toeuc="$1"	;;
	-nj)	tueoc='cat' tojis='cat' ;;
	-to)	shift; toaddress="$1" ;;
	-me)	shift; maintainer="$1" ;;
	-order)
	  if [ -x $cgi2args ]; then
	    formhead="<form method=\"GET\" action=\"$cgi2args\">
あなたのメイルアドレス: <input name=\"email\" size=\"40\" maxlength=\"40\"><br>
<input type=\"hidden\" name=\"execcmd\" value=\"./order.pl\">
<input type=submit value=\"注文\"><input type=reset value=\"やり直し\">"
	    formtail="</form>"
	    TABLE='   '				# -T と揃えること
	    SCALE="-xsize 120 -ysize 90"
	  else
	    echo "カレントディレクトリに cgi2args.cgi が必要です."
	    echo "(http://www.gentei.org/software/ にあるよ)"
	  fi
	  ;;
	*)
		if [ -s "$1" ]; then
			files="$files $1"
		fi ;;
    esac
    shift
done

if [ "$files" = "" ]; then
	myname=`basename $0`
	cat<<_EOU_
なんかファイルを指定してね。
$myname はjpgファイルをががっと読み込んで、サムネイルファイルを作り
それらを<img src..>で読み込んだ index.html を作ります。サムネイルを
クリックすると元の大きなjpgが出るようなリンクを張ってくれます。

またカレントディレクトリに $annotate というファイルを作り、その中に
ファイル名,	ALT文字列,	説明コメント
という行をたくさん書いておくと img src のALT属性、画像HTMLにつける
コメント文をそこから引っ張って来て書き込んでくれます。

$myname の使い方:
   $myname [オプション] *.jpg
   オプションは以下の通り(括弧内はデフォルト値)
	-s 倍率		元JPGからサムネイルを作るときの倍率
			-s 0.5 とすると元の0.5倍、
			-s "-xsize 120 -ysize 90" とすると 120x90 になります
	-4		-s "-xsize 48 -ysize 36" と同じ
	-6		-s "-xsize 60 -ysize 45" と同じ
	-1		-s "-xsize 120 -ysize 90" と同じ
	-ni		index.html を作らない(No Index)
	-t		table環境として吐き出す
	    -tb	数	tableのborder(デフォルト=0)
	    -tw	数	tableの左側(写真部分)の幅($TW)
	    -tw2 数	tableの右側(文章部分)の幅($TW2)
	-T		-t -1 同時指定の御気楽オプション
	-time		*.jpg ファイル群を時刻順に並べ変えてからHTMLを出力
	-bg		htmlファイルのbgcolor指定
	-back		htmlファイルのbackground指定
	-fg		htmlファイルのtext色指定
	-css		css定義ファイル名($cssfile)
	-pm		cssに定義するパラグラフの上下マージン($paragraphmargin)
	-an ファイル名	[画像, ALT文, コメント] を入れたファイル名($annotate)
	-tj コマンド	JIS変換するコマンド名($tojis)
	-na		$annotate を無視する
	-order		写真注文フォームを生成する 以下の2オプションと併用
	    -to	ADDR	注文受信者のemailアドレスをADDRに指定
	    -me ADDR	注文受理ページの管理者のアドレスをADDRに指定

$myname には netpbm と jpeglib(djpeg)が必要です。Unix上で画像処理をするには
欠かせないツールなので絶対にインストールしましょう。
_EOU_
	exit 0;
fi

catimg () {
    case $1 in
      *.jpg|*.jpeg)
	$DJPEG $1 ;;
      *.png)
        $CONVERT $1 PNM:- ;;
    esac
}

readcomment () {
    case $1 in
      *.jpg|*.jpeg)
	rdjpgcom $1 ;;
      *.png)
        identify -ver $1 | awk '/Comment:/{print $2}' ;;
    esac
}

if [ $noindex -eq 0 ]; then
	if [ ! -s $INDEX ]; then
	  init=1
	  echo "<html>"					>>$INDEX
	  echo "<head><title>No TITLE</title>"		>>$INDEX
	  echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$cssfile\">" >>$INDEX
	  echo "</head>"				>>$INDEX
	  echo ""					>>$INDEX
	  echo "<body>"					>>$INDEX
	  [ "$heading" ] && echo "<h1>$heading</h1>"	>>$INDEX
	fi
	[ "$TABLE" ] && echo "$formhead<table border=\"$TB\">"|$tojis >>$INDEX
fi
if [ "$timesort" ]; then
    files=`ls -tr $files`
fi

getannotate () {
    n=`wc -l < $annotate`
    l=1
    $toeuc $annotate \
	| grep -v '^[ 	]*#' \
	| grep ',.*,' \
	| while read line; do
	echo -n '
'$l/$n 1>&2
	l=`expr $l + 1`
	eval `echo "$line" | sed 's/\([^,]*\),\(.*\)/f="\1" a="\2"/'`
	if [ -f $f -a "$a" ]; then
	    f=`echo $f|sed -e 's/\..*//' -e 's/-/_/'`
	    #f=`expr $f : '\([^.]*\).'`
	    #echo eval `echo $a | sed 's/\([^,]*\),\(.*\)/alt="\1" cmt="\2"/'`
	    eval `echo "$a" | sed 's/\([^,]*\),\(.*\)/alt="\1" cmt="\2"/'`
	    ###echo "alt$f='`echo $alt`'"
	    echo "read alt$f <<_EOF_
$alt
_EOF_
"
	    #eval "alt$f='`echo $alt`'"
	    ###echo "cmt$f='`echo $cmt`'"
	    echo "read cmt$f <<_EOF_
$cmt
_EOF_
"
	    #eval "cmt$f='`echo $cmt`'"
	fi
    done 
    echo  1>&2
}

# Check img2www.txt
if [ -s $annotate ]; then
    echo Reading $annotate...
    # Use temporary file because eval in pipeline takes no effect to
    # the current shell environment
    trap "finalize" INT TERM
    getannotate > $anntmp
    trap  INT TERM
    . $anntmp
    rm -f $anntmp
    echo Done
    getalt() {
	f=`echo $1|sed -e 's/\..*//' -e 's/-/_/'`
	# echo eval "echo \$alt$f" 1>&2
	eval "echo \"\$alt$f\"" | $tojis
    }
    getcomment() {
	f=`echo $1|sed -e 's/\..*//' -e 's/-/_/'`
	eval "echo \$cmt$f" | $tojis
    }
else
    getalt() {
	echo Photo
    }
    getcomment() {
	echo 'この行を消してここにコメント
左寄せが良ければ <p class=\"l\"> に変える' \
	| sed -e 's/^/<!-- /' -e 's/$/ -->/' | $tojis
    }
fi

for i in $files
do
	case $i in
	    # *.gif)		# No more GIF!!
	    # cat=giftopnm ;;	# No more Unisys!!!
	    *.jpg|*.jpeg)
		cat=djpeg ;;
	esac
	case $i in
	    *mini*)
		echo "Skipping $i" ;;
	    *)
		minibase=`basename $i|sed 's/\(.*\)\..*/\1/'`
		minifile=`dirname $i`"/tn_${minibase}.jpg"
		html=`dirname $i`"/$minibase.html"
		imglink=`basename $i`
		echo mini=$minibase, mf=$minifile
		alt=`getalt $i`
		if [ "$alt" = "" ]; then
		    title=`readcomment $i`
		    if [ "$title" = "" ]; then
			title="$i"
		    fi
		else
		    title=`readcomment $i`
		    if [ x"$title" = x"" ]; then
			title="$alt"
		    else
			title="$title - $alt"
		    fi
		fi
		# get image size in pixel
		size=`catimg $i|pnmfile -| \
		 sed 's/[^0-9]*\([1-9][0-9]*\) by \([0-9]*\).*/width="\1" height="\2"/'`
		if [ ! -f $minifile -o $nomini -eq 0 ]; then
		 catimg $i | pnmscale $SCALE | ppmquant 128 |\
		    $CJPEG -progressive > $minifile
		fi
		minisize=`$DJPEG $minifile|pnmfile -| \
		 sed 's/[^0-9]*\([1-9][0-9]*\) by \([0-9]*\).*/width="\1" height="\2"/'`
		if [ $noindex -ne 1 ]; then
		  if [ "$TABLE" ]; then
		    echo " <tr>"			>>$INDEX
		    echo "  <td width=\"$TW\">"		>>$INDEX
		  fi
		  echo "$TABLE<a href=\"$html\">"	>>$INDEX
		  echo "$TABLE<img src=\"$minifile\" align=\"middle\" alt=\"$alt\"">>$INDEX
		  echo "$TABLE     $minisize></a>" >> $INDEX
		  if [ "$TABLE" ]; then
		    echo "  </td>"			>>$INDEX
		    CNT=`expr $CNT + 1`
		    echo "	<td width=\"$TW2\">"	>>$INDEX
		    if [ "$formhead" ]; then
		      echo "	<input type=checkbox name=\"$i\">$i" >>$INDEX
		    else 
		      echo "	 $title<br>"		>>$INDEX
		      echo "	 コメントその$CNT"|$tojis>>$INDEX
		    fi
		    echo "	</td>"			>>$INDEX
		    echo " </tr>"			>>$INDEX
		  fi
		fi
		echo "<html>"				 >$html
		echo "<head><title>$title</title>"	>>$html
		echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$cssfile\">" >>$html
		echo "</head>" 				>>$html
		echo "<body>"				>>$html
		echo "<p class=\"c\" align=\"center\">"	>>$html
		echo "<img src=\"$imglink\" alt=\"$alt\" $size>" >>$html
		echo "</p><hr><p class=\"c\">"		>>$html
		getcomment $i				>>$html
		echo "</p>"				>>$html
		echo "</body>"				>>$html
		echo "</html>"				>>$html
		;;
	esac
done
cssfile="`dirname $i`/$cssfile"
cat>$cssfile <<EOF
body {color: $fg; background: $bg $back;}
p.l {margin: $paragraphmargin $paragraphmargin;}
p.c {margin: $paragraphmargin $paragraphmargin; text-align: center;}
/* ここまでを実際に使用している. 以下はサンプル定義. */
/* たとえば一つだけ背景をpinkにしたかったら... */
body.hoge {background: pink;}
/* てなのを追加後pinkにしたいhtmlの body を <body class="hoge"> にすればOK */
EOF

##mkorder function
mk_order () {
    perl=`which perl`
    if [ -x /usr/lib/sendmail ]; then
	sendmail=/usr/lib/sendmail
    elif [ -x /usr/sbin/sendmail ]; then
	sendmail=/usr/sbin/sendmail
    else
	sendmail=sendmail
    fi
    tojis=`which nkf`
    while [ "$toaddress" = "" ]; do
	echo -n "注文を受ける人のemailアドレス: "
	read toaddress
    done
    while [ "$maintainer" = "" ]; do
	echo -n "このページの管理者(多分あなた)のemailアドレス: "
	read maintainer
    done
    cat>order.pl<<EOF
#!$perl

@list='';
\$sendmail='$sendmail';
\$replyto='$toaddress';
\$mailto='';
\$mailtainer='yuuji@itc.keio.ac.jp';
\$tojis='$tojis';

while (\$ARGV[0] =~ /^-/) {
    if (\$ARGV[0] =~ /^-email/) {
	\$mailto = \$ARGV[1];
    } elsif (\$ARGV[1] =~ /on/i) {
	push(@list, \$ARGV[0]);
    }
    shift; shift;
}

print "<html><head><title>Accepted</title><head><body>";

if (\$mailto) {
    open(MAIL, "|\$tojis | sendmail \$mailto");
    print MAIL "To: \$mailto
From: Photo Ordering System <\$mailtainer>
Subject: Confirmation of your order
Reply-to: \$replyto

以下の写真注文を受理しました。以下のリストであっているかどうか確認し、
これで良ければ、このメイル全体を引用して \$replyto にご返送ください。
";

    print MAIL join("\n", @list);
    close(MAIL);
    print "注文リストを送信しますので合っているかどうか確認してください。";
    
} else {
    print "メイルアドレスは必ず入れてください。";
}
print "</body>\n</html>";
EOF
    chmod +x order.pl
}

if [ "$noindex" -eq 0 ]; then
	if [ "$TABLE" ]; then
	  echo "</table>"				>>$INDEX
	  if [ "$formhead" ]; then
	    $tojis<<EOF>>$INDEX
<input type=submit value="注文">
$formtail
EOF
	  mk_order
	  fi
	fi
	if [ "$init" -ne 0 ]; then
	  echo ""					>>$INDEX
	  echo "</body>"				>>$INDEX
	  echo "</html>"				>>$INDEX
	fi
fi