#!/usr/bin/ruby
# -*- encoding: utf-8 -*-
require 'erb'
require 'rss'
require '../bin/rss_cdata.rb'
require '../bin/newsparser.rb'

# Layout class
class Layout
  extend ERB::DefMethod
  def_erb_method('layout', '../template/layout-news.html.erb')
end

# 
urlhead="http://vinelinux.org/"
newsdir="./"
topdir=".."
tmpdir = topdir+'/template/'
targetname="vlmagazine"
rssname=targetname+'.rdf'

header=File.open(topdir+"/template/header.tmpl")
footer=File.open(topdir+"/template/footer.tmpl")

# Create rss object
rss = RSS::Maker.make("1.0") do |maker|
  maker.channel.about = "http://vinelinux.org/#{targetname}/rss"
  maker.channel.title = "Vine Linux Magazine"
  maker.channel.description = "Vine Linux Magazine です"
  maker.channel.link = "http://vinelinux.org/#{targetname}/"
  maker.items.do_sort = true

# create news indexhtml contents
newsindex = <<EOF
<h1 class="title">Vine Linux Magazine</h1>
<div class="news">
<p>
Vine Linux Magazine では Vine Linux を活用するのに便利な情報を記事として提供していきます。
</p>
<dl>
 <dt>掲載予定</dt>
 <dd>- 定期的な VineSeed/VinePlus パッケージ紹介 (ほぼ月刊)<br/>
     - Vine Linux 活用記事 (月1本程度で不定期)</dd>
</dl>

<h2>記事一覧</h2>
<table>
EOF

# create all news page
Dir.open(newsdir) { |dir|
 for filename in dir.grep(/.dat$/).sort.reverse

  news = NewsParser.new IO.read(newsdir+filename)
  titletext = news.item['title'].gsub("\n","").gsub(/<[^>]+>/,"").strip
  $header_title = 'Vine Linux Magazine - '+titletext

  if (news.item['date'].gsub(/\,/,"") <= Time.now.strftime('%Y%m%d'))
   # index content
   newsindex += <<-EOF
   <tr>
    <td class="date">[#{news.item['date']}]</td>
    <td class="title"><a href="/#{targetname}/#{filename.gsub(/.dat/,".html")}">
        #{titletext}</a></td>
   </tr>
   EOF

   # rss
   rssitem = maker.items.new_item
   rssitem.link = urlhead+"#{targetname}/#{filename.gsub(/.dat/,".html")}"
   rssitem.title = news.item['title']
   rssitem.date = Time.parse(news.item['date'].gsub(/\,/,"/"))
   rssitem.dc_date = Time.parse(news.item['date'].gsub(/\,/,"/"))
   rssitem.content_encoded = news.item['description']
  end

  # news page content
  newshtml = <<-EOF
  <h1 class="title">Vine Linux Magazine<br/>#{news.item['title']}</h1>
  <div class="ads-middle-linkunit">
<script type="text/javascript"><!--
google_ad_client = "pub-0334540465096658";
/* vinelinux link-unit */
google_ad_slot = "4566651758";
google_ad_width = 728;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
  </div>
  EOF

  if (news.item['author'])
    newshtml += <<-EOF
    <div align="right">筆者: #{news.item['author']}</div>
    EOF
  end

  newshtml += <<-EOF
  <div align="right">発行日: #{news.item['date']}</div>
  <div class="news">
  <p>
#{news.item['description']}
  </p>
  </div>
  EOF

  # wrtite news page
  template = ERB.new(newshtml)
  newfile = open(filename.gsub(/.dat/,".html"), "w")
  newfile.puts Layout.new.layout { template.result }
  newfile.close
 end
}

newsindex += <<EOF
</table>
</div>
EOF

# write news indexhtml
$header_title = 'Vine Linux Magazine'
template = ERB.new(newsindex)
newsindexhtml = open("index.html", "w")
newsindexhtml.puts Layout.new.layout { template.result }
newsindexhtml.close

end

# write news rss
File.open(rssname, "w") do |file|
  file.puts rss.to_s
end
