2013-09-20 06:29:09 +00:00
|
|
|
class String
|
|
|
|
def message_quote
|
2015-05-07 12:10:38 +00:00
|
|
|
quote = split("\n")
|
2013-09-20 06:29:09 +00:00
|
|
|
body_quote = ''
|
|
|
|
quote.each do |line|
|
|
|
|
body_quote = body_quote + '> ' + line + "\n"
|
|
|
|
end
|
|
|
|
body_quote
|
|
|
|
end
|
2015-05-07 10:27:12 +00:00
|
|
|
|
2013-09-20 06:29:09 +00:00
|
|
|
def word_wrap(*args)
|
|
|
|
options = args.extract_options!
|
|
|
|
unless args.blank?
|
|
|
|
options[:line_width] = args[0] || 82
|
|
|
|
end
|
2015-04-27 13:42:53 +00:00
|
|
|
options.reverse_merge!(line_width: 82)
|
2013-09-20 06:29:09 +00:00
|
|
|
|
|
|
|
lines = self
|
|
|
|
lines.split("\n").collect do |line|
|
|
|
|
line.length > options[:line_width] ? line.gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1\n").strip : line
|
|
|
|
end * "\n"
|
|
|
|
end
|
2015-01-09 13:17:34 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
filename = 'Some::Module'.to_filename
|
|
|
|
|
|
|
|
returns
|
|
|
|
'some/module'
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2013-10-07 07:00:26 +00:00
|
|
|
def to_filename
|
2016-01-15 19:40:29 +00:00
|
|
|
camel_cased_word = "#{self}" # rubocop:disable Style/UnneededInterpolation
|
2015-12-16 21:47:10 +00:00
|
|
|
camel_cased_word.gsub(/::/, '/')
|
2016-01-15 17:22:57 +00:00
|
|
|
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
|
|
|
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
|
|
|
.tr('-', '_').downcase
|
2013-10-07 07:00:26 +00:00
|
|
|
end
|
2014-06-01 08:29:58 +00:00
|
|
|
|
2015-06-30 14:45:03 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
filename = 'some/module.rb'.to_classname
|
|
|
|
|
|
|
|
returns
|
|
|
|
'Some::Module'
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def to_classname
|
2016-01-15 19:40:29 +00:00
|
|
|
camel_cased_word = "#{self}" # rubocop:disable Style/UnneededInterpolation
|
2015-06-30 14:45:03 +00:00
|
|
|
camel_cased_word.gsub!(/\.rb$/, '')
|
|
|
|
camel_cased_word.split('/').map(&:camelize).join('::')
|
|
|
|
end
|
|
|
|
|
2014-06-01 08:29:58 +00:00
|
|
|
# because of mysql inno_db limitations, strip 4 bytes utf8 chars (e. g. emojis)
|
|
|
|
# unfortunaly UTF8mb4 will raise other limitaions of max varchar and lower index sizes
|
|
|
|
# More details: http://pjambet.github.io/blog/emojis-and-mysql/
|
|
|
|
def utf8_to_3bytesutf8
|
2016-01-19 22:30:23 +00:00
|
|
|
return self if Rails.application.config.db_4bytes_utf8
|
2015-05-07 12:10:38 +00:00
|
|
|
each_char.select {|c|
|
2014-06-01 08:29:58 +00:00
|
|
|
if c.bytes.count > 3
|
2015-07-03 15:18:01 +00:00
|
|
|
Rails.logger.warn "strip out 4 bytes utf8 chars '#{c}' of '#{self}'"
|
2014-06-01 08:29:58 +00:00
|
|
|
next
|
|
|
|
end
|
|
|
|
c
|
|
|
|
}
|
2016-01-15 17:22:57 +00:00
|
|
|
.join('')
|
2014-06-01 08:29:58 +00:00
|
|
|
end
|
2014-12-27 15:25:58 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
text = html_string.html2text
|
|
|
|
|
2015-01-09 13:17:34 +00:00
|
|
|
returns
|
|
|
|
|
2015-09-02 07:14:48 +00:00
|
|
|
'string with text only'
|
2015-01-09 13:17:34 +00:00
|
|
|
|
2014-12-27 15:25:58 +00:00
|
|
|
=end
|
|
|
|
|
2016-06-21 15:14:15 +00:00
|
|
|
def html2text(string_only = false, strict = false)
|
2016-01-15 19:40:29 +00:00
|
|
|
string = "#{self}" # rubocop:disable Style/UnneededInterpolation
|
2015-01-08 14:27:44 +00:00
|
|
|
|
|
|
|
# in case of invalid encodeing, strip invalid chars
|
|
|
|
# see also test/fixtures/mail21.box
|
|
|
|
# note: string.encode!('UTF-8', 'UTF-8', :invalid => :replace, :replace => '?') was not detecting invalid chars
|
|
|
|
if !string.valid_encoding?
|
2015-05-07 10:25:16 +00:00
|
|
|
string = string.chars.select(&:valid_encoding?).join
|
2014-12-27 15:25:58 +00:00
|
|
|
end
|
|
|
|
|
2015-01-08 14:27:44 +00:00
|
|
|
# find <a href=....> and replace it with [x]
|
|
|
|
link_list = ''
|
|
|
|
counter = 0
|
2015-09-02 07:14:48 +00:00
|
|
|
if !string_only
|
2016-06-03 13:25:06 +00:00
|
|
|
string.gsub!(/<a\s.*?href=("|')(.+?)("|').*?>/ix) {
|
2015-09-02 07:14:48 +00:00
|
|
|
link = $2
|
2016-01-15 17:22:57 +00:00
|
|
|
counter = counter + 1
|
2015-09-02 07:14:48 +00:00
|
|
|
link_list += "[#{counter}] #{link}\n"
|
|
|
|
"[#{counter}] "
|
|
|
|
}
|
2016-06-21 15:14:15 +00:00
|
|
|
else
|
|
|
|
string.gsub!(%r{<a\s+href=("|')(.+?)("|')(\s*|\s+[^>]*)>(.+?)<\s*/a\s*>}mxi) {|_placeholder|
|
|
|
|
link = $2
|
|
|
|
if !link.empty?
|
|
|
|
link.strip!
|
|
|
|
end
|
|
|
|
text = $5
|
|
|
|
if !text.empty?
|
|
|
|
text.strip!
|
|
|
|
end
|
|
|
|
placeholder = if !link.empty? && text.empty?
|
|
|
|
link
|
|
|
|
elsif link.empty? && !text.empty?
|
|
|
|
text
|
|
|
|
elsif !link.empty? && !text.empty? && (link.downcase == text.downcase || link.downcase == "mailto:#{text}".downcase || link.downcase == "http://#{text}".downcase)
|
|
|
|
text
|
|
|
|
else
|
|
|
|
"#{text} (#{link})"
|
|
|
|
end
|
|
|
|
}
|
2015-09-02 07:14:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# remove style tags with content
|
2016-06-03 13:25:06 +00:00
|
|
|
string.gsub!(%r{<style(|\s.+?)>(.+?)</style>}im, '')
|
|
|
|
|
2015-01-08 14:27:44 +00:00
|
|
|
# remove empty lines
|
2016-06-22 12:55:23 +00:00
|
|
|
string.gsub!(/^[[:space:]]*/m, '')
|
2016-06-21 15:14:15 +00:00
|
|
|
if strict
|
|
|
|
string.gsub!(%r{< \s* (/*) \s* (b|i|ul|ol|li|u|h1|h2|h3|hr) (\s*|\s+[^>]*) >}mxi, '######\1\2######')
|
|
|
|
end
|
2015-01-08 14:27:44 +00:00
|
|
|
|
|
|
|
# pre/code handling 1/2
|
2016-06-03 13:25:06 +00:00
|
|
|
string.gsub!(%r{<pre>(.+?)</pre>}m) { |placeholder|
|
2015-04-27 13:20:16 +00:00
|
|
|
placeholder = placeholder.gsub(/\n/, '###BR###')
|
2015-01-08 14:27:44 +00:00
|
|
|
}
|
2016-06-03 13:25:06 +00:00
|
|
|
string.gsub!(%r{<code>(.+?)</code>}m) { |placeholder|
|
2015-04-27 13:20:16 +00:00
|
|
|
placeholder = placeholder.gsub(/\n/, '###BR###')
|
2015-01-08 14:27:44 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 22:27:34 +00:00
|
|
|
# insert spaces on [A-z]\n[A-z]
|
2016-06-22 12:55:23 +00:00
|
|
|
string.gsub!(/([A-z])[[:space:]]([A-z])/m, '\1 \2')
|
2016-01-13 22:27:34 +00:00
|
|
|
|
2015-01-08 14:27:44 +00:00
|
|
|
# remove all new lines
|
2015-09-02 07:14:48 +00:00
|
|
|
string.gsub!(/(\n\r|\r\r\n|\r\n|\n)/, '')
|
|
|
|
|
|
|
|
# blockquote handling
|
2016-06-03 13:25:06 +00:00
|
|
|
string.gsub!(%r{<blockquote(| [^>]*)>(.+?)</blockquote>}m) {
|
2015-09-02 07:26:40 +00:00
|
|
|
"\n" + $2.html2text(true).gsub(/^(.*)$/, '> \1') + "\n"
|
2015-09-02 07:14:48 +00:00
|
|
|
}
|
2015-01-08 14:27:44 +00:00
|
|
|
|
|
|
|
# pre/code handling 2/2
|
2016-06-03 13:25:06 +00:00
|
|
|
string.gsub!(/###BR###/, "\n")
|
2015-01-08 14:27:44 +00:00
|
|
|
|
|
|
|
# add counting
|
|
|
|
string.gsub!(/<li(| [^>]*)>/i, "\n* ")
|
|
|
|
|
|
|
|
# add hr
|
2015-09-02 07:14:48 +00:00
|
|
|
string.gsub!(%r{<hr(|/| [^>]*)>}i, "\n___\n")
|
|
|
|
|
|
|
|
# add h\d
|
|
|
|
string.gsub!(%r{</h\d>}i, "\n")
|
2015-01-08 14:27:44 +00:00
|
|
|
|
|
|
|
# add new lines
|
2016-06-03 13:25:06 +00:00
|
|
|
string.gsub!(%r{</div><div(|\s.+?)>}im, "\n")
|
|
|
|
string.gsub!(%r{</p><p(|\s.+?)>}im, "\n")
|
2016-06-21 15:14:15 +00:00
|
|
|
string.gsub!(%r{<(div|p|pre|br|table|tr|h)(|/| [^>]*)>}i, "\n")
|
|
|
|
string.gsub!(%r{</(p|br|div)(|\s.+?)>}i, "\n")
|
2016-06-03 13:25:06 +00:00
|
|
|
string.gsub!(%r{</td>}i, ' ')
|
2015-01-08 14:27:44 +00:00
|
|
|
|
|
|
|
# strip all other tags
|
2016-06-03 13:25:06 +00:00
|
|
|
string.gsub!(/\<.+?\>/, '')
|
2015-01-08 14:27:44 +00:00
|
|
|
|
2015-09-14 23:27:14 +00:00
|
|
|
# replace multiple spaces with one
|
2015-09-02 07:14:48 +00:00
|
|
|
string.gsub!(/ /, ' ')
|
|
|
|
|
2016-02-03 13:33:03 +00:00
|
|
|
# try HTMLEntities, if it fails on invalid signes, use manual way
|
|
|
|
begin
|
|
|
|
coder = HTMLEntities.new
|
|
|
|
string = coder.decode(string)
|
|
|
|
rescue
|
|
|
|
|
|
|
|
# strip all & < > "
|
2016-06-03 13:25:06 +00:00
|
|
|
string.gsub!('&', '&')
|
|
|
|
string.gsub!('<', '<')
|
|
|
|
string.gsub!('>', '>')
|
|
|
|
string.gsub!('"', '"')
|
|
|
|
string.gsub!(' ', ' ')
|
2016-02-03 13:33:03 +00:00
|
|
|
|
|
|
|
# encode html entities like "–"
|
2016-06-03 13:25:06 +00:00
|
|
|
string.gsub!(/(&\#(\d+);?)/x) {
|
2016-02-03 13:33:03 +00:00
|
|
|
$2.chr
|
|
|
|
}
|
2015-01-08 14:27:44 +00:00
|
|
|
|
2016-02-03 13:33:03 +00:00
|
|
|
# encode html entities like "d;"
|
2016-06-03 13:25:06 +00:00
|
|
|
string.gsub!(/(&\#[xX]([0-9a-fA-F]+);?)/x) {
|
2016-02-03 13:33:03 +00:00
|
|
|
chr_orig = $1
|
|
|
|
hex = $2.hex
|
|
|
|
if hex
|
|
|
|
chr = hex.chr
|
|
|
|
if chr
|
|
|
|
chr_orig = chr
|
|
|
|
else
|
|
|
|
chr_orig
|
|
|
|
end
|
2015-01-08 14:27:44 +00:00
|
|
|
else
|
|
|
|
chr_orig
|
|
|
|
end
|
|
|
|
|
2016-02-03 13:33:03 +00:00
|
|
|
# check valid encoding
|
|
|
|
begin
|
|
|
|
if !chr_orig.encode('UTF-8').valid_encoding?
|
|
|
|
chr_orig = '?'
|
|
|
|
end
|
|
|
|
rescue
|
2015-01-08 14:27:44 +00:00
|
|
|
chr_orig = '?'
|
|
|
|
end
|
2016-02-03 13:33:03 +00:00
|
|
|
chr_orig
|
|
|
|
}
|
|
|
|
end
|
2015-01-08 14:27:44 +00:00
|
|
|
|
|
|
|
# remove tailing empty spaces
|
2016-06-22 12:55:23 +00:00
|
|
|
string.gsub!(/[[:blank:]]+$/, '')
|
2015-09-02 07:14:48 +00:00
|
|
|
|
2015-01-08 14:27:44 +00:00
|
|
|
# add extracted links
|
|
|
|
if link_list != ''
|
2015-09-02 07:14:48 +00:00
|
|
|
string += "\n\n\n" + link_list
|
2014-12-27 15:25:58 +00:00
|
|
|
end
|
2015-01-08 14:27:44 +00:00
|
|
|
|
2016-06-22 12:55:23 +00:00
|
|
|
# remove double multiple empty lines
|
|
|
|
string.gsub!(/\n\n\n/, "\n\n")
|
|
|
|
|
2015-01-08 14:27:44 +00:00
|
|
|
string.strip
|
2014-12-27 15:25:58 +00:00
|
|
|
end
|
2015-01-03 22:53:07 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
html = text_string.text2html
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def text2html
|
2016-01-15 19:40:29 +00:00
|
|
|
text = CGI.escapeHTML(self)
|
2015-01-03 22:53:07 +00:00
|
|
|
text.gsub!(/\n/, '<br>')
|
|
|
|
text.chomp
|
|
|
|
end
|
|
|
|
|
2016-06-21 15:14:15 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
html = text_string.text2html
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def html2html_strict
|
|
|
|
string = html2text(true, true)
|
|
|
|
string = string.text2html
|
|
|
|
string.gsub!(/######(.+?)######/, '<\1>')
|
|
|
|
string.chomp
|
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|