Class: Lavender::Static

Inherits:
Object
  • Object
show all
Defined in:
lib/lavender/static.rb

Instance Method Summary collapse

Constructor Details

#initialize(verbose = false) ⇒ Static

Returns a new instance of Static.



5
6
7
8
# File 'lib/lavender/static.rb', line 5

def initialize verbose = false
  @config = Lavender::Config.new
  @verbose = verbose
end

Instance Method Details

#defaultsObject



39
40
41
42
43
44
45
46
# File 'lib/lavender/static.rb', line 39

def defaults
  @config.defaults.to_hash.reduce({}) do |a,b|
    c = b.last
    c = c.to_sym if c.is_a? String
    a[b.first.to_sym] = c
    a
  end
end

#path(path) ⇒ Object



35
36
37
# File 'lib/lavender/static.rb', line 35

def path path
  File.join(@config.pwd, @config.paths.send(path))
end

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lavender/static.rb', line 10

def run
  # Public
  Dir["#{path(:public)}/**/*"].each do |file|
    out = file.sub(/^#{path(:public)}/, path(:compiled))
    write_file out, File.read(file)
  end

  # Layouts
  layouts = {}
  Dir["#{path(:layouts)}/**/*"].each do |file|
    name, ext = file.sub(/^#{path(:layouts)}\//,'').match(/^(.+)\.([^\.]+)$/)[1..-1]
    layouts[name.to_sym] = {ext.to_sym => File.read(file)}
  end

  # Pages
  Dir["#{path(:pages)}/**/*.yml"].each do |file|
    target = file.sub(/^#{path(:pages)}/, path(:compiled))
    target.sub!(/\.yml$/, '')

    conv = Lavender::Converter.new(:page => File.read(file), :layouts => layouts, :defaults => defaults)

    write_file target, conv.render
  end
end

#write_file(path, data) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/lavender/static.rb', line 48

def write_file path, data
  FileUtils.mkdir_p File.dirname(path)
  File.open(path, 'w+') do |f|
    f.write data
  end
  if @verbose
    basepath = path.sub(/^#{@config.pwd}\//,'')
    puts "  create: #{basepath}"
  end
end