(.+?)}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])[[:space:]]([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)(|[[:space:]].+?)>}i, "\n")
string.gsub!(%r{}i, ' ')
# strip all other tags
string.gsub!(/\<.+?\>/, '')
# replace multiple spaces with one
string.gsub!(/ /, ' ')
# add hyperlinks
if strict
string.gsub!(%r{([[:space:]])((http|https|ftp|tel)://.+?|(www..+?))([[:space:]]|\.[[:space:]]|,[[:space:]])}mxi) { |_placeholder|
pre = $1
content = $2
post = $5
if content =~ /^www/i
content = "http://#{content}"
end
placeholder = if content =~ /^(http|https|ftp|tel)/i
"#{pre}######LINKRAW:#{content}#######{post}"
else
"#{pre}#{content}#{post}"
end
}
end
# 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!(/[[:blank:]]+$/, '')
# remove double multiple empty lines
string.gsub!(/\n\n\n+/, "\n\n")
# add extracted links
if link_list != ''
string += "\n\n\n" + link_list
end
# remove double multiple empty lines
string.gsub!(/\n\n\n+/, "\n\n")
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 = "#{self}" # rubocop:disable Style/UnneededInterpolation
string = HtmlSanitizer.cleanup(string).strip
string = HtmlSanitizer.strict(string, true).strip
# as fallback, use html2text and text2html
if string.blank?
string = html2text.text2html
string.signature_identify('text')
marker_template = ''
string.sub!(/######SIGNATURE_MARKER######/, marker_template)
string.gsub!(/######SIGNATURE_MARKER######/, '')
return string.chomp
end
string.gsub!(%r{(
[[:space:]]*
([[:space:]]*)){2,}}im, '\2') string.gsub!(%r\
[[:space:]]*
(
[[:space:]]*
([[:space:]]*
}im, '
') string.gsub!(%r\(
[[:space:]]+
}im, '') string.gsub!(%r{\A(
[[:space:]]*(--|__)',
'(
|
|
[[:space:]]*[[:space:]]*(On|Am|Le|El|Den|Dňa|W dniu|Il|Op|Dne|Dana)[[:space:]]', '[[:space:]]*
[[:space:]]*(On|Am|Le|El|Den|Dňa|W dniu|Il|Op|Dne|Dana)[[:space:]].{1,500}: map['apple-de'] = '^(Am)[[:space:]].{6,20}[[:space:]](um)[[:space:]].{3,10}[[:space:]](schrieb)[[:space:]].{1,250}:' # Thunderbird # Am 04.03.2015 um 12:47 schrieb Alf Aardvark: map['thunderbird-de'] = '^(Am)[[:space:]].{6,20}[[:space:]](um)[[:space:]].{3,10}[[:space:]](schrieb)[[:space:]].{1,250}:' # Thunderbird default - http://kb.mozillazine.org/Reply_header_settings # On 01-01-2007 11:00 AM, Alf Aardvark wrote: map['thunderbird-en-default'] = '^(On)[[:space:]].{6,20}[[:space:]].{3,10},[[:space:]].{1,250}(wrote):' # http://kb.mozillazine.org/Reply_header_settings # Alf Aardvark wrote, on 01-01-2007 11:00 AM: map['thunderbird-en'] = '^.{1,250}[[:space:]](wrote),[[:space:]]on[[:space:]].{3,20}:' # otrs # 25.02.2015 10:26 - edv hotline wrote: # 25.02.2015 10:26 - edv hotline schrieb: map['otrs-en-de'] = '^.{6,10}[[:space:]].{3,10}[[:space:]]-[[:space:]].{1,250}[[:space:]](wrote|schrieb):' # Ms # rubocop:disable Style/AsciiComments # From: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc] # Send: Donnerstag, 2. April 2015 10:00 # To/Cc/Bcc: xxx # Subject: xxx # - or - # From: xxx # To/Cc/Bcc: xxx # Date: 01.04.2015 12:41 # Subject: xxx # - or - # De : xxx # À/?/?: xxx # Envoyé : mercredi 29 avril 2015 17:31 # Objet : xxx # rubocop:enable Style/AsciiComments # en/de/fr | sometimes ms adds a space to "xx : value" map['ms-en-de-fr_from'] = '^(Von|From|De|от|Z|Od|Ze|Fra|Van|Mistä|Από|Dal|から|Из|од|iz|Från|จาก|з|Từ)( ?):[[:space:]].+?' map['ms-en-de-fr_from_html'] = "\n######b######(From|Von|De)([[:space:]]?):([[:space:]]?)(######\/b######)[[:space:]].+?" # word 14 # edv hotline wrote: # edv hotline schrieb: #map['word-en-de'] = "[^#{marker}].{1,250}\s(wrote|schrieb):" map.each { |_key, regexp| begin string.sub!(/#{regexp}/) { |placeholder| placeholder = "#{marker}#{placeholder}" } rescue # regexp was not possible because of some string encoding issue, use next Rails.logger.debug "Invalid string/charset combination with regexp #{regexp} in string" end } string end end