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
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"
- 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.
".*"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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"
- 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
77 78 79 80 81 82 83 84 85 |
# File 'lib/hanami/assets/helpers.rb', line 77 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
781 782 783 |
# File 'lib/hanami/assets/helpers.rb', line 781 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
835 836 837 |
# File 'lib/hanami/assets/helpers.rb', line 835 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
%>
726 727 728 729 |
# File 'lib/hanami/assets/helpers.rb', line 726 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
452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'lib/hanami/assets/helpers.rb', line 452 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
379 380 381 382 383 384 385 386 387 388 |
# File 'lib/hanami/assets/helpers.rb', line 379 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
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/hanami/assets/helpers.rb', line 183 def javascript(*sources, push: true, **) = .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
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/hanami/assets/helpers.rb', line 288 def stylesheet(*sources, push: true, **) = .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
%>
593 594 595 596 |
# File 'lib/hanami/assets/helpers.rb', line 593 def video(source = nil, = {}, &blk) = (source, , as: :video, &blk) html.video(blk, ) end |