I'm wanting to throw a Ruby variable filled with an HTML file that I have grabbed with open-uri and nokogiri, into a backticks system process to tidy it up. The nature of the variable is confusing the process. I am thinking I need to escape it but I am not sure. Any advice appreciated.
require 'open-uri'
require 'nokogiri'
url = 'http://www.wikihow.com/Bathe-a-Cat'
page = Nokogiri::HTML(open(url))
pagestring = page.to_s
result = `tidy --break-before-br no --char-encoding utf8 --clean yes --drop-empty-paras yes ' #{pagestring}'`
puts results.length
Here is the error I get:
sh: -c: line 144: syntax error near unexpected token `"Search","Search","Custom_search"'
sh: -c: line 144: ` <input type="submit" id="cse_sa" value="Search" class="search_button" onmouseover="button_swap(this);" onmouseout="button_unswap(this);" onclick='gatTrack("Search","Search","Custom_search");'>'
Cheers
For normal arguments like file paths and stuff like that, you could use "str".shellescape (http://apidock.com/ruby/Shellwords/shellescape).
args_array = [ ... ]
`tidy #{args_array.map(&:shellescape).join(' ')`
However, to pass a complete html file as an command line argument, something like what was suggested above might be better. I just though I'd mention this here for reference to others for normal cli arguments.
You might want to use IO.popen instead. Then you can invoke the command with an array instead of stringifying it:
cmd = %w{ tidy --break-before-br no --char-encoding utf8 --clean yes --drop-empty-paras yes }
result = IO.popen(cmd, 'r+') {|io|
io.puts pagestring
io.close_write
io.read
}
assuming tidy reads HTML from stdin.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With