Class: Blueprint::SemanticClassNames

Inherits:
Object
  • Object
show all
Defined in:
lib/blueprint/semantic_class_names.rb

Overview

parses a hash of key/value pairs, key being output CSS selectors, value being a list of CSS selectors to draw from

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SemanticClassNames

Options

  • options

    • :namespace ??? Namespace to be used when matching CSS selectors to draw from

    • :source_file ??? Source file to use as reference of CSS selectors. Defaults to Blueprint's generated screen.css

    • :class_assignments ??? Hash of key/value pairs, key being output CSS selectors, value being a list of CSS selectors to draw from



13
14
15
16
17
# File 'lib/blueprint/semantic_class_names.rb', line 13

def initialize(options = {})
  @namespace = options[:namespace] || ""
  @source_file = options[:source_file] || File.join(Blueprint::BLUEPRINT_ROOT_PATH, "screen.css")
  self.class_assignments = options[:class_assignments] || {}
end

Instance Attribute Details

#class_assignmentsObject

Returns the value of attribute class_assignments



5
6
7
# File 'lib/blueprint/semantic_class_names.rb', line 5

def class_assignments
  @class_assignments
end

#namespaceObject (readonly)

Returns the value of attribute namespace



6
7
8
# File 'lib/blueprint/semantic_class_names.rb', line 6

def namespace
  @namespace
end

#source_fileObject (readonly)

Returns the value of attribute source_file



6
7
8
# File 'lib/blueprint/semantic_class_names.rb', line 6

def source_file
  @source_file
end

Instance Method Details

#css_from_assignments(assignments = {}) ⇒ Object

Returns a CSS string of semantic selectors and associated styles

Options

  • assignments ??? Hash of key/value pairs, key being output CSS selectors, value being a list of CSS selectors to draw from; defaults to what was passed in constructor or empty hash



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/blueprint/semantic_class_names.rb', line 22

def css_from_assignments(assignments = {})
  assignments ||= self.class_assignments

  # define a wrapper hash to hold all the new CSS assignments
  output_css = {}

  #loads full stylesheet into an array of hashes
  blueprint_assignments = CSSParser.new(File.path_to_string(self.source_file)).parse

  # iterates through each class assignment ('#footer' => '.span-24', '#header' => '.span-24')
  assignments.each do |semantic_class, blueprint_classes|
    # gathers all BP classes we're going to be mimicing
    blueprint_classes = blueprint_classes.split(/,|\s/).select {|c| !c.blank? }.flatten.map {|c| c.strip }
    classes = []
    # loop through each BP class, grabbing the full hash (containing tags, index, and CSS rules)
    blueprint_classes.each do |bp_class|
      match = if bp_class.include?(".")
                bp_class.gsub(".", ".#{self.namespace}")
              else
                ".#{self.namespace}#{bp_class}"
              end
      classes << blueprint_assignments.select do |line|
        line[:tags] =~ Regexp.new(/^([\w\.\-\:]+, ?)*#{match}(, ?[\w\.\-\:]+)*$/)
      end.uniq
    end

    # clean up the array
    classes = classes.flatten.uniq

    # set the semantic class to the rules gathered in classes, sorted by index
    # this way, the styles will be applied in the correct order from top of file to bottom
    output_css[semantic_class] = "#{classes.sort_by {|i| i[:idx] }.map {|i| i[:rules] }}"
  end

  # return the css in proper format
  css = ""
  output_css.each do |tags, rules|
    css += "#{tags} {#{rules}}\n"
  end
  css
end