Module: Middleman::CoreExtensions::DefaultHelpers::Helpers

Defined in:
middleman-core/lib/middleman-core/core_extensions/default_helpers.rb

Overview

The helpers

Instance Method Summary (collapse)

Instance Method Details

- (String) asset_path(kind, source)

Get the path of a file of a given type

Parameters:

  • kind (Symbol)

    The type of file

  • source (String)

    The path to the file

Returns:

  • (String)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'middleman-core/lib/middleman-core/core_extensions/default_helpers.rb', line 93

def asset_path(kind, source)
  return source if source =~ /^http/
  asset_folder  = case kind
    when :css    then css_dir
    when :js     then js_dir
    when :images then images_dir
    else kind.to_s
  end
  source = source.to_s.gsub(/\s/, '')
  ignore_extension = (kind == :images) # don't append extension
  source << ".#{kind}" unless ignore_extension or source =~ /\.#{kind}/
  result_path   = source if source =~ %r{^/} # absolute path
  result_path ||= asset_url(source, asset_folder)
  "#{result_path}"
end

- (String) auto_javascript_include_tag(separator = "/")

Output a javascript tag based on the current path

Parameters:

  • separator (String) (defaults to: "/")

    How to break up path in parts

Returns:

  • (String)


43
44
45
46
47
# File 'middleman-core/lib/middleman-core/core_extensions/default_helpers.rb', line 43

def auto_javascript_include_tag(separator="/")
  auto_tag(:js, separator) do |path|
    javascript_include_tag path
  end
end

Output a stylesheet link tag based on the current path

Parameters:

  • separator (String) (defaults to: "/")

    How to break up path in parts

Returns:

  • (String)


33
34
35
36
37
# File 'middleman-core/lib/middleman-core/core_extensions/default_helpers.rb', line 33

def auto_stylesheet_link_tag(separator="/")
  auto_tag(:css, separator) do |path|
    stylesheet_link_tag path
  end
end

- (void) auto_tag(asset_ext, separator = "/", asset_dir = nil) {|path| ... }

This method returns an undefined value.

Output a stylesheet link tag based on the current path

Parameters:

  • asset_ext (Symbol)

    The type of asset

  • separator (String) (defaults to: "/")

    How to break up path in parts

  • asset_dir (String) (defaults to: nil)

    Where to look for assets

Yields:

  • (path)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'middleman-core/lib/middleman-core/core_extensions/default_helpers.rb', line 55

def auto_tag(asset_ext, separator="/", asset_dir=nil)
  if asset_dir.nil?
    asset_dir = case asset_ext
      when :js  then js_dir
      when :css then 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 = full_path(current_path.dup)
  path = path.sub(%r{^/}, '')
  path = path.gsub(File.extname(path), ".#{asset_ext}")
  path = path.gsub("/", separator)

  yield path if sitemap.find_resource_by_path(File.join(asset_dir, path))
end


109
110
111
112
113
114
115
116
117
118
119
# File 'middleman-core/lib/middleman-core/core_extensions/default_helpers.rb', line 109

def link_to(*args, &block)
  url_arg_index = block_given? ? 0 : 1
  if url = args[url_arg_index]
    # Only try to work with absolute URLs
    if url.start_with? '/'
      resource = sitemap.find_resource_by_path(url)
      args[url_arg_index] = resource.url if resource
    end
  end
  super(*args, &block)
end

- (String) page_classes

Generate body css classes based on the current path

Returns:

  • (String)


76
77
78
79
80
81
82
83
84
85
86
# File 'middleman-core/lib/middleman-core/core_extensions/default_helpers.rb', line 76

def page_classes
  path = current_path.dup
  path << index_file if path.match(%r{/$})
  path = path.gsub(%r{^/}, '')

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

  classes.join(' ')
end