middleman/lib/middleman/features/default_helpers.rb

61 lines
2.1 KiB
Ruby
Raw Normal View History

module Middleman::Features::DefaultHelpers
class << self
def registered(app)
app.helpers Middleman::Features::DefaultHelpers::Helpers
end
alias :included :registered
end
module Helpers
2009-12-28 21:03:38 +01:00
def auto_stylesheet_link_tag(separator="/")
2009-11-03 21:34:57 +01:00
path = request.path_info.dup
path << self.class.index_file if path.match(%r{/$})
path = path.gsub(%r{^/}, '')
path = path.gsub(File.extname(path), '')
2009-12-28 21:03:38 +01:00
path = path.gsub("/", separator)
2009-11-03 21:34:57 +01:00
2011-06-01 00:12:36 +02:00
css_file = File.join(self.class.views, self.class.css_dir, "#{path}.css")
2009-11-03 21:49:54 +01:00
sass_file = File.join(self.class.views, self.class.css_dir, "#{path}.css.sass")
scss_file = File.join(self.class.views, self.class.css_dir, "#{path}.css.scss")
less_file = File.join(self.class.views, self.class.css_dir, "#{path}.css.less")
2009-11-03 21:49:54 +01:00
if File.exists?(css_file) || File.exists?(sass_file) || File.exists?(scss_file) || File.exists?(less_file)
2009-11-03 21:34:57 +01:00
stylesheet_link_tag "#{path}.css"
end
end
def page_classes
path = request.path_info.dup
2010-01-30 07:48:34 +01:00
path << settings.index_file if path.match(%r{/$})
2009-11-03 21:34:57 +01:00
path = path.gsub(%r{^/}, '')
classes = []
parts = path.split('.')[0].split('/')
parts.each_with_index { |path, i| classes << parts.first(i+1).join('_') }
2009-11-03 21:34:57 +01:00
classes.join(' ')
end
def asset_url(path, prefix="")
Middleman::Assets.get_url(path, prefix, request)
end
# Padrino's asset handling needs to pass through ours
def asset_path(kind, source)
return source if source =~ /^http/
asset_folder = case kind
when :css then settings.css_dir
when :js then settings.js_dir
when :images then settings.images_dir
else kind.to_s
end
source = source.to_s.gsub(/\s/, '')
2011-06-14 22:54:59 +02:00
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)
timestamp = asset_timestamp(result_path)
"#{result_path}#{timestamp}"
end
end
end