(.+?)}m) { |placeholder| placeholder = placeholder.gsub(/\n/, '###BR###') } string.gsub!(%r{
(.+?)
}m) { |placeholder|
placeholder = placeholder.gsub(/\n/, '###BR###')
}
# insert spaces on [A-z]\n[A-z]
string.gsub!(/([A-z])\n([A-z])/m, '\1 \2')
# remove all new lines
string.gsub!(/(\n\r|\r\r\n|\r\n|\n)/, '')
# blockquote handling
string.gsub!(%r{]*)>(.+?)}m) { "\n" + $2.html2text(true).gsub(/^(.*)$/, '> \1') + "\n" } # pre/code handling 2/2 string.gsub!(/###BR###/, "\n") # add counting string.gsub!(/
}im, "\n")
string.gsub!(%r{<(div|p|pre|br|table|tr|h)(|/| [^>]*)>}i, "\n")
string.gsub!(%r{(p|br|div)(|\s.+?)>}i, "\n")
string.gsub!(%r{}i, ' ')
# strip all other tags
string.gsub!(/\<.+?\>/, '')
# replace multiple spaces with one
string.gsub!(/ /, ' ')
# try HTMLEntities, if it fails on invalid signes, use manual way
begin
coder = HTMLEntities.new
string = coder.decode(string)
rescue
# strip all & < > "
string.gsub!('&', '&')
string.gsub!('<', '<')
string.gsub!('>', '>')
string.gsub!('"', '"')
string.gsub!(' ', ' ')
# encode html entities like "–"
string.gsub!(/(&\#(\d+);?)/x) {
$2.chr
}
# encode html entities like "d;"
string.gsub!(/(&\#[xX]([0-9a-fA-F]+);?)/x) {
chr_orig = $1
hex = $2.hex
if hex
chr = hex.chr
if chr
chr_orig = chr
else
chr_orig
end
else
chr_orig
end
# check valid encoding
begin
if !chr_orig.encode('UTF-8').valid_encoding?
chr_orig = '?'
end
rescue
chr_orig = '?'
end
chr_orig
}
end
# remove tailing empty spaces
string.gsub!(/\s+\n$/, "\n")
# remove multiple empty lines
string.gsub!(/\n\n\n/, "\n\n")
string.strip!
# add extracted links
if link_list != ''
string += "\n\n\n" + link_list
end
string.strip
end
=begin
html = text_string.text2html
=end
def text2html
text = CGI.escapeHTML(self)
text.gsub!(/\n/, '
')
text.chomp
end
=begin
html = text_string.text2html
=end
def html2html_strict
string = html2text(true, true)
string = string.text2html
string.gsub!(/######(.+?)######/, '<\1>')
string.chomp
end
end