Return to Snippet

Revision: 1340
at September 27, 2006 13:05 by gdonald


Updated Code
#!/usr/bin/env ruby

curl = `which curl 2>/dev/null`.chomp
validator = 'http://jigsaw.w3.org/css-validator/validator'

# All warnings, CSS2, all mediums
options = 'warning=2&profile=css2&usermedium=all'

base = File.expand_path("#{File.dirname(__FILE__)}/../../public/stylesheets")

# Got curl?
raise "Curl not found" if curl.empty?

# Get path to stylesheets
if ARGV.size > 0
  base = ARGV.shift
end

# All css files or just one?
glob = base =~ /css$/ ? base : "#{base}/*.css"

# Do files
Dir.glob(glob) do |file|

  next unless File.exists?( file )

  errors, warnings = [ ], [ ]

  # Send the css file to the validator
  results = `#{curl} -s -F "file=@#{file}" -F "#{options}" #{validator}`

  # Validator couldn't find the file
  #
  # OR the file didn't have _any_ valid css content before
  # the errors <- little gotcha
  #
  results.grep(/No style sheet found/) do |line|
    STDERR << "#{$&}\n"
    exit
  end

  # Add new lines to <li></li> tags so grep can find them easier
  results.gsub!(/\n/,'').gsub!(/<li>/,"\n<li>").gsub!(/<\/li>/,"</li>\n")

  results.grep(/<li>.*<\/li>/) do |line|

    # collect errors
    line.grep(/<span class='error'>/) do |error|
      errors << error.gsub!(/(<p>|<\/p>)/,"\n").gsub!(/<(.|\n)*?>/, '')
    end

  end

  # collect warnings
  results.grep(/<span class='warning'>/) do |line|
    warnings << line.gsub!(/<(.|\n)*?>/, '')
  end

  # Dump information to STDERR
  STDERR << "CSS File #{file}:\n\n"

  { 'Errors' => errors, 'Warnings' => warnings }.each do |k,v|

    if v.empty?
      STDERR << "No #{k.downcase} found\n---------------\n\n"
    else
      STDERR << "#{k} found:\n-------------\n\n"
      v.each {|line| STDERR << line}
    end

  end

end

Revision: 1339
at September 27, 2006 13:04 by gdonald


Initial Code
#!/usr/bin/env ruby

curl = `which curl 2>/dev/null`.chomp
validator = 'http://jigsaw.w3.org/css-validator/validator'

# All warnings, CSS2, all mediums
options = 'warning=2&profile=css2&usermedium=all'

base = File.expand_path("#{File.dirname(__FILE__)}/../../public/stylesheets")

# Got curl?
raise "Curl not found" if curl.empty?

# Get path to stylesheets
if ARGV.size > 0
  base = ARGV.shift
end

# All css files or just one?
glob = base =~ /css$/ ? base : "#{base}/*.css"

# Do files
Dir.glob(glob) do |file|

  next unless File.exists?( file )

  errors, warnings = [ ], [ ]

  # Send the css file to the validator
  results = `#{curl} -s -F "file=@#{file}" -F "#{options}" #{validator}`

  # Validator couldn't find the file
  #
  # OR the file didn't have _any_ valid css content before
  # the errors <- little gotcha
  #
  results.grep(/No style sheet found/) do |line|
    STDERR << "#{$&}\n"
    exit
  end

  # Add new lines to <li></li> tags so grep can find them easier
  results.gsub!(/\n/,'').gsub!(/<li>/,"\n<li>").gsub!(/<\/li>/,"</li>\n")

  results.grep(/<li>.*<\/li>/) do |line|

    # collect errors
    line.grep(/<span class='error'>/) do |error|
      errors << error.gsub!(/(<p>|<\/p>)/,"\n").gsub!(/<(.|\n)*?>/, '')
    end

  end

  # collect warnings
  results.grep(/<span class='warning'>/) do |line|
    warnings << line.gsub!(/<(.|\n)*?>/, '')
  end

  # Dump information to STDERR
  STDERR << "CSS File #{file}:\n\n"

  { 'Errors' => errors, 'Warnings' => warnings }.each do |k,v|

    if v.empty?
      STDERR << "No #{k.downcase} found\n---------------\n\n"
    else
      STDERR << "#{k} found:\n-------------\n\n"
      v.each {|line| STDERR << line}
    end

  end

end

Initial URL


Initial Description


Initial Title
Rails CSS uploader/validator

Initial Tags
css

Initial Language
Ruby