Skip to content

Instantly share code, notes, and snippets.

@vybs
Created December 19, 2011 02:10
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 vybs/1495123 to your computer and use it in GitHub Desktop.
Save vybs/1495123 to your computer and use it in GitHub Desktop.
Convert from sass to css using compass, generate locale specific rules
require 'rubygems'
require 'compass'
require 'compass/exec'
## start sass css converter
class SASS_CSS
module Sass::Script::Functions
def user_language()
Sass::Script::String.new(options[:custom][:lang])
end
end
# read the file and parse for $lang: user_language();
def self.parse_locale(filename)
file = File.new(filename, 'r')
while (line = file.gets)
if( line.include? "user_language()")
return true
end
end
return false
end
## Compass Compiler #########
def self.compass(filename, locale)
$LOAD_PATH.unshift "#{filename}"
top_scss_src_dir = $project.getProperty('scss.src')
options ={ :cache_location => "compass_cache", :load_paths => [top_scss_src_dir], :style => :compressed, :full_exception => :true, :syntax => :scss , :custom => { :lang => locale } }
scss_src_dir = File.dirname(filename)
dest_subdir = scss_src_dir[top_scss_src_dir.length()..-1]
if (locale != "" )
dest_file_name = File.basename(filename, '.scss') + "_" + locale + '.css'
#no locale specific
else
dest_file_name = File.basename(filename, '.scss') + '.css'
end
dest_dir = $project.getProperty('scss.build') + dest_subdir
subDirs = dest_subdir.split("/")
dest_dir = $project.getProperty('scss.build')
subDirs.each do
|subDir|
dest_dir += "/" + subDir
Dir.mkdir(dest_dir) unless File.exists?(dest_dir)
end
dest_file_path = dest_dir + "/" + dest_file_name
#puts "##>> dest_file_path = " + dest_file_path
compass_compiler = Compass::Compiler.new(top_scss_src_dir, "sass", "css", options)
compass_compiler.compile("#{filename}","#{dest_file_path}");
end
end
## end sass css converter
files = Dir.glob($project.getProperty('scss.src') + "/**/[^_]*.scss")
locales = ['ja_JP','pt_BR', 'en_US','fr_FR','ro_RO','ru_RU','es_ES','de_DE','it_IT','in_ID','nl_NL','ms_MY','tr_TR','da_DK','sv_SE','no_NO','cs_CZ','pl_PL','ko_KR']
total_time = 0
files.each do
| scss_file_name |
start_time = Time.now
has_locales = SASS_CSS.parse_locale(scss_file_name)
if ( has_locales )
puts " Compiling sass files for supported locales " + scss_file_name
locales.each do
|locale|
SASS_CSS.compass(scss_file_name, locale)
end
else
# no locale
SASS_CSS.compass(scss_file_name, "");
end
end_time = Time.now
total_time = total_time + (end_time - start_time )
end
puts " Compiling sass files to css took " + total_time.to_s + " secs"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment