class Middleman::CoreExtensions::DefaultHelpers

Public Class Methods

new(app, options_hash={}, &block) click to toggle source
Calls superclass method Middleman::Extension::new
# File lib/middleman-core/core_extensions/default_helpers.rb, line 23
def initialize(app, options_hash={}, &block)
  super

  require 'active_support/core_ext/object/to_query'

  ::Middleman::TemplateContext.send :include, ::Padrino::Helpers::OutputHelpers
  ::Middleman::TemplateContext.send :include, ::Padrino::Helpers::TagHelpers
  ::Middleman::TemplateContext.send :include, ::Padrino::Helpers::AssetTagHelpers
  ::Middleman::TemplateContext.send :include, ::Padrino::Helpers::FormHelpers
  ::Middleman::TemplateContext.send :include, ::Padrino::Helpers::FormatHelpers
  ::Middleman::TemplateContext.send :include, ::Padrino::Helpers::RenderHelpers
  ::Middleman::TemplateContext.send :include, ::Padrino::Helpers::NumberHelpers
  # ::Middleman::TemplateContext.send :include, ::Padrino::Helpers::TranslationHelpers
end

Public Instance Methods

asset_path(kind, source, options={}) click to toggle source

Get the path of a file of a given type

@param [Symbol] kind The type of file @param [String] source The path to the file @param [Hash] options Data to pass through. @return [String]

# File lib/middleman-core/core_extensions/default_helpers.rb, line 194
def asset_path(kind, source, options={})
  options_with_resource = {}.merge!(options).merge!(current_resource: current_resource)
  ::Middleman::Util.asset_path(app, kind, source, options_with_resource)
end
asset_stamp() click to toggle source

Disable Padrino cache buster

# File lib/middleman-core/core_extensions/default_helpers.rb, line 86
def asset_stamp
  false
end
asset_url(path, prefix='', options={}) click to toggle source

Get the URL of an asset given a type/prefix

@param [String] path The path (such as “photo.jpg”) @param [String] prefix The type prefix (such as “images”) @param [Hash] options Additional options. @return [String] The fully qualified asset url

# File lib/middleman-core/core_extensions/default_helpers.rb, line 205
def asset_url(path, prefix='', options={})
  options_with_resource = {}.merge!(options).merge!(current_resource: current_resource)
  ::Middleman::Util.asset_url(app, path, prefix, options_with_resource)
end
auto_find_proper_handler(&block) click to toggle source
# File lib/middleman-core/core_extensions/default_helpers.rb, line 76
def auto_find_proper_handler(&block)
  if block_given?
    engine = File.extname(block.source_location[0])[1..-1].to_sym
    ::Padrino::Helpers::OutputHelpers.handlers.select { |e, _| e == engine }.values.map { |h| h.new(self) }.find { |h| h.engine_matches?(block) }
  else
    find_proper_handler
  end
end
auto_javascript_include_tag() click to toggle source

Output a javascript tag based on the current path

@return [String]

# File lib/middleman-core/core_extensions/default_helpers.rb, line 102
def auto_javascript_include_tag
  auto_tag(:js) do |path|
    javascript_include_tag path
  end
end
auto_tag(asset_ext, asset_dir=nil) { |path| ... } click to toggle source

Output a stylesheet link tag based on the current path

@param [Symbol] asset_ext The type of asset @param [String] asset_dir Where to look for assets @return [void]

# File lib/middleman-core/core_extensions/default_helpers.rb, line 143
def auto_tag(asset_ext, asset_dir=nil)
  if asset_dir.nil?
    asset_dir = case asset_ext
    when :js
      config[:js_dir]
    when :css
      config[:css_dir]
    end
  end

  # If the basename of the request as no extension, assume we are serving a
  # directory and join index_file to the path.
  path = File.join(asset_dir, current_resource.path)
  path = path[0..-(File.extname(path).length + 1)] + ".#{asset_ext}"

  yield path if sitemap.find_resource_by_path(path)
end
capture_html(*args) { |*args| ... } click to toggle source
# File lib/middleman-core/core_extensions/default_helpers.rb, line 66
def capture_html(*args, &block)
  result = if handler = auto_find_proper_handler(&block)
    handler.capture_from_template(*args, &block)
  else
    yield(*args)
  end

  ::ActiveSupport::SafeBuffer.new.safe_concat(result)
end
content_tag(name, content=nil, options=nil, &block) click to toggle source

Make all block content html_safe

# File lib/middleman-core/core_extensions/default_helpers.rb, line 41
def content_tag(name, content=nil, options=nil, &block)
  # safe_content_tag(name, content, options, &block)
  if block_given?
    options = content if content.is_a?(Hash)
    content = capture_html(&block)
  end

  options    = parse_data_options(name, options)
  attributes = tag_attributes(options)
  output = ActiveSupport::SafeBuffer.new
  output.safe_concat "<#{name}#{attributes}>"

  if content.respond_to?(:each) && !content.is_a?(String)
    content.each do |c|
      output.safe_concat c
      output.safe_concat ::Padrino::Helpers::TagHelpers::NEWLINE
    end
  else
    output.safe_concat content.to_s
  end
  output.safe_concat "</#{name}>"

  block_is_template?(block) ? concat_content(output) : output
end
form_tag(url, options={}, &block) click to toggle source

Modified Padrino form_for that uses Middleman's url_for to transform the URL.

Calls superclass method
# File lib/middleman-core/core_extensions/default_helpers.rb, line 255
def form_tag(url, options={}, &block)
  url = url_for(url, options)
  super
end
image_tag(path, params={}) click to toggle source

Modified Padrino image_tag so that it finds the paths for srcset using asset_path for the images listed in the srcset param

Calls superclass method
# File lib/middleman-core/core_extensions/default_helpers.rb, line 262
def image_tag(path, params={})
  params.symbolize_keys!

  if params.key?(:srcset)
    images_sources = params[:srcset].split(',').map do |src_def|
      if src_def.include?('//')
        src_def
      else
        image_def, size_def = src_def.strip.split(/\s+/)
        asset_path(:images, image_def) + ' ' + size_def
      end
    end

    params[:srcset] = images_sources.join(', ')
  end

  super(path, params)
end
javascript_include_tag(*sources) click to toggle source

Override helper to add `relative` opt-out.

# File lib/middleman-core/core_extensions/default_helpers.rb, line 125
def javascript_include_tag(*sources)
  options = sources.extract_options!.symbolize_keys

  path_options = {}
  path_options[:relative] = options.delete(:relative) if options.key?(:relative)

  sources.flatten.reduce(::ActiveSupport::SafeBuffer.new) do |all, source|
    all << content_tag(:script, nil, {
      src: asset_path(:js, source, path_options)
    }.update(options))
  end
end
page_classes(path=current_path.dup, options={}) click to toggle source

Generate body css classes based on the current path

@return [String]

# File lib/middleman-core/core_extensions/default_helpers.rb, line 164
def page_classes(path=current_path.dup, options={})
  if path.is_a? Hash
    options = path
    path = current_path.dup
  end

  path << index_file if path.end_with?('/')
  path = ::Middleman::Util.strip_leading_slash(path)

  classes = Set.new
  parts = path.split('.').first.split('/')
  parts.each_with_index { |_, i| classes << parts.first(i + 1).join('_') }

  prefix = options[:numeric_prefix] || 'x'
  classes.map do |c|
    # Replace weird class name characters
    c = c.gsub(/[^a-zA-Z0-9\-_]/, '-')

    # Class names can't start with a digit
    c = "#{prefix}#{c}" if c =~ /\A\d/
    c
  end.join(' ')
end
partial(template, options={}, &block) click to toggle source
Calls superclass method
# File lib/middleman-core/core_extensions/default_helpers.rb, line 281
def partial(template, options={}, &block)
  including_parent_locals = {}
  including_parent_locals.merge!(@locs || {})
  including_parent_locals.merge!(options[:locals] || {})

  options[:locals] = including_parent_locals
  super(template, options, &block)
end
url_for(path_or_resource, options={}) click to toggle source

Given a source path (referenced either absolutely or relatively) or a Resource, this will produce the nice URL configured for that path, respecting :relative_links, directory indexes, etc.

# File lib/middleman-core/core_extensions/default_helpers.rb, line 213
def url_for(path_or_resource, options={})
  options_with_resource = {}.merge!(options).merge!(current_resource: current_resource)
  ::Middleman::Util.url_for(app, path_or_resource, options_with_resource)
end