Skip to content

Instantly share code, notes, and snippets.

@johnny5th
Created May 16, 2012 16:18
Show Gist options
  • Save johnny5th/2711796 to your computer and use it in GitHub Desktop.
Save johnny5th/2711796 to your computer and use it in GitHub Desktop.
IE Media Query Converter
#!/usr/bin/ruby
# IE Media Query Converter
# Usage: IEMQConv.rb filein fileout
#
# Grabs all media query content in a CSS file and outputs a file
# containing all of the CSS without the media query tags.
# Useful for giving <=IE8 all of the site styles without
# media query support.
if(ARGV.length > 0 && File.file?(ARGV[0]))
filein = ARGV[0]
if(!filein =~ /.*?.css/i)
raise "Input file must be a CSS file"
end
else
raise "Error: Must provide a CSS file to read"
end
if(ARGV.length > 1)
fileout = ARGV[1]
else
fileout = "ie.css"
end
cssLines = IO.readlines(filein)
count = 0
data = ""
for line in cssLines
if(line =~ /@media only screen.*?\((.*?)\).*?\{/i)
count += 1
elsif(count > 0)
line.split("").each do |i|
if(i == "{")
count += 1
end
if(i == "}")
count -= 1
end
if(count > 0)
data = data + i
else
break
end
end
end
end
genfound = false
File.open(fileout, "a+") do |ieFile|
ieFile.rewind
ieFile.each do |line|
if(line =~ /\/\*\*\*GENERATED\*\*\*\//)
ieFile.truncate(ieFile.pos)
ieFile.puts("\n"+data)
genfound = true
break
end
end
if(genfound == false)
ieFile.puts("/***GENERATED***/\n\n")
ieFile.puts(data)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment