Class: Chisel::View
- Inherits:
-
Object
- Object
- Chisel::View
- Defined in:
- lib/chisel/view.rb
Constant Summary collapse
- MarkupExtensions =
['erb', 'md', 'textile']
- @@cache =
[]
- @@output_paths =
[]
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
-
#raw_content ⇒ Object
Returns the value of attribute raw_content.
-
#resource ⇒ Object
Returns the value of attribute resource.
-
#site_dir ⇒ Object
Returns the value of attribute site_dir.
-
#type ⇒ Object
Returns the value of attribute type.
-
#view ⇒ Object
Returns the value of attribute view.
Class Method Summary collapse
Instance Method Summary collapse
- #evaluate(output_path, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ View
constructor
A new instance of View.
- #matches(options = {}) ⇒ Object
- #run(options = {}) ⇒ Object
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(={}) # Do not call the initializer directly; use View.fetch(options) # to take advantage of view caching @path = Pathname.new([:path]) if [:path] @site_dir = SiteDirectory.new([:site_dir]) if [:site_dir] if [:resource] @type = :resource @resource = [:resource] @view = [:view] @path = @site_dir.resource_type_view_path(@resource, @view) elsif [:layout] @type = :layout @view = [:layout] @path = @site_dir.layout_view_path(@view) elsif [:view] @view = [: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
#path ⇒ Object
Returns the value of attribute path.
10 11 12 |
# File 'lib/chisel/view.rb', line 10 def path @path end |
#raw_content ⇒ Object
Returns the value of attribute raw_content.
10 11 12 |
# File 'lib/chisel/view.rb', line 10 def raw_content @raw_content end |
#resource ⇒ Object
Returns the value of attribute resource.
10 11 12 |
# File 'lib/chisel/view.rb', line 10 def resource @resource end |
#site_dir ⇒ Object
Returns the value of attribute site_dir.
10 11 12 |
# File 'lib/chisel/view.rb', line 10 def site_dir @site_dir end |
#type ⇒ Object
Returns the value of attribute type.
10 11 12 |
# File 'lib/chisel/view.rb', line 10 def type @type end |
#view ⇒ Object
Returns the value of attribute view.
10 11 12 |
# File 'lib/chisel/view.rb', line 10 def view @view end |
Class Method Details
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, = {}) 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 [:resource] resource_key = [:resource].resource_type.to_sym header_vars[resource_key] = [:resource] header_vars[:resource] = [: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 [:binding] content_binding = [: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 [:locals] vars = header.merge([:locals]) else vars = header.clone end vars[:content] = nil vars[:layout] = nil unless vars.key?(:layout) if [:resource] resource_key = [:resource].resource_type.to_sym vars[resource_key] = [:resource] vars[:resource] = [: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( = {}) return true if .key?(:resource) and @type == :resource and @view == [:view] and @resource == [:resource] return true if .key?(:layout) and @type == :layout and @view == [:layout] return true if @path == [: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( = {}) if [:output_path] then output_path = [: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, ) f = File.new(output_path, 'w') f.write(result) f.close result end |