# $Id: ext_common.rb,v 1.10 2004/04/22 02:37:45 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 list()
      ul = @g.ul
      @site.pages.title_list.each {|page|
	ul << @g.li{[
	    @g.span(:class=>"date"){page.mtime.ymd+" : "},
	    get_link(page)
	  ]}
      }
      ul
    end

    def recent(max = -1)
      div = @g.div
      ul = nil
      cur_date = nil
      i = 0
      @site.pages.date_list.reverse.each {|page|
	break if 0 <= max && max < (i += 1)
	date = page.mtime.ymd
	if cur_date != date
	  div << ul if ul
	  ul = @g.ul
	  div << @g.h3{date}
	  cur_date = date
	end
	ul << @g.li{get_link(page)}
      }
      div << ul if ul
      div
    end

    def get_title(page)
      page.get_title().escapeHTML
    end

    def get_key(page)
      page.key.escapeHTML+".html"
    end

    def get_anchor(page)
      @g.a(get_key(page)){get_title(page)}
    end

    def get_link(page)
      @g.a(get_key(page)){page.key}
    end

    def make_anchor(url, text)
      @g.a(url){text}
    end

    def pagename()
      @pagename
    end

    def toc() ""; end

    def password(pass) ''; end

    def null() ''; end

    def menu(cmd, msg=nil)
      return '' if cmd != 'edit'
      msg = ""
      msg = '編集' if cmd == 'edit'
      make_anchor(@pagename.escape+'.html?c='+cmd, msg)
    end

    def dologout()
      @res.clear_cookies() #  remove the cookie from browser
      @g.p{"LOGOUTしました。"}
    end

    def mail(from="")
      str = yield
      msg = "* From: "+from+"\n"+str
      text(msg)
    end

    def include(pagename)
      page = @site[pagename]
      return "" if page.nil?
      str = page.load
      text(str)
    end

    def text(str="")
      str = "" if str.nil?
      if block_given?
	str2 = yield
	str += str2 if str2
      end
      xml = str.w.res(@site)
      d = @g.div
      xml.root.each {|e|
	d << e
      }	
      d
    end

    def last_modified
      @g.div{"last modified: "+@page.mtime.ymd}
    end
  end
end
