1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00
middleman--middleman/lib/middleman/helpers.rb

139 lines
3.6 KiB
Ruby
Raw Normal View History

2009-09-04 16:58:58 -04:00
module Table
include Haml::Filters::Base
def render(text)
output = '<div class="table"><table cellspacing="0" cellpadding="0">'
line_num = 0
text.each_line do |line|
line_num += 1
next if line.strip.empty?
output << %Q{<tr class="#{(line_num % 2 == 0) ? "even" : "odd" }#{(line_num == 1) ? " first" : "" }">}
columns = line.split("|").map { |p| p.strip }
columns.each_with_index do |col, i|
output << %Q{<td class="col#{i+1}">#{col}</td>}
end
output << "</tr>"
end
output + "</table></div>"
end
end
def find_and_include_related_sass_file
path = request.path_info.dup
path << "index.html" if path.match(%r{/$})
path.gsub!(%r{^/}, '')
path.gsub!(File.extname(path), '')
path.gsub!('/', '-')
sass_file = File.join(File.basename(self.class.views), "stylesheets", "#{path}.sass")
if File.exists? sass_file
stylesheet_link_tag "stylesheets/#{path}.css"
end
end
2009-08-04 19:40:33 -04:00
def link_to(title, url="#", params={})
params.merge!(:href => url)
params = params.map { |k,v| %Q{#{k}="#{v}"}}.join(' ')
%Q{<a #{params}>#{title}</a>}
end
def page_classes(*additional)
2009-08-11 18:39:20 -04:00
path = request.path_info
2009-08-11 14:21:49 -04:00
path << "index.html" if path.match(%r{/$})
path.gsub!(%r{^/}, '')
2009-08-04 19:40:33 -04:00
classes = []
2009-08-11 14:21:49 -04:00
parts = path.split('.')[0].split('/')
2009-08-04 19:40:33 -04:00
parts.each_with_index { |path, i| classes << parts.first(i+1).join('_') }
classes << "index" if classes.empty?
classes += additional unless additional.empty?
classes.join(' ')
end
def haml_partial(name, options = {})
item_name = name.to_sym
counter_name = "#{name}_counter".to_sym
if collection = options.delete(:collection)
collection.enum_for(:each_with_index).collect do |item,index|
haml_partial name, options.merge(:locals => {item_name => item, counter_name => index+1})
end.join
elsif object = options.delete(:object)
haml_partial name, options.merge(:locals => {item_name => object, counter_name => nil})
else
haml "_#{name}".to_sym, options.merge(:layout => false)
2009-08-04 19:32:01 -04:00
end
2009-08-04 19:40:33 -04:00
end
2009-09-24 19:24:41 -04:00
# def cache_buster
# if File.readable?(real_path)
# File.mtime(real_path).strftime("%s")
# else
# $stderr.puts "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}"
# end
# end
2009-08-04 19:40:33 -04:00
def asset_url(path)
path.include?("://") ? path : "/#{path}"
end
def image_tag(path, options={})
options[:alt] ||= ""
capture_haml do
haml_tag :img, options.merge(:src => asset_url(path))
2009-08-04 19:34:02 -04:00
end
2009-08-04 19:40:33 -04:00
end
def javascript_include_tag(path, options={})
capture_haml do
haml_tag :script, options.merge(:src => asset_url(path), :type => "text/javascript")
2009-08-04 15:18:05 -04:00
end
2009-07-31 12:35:12 -04:00
end
2009-08-04 19:40:33 -04:00
def stylesheet_link_tag(path, options={})
options[:rel] ||= "stylesheet"
capture_haml do
haml_tag :link, options.merge(:href => asset_url(path), :type => "text/css")
end
2009-08-09 16:10:09 -04:00
end
# Handle Sass errors
def sass_exception_string(e)
e_string = "#{e.class}: #{e.message}"
if e.is_a? Sass::SyntaxError
e_string << "\non line #{e.sass_line}"
if e.sass_filename
e_string << " of #{e.sass_filename}"
if File.exists?(e.sass_filename)
e_string << "\n\n"
min = [e.sass_line - 5, 0].max
begin
File.read(e.sass_filename).rstrip.split("\n")[
min .. e.sass_line + 5
].each_with_index do |line, i|
e_string << "#{min + i + 1}: #{line}\n"
end
rescue
e_string << "Couldn't read sass file: #{e.sass_filename}"
end
end
end
end
<<END
/*
#{e_string}
Backtrace:\n#{e.backtrace.join("\n")}
*/
body:before {
white-space: pre;
font-family: monospace;
content: "#{e_string.gsub('"', '\"').gsub("\n", '\\A ')}"; }
END
2009-08-04 19:40:33 -04:00
end