Welcome to mirror list, hosted at ThFree Co, Russian Federation.

direction_detector.rb « lib - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c5aa4575d67a4e60090a03bd3e9b94ffcb98fe77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# coding: utf-8
#   Copyright (c) 2010, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.
# Deeply inspired by https://gitorious.org/statusnet/mainline/blobs/master/plugins/DirectionDetector/DirectionDetectorPlugin.php

class String

  def is_rtl?
    return false if self.strip.empty?
    count = 0
    self.split(" ").each do |word|
      if starts_with_rtl_char?(word)
        count += 1
      else
        count -= 1
      end
    end
    return true if count > 0 # more than half of the words are rtl words
    return starts_with_rtl_char?(self) # otherwise let the first word decide
  end

  # Diaspora specific
  def cleaned_is_rtl?
    string = String.new(self)
    [ /@[^ ]+|#[^ ]+/u, # mention, tag
      /^RT[: ]{1}| RT | RT: |[♺♻:]/u # retweet
    ].each do |cleaner|
      string.gsub!(cleaner, '')
    end
    string.is_rtl?
  end

  def starts_with_rtl_char?(string = self)
    return false if string.strip.empty?
    char = string.strip.unpack('U*').first
    limits = [
      [1536, 1791], # arabic, persian, urdu, kurdish, ...
      [65136, 65279], # arabic peresent 2
      [64336, 65023], # arabic peresent 1
      [1424, 1535], # hebrew
      [64256, 64335], # hebrew peresent
      [1792, 1871], # syriac
      [1920, 1983], # thaana
      [1984, 2047], # nko
      [11568, 11647] # tifinagh
    ]
    limits.each do |limit|
      return true if char >= limit[0] && char <= limit[1]
    end
    return false
  end
end