Module: Hanami::Assets::Helpers
- Includes:
- Helpers::HtmlHelper
- Defined in:
- lib/hanami/assets/helpers.rb
Overview
HTML assets helpers
Include this helper in a view
rubocop:disable Metrics/ModuleLength rubocop:disable Naming/UncommunicativeMethodParamName
Constant Summary collapse
- NEW_LINE_SEPARATOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"\n".freeze
- WILDCARD_EXT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'.*'.freeze
- JAVASCRIPT_EXT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'.js'.freeze
- STYLESHEET_EXT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'.css'.freeze
- JAVASCRIPT_MIME_TYPE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'text/javascript'.freeze
- STYLESHEET_MIME_TYPE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'text/css'.freeze
- FAVICON_MIME_TYPE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'image/x-icon'.freeze
- STYLESHEET_REL =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'stylesheet'.freeze
- FAVICON_REL =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'shortcut icon'.freeze
- DEFAULT_FAVICON =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'favicon.ico'.freeze
- CROSSORIGIN_ANONYMOUS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'anonymous'.freeze
- ABSOLUTE_URL_MATCHER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
URI::DEFAULT_PARSER.make_regexp
- QUERY_STRING_MATCHER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
/\?/.freeze
Class Method Summary collapse
-
.included(base) ⇒ Object
private
Inject helpers into the given class.
Instance Method Summary collapse
-
#asset_path(source, push: false, as: nil) ⇒ String
It generates the relative URL for the given source.
-
#asset_url(source, push: false, as: nil) ⇒ String
It generates the absolute URL for the given source.
-
#audio(source = nil, options = {}, &blk) ⇒ Hanami::Utils::Helpers::HtmlBuilder
Generate
audio
tag for given source. -
#favicon(source = DEFAULT_FAVICON, options = {}) ⇒ Hanami::Utils::Helpers::HtmlBuilder
Generate
link
tag application favicon. -
#image(source, options = {}) ⇒ Hanami::Utils::Helpers::HtmlBuilder
Generate
img
tag for given source. -
#javascript(*sources, push: true, **options) ⇒ Hanami::Utils::Escape::SafeString
Generate
script
tag for given source(s). -
#stylesheet(*sources, push: true, **options) ⇒ Hanami::Utils::Escape::SafeString
Generate
link
tag for given source(s). -
#video(source = nil, options = {}, &blk) ⇒ Hanami::Utils::Helpers::HtmlBuilder
Generate
video
tag for given source.
Class Method Details
.included(base) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Inject helpers into the given class
76 77 78 79 80 81 82 83 84 |
# File 'lib/hanami/assets/helpers.rb', line 76 def self.included(base) conf = ::Hanami::Assets::Configuration.for(base) base.class_eval do include Utils::ClassAttribute class_attribute :assets_configuration self.assets_configuration = conf end end |
Instance Method Details
#asset_path(source, push: false, as: nil) ⇒ String
It generates the relative URL for the given source.
It can be the name of the asset, coming from the sources or third party gems.
Absolute URLs are returned as they are.
If Fingerprint mode is on, it returns the fingerprinted path of the source
If CDN mode is on, it returns the absolute URL of the asset.
‘subresource_integrity` modes are on and the asset is missing from the manifest
772 773 774 |
# File 'lib/hanami/assets/helpers.rb', line 772 def asset_path(source, push: false, as: nil) _asset_url(source, push: push, as: as) { _relative_url(source) } end |
#asset_url(source, push: false, as: nil) ⇒ String
It generates the absolute URL for the given source.
It can be the name of the asset, coming from the sources or third party gems.
Absolute URLs are returned as they are.
If Fingerprint mode is on, it returns the fingerprint URL of the source
If CDN mode is on, it returns the absolute URL of the asset.
‘subresource_integrity` modes are on and the asset is missing from the manifest
826 827 828 |
# File 'lib/hanami/assets/helpers.rb', line 826 def asset_url(source, push: false, as: nil) _asset_url(source, push: push, as: as) { _absolute_url(source) } end |
#audio(source = nil, options = {}, &blk) ⇒ Hanami::Utils::Helpers::HtmlBuilder
Generate audio
tag for given source
It accepts one string representing the name of the asset, if it comes from the application or third party gems. It also accepts string representing absolute URLs in case of public CDN (eg. Bootstrap CDN).
Alternatively, it accepts a block that allows to specify one or more sources via the source
tag.
If the “fingerprint mode” is on, src
is the fingerprinted version of the relative URL.
If the “CDN mode” is on, the src
is an absolute URL of the application CDN.
‘subresource_integrity` modes are on and the audio file is missing from the manifest
<%=
audio do
text "Your browser does not support the audio tag"
source src: asset_path("song.ogg", push: :audio), type: "audio/ogg"
source src: asset_path("song.wav"), type: "audio/wav"
end
%>
717 718 719 720 |
# File 'lib/hanami/assets/helpers.rb', line 717 def audio(source = nil, = {}, &blk) = (source, , as: :audio, &blk) html.audio(blk, ) end |
#favicon(source = DEFAULT_FAVICON, options = {}) ⇒ Hanami::Utils::Helpers::HtmlBuilder
Generate link
tag application favicon.
If no argument is given, it assumes favico.ico
from the application.
It accepts one string representing the name of the asset.
If the “fingerprint mode” is on, href
is the fingerprinted version of the relative URL.
If the “CDN mode” is on, the href
is an absolute URL of the application CDN.
‘subresource_integrity` modes are on and the favicon is file missing from the manifest
443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'lib/hanami/assets/helpers.rb', line 443 def favicon(source = DEFAULT_FAVICON, = {}) = .reject { |k, _| k.to_sym == :href } attributes = { href: asset_path(source, push: .delete(:push) || false, as: :image), rel: FAVICON_REL, type: FAVICON_MIME_TYPE } attributes.merge!() html.link(attributes) end |
#image(source, options = {}) ⇒ Hanami::Utils::Helpers::HtmlBuilder
Generate img
tag for given source
It accepts one string representing the name of the asset, if it comes from the application or third party gems. It also accepts string representing absolute URLs in case of public CDN (eg. Bootstrap CDN).
alt
Attribute is auto generated from src
. You can specify a different value, by passing the :src
option.
If the “fingerprint mode” is on, src
is the fingerprinted version of the relative URL.
If the “CDN mode” is on, the src
is an absolute URL of the application CDN.
‘subresource_integrity` modes are on and the image file is missing from the manifest
371 372 373 374 375 376 377 378 379 380 |
# File 'lib/hanami/assets/helpers.rb', line 371 def image(source, = {}) = .reject { |k, _| k.to_sym == :src } attributes = { src: asset_path(source, push: .delete(:push) || false, as: :image), alt: Utils::String.titleize(::File.basename(source, WILDCARD_EXT)) } attributes.merge!() html.img(attributes) end |
#javascript(*sources, push: true, **options) ⇒ Hanami::Utils::Escape::SafeString
Generate script
tag for given source(s)
It accepts one or more strings representing the name of the asset, if it comes from the application or third party gems. It also accepts strings representing absolute URLs in case of public CDN (eg. jQuery CDN).
If the “fingerprint mode” is on, src
is the fingerprinted version of the relative URL.
If the “CDN mode” is on, the src
is an absolute URL of the application CDN.
If the “subresource integrity mode” is on, integriy
is the name of the algorithm, then a hyphen, then the hash value of the file. If more than one algorithm is used, they’ll be separated by a space.
It makes the script(s) eligible for HTTP/2 Push Promise/Early Hints. You can opt-out with inline option: ‘push: false`.
‘subresource_integrity` modes are on and the javascript file is missing from the manifest
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/hanami/assets/helpers.rb', line 179 def javascript(*sources, push: true, **) # rubocop:disable Metrics/MethodLength = .reject { |k, _| k.to_sym == :src } (*sources) do |source| attributes = { src: _typed_asset_path(source, JAVASCRIPT_EXT, push: push, as: :script), type: JAVASCRIPT_MIME_TYPE } attributes.merge!() if _subresource_integrity? || attributes.include?(:integrity) attributes[:integrity] ||= _subresource_integrity_value(source, JAVASCRIPT_EXT) attributes[:crossorigin] ||= CROSSORIGIN_ANONYMOUS end html.script(**attributes).to_s end end |
#stylesheet(*sources, push: true, **options) ⇒ Hanami::Utils::Escape::SafeString
Generate link
tag for given source(s)
It accepts one or more strings representing the name of the asset, if it comes from the application or third party gems. It also accepts strings representing absolute URLs in case of public CDN (eg. Bootstrap CDN).
If the “fingerprint mode” is on, href
is the fingerprinted version of the relative URL.
If the “CDN mode” is on, the href
is an absolute URL of the application CDN.
If the “subresource integrity mode” is on, integriy
is the name of the algorithm, then a hyphen, then the hashed value of the file. If more than one algorithm is used, they’ll be separated by a space.
It makes the script(s) eligible for HTTP/2 Push Promise/Early Hints. You can opt-out with inline option: ‘push: false`.
‘subresource_integrity` modes are on and the stylesheet file is missing from the manifest
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/hanami/assets/helpers.rb', line 280 def stylesheet(*sources, push: true, **) # rubocop:disable Metrics/MethodLength = .reject { |k, _| k.to_sym == :href } (*sources) do |source| attributes = { href: _typed_asset_path(source, STYLESHEET_EXT, push: push, as: :style), type: STYLESHEET_MIME_TYPE, rel: STYLESHEET_REL } attributes.merge!() if _subresource_integrity? || attributes.include?(:integrity) attributes[:integrity] ||= _subresource_integrity_value(source, STYLESHEET_EXT) attributes[:crossorigin] ||= CROSSORIGIN_ANONYMOUS end html.link(**attributes).to_s end end |
#video(source = nil, options = {}, &blk) ⇒ Hanami::Utils::Helpers::HtmlBuilder
Generate video
tag for given source
It accepts one string representing the name of the asset, if it comes from the application or third party gems. It also accepts string representing absolute URLs in case of public CDN (eg. Bootstrap CDN).
Alternatively, it accepts a block that allows to specify one or more sources via the source
tag.
If the “fingerprint mode” is on, src
is the fingerprinted version of the relative URL.
If the “CDN mode” is on, the src
is an absolute URL of the application CDN.
‘subresource_integrity` modes are on and the video file is missing from the manifest
<%=
video do
text "Your browser does not support the video tag"
source src: asset_path("movie.mp4", push: :video), type: "video/mp4"
source src: asset_path("movie.ogg"), type: "video/ogg"
end
%>
584 585 586 587 |
# File 'lib/hanami/assets/helpers.rb', line 584 def video(source = nil, = {}, &blk) = (source, , as: :video, &blk) html.video(blk, ) end |