Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- # Example usage: ./stripjs.rb --files path/to/file.html
- # Example usage with find: ./stripjs.rb --files $(find . -name '*.html' -print0 | tr '\0' ,)
- require 'optparse'
- require 'nokogiri'
- options = {}
- optparse = OptionParser.new do |opts|
- # opts.banner = "Usage: test.rb [options]"
- opts.on("--files file1,file2", Array, "Html files") do |f|
- options[:files] = f
- end
- end
- begin
- optparse.parse!
- mandatory = [:files]
- missing = mandatory.select{ |param| options[param].nil? }
- unless missing.empty?
- raise OptionParser::MissingArgument.new(missing.join(', '))
- end
- rescue OptionParser::InvalidOption, OptionParser::MissingArgument
- puts $!.to_s
- puts optparse
- exit
- end
- options[:files].each do |file|
- doc = File.open(file) { |f| Nokogiri::HTML(f) }
- doc.search('//script').remove
- puts "Write file #{file}\n"
- File.write(file, doc.to_html)
- end
Add Comment
Please, Sign In to add comment