Sha256: 3ec7de927b050814242f3d00e5f177b672a7ca458649eec2f0ba3dde224a80ae

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

require 'erubis'
require 'ahcx_template_handler'

class Ahc

  def self.render(template_name, params, css_map = {})
    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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ahc-0.2.2 lib/ahc.rb