Guest User

Untitled

a guest
May 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. # Example usage: ./stripjs.rb --files path/to/file.html
  4. # Example usage with find: ./stripjs.rb --files $(find . -name '*.html' -print0 | tr '\0' ,)
  5.  
  6. require 'optparse'
  7. require 'nokogiri'
  8.  
  9. options = {}
  10. optparse = OptionParser.new do |opts|
  11. # opts.banner = "Usage: test.rb [options]"
  12. opts.on("--files file1,file2", Array, "Html files") do |f|
  13. options[:files] = f
  14. end
  15. end
  16.  
  17. begin
  18. optparse.parse!
  19. mandatory = [:files]
  20. missing = mandatory.select{ |param| options[param].nil? }
  21. unless missing.empty?
  22. raise OptionParser::MissingArgument.new(missing.join(', '))
  23. end
  24. rescue OptionParser::InvalidOption, OptionParser::MissingArgument
  25. puts $!.to_s
  26. puts optparse
  27. exit
  28. end
  29.  
  30. options[:files].each do |file|
  31. doc = File.open(file) { |f| Nokogiri::HTML(f) }
  32. doc.search('//script').remove
  33. puts "Write file #{file}\n"
  34. File.write(file, doc.to_html)
  35. end
Add Comment
Please, Sign In to add comment