Module: Mint::CSS

Defined in:
lib/mint/css_dsl.rb

Constant Summary collapse

CONTAINER =
"container"

Class Method Summary collapse

Class Method Details

.mappingsObject

Allows for a human-readable DSL to be used to generate CSS. Translates the following:


Font: Helvetica Margin: 1in Orientation: Landscape


… into something like this:

#container {

font-family: Helvetica;
padding-left: 1in;
@page { size: landscape };

}



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
# File 'lib/mint/css_dsl.rb', line 22

def self.mappings
  {
    font: "font-family",
    font_size: "font-size",
    font_color: "color",
    color: "color",
    top_margin: "padding-top",
    top: "padding-top",
    bottom_margin: "padding-bottom",
    bottom: "padding-bottom",
    left_margin: "padding-left",
    left: "padding-left",
    right_margin: "padding-right",
    right: "padding-right",
    height: "height",
    width: "width",
    columns: "column-count",
    column_gap: "column-gap",
    orientation: "@page { size: %s }",
    indentation: "p+p { text-indent: %s }",
    indent: "p+p { text-indent: %s }",
    bullet: "li { list-style-type: %s }",
    bullet_image: "li { list-style-image: url(%s) }",
    after_paragraph: "p { margin-bottom: %s }",
    before_paragraph: "p { margin-top: %s }"
  }
end

.parse(style) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mint/css_dsl.rb', line 63

def self.parse(style)
  css = style.map {|k,v| stylify(k, v) }.join("\n  ")
  container_scope = "##{CONTAINER}\n  #{css.strip}\n"
  
  # Suppress warnings by capturing $stderr
  original_stderr = $stderr
  $stderr = StringIO.new
  
  result = Sass.compile_string(container_scope, syntax: :indented)
  result.css
ensure
  $stderr = original_stderr
end

.stylify(key, value) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mint/css_dsl.rb', line 50

def self.stylify(key, value)
  symbol_key = key.to_s.downcase.gsub(' ', '_').to_sym
  selector = mappings[symbol_key]

  if selector.nil?
    ""
  elsif selector.include? "%"
    selector % value
  else
    "#{selector || key}: #{value}"
  end
end