Class: Homeostasis::Front

Inherits:
Stasis::Plugin
  • Object
show all
Includes:
Helpers
Defined in:
lib/homeostasis.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stasis) ⇒ Front

Returns a new instance of Front.



225
226
227
228
229
# File 'lib/homeostasis.rb', line 225

def initialize(stasis)
  @stasis = stasis
  @@front_site = FrontHash.new
  @@matcher = /\.erb|\.haml|\.html|\.md$/
end

Class Method Details

._front_key(stasis, filename) ⇒ Object

for other plugins



282
283
284
# File 'lib/homeostasis.rb', line 282

def self._front_key(stasis, filename) # for other plugins
  filename.sub(stasis.root, '')[1..-1]
end

._front_siteObject

for other plugins



278
279
280
# File 'lib/homeostasis.rb', line 278

def self._front_site # for other plugins
  @@front_site
end

.config(options) ⇒ Object



286
287
288
# File 'lib/homeostasis.rb', line 286

def self.config(options)
  @@matcher = options[:matcher] if options[:matcher]
end

.preamble_load(path) ⇒ Object



290
291
292
293
294
295
296
# File 'lib/homeostasis.rb', line 290

def self.preamble_load(path)
  return nil if path.nil? || path !~ @@matcher
  data = Preamble.load(path)
  [data., data.content]
rescue
  [{}, Helpers.read(path)]
end

Instance Method Details

#after_renderObject



261
262
263
264
265
266
267
268
# File 'lib/homeostasis.rb', line 261

def after_render
  if @tmpfile
    @stasis.path = Helpers.stasis_path if @stasis.path !~ /^#{@stasis.root}/
    Helpers.set_stasis_path(@stasis, nil)
    @tmpfile.unlink
    @tmpfile = nil
  end
end

#before_allObject



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/homeostasis.rb', line 231

def before_all
  @stasis.paths.each do |path|
    yaml, body = Front.preamble_load(path)
    next if yaml.nil?

    # add special :path key for generated files
    if !ignore?(path)
      relative = path[(@stasis.root.length+1)..-1]
      ext = Tilt.mappings.keys.find { |ext| File.extname(path)[1..-1] == ext }
      dest = (ext && File.extname(relative) == ".#{ext}") ?
        relative[0..-1*ext.length-2] :
        relative
      yaml[:path] = trailify(Multi.drop_tilt_exts(dest))
    end
    @@front_site[front_key(path)] = FrontHash.from_hash(yaml)
  end
end

#before_renderObject



249
250
251
252
253
254
255
256
257
258
259
# File 'lib/homeostasis.rb', line 249

def before_render
  if @stasis.path && @stasis.path =~ @@matcher && !ignore?(@stasis.path)
    ext = File.basename(@stasis.path).split('.', 2).last
    yaml, body = Front.preamble_load(@stasis.path)
    @tmpfile = Tempfile.new(['temp', ".#{ext}"])
    @tmpfile.puts(body)
    @tmpfile.close
    Helpers.set_stasis_path(@stasis, @stasis.path)
    @stasis.path = @tmpfile.path
  end
end

#frontObject



270
271
272
# File 'lib/homeostasis.rb', line 270

def front
  @@front_site[front_key(Helpers.stasis_path || @stasis.path)] || FrontHash.new
end

#front_siteObject



274
275
276
# File 'lib/homeostasis.rb', line 274

def front_site
  @@front_site
end