Sha256: 36803a3d64297cbce725e6f028ae9c980f4f45ad0374fd0986cf282ac2a614dc

Contents?: true

Size: 1.95 KB

Versions: 20

Compression:

Stored size: 1.95 KB

Contents

require 'haml'
require 'haml/compiler'

class Haml::Options
  attr_accessor :css_framework

  def self.defaults
    @defaults[:css_framework] ||= nil
    @defaults
  end

  def css_framework=(framework)
    case framework.to_s.downcase.to_sym
      when :foundation5 then @css_framework = :foundation5
      when :bootstrap3 then @css_framework = :bootstrap3
      else @css_framework = nil
    end
  end
end

class Haml::Compiler
  FRAMEWORKS = {
      foundation5: {
          tags: {},
          classes: [
              {regexp: 'col-width(\d+)', class: 'small-12 medium-{{1}} column'}
          ]
      },
      bootstrap3: {
          tags: {
              input: {class: 'form-control'},
              select: {class: 'form-control'}
          },
          classes: [
              {regexp: 'col-width(\d+)', class: 'col-md-{{1}}'},
              {regexp: 'form-row', class: 'form-group'}
          ]
      }
  }

  def compile(node)
    parent, @node = @node, node

    if node.children.empty?
      precompile_tag if node.type == :tag
      send(:"compile_#{node.type}")
    else
      precompile_tag if node.type == :tag
      send(:"compile_#{node.type}") {node.children.each {|c| compile c}}
    end
  ensure
    @node = parent
  end

  def precompile_tag
    if @options.css_framework
      framework = FRAMEWORKS[@options.css_framework]
      if @node.value[:name].present? and framework[:tags][@node.value[:name].to_sym]
        @node.value[:attributes]['class'] = "#{@node.value[:attributes]['class'].to_s.strip} #{framework[:tags][@node.value[:name].to_sym][:class]}"
      end
      framework[:classes].each do |class_obj|
        class_str = " #{@node.value[:attributes]['class']} "
        regexp = Regexp.new(" #{class_obj[:regexp]} ")
        if regexp.match(class_str)
          @node.value[:attributes]['class'] = (class_str.gsub(regexp, ' ') + class_obj[:class].gsub('{{1}}', $1.to_s)).strip
        end
      end if @node.value[:attributes]['class']
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
fanforce-plugin-factory-1.6.0.rc5 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-1.6.0.rc4 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-1.6.0.rc3 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-1.6.0.rc2 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-1.6.0.rc1 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-app-factory-2.0.0.rc38 lib/fanforce/app_factory/sinatra/helpers/css_frameworks.rb
fanforce-app-factory-2.0.0.rc37 lib/fanforce/app_factory/sinatra/helpers/css_frameworks.rb
fanforce-app-factory-2.0.0.rc36 lib/fanforce/app_factory/sinatra/helpers/css_frameworks.rb
fanforce-app-factory-2.0.0.rc35 lib/fanforce/app_factory/sinatra/helpers/css_frameworks.rb
fanforce-app-factory-2.0.0.rc34 lib/fanforce/app_factory/sinatra/helpers/css_frameworks.rb
fanforce-app-factory-2.0.0.rc33 lib/fanforce/app_factory/sinatra/helpers/css_frameworks.rb
fanforce-app-factory-2.0.0.rc32 lib/fanforce/app_factory/sinatra/helpers/css_frameworks.rb
fanforce-app-factory-2.0.0.rc31 lib/fanforce/app_factory/sinatra/helpers/css_frameworks.rb
fanforce-app-factory-2.0.0.rc30 lib/fanforce/app_factory/sinatra/helpers/css_frameworks.rb
fanforce-app-factory-2.0.0.rc29 lib/fanforce/app_factory/sinatra/helpers/css_frameworks.rb
fanforce-app-factory-2.0.0.rc27 lib/fanforce/app_factory/sinatra/helpers/css_frameworks.rb
fanforce-app-factory-2.0.0.rc26 lib/fanforce/app_factory/sinatra/helpers/css_frameworks.rb
fanforce-app-factory-2.0.0.rc25 lib/fanforce/app_factory/sinatra/helpers/css_frameworks.rb
fanforce-app-factory-2.0.0.rc24 lib/fanforce/app_factory/sinatra/helpers/css_frameworks.rb
fanforce-app-factory-2.0.0.rc23 lib/fanforce/app_factory/sinatra/helpers/css_frameworks.rb