about summary refs log tree commit diff
path: root/app/helpers
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2017-06-10 15:06:50 +0200
committerGitHub <noreply@github.com>2017-06-10 15:06:50 +0200
commit8015fd76003419bd3ccca585ffe4416312800fe0 (patch)
treef1995858c87e85827e2ef3267aa61d3f2bed6f8b /app/helpers
parent4919b89ab83f8eee43b89ff4786eae46bf2fa30e (diff)
Improve RTL detection (#3682)
- Use plaintext
- Strip out URLs
- Strip out mentions
- Strip out hashtags
- Strip out whitespace from "overall" count
- Consistent between JS and Ruby
Diffstat (limited to 'app/helpers')
-rw-r--r--app/helpers/stream_entries_helper.rb19
1 files changed, 18 insertions, 1 deletions
diff --git a/app/helpers/stream_entries_helper.rb b/app/helpers/stream_entries_helper.rb
index 275762c87..a17b02128 100644
--- a/app/helpers/stream_entries_helper.rb
+++ b/app/helpers/stream_entries_helper.rb
@@ -47,11 +47,16 @@ module StreamEntriesHelper
     end
   end
 
+  def rtl_status?(status)
+    status.local? ? rtl?(status.text) : rtl?(strip_tags(status.text))
+  end
+
   def rtl?(text)
+    text = simplified_text(text)
     rtl_characters = /[\p{Hebrew}|\p{Arabic}|\p{Syriac}|\p{Thaana}|\p{Nko}]+/m.match(text)
 
     if rtl_characters.present?
-      total_size = text.strip.size.to_f
+      total_size = text.size.to_f
       rtl_size(rtl_characters.to_a) / total_size > 0.3
     else
       false
@@ -60,6 +65,18 @@ module StreamEntriesHelper
 
   private
 
+  def simplified_text(text)
+    text.dup.tap do |new_text|
+      URI.extract(new_text).each do |url|
+        new_text.gsub!(url, '')
+      end
+
+      new_text.gsub!(Account::MENTION_RE, '')
+      new_text.gsub!(Tag::HASHTAG_RE, '')
+      new_text.gsub!(/\s+/, '')
+    end
+  end
+
   def rtl_size(characters)
     characters.reduce(0) { |acc, elem| acc + elem.size }.to_f
   end