# $Id: ext_html.rb,v 1.3 2004/04/13 01:48:11 eto Exp $
# Copyright (C) 2003-2004 Kouichirou Eto, All rights reserved.
# This is free software with ABSOLUTELY NO WARRANTY.
# You can redistribute it and/or modify it under the terms of the GNU GPL2.

module Qwik
  class Plugin
    def a(page, text)
      @g.a(page+".html"){text}
    end

    def img(f, alt=f)
      @g.img(f, alt)
    end

    def style_valid?(arg="")
      (arg =~ /^[-:;\w\s]+$/) != nil
    end

    def style_div(style="", a="")
      return REXML::Text.new("") unless style_valid?(style)
      b = block_given? ? yield : ""
      b = "" if b.nil?
      msg = a + b
      x = msg.w.res(@site)
      d = @g.div(:style=>style){}
      x.root.each {|e|
	if e.is_a?(REXML::Element) && e.name = 'p'
	  e.each {|ee|
	    d << ee
	  }
	elsif e.is_a?(REXML::Text)
	  text = e.to_s
	  if text !~ /^\s+$/
	    d << e
	  end
	else
	  d << e
	end
      }	
      d
    end

    def center(a="", &b)
      style_div('text-align: center;', &b)
    end

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

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

    def style_span(style="", a="")
      return "" unless style_valid?(style)
      b = block_given? ? yield : ""
      b = "" if b.nil?
      msg = a + b
      x = msg.w.res(@site)
      d = @g.span(:style=>style){}
      x.root.each {|e|
	if e.is_a?(REXML::Element) && e.name = 'p'
	  e.each {|ee|
	    d << ee
	  }
	elsif e.is_a?(REXML::Text)
	  text = e.to_s
	  if text !~ /^\s+$/
	    d << e
	  end
	else
	  d << e
	end
      }	
      d
    end

    def small(a="", &b)
      style_span('font-size: smaller;', a, &b)
    end

    def br()
      @g.br
    end
  end
end
