lib/nanoc/base/source_data/site.rb in nanoc-3.8.0 vs lib/nanoc/base/source_data/site.rb in nanoc-4.0.0a1

- old
+ new

@@ -1,22 +1,24 @@ # encoding: utf-8 -module Nanoc +module Nanoc::Int # The in-memory representation of a nanoc site. It holds references to the # following site data: # - # * {#items} — the list of items ({Nanoc::Item}) - # * {#layouts} — the list of layouts ({Nanoc::Layout}) - # * {#code_snippets} — the list of code snippets ({Nanoc::CodeSnippet}) + # * {#items} — the list of items ({Nanoc::Int::Item}) + # * {#layouts} — the list of layouts ({Nanoc::Int::Layout}) + # * {#code_snippets} — the list of code snippets ({Nanoc::Int::CodeSnippet}) # * {#data_sources} — the list of data sources ({Nanoc::DataSource}) # # In addition, each site has a {#config} hash which stores the site # configuration. # - # The physical representation of a {Nanoc::Site} is usually a directory + # The physical representation of a {Nanoc::Int::Site} is usually a directory # that contains a configuration file, site data, a rakefile, a rules file, # etc. The way site data is stored depends on the data source. + # + # @api private class Site # The default configuration for a data source. A data source's # configuration overrides these options. DEFAULT_DATA_SOURCE_CONFIG = { type: 'filesystem_unified', @@ -24,11 +26,11 @@ layouts_root: '/', config: {} } # The default configuration for a site. A site's configuration overrides - # these options: when a {Nanoc::Site} is created with a configuration + # these options: when a {Nanoc::Int::Site} is created with a configuration # that lacks some options, the default value will be taken from # `DEFAULT_CONFIG`. DEFAULT_CONFIG = { text_extensions: %w( css erb haml htm html js less markdown md php rb sass scss txt xhtml xml coffee hb handlebars mustache ms slim ).sort, lib_dirs: %w( lib ), @@ -59,41 +61,32 @@ end # Returns the compiler for this site. Will create a new compiler if none # exists yet. # - # @return [Nanoc::Compiler] The compiler for this site + # @return [Nanoc::Int::Compiler] The compiler for this site def compiler - @compiler ||= Compiler.new(self) + @compiler ||= Nanoc::Int::Compiler.new(self) end # Returns the data sources for this site. Will create a new data source if # none exists yet. # # @return [Array<Nanoc::DataSource>] The list of data sources for this # site # - # @raise [Nanoc::Errors::UnknownDataSource] if the site configuration + # @raise [Nanoc::Int::Errors::UnknownDataSource] if the site configuration # specifies an unknown data source def data_sources load_code_snippets @data_sources ||= begin @config[:data_sources].map do |data_source_hash| # Get data source class data_source_class = Nanoc::DataSource.named(data_source_hash[:type]) - raise Nanoc::Errors::UnknownDataSource.new(data_source_hash[:type]) if data_source_class.nil? + raise Nanoc::Int::Errors::UnknownDataSource.new(data_source_hash[:type]) if data_source_class.nil? - # Warn about deprecated data sources - # TODO: [in nanoc 4.0] remove me - case data_source_hash[:type] - when 'filesystem' - warn "Warning: the 'filesystem' data source has been renamed to 'filesystem_verbose'. Using 'filesystem' will work in nanoc 3.1.x, but it will likely not work anymore in a future release of nanoc. Please update your data source configuration and replace 'filesystem' with 'filesystem_verbose'." - when 'filesystem_combined', 'filesystem_compact' - warn "Warning: the 'filesystem_combined' and 'filesystem_compact' data source has been merged into the new 'filesystem_unified' data source. Using 'filesystem_combined' and 'filesystem_compact' will work in nanoc 3.1.x, but it will likely not work anymore in a future release of nanoc. Please update your data source configuration and replace 'filesystem_combined' and 'filesystem_compact with 'filesystem_unified'." - end - # Create data source data_source_class.new( self, data_source_hash[:items_root], data_source_hash[:layouts_root], @@ -103,28 +96,28 @@ end end # Returns this site’s code snippets. # - # @return [Array<Nanoc::CodeSnippet>] The list of code snippets in this + # @return [Array<Nanoc::Int::CodeSnippet>] The list of code snippets in this # site def code_snippets load @code_snippets end # Returns this site’s items. # - # @return [Array<Nanoc::Item>] The list of items in this site + # @return [Array<Nanoc::Int::Item>] The list of items in this site def items load @items end # Returns this site’s layouts. # - # @return [Array<Nanoc::Layouts>] The list of layout in this site + # @return [Array<Nanoc::Int::Layouts>] The list of layout in this site def layouts load @layouts end @@ -181,18 +174,18 @@ def setup_child_parent_links teardown_child_parent_links item_map = {} @items.each do |item| - item_map[item.identifier] = item + item_map[item.identifier.to_s] = item end @items.each do |item| - parent_id_end = item.identifier.rindex('/', -2) + parent_id_end = item.identifier.to_s.rindex('/', -2) next unless parent_id_end - parent_id = item.identifier[0..parent_id_end] + parent_id = item.identifier.to_s[0..parent_id_end] parent = item_map[parent_id] next unless parent item.parent = parent parent.children << item @@ -213,22 +206,16 @@ # Prevents all further modifications to itself, its items, its layouts etc. # # @return [void] def freeze - config.freeze_recursively + config.__nanoc_freeze_recursively items.each(&:freeze) layouts.each(&:freeze) code_snippets.each(&:freeze) end - # @deprecated It is no longer necessary to explicitly load site data. It - # is safe to remove all {#load_data} calls. - def load_data(_force = false) - warn 'It is no longer necessary to call Nanoc::Site#load_data. This method no longer has any effect. All calls to this method can be safely removed.' - end - # Loads the site data. It is not necessary to call this method explicitly; # it will be called when it is necessary. # # @api private # @@ -315,11 +302,11 @@ # Get code snippets @code_snippets = [] config[:lib_dirs].each do |lib| code_snippets = Dir["#{lib}/**/*.rb"].sort.map do |filename| - Nanoc::CodeSnippet.new( + Nanoc::Int::CodeSnippet.new( File.read(filename), filename ) end @code_snippets.concat(code_snippets) @@ -335,15 +322,15 @@ @items_loaded ||= false return if @items_loaded @items_loaded = true # Get items - @items = Nanoc::ItemArray.new + @items = Nanoc::Int::ItemArray.new data_sources.each do |ds| items_in_ds = ds.items items_in_ds.each do |i| - i.identifier = File.join(ds.items_root, i.identifier) + i.identifier = Nanoc::Identifier.new(File.join(ds.items_root, i.identifier.to_s)) i.site = self end @items.concat(items_in_ds) end end @@ -356,30 +343,32 @@ # Get layouts @layouts = [] data_sources.each do |ds| layouts_in_ds = ds.layouts - layouts_in_ds.each { |i| i.identifier = File.join(ds.layouts_root, i.identifier) } + layouts_in_ds.each do |l| + l.identifier = Nanoc::Identifier.new(File.join(ds.layouts_root, l.identifier.to_s)) + end @layouts.concat(layouts_in_ds) end end # Loads a configuration file. def load_config(config_path) - YAML.load_file(config_path).symbolize_keys_recursively + YAML.load_file(config_path).__nanoc_symbolize_keys_recursively end def apply_parent_config(config, config_paths = []) parent_config_file = config[:parent_config_file] if parent_config_file config.delete(:parent_config_file) config_path = File.absolute_path(parent_config_file, File.dirname(config_paths.last)) unless File.file?(config_path) - raise Nanoc::Errors::GenericTrivial, "Could not find parent configuration file '#{parent_config_file}'" + raise Nanoc::Int::Errors::GenericTrivial, "Could not find parent configuration file '#{parent_config_file}'" end if config_paths.include?(config_path) - raise Nanoc::Errors::GenericTrivial, "Cycle detected. Could not use parent configuration file '#{parent_config_file}'" + raise Nanoc::Int::Errors::GenericTrivial, "Cycle detected. Could not use parent configuration file '#{parent_config_file}'" end parent_config = load_config(config_path) apply_parent_config(parent_config, config_paths + [config_path]).merge(config) else config @@ -388,11 +377,11 @@ def ensure_identifier_uniqueness(objects, type) seen = Set.new objects.each do |obj| if seen.include?(obj.identifier) - raise Nanoc::Errors::DuplicateIdentifier.new(obj.identifier, type) + raise Nanoc::Int::Errors::DuplicateIdentifier.new(obj.identifier, type) end seen << obj.identifier end end @@ -400,34 +389,34 @@ # {#initialize} for details. def build_config(dir_or_config_hash) if dir_or_config_hash.is_a? String # Check whether it is supported if dir_or_config_hash != '.' - warn 'WARNING: Calling Nanoc::Site.new with a directory that is not the current working directory is not supported. It is recommended to change the directory before calling Nanoc::Site.new. For example, instead of Nanoc::Site.new(\'abc\'), use Dir.chdir(\'abc\') { Nanoc::Site.new(\'.\') }.' + warn 'WARNING: Calling Nanoc::Int::Site.new with a directory that is not the current working directory is not supported. It is recommended to change the directory before calling Nanoc::Int::Site.new. For example, instead of Nanoc::Int::Site.new(\'abc\'), use Dir.chdir(\'abc\') { Nanoc::Int::Site.new(\'.\') }.' end # Read config from nanoc.yaml/config.yaml in given dir config_path = Dir.chdir(dir_or_config_hash) do filename = self.class.config_filename_for_cwd if filename.nil? - raise Nanoc::Errors::GenericTrivial, 'Could not find nanoc.yaml or config.yaml in the current working directory' + raise Nanoc::Int::Errors::GenericTrivial, 'Could not find nanoc.yaml or config.yaml in the current working directory' end File.absolute_path(filename, dir_or_config_hash) end @config = apply_parent_config(load_config(config_path), [config_path]) else # Use passed config hash - @config = apply_parent_config(dir_or_config_hash.symbolize_keys_recursively) + @config = apply_parent_config(dir_or_config_hash.__nanoc_symbolize_keys_recursively) end # Merge config with default config @config = DEFAULT_CONFIG.merge(@config) # Merge data sources with default data source config @config[:data_sources] = @config[:data_sources].map { |ds| DEFAULT_DATA_SOURCE_CONFIG.merge(ds) } # Convert to proper configuration - @config = Nanoc::Configuration.new(@config) + @config = Nanoc::Int::Configuration.new(@config) end end end