Create a Cascading Style Sheet (CSS) using Ruby.

Example usage:

  css = Builder::CSS.new

  text_color      = '#7F7F7F'
  preferred_fonts = 'Helvetica, Arial, sans_serif'

  css.comment! 'This is our stylesheet'
  css.body {
    background_color '#FAFAFA'
    font_size        'small'
    font_family      preferred_fonts
    color            text_color
  }

  css.id!('navbar') {
    width            '500px'
  }

  css.class!('navitem') {
    color            'red'
  }

  css.a :hover {
    text_decoration  'underline'
  }

  css.div(:id => 'menu') {
    background       'green'
  }

  css.div(:class => 'foo') {
    background       'red'
  }

This will yield the following stylesheet:

  /* This is our stylesheet */
  body {
    background_color: #FAFAFA;
    font_size:        small;
    font_family:      Helvetica, Arial, sans_serif;
    color:            #7F7F7F;
  }

  #navbar {
    width:            500px;
  }

  .navitem {
    color:            red;
  }

  a:hover {
    text_decoration:  underline;
  }

  div#menu {
    background:       green;
  }

  div.foo {
    background:       red;
  }
Methods
Public Class methods
new(indent=2)

Create a CSS builder.

out:Object receiving the markup.1 out must respond to <<.
indent:Number of spaces used for indentation (0 implies no indentation and no line breaks).
     # File lib/builder/css.rb, line 101
101:     def initialize(indent=2)
102:       @indent      = indent
103:       @target      = []
104:       @parts       = []
105:       @library     = {}
106:     end
Public Instance methods
+(part)
     # File lib/builder/css.rb, line 108
108:     def +(part)
109:       _join_with_op! '+'
110:       self
111:     end
>(part)
     # File lib/builder/css.rb, line 118
118:     def >(part)
119:       _join_with_op! '>'
120:       self
121:     end
>>(part)
     # File lib/builder/css.rb, line 113
113:     def >>(part)
114:       _join_with_op! ''
115:       self
116:     end
class!(arg, &block)
     # File lib/builder/css.rb, line 145
145:     def class!(arg, &block)
146:       _start_container('.'+arg.to_s, nil, ::Kernel.block_given?)
147:       _css_block(block) if block
148:       _unify_block
149:       self
150:     end
comment!(comment_text)

Create a comment string in the output.

     # File lib/builder/css.rb, line 134
134:     def comment!(comment_text)
135:       @target << "/* #{comment_text} */\n"
136:     end
group!(*args, &block)
     # File lib/builder/css.rb, line 156
156:     def group!(*args, &block)
157:       args.each do |arg|
158:         if arg.is_a?(Symbol)
159:           instance_eval(&@library[arg])
160:         else
161:           instance_eval(&arg)
162:         end
163:         _text ', ' unless arg == args.last
164:       end
165:       if block
166:         _css_block(block)
167:         _unify_block
168:       end
169:     end
id!(arg, &block)
     # File lib/builder/css.rb, line 138
138:     def id!(arg, &block)
139:       _start_container('#'+arg.to_s, nil, ::Kernel.block_given?)
140:       _css_block(block) if block
141:       _unify_block
142:       self
143:     end
method_missing(sym, *args, &block)
     # File lib/builder/css.rb, line 171
171:     def method_missing(sym, *args, &block)
172:       sym = "#{sym}:#{args.shift}" if args.first.kind_of?(::Symbol)
173:       if block
174:         _start_container(sym, args.first)
175:         _css_block(block)
176:         _unify_block
177:       elsif @in_block
178:         _indent
179:         _css_line(sym, *args)
180:         _newline
181:         return self
182:       else
183:         _start_container(sym, args.first, false)
184:         _unify_block
185:       end
186:       self
187:     end
nil?()

"Cargo culted" from Jim who also "cargo culted" it. See xmlbase.rb.

     # File lib/builder/css.rb, line 190
190:     def nil?
191:       false
192:     end
store!(sym, &block)
     # File lib/builder/css.rb, line 152
152:     def store!(sym, &block)
153:       @library[sym] = block.to_proc
154:     end
target!()

Return the target of the builder

     # File lib/builder/css.rb, line 129
129:     def target!
130:       @target * ''
131:     end
|(part)
     # File lib/builder/css.rb, line 123
123:     def |(part)
124:       _join_with_op! ','
125:       self
126:     end