Class: Bridgetown::RubyTemplateView::Helpers
- Inherits:
-
Object
- Object
- Bridgetown::RubyTemplateView::Helpers
- Includes:
- Filters, Filters::FromLiquid, Inclusive, Streamlined::Helpers
- Defined in:
- lib/bridgetown-core/helpers.rb
Defined Under Namespace
Classes: Context
Instance Attribute Summary collapse
- #site ⇒ Bridgetown::Site readonly
- #view ⇒ Bridgetown::RubyTemplateView readonly
Instance Method Summary collapse
- #asset_path(asset_type) ⇒ Object
-
#bypass_tracking ⇒ Object
TODO: docu.
-
#class_map(pairs = {}) ⇒ String
Space-separated keys where the values are truthy.
-
#dsd(input = nil, &block) ⇒ Object
TODO: docu.
-
#dsd_style ⇒ Object
TODO: docu.
- #find_relative_url_for_path(relative_path) ⇒ Object
-
#initialize(view = nil, site = nil) ⇒ Helpers
constructor
A new instance of Helpers.
-
#link_to(text, relative_path = nil, options = {}, &block) ⇒ String
This helper will generate the correct permalink URL for the file path.
- #live_reload_dev_js ⇒ Object
-
#localize ⇒ String
(also: #l)
Delegates to I18n.localize with no additional functionality.
-
#markdownify(input = nil, &block) ⇒ String
Convert a Markdown string into HTML output.
-
#safe(input) ⇒ String
(also: #raw)
For template contexts where ActiveSupport's output safety is loaded, we can ensure a string has been marked safe.
-
#slot(name, input = nil, replace: false, transform: true, &block) ⇒ void
Define a new content slot.
-
#slotted(name, default_input = nil, &default_block) ⇒ String
Render out a content slot.
-
#slotted?(name) ⇒ Boolean
Check if a content slot has been defined.
-
#translate(key, **options) ⇒ String
(also: #t)
Delegates to I18n#translate but also performs two additional functions.
- #translate_package ⇒ Bridgetown::Foundation::SafeTranslations
- #translate_with_html(key, **options) ⇒ Object
-
#url_for(relative_path) ⇒ String
(also: #link)
This helper will generate the correct permalink URL for the file path.
- #webpack_path ⇒ Object
Methods included from Filters::FromLiquid
#newline_to_br, #strip_html, #strip_newlines, #truncate, #truncate_words
Methods included from Filters
#array_to_sentence_string, #cgi_escape, #inspect, #jsonify, #normalize_whitespace, #number_of_words, #obfuscate_link, #pop, #push, #reading_time, #sample, #shift, #slugify, #smartify, #sort, #titleize, #to_integer, #unshift, #uri_escape, #where, #where_exp, #xml_escape
Methods included from Filters::ConditionHelpers
#parse_binary_comparison, #parse_comparison, #parse_condition
Methods included from Filters::DateFilters
#date_to_long_string, #date_to_rfc822, #date_to_string, #date_to_xmlschema
Methods included from Filters::GroupingFilters
Methods included from Filters::URLFilters
#absolute_url, #in_locale, #relative_url, #strip_extname, #strip_index
Constructor Details
#initialize(view = nil, site = nil) ⇒ Helpers
Returns a new instance of Helpers.
27 28 29 30 31 32 33 |
# File 'lib/bridgetown-core/helpers.rb', line 27 def initialize(view = nil, site = nil) @view = view @site = site || Bridgetown::Current.site # duck typing for Liquid context @context = Context.new({ site: }) end |
Instance Attribute Details
#site ⇒ Bridgetown::Site (readonly)
18 19 20 |
# File 'lib/bridgetown-core/helpers.rb', line 18 def site @site end |
#view ⇒ Bridgetown::RubyTemplateView (readonly)
15 16 17 |
# File 'lib/bridgetown-core/helpers.rb', line 15 def view @view end |
Instance Method Details
#asset_path(asset_type) ⇒ Object
35 36 37 |
# File 'lib/bridgetown-core/helpers.rb', line 35 def asset_path(asset_type) Bridgetown::Utils.parse_frontend_manifest_file(site, asset_type.to_s) end |
#bypass_tracking ⇒ Object
TODO: docu
297 |
# File 'lib/bridgetown-core/helpers.rb', line 297 def bypass_tracking(...) = Signalize.untracked(...) |
#class_map(pairs = {}) ⇒ String
Returns Space-separated keys where the values are truthy.
54 55 56 |
# File 'lib/bridgetown-core/helpers.rb', line 54 def class_map(pairs = {}) pairs.select { |_key, truthy| truthy }.keys.join(" ") end |
#dsd(input = nil, &block) ⇒ Object
TODO: docu
269 270 271 272 273 |
# File 'lib/bridgetown-core/helpers.rb', line 269 def dsd(input = nil, &block) tmpl_content = block.nil? ? input.to_s : view.capture(&block) Bridgetown::Utils.dsd_tag(tmpl_content) end |
#dsd_style ⇒ Object
TODO: docu
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/bridgetown-core/helpers.rb', line 276 def dsd_style tmpl_path = caller_locations(1, 2).find do |loc| loc.label.include?("method_missing").! end&.path return unless tmpl_path # virtually guaranteed not to happen tmpl_basename = File.basename(tmpl_path, ".*") style_path = File.join(File.dirname(tmpl_path), "#{tmpl_basename}.dsd.css") unless File.file?(style_path) raise Bridgetown::Errors::FatalException, "Missing stylesheet at #{style_path}" end style_tag = site.tmp_cache["dsd_style:#{style_path}"] ||= "<style>#{File.read(style_path)}</style>" style_tag.html_safe end |
#find_relative_url_for_path(relative_path) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/bridgetown-core/helpers.rb', line 92 def find_relative_url_for_path(relative_path) site.each_site_file do |item| if item.relative_path.to_s == relative_path || item.relative_path.to_s == "/#{relative_path}" return safe(item.respond_to?(:relative_url) ? item.relative_url : relative_url(item)) end end raise ArgumentError, <<~MSG Could not find document '#{relative_path}' in 'url_for' helper. Make sure the document exists and the path is correct. MSG end |
#link_to(text, relative_path = nil, options = {}, &block) ⇒ String
This helper will generate the correct permalink URL for the file path.
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/bridgetown-core/helpers.rb', line 115 def link_to(text, relative_path = nil, = {}, &block) if block.present? = relative_path || {} relative_path = text text = view.respond_to?(:capture) ? view.capture(&block) : yield elsif relative_path.nil? raise ArgumentError, "You must provide a relative path" end segments = html_attributes({ href: url_for(relative_path) }.merge()) safe("<a #{segments}>#{text}</a>") end |
#live_reload_dev_js ⇒ Object
47 48 49 |
# File 'lib/bridgetown-core/helpers.rb', line 47 def live_reload_dev_js Bridgetown::Utils.live_reload_js(site) end |
#localize ⇒ String Also known as: l
Delegates to I18n.localize with no additional functionality.
177 178 179 |
# File 'lib/bridgetown-core/helpers.rb', line 177 def localize(...) I18n.localize(...) end |
#markdownify(input = nil, &block) ⇒ String
Convert a Markdown string into HTML output.
62 63 64 65 66 67 68 |
# File 'lib/bridgetown-core/helpers.rb', line 62 def markdownify(input = nil, &block) content = Bridgetown::Utils.reindent_for_markdown( block.nil? ? input.to_s : view.capture(&block) ) converter = site.find_converter_instance(Bridgetown::Converters::Markdown) safe(converter.convert(content).strip) end |
#safe(input) ⇒ String Also known as: raw
For template contexts where ActiveSupport's output safety is loaded, we can ensure a string has been marked safe
187 188 189 |
# File 'lib/bridgetown-core/helpers.rb', line 187 def safe(input) input.to_s.html_safe end |
#slot(name, input = nil, replace: false, transform: true, &block) ⇒ void
This method returns an undefined value.
Define a new content slot
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/bridgetown-core/helpers.rb', line 199 def slot(name, input = nil, replace: false, transform: true, &block) content = Bridgetown::Utils.reindent_for_markdown( block.nil? ? input.to_s : view.capture(&block) ) resource = if view.respond_to?(:resource) # We're in a resource rendering context view.resource elsif view.respond_to?(:view_context) # We're in a component rendering context, although it's # likely the component's own `slot` method will be called # in this context view.view_context.resource end name = name.to_s resource.slots.reject! { _1.name == name } if replace resource.slots << Slot.new( name:, content:, context: resource, transform: ) nil end |
#slotted(name, default_input = nil, &default_block) ⇒ String
Render out a content slot
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/bridgetown-core/helpers.rb', line 231 def slotted(name, default_input = nil, &default_block) # rubocop:todo Metrics resource = if view.respond_to?(:resource) view.resource elsif view.respond_to?(:view_context) view.view_context.resource end return unless resource name = name.to_s filtered_slots = resource.slots.select do |slot| slot.name == name end return filtered_slots.map(&:content).join.html_safe if filtered_slots.length.positive? default_block.nil? ? default_input.to_s : view.capture(&default_block) end |
#slotted?(name) ⇒ Boolean
Check if a content slot has been defined
253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/bridgetown-core/helpers.rb', line 253 def slotted?(name) resource = if view.respond_to?(:resource) view.resource elsif view.respond_to?(:view_context) view.view_context.resource end return false unless resource name = name.to_s resource.slots.any? do |slot| slot.name == name end end |
#translate(key, **options) ⇒ String Also known as: t
Delegates to I18n#translate but also performs two additional functions.
First, if the key starts with a period translate will scope the key by the current view. Calling translate(".foo") from the people/index.html.erb template is equivalent to calling translate("people.index.foo"). This makes it less repetitive to translate many keys within the same view and provides a convention to scope keys consistently.
Second, the translation will be marked as html_safe if the key has the suffix "_html" or the last element of the key is "html". Calling translate("footer_html") or translate("footer.html") will return an HTML safe string that won't be escaped by other HTML helper methods. This naming convention helps to identify translations that include HTML tags so that you know what kind of output to expect when you call translate in a template and translators know which keys they can provide HTML values for.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/bridgetown-core/helpers.rb', line 152 def translate(key, **) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity return key.map { |k| translate(k, **) } if key.is_a?(Array) key = key&.to_s if key&.start_with?(".") view_path = view&.page&.relative_path&.to_s&.split(".")&.first key = "#{view_path.tr("/", ".")}#{key}" if view_path.present? end return I18n.translate(key, **) unless %r{(?:_|\b)html\z}.match?(key) translate_with_html(key, **) end |
#translate_package ⇒ Bridgetown::Foundation::SafeTranslations
23 |
# File 'lib/bridgetown-core/helpers.rb', line 23 packages def translate_package = [Bridgetown::Foundation::Packages::SafeTranslations] |
#translate_with_html(key, **options) ⇒ Object
168 169 170 171 |
# File 'lib/bridgetown-core/helpers.rb', line 168 def translate_with_html(key, **) escaper = ->(input) { input.to_s.encode(xml: :attr).gsub(%r{\A"|"\Z}, "") } translate_package.translate(key, escaper, **) end |
#url_for(relative_path) ⇒ String Also known as: link
This helper will generate the correct permalink URL for the file path.
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/bridgetown-core/helpers.rb', line 76 def url_for(relative_path) if relative_path.respond_to?(:relative_url) return safe(relative_path.relative_url) # new resource engine elsif relative_path.respond_to?(:url) return safe(relative_url(relative_path.url)) # old legacy engine elsif relative_path.to_s.start_with?("/", "http", "#", "mailto:", "tel:") return safe(relative_path) end find_relative_url_for_path(relative_path) end |
#webpack_path ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/bridgetown-core/helpers.rb', line 39 def webpack_path(*) source_file = caller_locations.find { _1.path.start_with?(site.source) }.path raise( Bridgetown::Errors::FatalException, "🚨 Oops, you'll need to change `webpack_path' to `asset_path' in:\n#{source_file}\n" ) end |