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

require "uri"
require "net/http"

module Qwik
  class Action
    def plg_a(page, text)
      [:a, {:href=>page+".html"}, text]
    end

    def plg_img(f, alt=f)
      [:img, {:src=>f, :alt=>alt}]
    end

    def plg_style_div(style="", a="", &b)
      return unless CSS.valid?(style)
      b = ""
      b = yield if block_given?
      msg = a.to_s + b.to_s
      x = c_res(msg)
      [:div, {:style=>style}, x]
    end
    
    def plg_block_div(div_class="",a="", &b)
      return unless CSS.valid?(div_class)
      b = ""
      b = yield if block_given?
      msg = a.to_s + b.to_s
      x = c_res(msg)
      [:div, {:class=>div_class}, x]
    end
    
    def plg_center(a="", &b)
      plg_style_div("text-align:center;", &b)
    end

    def plg_right(a="", &b)
      plg_style_div("text-align:right;", &b)
    end

    def style_strip_p(str)
      str.delete("\n").sub(/^<p>/, "").sub(%r|</p>$|, "")
   end

    def plg_style_span(style="", a="")
      return unless CSS.valid?(style)
      b = ""
      b = yield if block_given?
      msg = a.to_s + b.to_s
      x = c_res(msg)
      w = x[0]
      war = []
      if w.nil?
	war << "" 
      elsif w.first == :p
	w.shift
	war += w
      else
	war << w
      end
      [:span, {:style=>style}, *war]
    end
    def plg_small(a="", &b)
      plg_style_span("font-size:smaller;", a, &b)
    end

    def plg_css
      str = yield
      return "error" unless CSS.valid?(str)
      [:style, str]
    end
    alias plg_style plg_css

    def act_css
      str = ""
      if 1 < @req.path_args.length
	url = @req.path_args.join("/")
	url = url.sub(/\Ahttp:\//, "http://") if /\Ahttp:\/[^\/]/ =~ url
	str = c_get_url(url)
      else
	filename = @req.path_args[0]
	ext = File.extname(filename).sub(/\A\./, "")
	raise if ext != "css"
	file = @site.attach.path(filename)
	raise unless file.exist?
	str = file.open {|f| f.read}
      end
      return c_nerror(_("Access Failed"), nil, 404) if str.nil?
      str = "/* invalid css */" unless CSS.valid?(str)
      @res.body = str
    end

    def c_get_url(url)
      uri = URI.parse(url)
      raise if uri.scheme != "http"
      res = nil
      begin
	Net::HTTP.start(uri.host, uri.port) {|http|
	  res = http.get(uri.path)
	}
      rescue
	return nil
      end
      res.body
    end
  end
end

if $0 == __FILE__
  $LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
  require "t/test"
  $debug = true
end

if $debug
  class TC_ActStyle < Test::Unit::TestCase
    include TestSession

    def test_all
      # test_style_span
      assert_wiki("<img alt=\"t\" src=\"t\"/>", "{{img(t)}}")
      assert_wiki("<img alt=\"m\" src=\"t\"/>", "{{img(t, m)}}")
      assert_wiki("<span style=\"font-size:smaller;\">a</span>", "{{small(a)}}")
      assert_wiki("<span style=\"font-size:smaller;\">a</span>",
		  "{{small\na\n}}")
      assert_wiki("<span style=\"font-size:smaller;\">a</span>",
		  "{{small(a)\n}}")
      assert_wiki("<span style=\"font-size:smaller;\">aa</span>",
		  "{{small(a)\na\n}}")
      assert_wiki("<span style=\"font-size:smaller;\"></span>", "{{small\n}}")
      assert_wiki("<span style=\"font-size:smaller;\"><img alt=\"t\" src=\"t\"/></span>", "{{small\n{{img(t)}}\n}}")

      # test_style_div
      assert_wiki("<div style=\"text-align:center;\"></div>", "{{center(a)}}")
      assert_wiki("<div style=\"text-align:center;\"></div>", "{{center(a)\n}}")
      assert_wiki("<div style=\"text-align:center;\"><p>y</p></div>", "{{center\ny\n}}")
      assert_wiki("<div style=\"text-align:center;\"><img alt=\"t\" src=\"t\"/></div>", "{{center\n{{img(t)}}\n}}")
      assert_wiki("<div style=\"text-align:right;\"><img alt=\"t\" src=\"t\"/></div>", "{{right\n{{img(t)}}\n}}")
      assert_wiki("<div style=\"text-align:center;\"><p>y</p></div>", "{{style_div(text-align:center;)\ny\n}}")
      assert_wiki("<div style=\"text-align:center;\"><p>&lt;</p></div>", "{{style_div(text-align:center;)\n<\n}}")
      assert_wiki("", "{{style_div(@i)\ny\n}}")
      assert_wiki("<br/>", "{{br}}")

      # test_css_plugin
      assert_wiki("<style>h2 { color: red }\n)}}</style>",
		  "{{css\nh2 { color: red }\n)}}")
      assert_wiki("error", "{{css\n@import\n}}")
      assert_wiki("error", "{{css\n\\important\n}}")
      assert_wiki("error", "{{css\njavascript\n}}")

      # test_extcss
      d = @dir+".attach"
      d.erase_all_for_test if d.exist?

      @site.attach.put("test.css", "/* test */")
      session("/test/.css/test.css")
      assert_equal("/* test */", @res.body)
      @site.attach.delete("test.css")

      @site.attach.put("test.css", "@i")
      session("/test/.css/test.css")
      assert_equal("/* invalid css */", @res.body)
      @site.attach.delete("test.css")

      # test_style_with_plugin
      assert_wiki("<div style=\"text-align:center;\"><p>t</p></div>",
		  "{{center\nt\n}}")
      assert_wiki("<div style=\"text-align:center;\">test</div>",
		  "{{center\n{{test}}\n}}")
    end
  end
end
