0

I'm working with the wkhtmltoimage utility which does not support protocol relative URLs such as this: <script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/1.4.5/numeral.min.js"></script>

I need to convert it to: <script src="http://cdnjs.cloudflare.com/ajax/libs/numeral.js/1.4.5/numeral.min.js"></script> in Ruby. I'm wondering what the most efficient way of finding these type of URLs as I can't figure out how to write a regex for them and then prepend http: to it.

Thanks in advance.

kartikluke
  • 2,375
  • 1
  • 19
  • 32

2 Answers2

0

You can use perl for that :

perl -pi -e 's/src="\/\//src="http:\/\//' *

This statement above will replace all patterns found in a directory.

Alain Beauvois
  • 5,896
  • 3
  • 44
  • 26
0

There's no need to use Regexps here, just use an HTML parser for the HTML and a URI parser for the URI:

require 'nokogiri'
require 'uri'

doc = Nokogiri.HTML(<<-EOHTML)
  <script 
    src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/1.4.5/numeral.min.js">
  </script>
EOHTML

doc.xpath('//script[@src]').each do |script|
  uri = URI.parse(script['src'])  
  uri.scheme = 'http' if uri.scheme.nil?  
  script['src'] = uri.to_s
end
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653