Skip to content

Instantly share code, notes, and snippets.

@axiixc
Created November 11, 2011 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axiixc/1359472 to your computer and use it in GitHub Desktop.
Save axiixc/1359472 to your computer and use it in GitHub Desktop.
A quick-and-dirty CSS generator. Basically takes a file an does some text replacements, but allows you to use constants and blocks in your css.
---
:definitions:
MAIN_BG: white
MAIN_TEXT_COLOR: black
MAIN_LINK_COLOR: black
MAIN_LINK_HIGHLIGHT_COLOR: "#FF0"
:partials:
MASTHEAD_GRADIENT: |
background: rgb(40,40,40);
background: -moz-linear-gradient(top, rgba(40,40,40,1) 0%, rgba(25,25,25,1) 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(40,40,40,1)), color-stop(100%,rgba(25,25,25,1)));
background: -webkit-linear-gradient(top, rgba(40,40,40,1) 0%,rgba(25,25,25,1) 100%);
background: -o-linear-gradient(top, rgba(40,40,40,1) 0%,rgba(25,25,25,1) 100%);
background: -ms-linear-gradient(top, rgba(40,40,40,1) 0%,rgba(25,25,25,1) 100%);
background: linear-gradient(top, rgba(40,40,40,1) 0%,rgba(25,25,25,1) 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#282828', endColorstr='#191919',GradientType=0 );
.body {
background: MAIN_BG;
color: MAIN_TEXT_COLOR;
}
.masthead {
<MASTHEAD_GRADIENT>
}
#!/usr/bin/ruby
require 'yaml'
CONFIG_FILE = 'config.yaml'
if !File.exists? CONFIG_FILE
IO.write(CONFIG_FILE, {
:partials => { 'PARTIAL' => 'PARTIAL' },
:definitions => { 'DEFINITION' => 'DEFINITION' }
}.to_yaml)
puts "No config file found, created `config.yaml`"
end
input_file = ARGV[0]
input_file ||= 'style.css'
output_file = ARGV[1]
output_file ||= File.join Dir.pwd, File.basename(input_file, '.css') + '-gen.css'
last_modified = {
:css => nil,
:config => nil
}
loop do
current_modified = {
:css => File.mtime(input_file),
:config => File.mtime(CONFIG_FILE)
}
if last_modified != current_modified
last_modified = current_modified
css = IO.read input_file
config = YAML.load_file CONFIG_FILE
# Due to gsub order, partials can contain constants, and will
# have them replaced correctly!
config[:partials].each { |k,v| css.gsub! "<#{k}>", v }
config[:definitions].each { |k,v| css.gsub! k, v }
File.open(output_file, 'w') { |f| f.write css }
puts "Update!"
end
sleep 0.2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment