2010-09-07 03:48:25 +02:00
|
|
|
module Middleman::Features::DefaultHelpers
|
|
|
|
class << self
|
|
|
|
def registered(app)
|
|
|
|
app.helpers Middleman::Features::DefaultHelpers::Helpers
|
|
|
|
end
|
|
|
|
alias :included :registered
|
2009-10-23 02:25:15 +02:00
|
|
|
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")
|
2010-09-05 05:26:48 +02:00
|
|
|
scss_file = File.join(self.class.views, self.class.css_dir, "#{path}.css.scss")
|
2010-09-07 03:48:25 +02:00
|
|
|
less_file = File.join(self.class.views, self.class.css_dir, "#{path}.css.less")
|
2009-11-03 21:49:54 +01:00
|
|
|
|
2010-09-07 03:48:25 +02: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{^/}, '')
|
2009-10-23 02:25:15 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2009-10-23 02:25:15 +02:00
|
|
|
classes.join(' ')
|
|
|
|
end
|
|
|
|
|
|
|
|
def asset_url(path, prefix="")
|
2010-09-05 05:26:48 +02:00
|
|
|
Middleman::Assets.get_url(path, prefix, request)
|
2009-10-23 02:25:15 +02:00
|
|
|
end
|
|
|
|
|
2010-09-07 03:48:25 +02:00
|
|
|
# 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
|
2010-09-07 03:48:25 +02:00
|
|
|
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
|
2009-10-23 02:25:15 +02:00
|
|
|
end
|
2010-09-07 03:48:25 +02:00
|
|
|
end
|