#!/usr/bin/env ruby
#
# 表題: DCCHART study 領域 お絵描きサムネイル HTML 作成スクリプト
#
# 履歴: 2005/01/19 塚原 大輔  -- ape-thum.rb をカスタム
#
# 注意: 本プログラムは, 
#       dcphoto.pl Ver 1.02 - 写真用 HTML作成スクリプト
#       Mitsuda Chihiro   <totera@ep.sci.hokudai.ac.jp>
#       を山田由貴子さんが ruby で写経しカスタマイズした
#       ape-thum.rb 
#       を塚原が譲り受けカスタムしたものである.
#
#
#

# ---------------------------------------------------------
# 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


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


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

def mkinfo

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

  # 画像ファイル数
  $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 << %|#{$title}\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="dcchart-study-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}/#{picname}#{$base}">
         <img src="#{$figdir}/#{picname}#{$base}" border="1" width="#{$width}" height="#{$height}">
        </A>
        <br>
        <small>
         <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
#
# 表題: DCCHART study 領域 お絵描きサムネイル HTML 作成スクリプト
#
# 履歴: 2005/01/19 塚原 大輔  -- ape-thum.rb をカスタム
#
# 注意: 本プログラムは, 
#       dcphoto.pl Ver 1.02 - 写真用 HTML作成スクリプト
#       Mitsuda Chihiro   <totera@ep.sci.hokudai.ac.jp>
#       を山田由貴子さんが ruby で写経しカスタマイズした
#       ape-thum.rb 
#       を塚原が譲り受けカスタムしたものである.
#

END\{

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

\}


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


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

# インデックスファイル  \$PWD/../html/\$\{index\}.html, \$PWD/../html/table\_\$\{index\}.html
$index = "test"

# 拡張子
$base = ".gif"

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

# 横にならべるファイル数

# $figtable_num = 5
$figtable_num = 3

#画像ファイルサイズ

# $width  = "160"
# $height = "120"

$width  = "200"
$height = "150"

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

# html ヘッダタイトル
$title  = "25 年平均値"

# サムネイルタイトル
$header1 = $title 
$header2 = "運たら"

# サムネイルフッタ ;
$footer = "Last update: \#\{Time.now\} <br> 
           Copyright &copy\\; 2005 TSUKAHARA Daisuke"



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

require 'date'
require '/home/daktu32/bin/dcchart-study-thum.rb'
$KCODE = "e"

EOF

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

end






