module Haml
module Helpers
# Generates links to all stylesheets in the source directory
def stylesheets
stylesheet_dir = "#{@base_dir}/src/stylesheets"
output = ""
Dir["#{stylesheet_dir}/**/*.sass"].each do |path|
filename_without_extension = File.basename(path).chomp(File.extname(path))
output << tag(:link, :href => "stylesheets/#{filename_without_extension}.css", :rel => 'stylesheet', :media => 'all')
end
output
end
# Generate javascript source tags for the specified files
#
# javascripts('test') ->
#
def javascripts(*files)
output = ""
files.each do |file|
if !file.match(/^http\:\/\//)
output << tag(:script, :language => 'javascript', :src => "javascripts/#{file}.js") { "" }
end
end
output
end
# Generates a form text field
#
def text_field(name, value)
tag(:input, :type => "text", :name => name, :value => value)
end
# Generate an HTML link
#
# If only the title is passed, it will automatically
# create a link from this value:
#
# link('Test') -> Test
#
def link(title, page = "", options = {})
if page.blank?
page = urlify(title) + ".html"
end
tag(:a, :href => page) { title }
end
# Generates an image tag
def img(name, options = {})
tag :img, options.merge!({:src => "images/#{name}"})
end
# Generates HTML tags:
#
# tag(:br) ->
# tag(:a, :href => 'test.html') { "Test" } -> Test
#
def tag(name, options = {}, &block)
output = "<#{name}"
options.each do |attribute, value|
output << " #{attribute}=\"#{value}\""
end
if block_given?
output << ">"
output << yield
output << "#{name}>\n"
else
output << "/>\n"
end
output
end
# Generates a URL friendly string from the value passed:
#
# "We love Haml" -> "we_love_haml"
# "Elf & Ham" -> "elf_and_ham"
# "Stephen's gem" -> "stephens_gem"
#
def urlify(string)
string.tr(" ", "_").
sub("&", "and").
sub("@", "at").
tr("^A-Za-z0-9_", "").
sub(/_{2,}/, "_").
downcase
end
end
end