Home

Awesome

CoffeeStylesheets

What if you could write CoffeeScript like:

body ->
  background 'black'
  color 'red'
  p ->
    font_size '12px'
    border_radius px 5

comment 'and now something more complex'
serve_pie = ->
  # adds ie7 support for css3 border-radius, box-shadow, and linear-gradient
  position 'relative'
  prop 'behavior', 'url(/assets/css3pie/PIE.htc)'
pie_gradient = (a, b) ->
  background a
  prop '-pie-background', linear_gradient a, b # prop stands for property
s 'html.ie7', -> # s stands for selector
  s '.section#content', ->
    table '#anyId.anyClass1.anyClass2', ->
      serve_pie()
      th id: 'anotherId', class: 'anotherClass1 anotherClass2', ->
        serve_pie()
        # literal means literally what you provide will be output without modification
        literal '-webkit-filter: blur(2px) grayscale (.5) opacity(0.8) hue-rotate(120deg);'

and get back a CSS3 stylesheet, like:

/* line 1418, precompile/assets/stylesheets/ie7.css.coffee */
body {
  background: black;
  color: red; }
/* line 1420, precompile/assets/stylesheets/ie7.css.coffee */
body p {
  font-size: 12px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  border-radius: 5px; }

/* and now something more complex */
/* line 1421, precompile/assets/stylesheets/ie7.css.coffee */
html.ie7 .section#content table#anyId.anyClass1.anyClass2 {
  position: relative;
  behavior: url(/assets/css3pie/PIE.htc); }
/* line 1423, precompile/assets/stylesheets/ie7.css.coffee */
html.ie7 .section#content table#anyId.anyClass1.anyClass2 th#anotherId.anotherClass1.anotherClass2 {
  position: relative;
  behavior: url(/assets/css3pie/PIE.htc); 
  -webkit-filter: blur(2px) grayscale (.5) opacity(0.8) hue-rotate(120deg); }

Now, you can!

Just wrap it in this:

engine = new CoffeeStylesheets
 format: true # optional
 globals: # optional
   px: (i) -> i + 'px' # a silly example of custom helpers
   # the cross-browser version of border_radius() is provided by the CoffeeStylesheetsCompassFramework plugin 
engine.render stylesheet, (err, css) ->
  console.log css

Why this monstrosity?

FAQ

See also