Sha256: d12585de24a3eb44746e2e5974a6d066adb507266ba1e6fc02153bcb33acb4e5

Contents?: true

Size: 2 KB

Versions: 29

Compression:

Stored size: 2 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'},
              {regexp: 'form-row', class: 'row'}
          ]
      },
      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

29 entries across 29 versions & 1 rubygems

Version Path
fanforce-plugin-factory-2.0.0.rc29 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-2.0.0.rc28 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-2.0.0.rc27 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-2.0.0.rc26 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-2.0.0.rc25 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-2.0.0.rc24 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-2.0.0.rc23 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-2.0.0.rc22 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-2.0.0.rc21 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-2.0.0.rc19 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-2.0.0.rc18 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-2.0.0.rc17 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-2.0.0.rc16 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-2.0.0.rc15 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-2.0.0.rc13 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-2.0.0.rc12 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-2.0.0.rc11 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-2.0.0.rc10 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-2.0.0.rc9 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb
fanforce-plugin-factory-2.0.0.rc8 lib/fanforce/plugin_factory/sinatra/helpers/css_frameworks.rb