# # responsible for loading all the pages of the site. # WORK IN PROGRESS, FACTOR OUT FROM SITE module Amber class SiteLoader # # page_paths # pages_by_name # pages_by_path # pages_by_locale_path # root # dir_list # def self.load(config) config.reset_timestamp # create base_page base_page = begin if config.path.nil? @root = StaticPage.new(nil, 'root', config.pages_dir) add_page(@root) @root else name = File.basename(config.path) page = StaticPage.new(find_parent(config.path), name, config.pages_dir, config.path_prefix) add_page(page) page end end base_page.config = config # load menu and locals I18n.load_path += Dir[File.join(config.locales_dir, '/*.{rb,yml,yaml}')] if config.locales_dir # add the full directory tree base_page.scan_directory_tree do |page, asset_dir| add_page(page) if page @dir_list << asset_dir if asset_dir end @page_paths += @pages_by_path.keys # recursively add sub-sites config.children.each do |sub_config| add_configuration(sub_config) end end private def self.find_parent(path) so_far = [] path.split('/').compact.each do |path_segment| so_far << path_segment if page = @pages_by_path[so_far.join('/')] return page end end return @root end # # registers a page with the site, indexing the page path in our various hashes # def add_page(page) @pages_by_name[page.name] = page @pages_by_path[page.path.join('/')] = page add_aliases(I18n.default_locale, page, @pages_by_path) page.locales.each do |locale| next if locale == I18n.default_locale add_aliases(locale, page, @pages_by_locale_path[locale]) end @page_list << page end # # registers a page's aliases with the site # def add_aliases(locale, page, path_hash) page.aliases(locale).each do |alias_path| alias_path_str = alias_path.join('/') if path_hash[alias_path_str] Amber.logger.warn "WARNING: page `#{page.path.join('/')}` has alias `#{alias_path_str}`, but this path is already taken by `#{path_hash[alias_path_str].path.join('/')}` (locale = #{locale})." else path_hash[alias_path_str] = page end end end end end