Class: Stic::Site

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

Overview

The Site is the main class representing the hole site project.

It contains the configuration, source and target paths and provides all functionality to invoke Generators, manage and access Blobs and write the rendered site project.

Examples:

site = ::Stic::Site.lookup
site.run
site.write

Attributes collapse

Construction collapse

Accessors collapse

Actions collapse

Constructor Details

#initialize(source, config) ⇒ Site

Initializes new Stic::Site object.

Parameters:

  • source (String, Path)

    The source directory where the rendered site should be written to.

  • config (Config)

    The configuration that should be used. Usually loaded from configuration file located in the source directory.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/stic/site.rb', line 53

def initialize(source, config)
  @config  = config
  @source  = Path.new source
  @target  = Path.new(source).join('site').expand
  @blobs   = []
  @layouts = []

  @generators = self.class.generators.map do |generator_class|
    generator_class.new self, config['generators']
  end
end

Instance Attribute Details

#configConfig (readonly)

The project’s configuration object. Will be initialized with the values loaded from configuration file found in project’s source directory.

Returns:

  • (Config)

    The project configuration.



35
36
37
# File 'lib/stic/site.rb', line 35

def config
  @config
end

#generatorsArray<Generator> (readonly)

The list of initialized Generators. They will be initialized within the #initialize method.

Returns:



42
43
44
# File 'lib/stic/site.rb', line 42

def generators
  @generators
end

#sourcePath (readonly)

The project’s source path.

Returns:

  • (Path)

    The source path.



22
23
24
# File 'lib/stic/site.rb', line 22

def source
  @source
end

#targetPath (readonly)

The project’s target path.

Returns:

  • (Path)

    The target path.



28
29
30
# File 'lib/stic/site.rb', line 28

def target
  @target
end

Class Method Details

.generatorsObject

Return list of available generator classes.



169
170
171
# File 'lib/stic/site.rb', line 169

def generators
  @generators ||= []
end

.lookup(dir = Path.getwd) ⇒ Object

Try to find a stic site in given directory or above. If no directory is given the current working directory will be used.



163
164
165
166
# File 'lib/stic/site.rb', line 163

def lookup(dir = Path.getwd)
  file = Path.new(dir).lookup(/^stic.ya?ml$/)
  file ? new(file.dirname, Stic::Config.load(file)) : nil
end

Instance Method Details

#<<(blob) ⇒ Object

Add new Blob to site.

You should not add Blobs with same ‘target_url` or `relative_target_path` as they later would override the previous file.

Parameters:



154
155
156
# File 'lib/stic/site.rb', line 154

def <<(blob)
  blobs << blob
end

#blobs(opts = {}) ⇒ Object

Return list of Blobs.

You can pass a class as ‘:type` option that will be used to filter the returned Blobs by Inheritance. Only Blobs of given class or Blobs that inherit given class will be returned.

Parameters:

  • opts (Hash) (defaults to: {})

    Option hash.

Options Hash (opts):

  • :type (Class)

    Class used as a filter.



82
83
84
85
# File 'lib/stic/site.rb', line 82

def blobs(opts = {})
  return @blobs if opts[:type].nil?
  @blobs.select{|blob| blob.class <= opts[:type] }
end

#cleanupSelf

Cleanup target directory. That will remove all files and directories not referenced by any Blob.

Returns:

  • (Self)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/stic/site.rb', line 132

def cleanup
  paths = blobs.map(&:target_path)

  # Cleanup not longer referenced files
  target.glob('**/*').each do |path|
    if path.file? && !paths.include?(path)
      path.unlink
    elsif path.directory? && !paths.any?{|p| p.path.starts_with? path.to_s }
      path.rm_rf
    end
  end

  self
end

#layout(name) ⇒ Object

Get layout with given name.



69
70
71
# File 'lib/stic/site.rb', line 69

def layout(name)
  @layouts.fetch(name)
end

#run {|generator| ... } ⇒ Self

Run all registered and initialized Generators.

A block can be passed that will be called before each Generator is run.

Yields:

  • (generator)

    Called before given Generator will be run.

Yield Parameters:

Returns:

  • (Self)


98
99
100
101
102
103
104
105
106
107
# File 'lib/stic/site.rb', line 98

def run(&block)
  @layouts = Layout.load(self, @source, @config)

  generators.each do |generator|
    block.call generator if block
    generator.run
  end

  self
end

#write {|blob| ... } ⇒ Self

Write all added Blobs to their destination.

A block can be passed that will be called before each Blob is written.

Yields:

  • (blob)

    Called before given Blob will be written.

Yield Parameters:

  • blob (Blob)

    Blob that will be written next.

Returns:

  • (Self)


118
119
120
121
122
123
124
125
# File 'lib/stic/site.rb', line 118

def write(&block)
  blobs.each do |blob|
    block.call blob if block
    blob.write
  end

  self
end