Class: Hanami::PapercraftView

Inherits:
View
  • Object
show all
Extended by:
Helpers::AssetsHelper
Includes:
Helpers::AssetsHelper
Defined in:
lib/hanami/papercraft_view.rb

Constant Summary collapse

CONTENT_SECURITY_POLICY_NONCE_REQUEST_KEY =
"hanami.content_security_policy_nonce"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._absolute_url?(source) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/hanami/papercraft_view.rb', line 31

def _absolute_url?(source)
  ABSOLUTE_URL_MATCHER.match?(source.respond_to?(:url) ? source.url : source)
end

._append_extension?(source, ext) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/hanami/papercraft_view.rb', line 20

def _append_extension?(source, ext)
  source !~ QUERY_STRING_MATCHER && source !~ /#{Regexp.escape(ext)}\z/
end

._nonce(context, source, nonce_option) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/hanami/papercraft_view.rb', line 35

def _nonce(context, source, nonce_option)
  if nonce_option == false
    nil
  elsif nonce_option == true || (nonce_option.nil? && !_absolute_url?(source))
    content_security_policy_nonce(context)
  else
    nonce_option
  end
end

._subresource_integrity_value(context, source, ext) ⇒ Object



53
54
55
56
57
58
# File 'lib/hanami/papercraft_view.rb', line 53

def _subresource_integrity_value(context, source, ext)
  return if _absolute_url?(source)

  source = "#{source}#{ext}" unless /#{Regexp.escape(ext)}\z/.match?(source)
  context.assets[source].sri
end

._typed_url(context, source, ext) ⇒ Object



15
16
17
18
# File 'lib/hanami/papercraft_view.rb', line 15

def _typed_url(context, source, ext)
  source = "#{source}#{ext}" if source.is_a?(String) && _append_extension?(source, ext)
  asset_url(context, source)
end

.asset_url(context, source) ⇒ Object



24
25
26
27
28
29
# File 'lib/hanami/papercraft_view.rb', line 24

def asset_url(context, source)
  return source.url if source.respond_to?(:url)
  return source if _absolute_url?(source)

  context.assets[source].url
end

.content_security_policy_nonce(context) ⇒ Object



47
48
49
50
51
# File 'lib/hanami/papercraft_view.rb', line 47

def content_security_policy_nonce(context)
  return unless context.request

  context.request.env[CONTENT_SECURITY_POLICY_NONCE_REQUEST_KEY]
end

Instance Method Details

#call(context:, layout: nil, **props) ⇒ Object



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
# File 'lib/hanami/papercraft_view.rb', line 108

def call(context:, layout: nil, **props)
  layout ||= config.layout

  locals = exposures.(context:, **props) do |value, exposure|
    # TODO: what is decorate? and how do we support this without a rendering object
    # if exposure.decorate? && value
    #   rendering.part(exposure.name, value, as: exposure.options[:as])
    # else
      value
    # end
  end
  
  template_params = {
    context: context,
    config: config,
    **props,
    **locals
  }

  puts '*' * 40
  p params: template_params.keys
  puts

  template = load_template(config.template)
  puts template.compiled_code
  puts

  if layout
    layout_template = load_layout_template(layout)
    layout_template.render(**template_params, &template)
  else
    template.render(**template_params)
  end
end

#load_layout_template(name) ⇒ Object



143
144
145
146
147
148
# File 'lib/hanami/papercraft_view.rb', line 143

def load_layout_template(name)
  root ||= config.paths.first.root
  fn = File.join(root, "layouts/#{name}.papercraft.rb")
  source = IO.read(fn)
  eval(source, binding, fn)
end

#load_template(name) ⇒ Object



150
151
152
153
154
155
# File 'lib/hanami/papercraft_view.rb', line 150

def load_template(name)
  root ||= config.paths.first.root
  fn = File.join(root, "#{name}.papercraft.rb")
  source = IO.read(fn)
  eval(source, binding, fn)
end