Class: Middleman::FileRenderer
- Inherits:
-
Object
- Object
- Middleman::FileRenderer
- Extended by:
- Forwardable
- Includes:
- Contracts
- Defined in:
- lib/middleman-core/file_renderer.rb
Constant Summary
Constants included from Contracts
Class Method Summary collapse
Instance Method Summary collapse
-
#Hash ⇒ String
Render an on-disk file.
-
#initialize(app, path) ⇒ FileRenderer
constructor
A new instance of FileRenderer.
- #render(locs, opts, context, &block) ⇒ Object
-
#String ⇒ String
Get the template data from a path.
- #template_data_for_file ⇒ Object
Methods included from Contracts
Constructor Details
#initialize(app, path) ⇒ FileRenderer
Returns a new instance of FileRenderer.
20 21 22 23 |
# File 'lib/middleman-core/file_renderer.rb', line 20 def initialize(app, path) @app = app @path = path.to_s end |
Class Method Details
.cache ⇒ Object
14 15 16 |
# File 'lib/middleman-core/file_renderer.rb', line 14 def self.cache @_cache ||= ::Tilt::Cache.new end |
Instance Method Details
#Hash ⇒ String
Render an on-disk file. Used for everything, including layouts.
31 |
# File 'lib/middleman-core/file_renderer.rb', line 31 Contract Hash, Hash, Any, Maybe[Proc] => String |
#render(locs, opts, context, &block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/middleman-core/file_renderer.rb', line 32 def render(locs, opts, context, &block) path = @path.dup # Detect the remdering engine from the extension extension = File.extname(path) engine = extension[1..-1].to_sym # Store last engine for later (could be inside nested renders) context.current_engine, engine_was = engine, context.current_engine # Save current buffer for later buf_was = context.save_buffer # Read from disk or cache the contents of the file body = if opts[:template_body] opts.delete(:template_body) else template_data_for_file end # Merge per-extension options from config extension = File.extname(path) = {}.merge!(opts).merge!((extension)) [:outvar] ||= '@_out_buf' [:context] = context .delete(:layout) # Overwrite with frontmatter options = .deep_merge([:renderer_options]) if [:renderer_options] template_class = ::Middleman::Util.tilt_class(path) # Allow hooks to manipulate the template before render body = @app.callbacks_for(:before_render).reduce(body) do |sum, callback| callback.call(sum, path, locs, template_class) || sum end # Read compiled template from disk or cache template = ::Tilt.new(path, 1, ) { body } # template = cache.fetch(:compiled_template, extension, options, body) do # ::Tilt.new(path, 1, options) { body } # end # Render using Tilt # content = ::Middleman::Util.instrument 'render.tilt', path: path do # template.render(context, locs, &block) # end content = template.render(context, locs, &block) # Allow hooks to manipulate the result after render content = @app.callbacks_for(:after_render).reduce(content) do |sum, callback| callback.call(sum, path, locs, template_class) || sum end output = ::ActiveSupport::SafeBuffer.new '' output.safe_concat content output ensure # Reset stored buffer context.restore_buffer(buf_was) context.current_engine = engine_was end |
#String ⇒ String
Get the template data from a path
98 |
# File 'lib/middleman-core/file_renderer.rb', line 98 Contract String |
#template_data_for_file ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/middleman-core/file_renderer.rb', line 99 def template_data_for_file file = @app.files.find(:source, @path) if @app.extensions[:front_matter] && (file && !file[:types].include?(:no_frontmatter)) result = @app.extensions[:front_matter].template_data_for_file(@path) return result unless result.nil? end file ? file.read : ::File.read(@path) end |