class Bridgetown::RubyTemplateView::Helpers

Constants

Context

Attributes

site[R]

@return [Bridgetown::Site]

view[R]

@return [Bridgetown::RubyTemplateView]

Public Class Methods

new(view, site) click to toggle source
# File lib/bridgetown-core/helpers.rb, line 17
def initialize(view, site)
  @view = view
  @site = site

  # duck typing for Liquid context
  @context = Context.new({ site: site })
end

Public Instance Methods

class_map(pairs = {}) click to toggle source

@param pairs [Hash] A hash of key/value pairs.

@return [String] Space-separated keys where the values are truthy.

# File lib/bridgetown-core/helpers.rb, line 32
def class_map(pairs = {})
  pairs.select { |_key, truthy| truthy }.keys.join(" ")
end
find_relative_url_for_path(relative_path) click to toggle source

@param relative_path [String] source file path, e.g.

"_posts/2020-10-20-my-post.md"

@raise [ArgumentError] if the file cannot be found

# File lib/bridgetown-core/helpers.rb, line 70
      def find_relative_url_for_path(relative_path)
        site.each_site_file do |item|
          if item.relative_path == relative_path || item.relative_path == "/#{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
markdownify(input = nil, &block) click to toggle source

Convert a Markdown string into HTML output.

@param input [String] the Markdown to convert, if no block is passed @return [String]

# File lib/bridgetown-core/helpers.rb, line 40
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
raw(input)
Alias for: safe
safe(input) click to toggle source

For template contexts where ActiveSupport's output safety is loaded, we can ensure a string has been marked safe

@param input [Object] @return [String]

# File lib/bridgetown-core/helpers.rb, line 117
def safe(input)
  input.to_s.html_safe
end
Also aliased as: raw
t(*args) click to toggle source

Forward all arguments to I18n.t method

@return [String] the translated string @see I18n

# File lib/bridgetown-core/helpers.rb, line 108
def t(*args)
  I18n.send :t, *args
end
url_for(relative_path) click to toggle source

This helper will generate the correct permalink URL for the file path.

@param relative_path [String, Object] source file path, e.g.

"_posts/2020-10-20-my-post.md", or object that responds to either
`url` or `relative_url`

@return [String] the permalink URL for the file

# File lib/bridgetown-core/helpers.rb, line 54
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.start_with?("/", "http")
    return safe(relative_path)
  end

  find_relative_url_for_path(relative_path)
end
Also aliased as: link
webpack_path(asset_type) click to toggle source
# File lib/bridgetown-core/helpers.rb, line 25
def webpack_path(asset_type)
  Bridgetown::Utils.parse_webpack_manifest_file(site, asset_type.to_s)
end