Class: Chisel::View

Inherits:
Object
  • Object
show all
Defined in:
lib/chisel/view.rb

Constant Summary collapse

MarkupExtensions =
['erb', 'md', 'textile']
@@cache =
[]
@@output_paths =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ View

Returns a new instance of View.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chisel/view.rb', line 22

def initialize(options={})

  # Do not call the initializer directly; use View.fetch(options)
  # to take advantage of view caching

  @path = Pathname.new(options[:path]) if options[:path]
  @site_dir = SiteDirectory.new(options[:site_dir]) if options[:site_dir]
  
  if options[:resource]
    @type = :resource
    @resource = options[:resource]
    @view = options[:view]
    @path = @site_dir.resource_type_view_path(@resource, @view)
  elsif options[:layout]
    @type = :layout
    @view = options[:layout]
    @path = @site_dir.layout_view_path(@view)
  elsif options[:view]
    @view = options[:view]
    @path = @site_dir.view_path(@view)
  end

  raise 'Cannot initialize view without a path' unless @path

  @raw_content = File.read(@path)
  @@cache << self

end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



10
11
12
# File 'lib/chisel/view.rb', line 10

def path
  @path
end

#raw_contentObject

Returns the value of attribute raw_content.



10
11
12
# File 'lib/chisel/view.rb', line 10

def raw_content
  @raw_content
end

#resourceObject

Returns the value of attribute resource.



10
11
12
# File 'lib/chisel/view.rb', line 10

def resource
  @resource
end

#site_dirObject

Returns the value of attribute site_dir.



10
11
12
# File 'lib/chisel/view.rb', line 10

def site_dir
  @site_dir
end

#typeObject

Returns the value of attribute type.



10
11
12
# File 'lib/chisel/view.rb', line 10

def type
  @type
end

#viewObject

Returns the value of attribute view.



10
11
12
# File 'lib/chisel/view.rb', line 10

def view
  @view
end

Class Method Details

.fetch(options = {}) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/chisel/view.rb', line 14

def self.fetch(options={})
  @@cache.each do |view|
    return view if view.matches(options)
  end

  return View.new(options)
end

Instance Method Details

#evaluate(output_path, options = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/chisel/view.rb', line 85

def evaluate(output_path, options = {})

  if @type == :layout
    puts "Evaluating layout for #{output_path}..."
  else
    puts "Evaluating #{output_path}..."
  end
  
  helper = ViewHelper.new(@site_dir, output_path)
  
  # First evaluate header as ERB if necessary
  
  header_vars = {}
  if options[:resource]
    resource_key = options[:resource].resource_type.to_sym
    header_vars[resource_key] = options[:resource]
    header_vars[:resource] = options[:resource]
  end
  
  header_binding = header_vars.to_binding(helper)
  header_string = ERB.new(YAML.header_string(@raw_content)).result(header_binding)
  if header_string == ''
    header = {}
  else
    header = YAML.load(header_string).symbolize_keys
  end
  
  # Maintin the binding if we were passed one; otherwise,
  # create a brand new binding from our header info

  if options[:binding]
    content_binding = options[:binding]
  
    # Overwrite layout that was passed in
    if header[:layout]
      content_binding.eval("layout = '#{header[:layout]}'")
    else
      content_binding.eval("layout = nil")
    end
  
  else
  
    if options[:locals]
      vars = header.merge(options[:locals])
    else
      vars = header.clone
    end
    vars[:content] = nil
    vars[:layout] = nil unless vars.key?(:layout)
  
    if options[:resource]
      resource_key = options[:resource].resource_type.to_sym
      vars[resource_key] = options[:resource]
      vars[:resource] = options[:resource] # alias as "resource"
    end
  
    content_binding = vars.to_binding(helper)
  
  end

  # Evaluate the content with appropriate markup and ERB
  
  content = YAML.remove_header(@raw_content)
  
  markup_extensions.reverse.each do |extension|
    case extension.downcase
    when 'erb'
      content = ERB.new(content).result(content_binding)
    when 'textile'
      begin
        require 'RedCloth'
      rescue LoadError
        puts "
The RedCloth gem is required to evaluate Textile markup. Please run:

  gem install RedCloth"
        exit
      end
      content = RedCloth.new(content).to_html
    when 'md'
      begin
        require 'maruku'
      rescue LoadError
        puts "
The Maruku gem is required to evaluate Markdown markup. Please run:

  gem install maruku"
        exit
      end
      content = Maruku.new(content).to_html
    end
  end

  # Now evaluate the layout

  if content_binding.eval('layout')
    content_binding.eval("content = <<EOS\n#{content}\nEOS")
    layout_view = View.fetch(:layout => content_binding.eval('layout'), :site_dir => @site_dir)
    content = layout_view.evaluate(output_path, :binding => content_binding)
  end

  content
end

#matches(options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/chisel/view.rb', line 51

def matches(options = {})

  return true if options.key?(:resource) and @type == :resource and @view == options[:view] and @resource == options[:resource]
  return true if options.key?(:layout) and @type == :layout and @view == options[:layout]
  return true if @path == options[:path]

  return false

end

#run(options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/chisel/view.rb', line 61

def run(options = {})
  if options[:output_path] then
    output_path = options[:output_path]
  else
    view_relative_dir = @path.relative_path_from(@site_dir).dirname
    file_output_dir = @site_dir.output_dir.join(view_relative_dir)
    file_output_dir.mkpath
    output_path = file_output_dir.join(output_basename)
  end

  return if @@output_paths.index(output_path)
  @@output_paths << output_path

  FileUtils.mkdir_p(output_path.dirname)

  result = evaluate(output_path, options)

  f = File.new(output_path, 'w')
  f.write(result)
  f.close

  result
end