Class: Middleman::FileRenderer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Contracts
Defined in:
middleman-core/lib/middleman-core/file_renderer.rb

Constant Summary

Constants included from Contracts

Contracts::ImmutableSetOf, Contracts::ImmutableSortedSetOf, Contracts::OldResourceList, Contracts::PATH_MATCHER, Contracts::ResourceList, Contracts::VectorOf

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Contracts

#Contract

Constructor Details

#initialize(app, path) ⇒ FileRenderer

Returns a new instance of FileRenderer



23
24
25
26
27
# File 'middleman-core/lib/middleman-core/file_renderer.rb', line 23

def initialize(app, path)
  @app = app
  @path = path.to_s
  @vertices = ::Hamster::Set.empty
end

Instance Attribute Details

#verticesObject (readonly)

Returns the value of attribute vertices



19
20
21
# File 'middleman-core/lib/middleman-core/file_renderer.rb', line 19

def vertices
  @vertices
end

Class Method Details

.cacheObject



14
15
16
# File 'middleman-core/lib/middleman-core/file_renderer.rb', line 14

def self.cache
  @_cache ||= ::Tilt::Cache.new
end

Instance Method Details

#HashString

Render an on-disk file. Used for everything, including layouts.

Parameters:

  • locs (Hash)
  • opts (Hash)
  • context (Class)

Returns:



35
# File 'middleman-core/lib/middleman-core/file_renderer.rb', line 35

Contract Hash, Hash, Any, Maybe[Proc] => String

#render(locs, opts, context, &block) ⇒ Object



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
# File 'middleman-core/lib/middleman-core/file_renderer.rb', line 36

def render(locs, opts, context, &block)
  path = @path.dup

  # Detect the rendering engine from the extension
  extension = File.extname(path)
  engine = extension[1..-1].to_sym

  # Store last engine for later (could be inside nested renders)
  engine_was = context.current_engine
  context.current_engine = 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)
  options = {}.merge!(opts).merge!(options_for_ext(extension))
  options[:outvar] ||= '@_out_buf'
  options[:context] = context
  options.delete(:layout)

  # Overwrite with frontmatter options
  options = options.deep_merge(options[:renderer_options]) if options[:renderer_options]

  # Read compiled template from disk or cache
  template = ::Tilt.new(path, 1, options) { body }
  # template = cache.fetch(:compiled_template, extension, options, body) do
  #   ::Tilt.new(path, 1, options) { body }
  # end

  @vertices = ::Hamster::Set.empty

  # Render using Tilt
  content = ::Middleman::Util.instrument 'render.tilt', path: path do
    template.render(context, locs, &block).tap do
      @vertices = template.vertices if template.respond_to?(:vertices)
    end
  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

#StringString

Get the template data from a path

Parameters:

Returns:



94
# File 'middleman-core/lib/middleman-core/file_renderer.rb', line 94

Contract String

#template_data_for_fileObject



95
96
97
98
99
100
101
102
103
104
# File 'middleman-core/lib/middleman-core/file_renderer.rb', line 95

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 : ::Middleman::Util.read_file(@path)
end