module Atchoum # Every Atchoum website script extends the +Website+ class. # A website is composed of pages (methods ending with +_page+). # The +index_page+ method is the root of the website. # Every pages are passed trough the +layout+ method as a block, # the default layout wraps your stuff in a soft html and body tags. class Website < Markaby::Builder include Markup attr_reader :pages, :reloader def initialize super @pages = public_methods.collect do |m| /^(\w+)_page$/ =~ m ? $1 : nil end.compact reset! end def reset! @elements = {} end # Default layout for every renderd page. # Redefine this to setup a custom layout. def layout html do body do self << yield end end end # Render the content of a page as a string # containing the magic HTML. def render(page) reset! @rendered_page = page.to_sym s = capture { send("#{page}_page") } s = capture { send(:layout) { s } } if respond_to? :layout @rendered_page = nil s end # Render all pages to seperate html files in correponding folders. # For exemple, +index_page+ will be parsed to +/index.htm+ # and +contact_page+ to +/contact/index.htm+. def to_files pages.each do |page| if page.to_s == 'index' path = '.' else path = page Dir::mkdir path if !File.exist? path end File.open("#{path}/index.htm", 'w') do |f| f << render(page) end end end end end