# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

require "pp"
$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/config"
require "qwik/htree-to-wabisabi"
require "qwik/util-path"

module Qwik
  class TemplateGenerator
    def self.main(argv)
      config = Config.new
      config.parse_args(argv)
      tg = self.new(config)
      tg.generate_all
    end

    def initialize(config)
      @template_path = config.template_dir.path
    end

    def generate_all
      path = @template_path
      Pathname.glob(path+"*.html") {|file|
	basename = file.basename(".html").to_s
	make(basename)
      }
    end

    def make(basename)
      path = @template_path
      outfile = path+(basename+".rb")
      file = path+(basename+".html")

      in_mtime  = file.mtime
      out_mtime = outfile.exist? ? outfile.mtime : Time.at(0)
      return unless out_mtime < in_mtime

      puts "generate #{basename}" unless $debug

      html = file.open {|f| f.read }
      w = generate_template(html)

      src = ""
      PP.pp(w, src)	# inspect with pretty print.

      str = <<"EOT"
# automatically generated.
# PLEASE DO NOT EDIT THIS FILE.

module Qwik
  class Template
    def self.generate_#{basename}
      #{src}
    end
  end
end
EOT
      outfile.open("wb") {|out|
	out.puts str
      }
    end

    def generate_template(html)
      html = html.map {|line| line.strip }.join
      return HTree(html).to_wabisabi
    end
  end
end

if $0 == __FILE__
  require "test/unit"
  require "qwik/config"
  require "qwik/qp"
  $debug = true
end

if $debug
  class TC_TemplateGenerator < Test::Unit::TestCase
    def setup
      @config = defined?($test_config) ? $test_config : Qwik::Config.new(true)
      @tg = Qwik::TemplateGenerator.new(@config)
    end

    def test_generate_template
      html = '
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns:v="urn:schemas-microsoft-com:vml">
  <head id="header"></head>
  <body onload="qwik_onload();" ondblclick="wema_dblClick(this);" ondragdrop="qwik_dragdrop(this);">
    <div class="container">
      <div class="main">
        <div class="adminmenu" id="adminmenu"></div><!--adminmenu-->
        <div class="toc" id="toc"></div><!--toc-->
        <h1 id="view_title"></h1>
        <div id="body_enter"></div><!--id:body_enter-->
        <div id="body"></div><!--id:body-->
        <div id="body_leave"></div><!--id:body_leave-->
      </div><!--main-->
      <div class="sidebar" id="sidemenu"></div><!--sidebar-->
      <div class="footer" id="footer"></div><!--footer-->
    </div><!--container-->
  </body>
</html>'
      w = [[:"!DOCTYPE", "html", "PUBLIC",
	  "-//W3C//DTD HTML 4.01 Transitional//EN",
	  "http://www.w3.org/TR/html4/loose.dtd"],
	[:html,
	  [:head, {:id=>"header"}],
	  [:body, {:ondragdrop=>"qwik_dragdrop(this);",
	      :onload=>"qwik_onload();", :ondblclick=>"wema_dblClick(this);"},
	    [:div, {:class=>"container"},
	      [:div, {:class=>"main"},
		[:div, {:id=>"adminmenu", :class=>"adminmenu"}],
		[:"!--", "adminmenu"],
		[:div, {:id=>"toc", :class=>"toc"}], [:"!--", "toc"],
		[:h1, {:id=>"view_title"}],
		[:div, {:id=>"body_enter"}], [:"!--", "id:body_enter"],
		[:div, {:id=>"body"}], [:"!--", "id:body"],
		[:div, {:id=>"body_leave"}], [:"!--", "id:body_leave"]],
	      [:"!--", "main"],
	      [:div, {:id=>"sidemenu", :class=>"sidebar"}], [:"!--", "sidebar"],
	      [:div, {:id=>"footer", :class=>"footer"}], [:"!--", "footer"]],
	    [:"!--", "container"]]]]
      assert_equal(w, @tg.generate_template(html))
    end

    def test_generate_all
      @tg.generate_all
    end
  end
end
