def append_query_string(doc, qs)
return doc if qs.nil?
qs.to_s.gsub!(/^[\?]*/, '').strip!
return doc if qs.empty?
begin
current_host = @base_url.host
rescue
current_host = nil
end
$stderr.puts "Attempting to append_query_string: #{qs}" if @options[:verbose]
doc.search('a').each do|el|
href = el.attributes['href'].to_s.strip
next if href.nil? or href.empty?
next if href[0,1] =~ /[\#\{\[\<\%]/
begin
href = URI.parse(href)
if current_host and href.host != nil and href.host != current_host
$stderr.puts "Skipping append_query_string for: #{href.to_s} because host is no good" if @options[:verbose]
next
end
if href.scheme and href.scheme != 'http' and href.scheme != 'https'
puts "Skipping append_query_string for: #{href.to_s} because scheme is no good" if @options[:verbose]
next
end
if href.query and not href.query.empty?
amp = @options[:unescaped_ampersand] ? '&' : '&'
href.query = href.query + amp + qs
else
href.query = qs
end
el['href'] = href.to_s
rescue URI::Error => e
$stderr.puts "Skipping append_query_string for: #{href.to_s} (#{e.message})" if @options[:verbose]
next
end
end
doc
end