direction_detector.rb 660 Bytes
Newer Older
MrZYX's avatar
MrZYX committed
1
# coding: utf-8
2 3
# frozen_string_literal: true

danielgrippi's avatar
danielgrippi committed
4
#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
MrZYX's avatar
MrZYX committed
5 6 7 8
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

class String
9 10 11
  RTL_CLEANER_REGEXES = [ /@[^ ]+|#[^ ]+/u, # mention, tag
      /^RT[: ]{1}| RT | RT: |[♺♻:]/u # retweet
    ]
MrZYX's avatar
MrZYX committed
12 13 14

  def is_rtl?
    return false if self.strip.empty?
15 16
    detector = StringDirection::Detector.new(:dominant)
    detector.rtl? self
MrZYX's avatar
MrZYX committed
17 18 19 20 21
  end

  # Diaspora specific
  def cleaned_is_rtl?
    string = String.new(self)
22
    RTL_CLEANER_REGEXES.each do |cleaner|
MrZYX's avatar
MrZYX committed
23 24 25 26 27
      string.gsub!(cleaner, '')
    end
    string.is_rtl?
  end
end