#!/usr/bin/ruby
require 'erb'

contentsdir = 'contents.d/'
tmpdir = 'template/'

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

Dir.foreach(contentsdir).grep(/.html$/) do |filename|
  template = ERB.new(IO.read(contentsdir+filename),nil,'<>')
  newfile = open(filename, "w")
  header_title_base = 'Vine Linux'
  if filename == "index.html"
    newfile.puts Layout.new.layout_top { template.result }
  else
    title=File.read(contentsdir+filename).grep(/h1 class=\"title/)
    if title[0]
      $header_title = header_title_base+' - '+title[0].gsub(/.*>(.*)<.*/,"\\1").chomp
    else
      $header_title = header_title_base
    end
    newfile.puts Layout.new.layout { template.result }
  end
  newfile.close
end
