# $Id: ext_siteconfig.rb,v 1.3 2004/04/23 17:05:47 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.

# require 'qwik/ext_wikidb'

module Qwik
  class Site
    def siteconfig()
      @siteconfig = SiteConfig.new(@config, self) if @siteconfig.nil?
      @siteconfig
    end

    alias config siteconfig

    def open?
      self.config[:open] == true
    end

    def title(pagename=nil)
      t = siteconfig()[:sitename]
      t = site_title() if t == 'qwik.jp'
      pt = page_title(pagename)
      t = pt+" - "+t if pt
      t
    end
  end

  class SiteConfig
    def initialize(config, site)
      @config = config
      @site = site
      @default = {
	:open => false,
	:theme => @config.default_theme,
	:sitename => 'qwik.jp',
      }
      # @key = '_config'
      @key = @config.siteconfig
    end

    def [](a)
      h = @default.dup
      @site[@key].load.each {|line|
	next if line !~ /^\:/
	ar = line.chomp.split(/\:/)
	ar.shift # drop the first array
	k, v = ar
	next if k == nil || v == nil
	k = k.intern
	v = true  if v =~ /^true$/i
	v = false if v =~ /^false$/i
	v = v.to_i if v =~ /^\d+$/
	h[k] = v
      }
      h[a]
    end
  end
end
