#!/usr/bin/env ruby
#
# 表題: ape お絵描きサムネイル HTML 作成スクリプト
#
# 履歴: 2003/09/28 やまだ由 
#       2004/02/18 やまだ由 ($head 付加)
#
# 注意: 本プログラムは, 
#       dcphoto.pl Ver 1.02 - 写真用 HTML作成スクリプト
#       Mitsuda Chihiro   <totera@ep.sci.hokudai.ac.jp>
#       を ruby で写経し, やまだ由用にカスタマイズしたものである 
#
#
#
#

# ---------------------------------------------------------
# include
require 'date'
$KCODE = "e"

# ---------------------------------------------------------
# main()

def apethum_rd 
  mkinfo       ; p "mkinfo end"
  makethum_rd  ; p "mkthum_rd end"
end

def apethum
  mkinfo       ; p "mkinfo end"
  makethum     ; p "mkthum end"
  makethum_rd  ; p "mkthum_rd end"
end

def apethum_html
  mkinfo       ; p "mkinfo end"
  makethum     ; p "mkthum end"
end


# ---------------------------------------------------------
# 以下サブルーチン群


# -------------------------
# 画像情報取得 (jpg ファイル名, 枚数, コメントファイル)

def mkinfo

  # img-web から画像ファイル名一覧をとりだし, 配列 imgfiles へ代入
  $imgfiles = []
  Dir.foreach("../#{$figdir}") { |item|
    if item =~ /#{$base}$/ 
      $imgfiles.push( (File.basename(item, $base) ).split($head)  )
    end
  }

  # 画像ファイル数
  $file_num = $imgfiles.size

  $imgfiles = $imgfiles.sort

  # $infofile がなければ作成
  unless File.exist?($infofile) then
    ifile = open($infofile, "w")
    $file_num.times{ |num|
#      ifile.print "%03d:\n" % num
      ifile.print "#{$imgfiles[num]}:\n" % num
    }
    ifile.close
  end


  # $infofile からファイル名とコメントを取り出す
  # $info[true][0] にはファイル名, $info[0][true] にはコメントが入る
  $info = [] 
  ifile = open($infofile, "r")
  ifile.each { |line|
    $info.push(line.chomp.split(/:/))
  }
  ifile.close

  # $infofile からファイル数を再定義
  $file_num = $info.size
#  p $info

end

# -------------------------
# サムネイル $index の作成

def makethum

  # 元ファイル削除
  File.delete("../" + $index + ".html") if File.exist?("../" + $index  + ".html") 

  # 初期化
  $htmls = ""
  # ヘッダ部
  $htmls = htmlhead
  # 本文表題部
  $htmls << htmltitle

  # サムネイル部分作成 
  $file_num.times { |num|
    $htmls << htmlthum(num)
  }
  # table 末処理
  amari = ($file_num - 1 ) % $figtable_num
  figtable_1 = $figtable_num -1
  figtable_2 = $figtable_num -2
  amari.upto(figtable_2) { |num|
    $htmls << <<HTMLEOF
       <td align="center" width="20%" valign="top">
        &nbsp;
       </td>
HTMLEOF
    }
  $htmls << %|      </tr>\n| unless amari == figtable_1

  # フッタ処理
  $htmls << htmlfoot

  # ファイルの書きだし
  ifile = open( "../" + $index + ".html", "w")
  ifile.print $htmls
  ifile.close

end


# -------------------------
# rd 組み込み用サムネイル $index の作成

def makethum_rd

  # ディレクトリ作成, 元ファイル削除
  Dir.mkdir("../html",0775)  unless File.exist?("../html") 
  File.delete("../html/table_" + $index + ".rd") if File.exist?("../html/table_" + $index  + ".rd")

  # 初期化
  $htmls = ""
  $htmls << %|=begin html\n|
  $htmls << %|<center>\n|
  $htmls << %|<table BORDER="0" cellspacing="5" width="80%" align="center">\n|

  # サムネイル部分作成 
  $file_num.times { |num|
    $htmls << htmlthum(num)
  }
  # table 末処理
  amari = ($file_num - 1 ) % $figtable_num
  figtable_1 = $figtable_num -1
  figtable_2 = $figtable_num -2
  amari.upto(figtable_2) { |num|
    $htmls << <<HTMLEOF
       <td align="center" width="20%" valign="top">
        &nbsp;
       </td>
HTMLEOF
    }
  $htmls << %|      </tr>\n| unless amari == figtable_1
  $htmls << %|</table>\n|
  $htmls << %|</center>\n|
  $htmls << %|=end html\n|

  # ファイルの書きだし
  ifile = open( "../html/table_" + $index + ".rd", "w")
  ifile.print $htmls
  ifile.close

end



#-------------------------------------------------------------
# サムネイル $index 用サブルーチン

# html ヘッダ部
def htmlhead
   htmls = <<HTMLEOF
<html lang="ja">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=x-euc-jp">
  <meta name="Author" content="YUKIKO YAMADA">
  <meta name="robots" content="noindex,nofollow">
  <meta name="robots" content="noarchive">
  <meta name="generator" content="ape-thum.rb">
  <title>#{$title}</title>
  <style type="text/css">
  <!--
    BODY { font-style:osaka;color:#{$color_font};}
    a:link { color:#{$color_link}; }
    a:visited { color:#{$color_visited}; }
    A{text-decoration:none; font-weight:bold; }
    A:hover {color:#{$color_hover}}
    hr { border: #{$color_font} 1px solid; }
  -->
  </STYLE>
</head>
HTMLEOF
return htmls
end

# html thumnail 繰り返し部
def htmlthum(num)
  picname = $info[num][0]
  amari   = num % $figtable_num 
  figtable_1 = $figtable_num -1
  comment = $info[num][1]

  # テーブルの配置によって <tr> のつけたしをおこなう.
  # 左端 : 3n+1   ($amari == 0)
  # 右端 : 3n     ($amari == 2)

  htmls = ""
  htmls = %|      <tr align="center" valign="center">\n| if amari == 0

  if picname == "title" then 
    htmls << <<HTMLEOF
       <td align="center" valign="center">
        <small>
         #{comment}
        </small>
       </td>
HTMLEOF

  elsif picname == nil then 
    htmls << <<HTMLEOF
       <td align="center" width="20%" valign="top">
	  &nbsp;
       </td>
HTMLEOF


  else
    htmls << <<HTMLEOF    
       <td align="center" width="20%" valign="top">
         <A HREF="#{$figdir}/#{$head}#{picname}#{$base}">
         <img src="#{$figdir}/#{$head}#{picname}#{$base}" border="1" width="#{$width}" height="#{$height}">
        </A>
        <br>
        <small>
         #{picname.gsub("-brunt","-brunt<br>")}<br>
         #{comment}<br><br>
        </small>
       </td>
HTMLEOF

  end

  htmls << %|      </tr>\n| if amari == figtable_1
  return htmls
end

# フッター
def htmlfoot
  htmls = <<HTMLEOF    
<tr><td colspan="#{$figtable_num}" bgcolor="#{$color_table}" align="center">
<B><small>#{$footer}</small></B>
</td></tr>

</table>

 </center>
 </body>
</html>
HTMLEOF
  return htmls
end

# 本文表題部
def htmltitle
  htmls = <<HTMLEOF
 <body>
  <center>
<table BORDER="0" cellspacing="10" width="80%" align="center">
<tr><td colspan="#{$figtable_num}" bgcolor="#{$color_table}" align="center">
&nbsp;
<br>
<B><big><< #{$header1} >></B></big><br>
<small>#{$header2}</small>
<br>&nbsp;
</td></tr>
HTMLEOF
return htmls
end


######################################################
if $0 == __FILE__

apethum = <<EOF
#!/usr/bin/env ruby
#
# 表題: ape お絵描きサムネイル HTML 作成 設定ファイル
#
# 履歴: 2003/09/28 やまだ由 
#       2004/02/18 やまだ由 ($head 付加)
#
# 注意: 本プログラムは, 
#       dcphoto.pl Ver 1.02 - 写真用 HTML作成スクリプト
#       Mitsuda Chihiro   <totera@ep.sci.hokudai.ac.jp>
#       を ruby で写経し, やまだ由用にカスタマイズしたものである 
#
#
#
#

END\{

# rd のみの出力
#apethum_rd 
# html も出力
#apethum
# html のみ出力
apethum_html


\}


########################################################
##  ユーザ設定
########################################################


# html ファイル名  \$PWD/../html/\$\{index\}.html, \$PWD/../html/table\_\$\{index\}.rd
$index = "htmlname"

# 情報ファイル, $PWD/${infofile}
$infofile = $index + ".txt"

# 拡張子
$base = ".gif"

# 接頭子
# $head = "figure_head"
$head = ""

# 絵のあるディレクトリ名, \$PWD/../\$\{figdir\}
$figdir = "figdir"

# 画像ファイルサイズ, 横にならべるファイル数
# $width  = "160";  $height = "120" ; $figtable_num = 5
$width  = "200";  $height = "150" ; $figtable_num = 4
# $width  = "280" ; $height = "210" ; $figtable_num = 3

# テーブル, フォントの色
$color_table   = "#ADD8E6" 
$color_font    = "#000080" 
$color_link    = "#269900" 
$color_visited = "#269900" 
$color_hover   = "#99FF33" 

# html ヘッダタイトル
$title  = "テストタイトル"

# サムネイルタイトル
$header1 = $title 
$header2 = "<br>
実験設定などはここに書き込む.  <br>
ここに書いた文字はセンタリングされてしまう.  <br>
ちょっと恰好悪い. <br><br>
<table BORDER=\\"0\\" cellspacing=\\"10\\" align=\\"center\\">
<tr><td><small>
テーブルをさらに作ってその中に書き込む. <br>
そうするとこんな感じ. 
</small></td></tr></table>
<br><br>
<hr>
[<a href =\\"../.\\">HOME</a>]
"

# サムネイルフッタ ;
$footer = "Last update: #{'#{Time.now}'} <br>
           Copyright &copy; AGU for APE 2005 (YAMADA Yukiko)"


########################################################
##  include ファイル
########################################################

require 'date'
require '/home/yukiko/lib/ape-thum.rb'
$KCODE = "e"

EOF

if File.exist?("#{Dir.pwd}/ape-thum-make.rb") then
  print "ape-thum-make.rb exist\n" 
else  
  wfile = open("#{Dir.pwd}/ape-thum-make.rb","w") 
  wfile.print apethum
  wfile.close
  print "ape-thum-make.rb create !\n"
end

end






