require 'erubis' require 'ahcx_template_handler' class Ahc def self.render(template_name, params) if @@templates[template_name] css = lambda {|css_class| @@css_map[css_class] || css_class } return @@templates[template_name].result({:params => params, :css => css}).strip end return '' end def self.read_templates(paths) @@templates = {} @@css_map ||= {} paths.each do |path| if File.directory?(path) Dir.glob(path+"/**/*.css") do |item| next if File.directory?(item) provides = css_provides(item) template = get_template(item) provides.each do |provide| @@templates[provide] = template if template end end end end end def self.set_css_map(css_map) @@css_map = css_map end def self.css_provides(stylesheet_path) provides = [] File.readlines(stylesheet_path).each do |line| match = /@provide ['|"](.*)['|"]/.match(line) if match provides << match[1] end end provides end def self.get_template(stylesheet_path) template_path = stylesheet_path.sub(%r{\.css$}, ".erb") return nil unless File.exists?(template_path) return Erubis::Eruby.new(File.read(template_path)) end end Ahc.read_templates(['components']) if File.directory?('components') if defined? Rails ActionView::Template.register_template_handler(:ahcx, AhcxTemplateHandler) end