Class: Henshin::Site

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(override = {}) ⇒ self

A new instance of site

Parameters:

  • override (Hash{String => Object}) (defaults to: {})

    data to override loaded options



25
26
27
28
29
30
# File 'lib/henshin/site.rb', line 25

def initialize(override={})
  self.reset
  self.configure(override)
  self.load_plugins
  self
end

Instance Attribute Details

#archiveObject

Returns the value of attribute archive.



18
19
20
# File 'lib/henshin/site.rb', line 18

def archive
  @archive
end

#baseString

Returns a path which should be prepended to all urls.

Returns:

  • (String)

    a path which should be prepended to all urls



15
16
17
# File 'lib/henshin/site.rb', line 15

def base
  @base
end

#categoriesObject

Returns the value of attribute categories.



18
19
20
# File 'lib/henshin/site.rb', line 18

def categories
  @categories
end

#configHash

Returns the configuration made up of the override, options.yaml and Henshin::Defaults.

Returns:

  • (Hash)

    the configuration made up of the override, options.yaml and Henshin::Defaults



6
7
8
# File 'lib/henshin/site.rb', line 6

def config
  @config
end

#gensObject

Returns the value of attribute gens.



17
18
19
# File 'lib/henshin/site.rb', line 17

def gens
  @gens
end

#layoutsObject

Returns the value of attribute layouts.



17
18
19
# File 'lib/henshin/site.rb', line 17

def layouts
  @layouts
end

#pluginsObject

Returns the value of attribute plugins.



19
20
21
# File 'lib/henshin/site.rb', line 19

def plugins
  @plugins
end

#postsObject

Returns the value of attribute posts.



17
18
19
# File 'lib/henshin/site.rb', line 17

def posts
  @posts
end

#rootPathname

Returns the path to the site to generate from where the command is executed.

Returns:

  • (Pathname)

    the path to the site to generate from where the command is executed



9
10
11
# File 'lib/henshin/site.rb', line 9

def root
  @root
end

#staticsObject

Returns the value of attribute statics.



17
18
19
# File 'lib/henshin/site.rb', line 17

def statics
  @statics
end

#tagsObject

Returns the value of attribute tags.



18
19
20
# File 'lib/henshin/site.rb', line 18

def tags
  @tags
end

#targetPathname

Returns the path to where the finished site should be written.

Returns:

  • (Pathname)

    the path to where the finished site should be written



12
13
14
# File 'lib/henshin/site.rb', line 12

def target
  @target
end

Instance Method Details

#buildObject

Read, process, render and write



104
105
106
107
108
109
110
# File 'lib/henshin/site.rb', line 104

def build
  self.reset
  self.read
  self.process
  self.render
  self.write
end

#configure(override) ⇒ Object

Creates the configuration hash by merging defaults, supplied options and options read from the ‘options.yaml’ file. Then sets root, target, base and appends special directories to @config

Parameters:

  • override (Hash)

    to override other set options



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/henshin/site.rb', line 53

def configure(override)  
  @config = {}
  config_file = File.join((override['root'] || Defaults['root']), '/options.yaml')
  
  # change target to represent given root, only if root given
  if override['root'] && !override['target']
    override['target'] = File.join(override['root'], Defaults['target'])
  end
  
  begin
    config = YAML.load_file(config_file)
    @config = Defaults.merge(config).merge(override)
  rescue => e
    $stderr.puts "\nCould not read configuration, falling back to defaults..."
    $stderr.puts "-> #{e.to_s}"
    @config = Defaults.merge(override)
  end
  @root = @config['root'].to_p
  @target = @config['target'].to_p
  
  @base = @config['base'] || "/"
  @base = '/' + @base unless @base[0] == '/' # need to make sure it starts with slash
  
  @config['exclude'] << '/_site' << '/plugins'
end

#gen?(path) ⇒ Bool

Returns whether the path points to a gen.

Parameters:

Returns:

  • (Bool)

    whether the path points to a gen



230
231
232
233
234
235
# File 'lib/henshin/site.rb', line 230

def gen?( path )
  return false if post?(path) || layout?(path) || ignored?(path)
  return true if @plugins[:generators].has_key? path.to_p.extname[1..-1]
  return true if File.open(path, "r").read(3) == "---"
  false
end

#ignored?(path) ⇒ Bool

Returns whether the path points to a file which should be ignored.

Parameters:

Returns:

  • (Bool)

    whether the path points to a file which should be ignored



239
240
241
242
243
244
245
246
# File 'lib/henshin/site.rb', line 239

def ignored?( path )
  ignored = ['/options.yaml'] + @config['exclude']
  ignored.collect! {|i| File.join(@root, i)}
  ignored.each do |i|
    return true if path.include? i
  end
  false
end

#layout?(path) ⇒ Bool

Returns whether the path points to a layout.

Parameters:

Returns:

  • (Bool)

    whether the path points to a layout



218
219
220
# File 'lib/henshin/site.rb', line 218

def layout?( path )
  path.include?('layouts/') && !ignored?(path)
end

#load_pluginsObject

Requires each plugin in @config, then loads and sorts them into plugins by type



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/henshin/site.rb', line 81

def load_plugins
  @plugins = {:generators => {}, :layoutors => []}

  @config['plugins'].each do |plugin|
    begin
      require File.join('henshin', 'plugins', plugin)
    rescue LoadError
      require File.join(@root, 'plugins', plugin)
    end
  end
  
  Henshin::Generator.subclasses.each do |plugin|
    plugin = plugin.new(self)
    plugin.extensions[:input].each do |ext|
      @plugins[:generators][ext] = plugin
    end
  end
  
  @plugins[:layoutors] = Henshin::Layoutor.subclasses.map {|l| l.new(self)}.sort
end

#payloadHash

Returns the payload for the layout engine.

Returns:

  • (Hash)

    the payload for the layout engine



176
177
178
179
180
181
182
183
184
# File 'lib/henshin/site.rb', line 176

def payload
  r = {'site' => @config}
  r['site']['created_at'] = Time.now
  r['site']['posts'] = @posts.collect{|i| i.to_hash}
  r['site']['tags'] = @tags.to_hash
  r['site']['categories'] = @categories.to_hash
  r['site']['archive'] = @archive.to_hash
  r
end

#post?(path) ⇒ Bool

Returns whether the path points to a post.

Parameters:

Returns:

  • (Bool)

    whether the path points to a post



224
225
226
# File 'lib/henshin/site.rb', line 224

def post?( path )
  path.include?('posts/') && !ignored?(path)
end

#processObject

Processes all of the necessary files



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/henshin/site.rb', line 163

def process
  @posts.sort!
  @gens.sort!
  
  @posts.each do |post|
    @tags << post if post.data['tags']
    @categories << post if post.data['category']
    @archive << post
  end
  self
end

#readself

Reads all necessary files and puts them into the necessary arrays

Returns:

  • (self)


117
118
119
120
121
122
123
# File 'lib/henshin/site.rb', line 117

def read
  self.read_layouts
  self.read_posts
  self.read_gens
  self.read_statics
  self
end

#read_gensObject

Adds all files that need to be run through a plugin in an array



143
144
145
146
147
148
149
# File 'lib/henshin/site.rb', line 143

def read_gens
  files = Dir.glob( File.join(@root, '**', '*.*') )
  gens = files.select {|i| gen?(i) }
  gens.each do |gen|
    @gens << Gen.new(gen.to_p, self).read
  end
end

#read_layoutsObject

Adds all items in ‘/layouts’ to the layouts array



126
127
128
129
130
131
132
# File 'lib/henshin/site.rb', line 126

def read_layouts
  path = File.join(@root, 'layouts')
  Dir.glob(path + '/*.*').each do |layout|
    layout =~ /([a-zA-Z0-9 _-]+)\.([a-zA-Z0-9-]+)/
    @layouts[$1] = File.open(layout, 'r') {|f| f.read}
  end
end

#read_postsObject

Adds all items in ‘/posts’ to the posts array



135
136
137
138
139
140
# File 'lib/henshin/site.rb', line 135

def read_posts
  path = File.join(@root, 'posts')
  Dir.glob(path + '/**/*.*').each do |post|
    @posts << Post.new(post.to_p, self).read
  end
end

#read_staticsObject

Adds all static files to an array



152
153
154
155
156
157
158
# File 'lib/henshin/site.rb', line 152

def read_statics
  files = Dir.glob( File.join(@root, '**', '*.*') )
  static = files.select {|i| static?(i) }
  static.each do |static|
    @statics << Static.new(static.to_p, self)
  end
end

#renderObject

Renders the files



188
189
190
191
192
193
# File 'lib/henshin/site.rb', line 188

def render
  @posts.each {|post| post.render}
  @gens.each {|gen| gen.render}
  
  self
end

#resetself

Resets all instance variables, except config and plugins as these shouldn’t need to be reloaded each time.

Returns:

  • (self)


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/henshin/site.rb', line 36

def reset     
  @gens    = []
  @posts   = []
  @statics = []
  @layouts = {}
  
  @archive    = Archive.new(self)
  @tags       = Labels.new('tag', self)
  @categories = Labels.new('category', self)
  self
end

#static?(path) ⇒ Bool

Returns whether the path points to a static.

Parameters:

Returns:

  • (Bool)

    whether the path points to a static



212
213
214
# File 'lib/henshin/site.rb', line 212

def static?( path )
  !( layout?(path) || post?(path) || gen?(path) || ignored?(path) )
end

#writeObject

Writes the files



198
199
200
201
202
203
204
205
206
207
# File 'lib/henshin/site.rb', line 198

def write
  @posts.each {|post| post.write}
  @gens.each {|gen| gen.write}
  @statics.each {|static| static.write}
  
  @archive.write
  @tags.write
  @categories.write
  self
end