From 71ec0edea3f87fb69222dbb6fe025c2211402ca2 Mon Sep 17 00:00:00 2001 From: elijah Date: Tue, 12 Feb 2013 21:33:39 -0800 Subject: added capacity for pulling static pages from multiple directory source trees. --- lib/static_page.rb | 149 ++++++++++++++++++++++++++--------------------------- 1 file changed, 72 insertions(+), 77 deletions(-) (limited to 'lib/static_page.rb') diff --git a/lib/static_page.rb b/lib/static_page.rb index 1c5336e..eb32319 100644 --- a/lib/static_page.rb +++ b/lib/static_page.rb @@ -1,72 +1,50 @@ +# +# class StaticPage +# +# represents a static website page. +# +# + require 'property_set' require 'i18n' require 'pathname' class StaticPage - - class PageArray < Array - def limit(num) - PageArray.new(self[0..(num-1)]) - end - def order_by(attr, options={}) - locale = options[:locale] || I18n.locale - direction = options[:direction] || :asc - array = sort do |a,b| - if direction == :desc - a, b = b, a - end - a_prop = a.props.locale(locale).send(attr) - b_prop = b.props.locale(locale).send(attr) - if a_prop.nil? && b_prop.nil? - 0 - elsif a_prop.nil? - 1 - elsif b_prop.nil? - -1 - else - a_prop <=> b_prop - end - end - array.delete_if do |page| - page.props.locale(locale).send(attr).nil? - end - return PageArray.new.replace array - end - end - - attr_accessor :path, :children, :name, :props, :parent + attr_accessor :path, :children, :name, :file_path, :props, :parent, :mount_point ## ## CLASS METHODS ## - def self.find(filter) + def self.find(site, filter) if filter =~ /\// path = filter.split('/').map{|segment| segment.gsub(/[^0-9a-z_-]/, '')} - page = @@pages[path.join('/')] + page = site.pages[path.join('/')] if page return page else - return @@pages[path.last] + return site.pages[path.last] end else - @@pages[filter] + site.pages[filter] end end - def self.all - @@pages_array - end - - def self.load(directory) - @@pages = {} - @@page_array = PageArray.new - @@root_directory = directory - @@relative_root_directory = relative_to_rails_view_root(directory) - scan_directory(directory) do |page| - @@pages[page.name] ||= page - @@pages[page.path.join('/')] = page - @@page_array << page + # + # 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 @@ -74,17 +52,26 @@ class StaticPage ## INSTANCE METHODS ## - def initialize(parent, name) + 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 - @props = load_properties(file_path) 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) @@ -92,21 +79,25 @@ class StaticPage end def all_children - PageArray.new(child_tree.flatten.compact) + StaticPageArray.new(child_tree.flatten.compact) end # # e.g. /home/user/dev/leap-public-site/app/views/pages/about-us/contact # - def file_path - "#{@@root_directory}/#{@path.join('/')}" - end + #def file_path + # "#{@mount_point.directory}/#{@path.join('/')}" + #end # # e.g. pages/about-us/contact/en # def template_path(locale=I18n.locale) - "#{@@relative_root_directory}/#{@path.join('/')}/#{locale}" + if @simple_page + "#{@mount_point.relative_directory}/#{@path.join('/')}" + else + "#{@mount_point.relative_directory}/#{@path.join('/')}/#{locale}" + end end def inspect @@ -133,32 +124,19 @@ class StaticPage private - def self.scan_directory(directory, parent=nil, &block) - parent ||= StaticPage.new(nil, 'root') - Dir.chdir directory do - Dir.glob("*").each do |child_dir| - next unless File.directory?(child_dir) - page = StaticPage.new(parent, child_dir) - yield page - scan_directory(child_dir, page, &block) - end - end - end + #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 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(file_path) + 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 = "" - #p content_file_path #File.open(content_file_path) do |f| # while (line = f.gets) =~ /^- @/ # variable_header << line @@ -169,6 +147,23 @@ class StaticPage 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 -- cgit v1.2.3