# # class StaticPage # # represents a static website page. # # require 'property_set' require 'i18n' require 'pathname' class StaticPage attr_accessor :path, :children, :name, :file_path, :props, :parent, :mount_point ## ## CLASS METHODS ## def self.find(site, filter) if filter =~ /\// path = filter.split('/').map{|segment| segment.gsub(/[^0-9a-z_-]/, '')} page = site.pages[path.join('/')] if page return page else return site.pages[path.last] end else site.pages[filter] end end # # loads a directory, creating StaticPages from the directory structure # def scan(&block) Dir.chdir(file_path) do Dir.glob("*").each do |child_name| if File.directory?(child_name) child = StaticPage.new(self, child_name) yield child child.scan(&block) elsif is_simple_page?(child_name) child = StaticPage.new(self, file_without_suffix(child_name)) yield child end end end end ## ## INSTANCE METHODS ## def initialize(parent, name, file_path=nil) @children = [] @name = name if parent @parent = parent @mount_point = @parent.mount_point @parent.add_child(self) @path = [@parent.path, @name].flatten.compact else @path = [] end if file_path @file_path = file_path elsif @parent && @parent.file_path @file_path = File.join(@parent.file_path, @name) else raise 'file path must be specified or in parent' end @simple_page = !File.directory?(@file_path) @props = load_properties end def add_child(page) @children << page end def all_children StaticPageArray.new(child_tree.flatten.compact) end # # e.g. /home/user/dev/leap-public-site/app/views/pages/about-us/contact # #def file_path # "#{@mount_point.directory}/#{@path.join('/')}" #end # # e.g. pages/about-us/contact/en # def template_path(locale=I18n.locale) if @simple_page "#{@mount_point.relative_directory}/#{@path.join('/')}" else "#{@mount_point.relative_directory}/#{@path.join('/')}/#{locale}" end end def inspect "<'#{@path.join('/')}' #{children.inspect}>" end def title begin I18n.t!('pages.' + @name, :raise => true) rescue I18n::MissingTranslationData props.title end end def id self.name end protected def child_tree [self, children.collect{|child| child.child_tree}] end private #def self.relative_to_rails_view_root(absolute_path) # if Rails.root # absolute = Pathname.new(absolute_path) # rails_view_root = Pathname.new(Rails.root + 'app/views') # absolute.relative_path_from(rails_view_root).to_s # end #end def load_properties props = PropertySet.new(self) Dir.glob(file_path + '/*.haml') do |content_file_path| locale = File.basename(content_file_path).sub(File.extname(content_file_path),'') #variable_header = "" #File.open(content_file_path) do |f| # while (line = f.gets) =~ /^- @/ # variable_header << line # end #end props.eval(locale, File.read(content_file_path)) end return props end SUFFIXES = '(haml|md)' # # returns true if the name of a file could be a 'simple' static page # with only one translation. # # rules: # * we include files that end in appriopriate suffixes # * we exclude file names that are locales. # def is_simple_page?(name) name =~ /\.#{SUFFIXES}$/ && name !~ /^(#{AVAILABLE_LANGUAGES.join('|')})\.#{SUFFIXES}$/ end def file_without_suffix(name) name.sub(/^(.*?)\.#{SUFFIXES}$/, "\\1") end end