Class: Nanoc2::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc2/base/site.rb

Overview

A Nanoc2::Site is the in-memory representation of a nanoc site. It holds references to the following site data:

  • pages is a list of Nanoc2::Page instances representing pages

  • assets is a list of Nanoc2::Asset instances representing assets

  • page_defaults is a Nanoc2::PageDefaults instance representing page defaults

  • asset_defaults is a Nanoc2::AssetDefaults instance representing asset defaults

  • layouts is a list of Nanoc2::Layout instances representing layouts

  • templates is a list of Nanoc2::Template representing templates

  • code is a Nanoc2::Code instance representing custom site code

In addition, each site has a config hash which stores the site configuration. This configuration hash can have the following keys:

output_dir

The directory to which compiled pages and assets will be written. This path is relative to the current working directory, but can also be an absolute path.

data_source

The identifier of the data source that will be used for loading site data.

router

The identifier of the router that will be used for determining page and asset representation paths.

index_filenames

A list of filenames that will be stripped off full page and asset paths to create cleaner URLs (for example, ‘/about/’ will be used instead of ‘/about/index.html’). The default value should be okay in most cases.

A site also has several helper classes:

  • router is a Nanoc2::Router subclass instance used for determining page and asset paths.

  • data_source is a Nanoc2::DataSource subclass instance used for managing site data.

  • compiler is a Nanoc2::Compiler instance that compiles page and asset representations.

The physical representation of a Nanoc2::Site is usually a directory that contains a configuration file, site data, and some rake tasks. However, different frontends may store data differently. For example, a web-based frontend would probably store the configuration and the site content in a database, and would not have rake tasks at all.

Constant Summary collapse

DEFAULT_CONFIG =

The default configuration for a site. A site’s configuration overrides these options: when a Nanoc2::Site is created with a configuration that lacks some options, the default value will be taken from DEFAULT_CONFIG.

{
  :output_dir       => 'output',
  :data_source      => 'filesystem',
  :router           => 'default',
  :index_filenames  => [ 'index.html' ]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Site

Returns a Nanoc2::Site object for the site specified by the given configuration hash config.

config

A hash containing the site configuration.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/nanoc2/base/site.rb', line 71

def initialize(config)
  # Load configuration
  @config = DEFAULT_CONFIG.merge(config.clean)

  # Create data source
  @data_source_class = Nanoc2::DataSource.named(@config[:data_source])
  raise Nanoc2::Errors::UnknownDataSourceError.new(@config[:data_source]) if @data_source_class.nil?
  @data_source = @data_source_class.new(self)

  # Create compiler
  @compiler = Compiler.new(self)

  # Load code (necessary for custom routers)
  load_code

  # Create router
  @router_class = Nanoc2::Router.named(@config[:router])
  raise Nanoc2::Errors::UnknownRouterError.new(@config[:router]) if @router_class.nil?
  @router = @router_class.new(self)

  # Initialize data
  @page_defaults        = PageDefaults.new({})
  @page_defaults.site   = self
  @asset_defaults       = AssetDefaults.new({})
  @asset_defaults.site  = self
  @pages                = []
  @assets               = []
  @layouts              = []
  @templates            = []
end

Instance Attribute Details

#asset_defaultsObject (readonly)

Returns the value of attribute asset_defaults.



64
65
66
# File 'lib/nanoc2/base/site.rb', line 64

def asset_defaults
  @asset_defaults
end

#assetsObject (readonly)

Returns the value of attribute assets.



65
66
67
# File 'lib/nanoc2/base/site.rb', line 65

def assets
  @assets
end

#codeObject (readonly)

Returns the value of attribute code.



65
66
67
# File 'lib/nanoc2/base/site.rb', line 65

def code
  @code
end

#compilerObject (readonly)

Returns the value of attribute compiler.



63
64
65
# File 'lib/nanoc2/base/site.rb', line 63

def compiler
  @compiler
end

#configObject (readonly)

Returns the value of attribute config.



62
63
64
# File 'lib/nanoc2/base/site.rb', line 62

def config
  @config
end

#data_sourceObject (readonly)

Returns the value of attribute data_source.



63
64
65
# File 'lib/nanoc2/base/site.rb', line 63

def data_source
  @data_source
end

#layoutsObject (readonly)

Returns the value of attribute layouts.



65
66
67
# File 'lib/nanoc2/base/site.rb', line 65

def layouts
  @layouts
end

#page_defaultsObject (readonly)

Returns the value of attribute page_defaults.



64
65
66
# File 'lib/nanoc2/base/site.rb', line 64

def page_defaults
  @page_defaults
end

#pagesObject (readonly)

Returns the value of attribute pages.



65
66
67
# File 'lib/nanoc2/base/site.rb', line 65

def pages
  @pages
end

#routerObject (readonly)

Returns the value of attribute router.



63
64
65
# File 'lib/nanoc2/base/site.rb', line 63

def router
  @router
end

#templatesObject (readonly)

Returns the value of attribute templates.



65
66
67
# File 'lib/nanoc2/base/site.rb', line 65

def templates
  @templates
end

Instance Method Details

#load_data(force = false) ⇒ Object

Loads the site data. This will query the Nanoc2::DataSource associated with the site and fetch all site data. The site data is cached, so calling this method will not have any effect the second time, unless force is true.

force

If true, will force load the site data even if it has been loaded before, to circumvent caching issues.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/nanoc2/base/site.rb', line 109

def load_data(force=false)
  # Don't load data twice
  @data_loaded ||= false
  return if @data_loaded and !force

  # Load all data
  @data_source.loading do
    load_code(force)
    load_page_defaults
    load_pages
    load_asset_defaults
    load_assets
    load_layouts
    load_templates
  end

  # Set loaded
  @data_loaded = true
end