require "factory.rb"
require "model.rb"
require "content.rb"
require "config.rb"
require "html.rb"


#================================================
# Classes that do the actual content generation
# via templates and content holders.
#================================================


def get_store
  Factory.for_name(Config::STORE_CLASS).new
end

#--------------------------------------------------------------
# Criteria
# Interprets the URL to provide the criteria represented by
# that URL.
#--------------------------------------------------------------
class Criteria
  def initialize(path)
    @path = path.chomp  
  end
  
  def expression
    @path
  end
  
  def root?
    @path == "" || @path = "/"
  end
  
  def directory?
    !post? 
  end
  
  def post?
    @path =~ /\.html$/
  end
end


#--------------------------------------------------------------
# Template - represents templates with replaceable parameters, 
# and provides the method by which the replacements are made.
#--------------------------------------------------------------
class Template
  def initialize(text)
    @text = text
  end

  def text
    @text
  end

  # Trickiest code in the entire system!  Apply "attribute" values 
  # from the passed Hash, to matching Template content variables 
  # (which have the form #{variable_name}), resulting in resolved content.  
  def apply(attribs_hash)
    # Basically, the below finds each variable occurence, and the block supplies the
    # replacement value for that occurence from the attributes hash.  The outer gsub
    # ensures that all occurences of that variable are replaced.
    result = @text.gsub(/#\{\w+\}/){ |m|
               keyword_regexp = /#\{(\w+)\}/ # capture variable name in $1
               keyword_regexp.match(m)       # get the template symbol
               attribs_hash[$1]              # yield return replacement for the symbol (variable_name) 
                                             # from hash, for use in gsub.
             }
    result
  end 

  def to_s
    @text
  end
end


#----------------------------------------------------
# Weblog - represents the HTML document to be
# returned.
#----------------------------------------------------
class Weblog
  def initialize(criteria)
    @criteria = criteria
  end 

  def generate_content
    attrs = Hash.new
    log_entries = get_store().get_all_log_entries(@criteria, Config::MAX_ENTRIES)
    entry_template = get_store().get_entry_template()
    attrs.update(LogEntries.new(log_entries, entry_template).attribs)
    
    sidebar_entries = get_store().get_all_sidebar_entries(@criteria)
    sidebar_template = get_store().get_sidebox_template()
    attrs.update(SidebarEntries.new(sidebar_entries, sidebar_template).attribs)
    
    attrs.update(Categories.new(@criteria.expression).attribs)
    attrs.update(BreadCrumbs.new(@criteria.expression).attribs)
    attrs.update(Miscellaneous.new(@criteria.expression).attribs)
        
    # apply the attributes to the template
    template = get_store().get_body_template()
    template.apply(attrs)
  end
end
