Class: Bridgetown::RubyTemplateView::Helpers
- Inherits:
-
Object
- Object
- Bridgetown::RubyTemplateView::Helpers
- Includes:
- Filters, Filters::FromLiquid
- 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 (also: #webpack_path)
-
#attributes_from_options(options) ⇒ String
Create a set of attributes from a hash.
-
#class_map(pairs = {}) ⇒ String
Space-separated keys where the values are truthy.
- #dsd(input = nil, &block) ⇒ Object
- #dsd_style ⇒ Object
- #find_relative_url_for_path(relative_path) ⇒ Object
-
#initialize(view, site) ⇒ Helpers
constructor
A new instance of Helpers.
-
#l(*args, **kwargs) ⇒ String
Forward all arguments to I18n.l method.
-
#link_to(text, relative_path = nil, options = {}) ⇒ String
This helper will generate the correct permalink URL for the file path.
- #live_reload_dev_js ⇒ Object
-
#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.
-
#t(*args, **kwargs) ⇒ String
Forward all arguments to I18n.t method.
-
#url_for(relative_path) ⇒ String
(also: #link)
This helper will generate the correct permalink URL for the file path.
Methods included from Filters::FromLiquid
#newline_to_br, #strip_html, #strip_newlines
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
Instance Attribute Details
#site ⇒ Bridgetown::Site (readonly)
13 14 15 |
# File 'lib/bridgetown-core/helpers.rb', line 13 def site @site end |
#view ⇒ Bridgetown::RubyTemplateView (readonly)
10 11 12 |
# File 'lib/bridgetown-core/helpers.rb', line 10 def view @view end |
Instance Method Details
#asset_path(asset_type) ⇒ Object Also known as: webpack_path
27 28 29 |
# File 'lib/bridgetown-core/helpers.rb', line 27 def asset_path(asset_type) Bridgetown::Utils.parse_frontend_manifest_file(site, asset_type.to_s) end |
#attributes_from_options(options) ⇒ String
Create a set of attributes from a hash.
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/bridgetown-core/helpers.rb', line 116 def () segments = [] .each do |attr, option| attr = dashed(attr) if option.is_a?(Hash) option = option.transform_keys { |key| "#{attr}-#{dashed(key)}" } segments << (option) else segments << attribute_segment(attr, option) end end safe(segments.join(" ")) end |
#class_map(pairs = {}) ⇒ String
Returns Space-separated keys where the values are truthy.
39 40 41 |
# File 'lib/bridgetown-core/helpers.rb', line 39 def class_map(pairs = {}) pairs.select { |_key, truthy| truthy }.keys.join(" ") end |
#dsd(input = nil, &block) ⇒ Object
232 233 234 235 236 |
# File 'lib/bridgetown-core/helpers.rb', line 232 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
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/bridgetown-core/helpers.rb', line 238 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
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/bridgetown-core/helpers.rb', line 77 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 |
#l(*args, **kwargs) ⇒ String
Forward all arguments to I18n.l method
142 143 144 |
# File 'lib/bridgetown-core/helpers.rb', line 142 def l(*args, **kwargs) I18n.send :l, *args, **kwargs end |
#link_to(text, relative_path = nil, options = {}) ⇒ String
This helper will generate the correct permalink URL for the file path.
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/bridgetown-core/helpers.rb', line 100 def link_to(text, relative_path = nil, = {}) if block_given? relative_path = text text = yield elsif relative_path.nil? raise ArgumentError, "You must provide a relative path" end segments = ({ href: url_for(relative_path) }.merge()) safe("<a #{segments}>#{text}</a>") end |
#live_reload_dev_js ⇒ Object
32 33 34 |
# File 'lib/bridgetown-core/helpers.rb', line 32 def live_reload_dev_js Bridgetown::Utils.live_reload_js(site) end |
#markdownify(input = nil, &block) ⇒ String
Convert a Markdown string into HTML output.
47 48 49 50 51 52 53 |
# File 'lib/bridgetown-core/helpers.rb', line 47 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
151 152 153 |
# File 'lib/bridgetown-core/helpers.rb', line 151 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
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/bridgetown-core/helpers.rb', line 163 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: name, content: content, context: resource, transform: transform ) nil end |
#slotted(name, default_input = nil, &default_block) ⇒ String
Render out a content slot
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/bridgetown-core/helpers.rb', line 195 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
217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/bridgetown-core/helpers.rb', line 217 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 |
#t(*args, **kwargs) ⇒ String
Forward all arguments to I18n.t method
134 135 136 |
# File 'lib/bridgetown-core/helpers.rb', line 134 def t(*args, **kwargs) I18n.send :t, *args, **kwargs end |
#url_for(relative_path) ⇒ String Also known as: link
This helper will generate the correct permalink URL for the file path.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/bridgetown-core/helpers.rb', line 61 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 |