Sha256: c948a33bd26f334e3b16cd0d476e4008f4c6476e61ba8b16f636771f5e24e078

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

require 'erubis'
require 'ahcx_template_handler'

class Ahc

  def self.render(template_name, params)
    if @@templates[template_name]
      return @@templates[template_name].result({:params => params}).strip
    end
    return ''
  end

  def self.read_templates(paths)
    @@templates = {}
    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.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

2 entries across 2 versions & 1 rubygems

Version Path
ahc-0.2.1 lib/ahc.rb
ahc-0.2.0 lib/ahc.rb