lib/awestruct/context_helper.rb in awestruct-0.2.11 vs lib/awestruct/context_helper.rb in awestruct-0.2.12
- old
+ new
@@ -1,18 +1,18 @@
require 'hpricot'
module Awestruct
module ContextHelper
-
+
def html_to_text(str)
str.gsub( /<[^>]+>/, '' ).gsub( / /, ' ' )
end
-
+
def clean_html(str)
str.gsub( / /, ' ' )
end
-
+
def without_images(str)
str.gsub(/<img[^>]+>/,'').gsub(/<a[^>]+>([^<]*)<\/a>/, '\1')
end
def close_tags(s)
@@ -47,10 +47,17 @@
link['href'] = fix_url( base_url, link['href'] )
end
doc.search( "//img" ).each do |img|
img['src'] = fix_url( base_url, img['src'] )
end
- return doc.to_s
+ # Hpricot::Doc#to_s output encoding is not necessarily the same as the encoding of text
+ if RUBY_VERSION.start_with? '1.8'
+ doc.to_s
+ else
+ doc.to_s.tap do |d|
+ d.force_encoding(text.encoding) if d.encoding != text.encoding
+ end
+ end
end
def fix_url(base_url, url)
return url unless ( url =~ /^\// )
"#{base_url}#{url}"