Module: RtlThatString

Defined in:
lib/rtl_that_string/version.rb,
lib/rtl_that_string/rtl_that_string.rb

Constant Summary collapse

VERSION =
"0.3.0"
RTL_MARK =
"\u202F".freeze
LTR_MARK =

RIGHT-TO-LEFT MARK

"\u202E".freeze
BIDI_START =

LEFT-TO-RIGHT MARK

"\u202B".freeze
BIDI_END =

RIGHT-TO-LEFT EMBEDDING (RLE)

"\u202C".freeze
BDO_OPEN =
'<bdo dir="rtl">'.freeze
BDO_CLOSE =
'</bdo>'.freeze
BDE_OPEN =
'<bde dir="rtl">'.freeze
BDE_CLOSE =
'</bde>'.freeze
STRIP_TAGS_REGEX =

Strip any html tag, with or without attributes

/<.*?>/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



20
21
22
# File 'lib/rtl_that_string/rtl_that_string.rb', line 20

def self.included(base)
  @@direction_detector ||= StringDirection::Detector.new
end

Instance Method Details

#with_rtl(options = {}) ⇒ Object

Wrap plain text or html in direction control unicode characters or tags

If the string is detected as all RTL characters it will be bordered with directional override characters. If it has mixed RTL and LTR it will get wrapped with bi-directional override characters.

If the ‘html: true` option is passed the string will get wrapped in either a <bdo></bdo> or <bde></bde> tag.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rtl_that_string/rtl_that_string.rb', line 32

def with_rtl(options = {})
  return with_rtl_html(options) if options.delete(:html)

  case @@direction_detector.direction(self)
  when StringDirection::RTL
    rtl_string_borders
  when StringDirection::BIDI
    majority_rtl? ? bidi_string_borders : self
  else
    self
  end
end

#with_rtl!(options = {}) ⇒ Object

Modifies self with the result from with_rtl



46
47
48
# File 'lib/rtl_that_string/rtl_that_string.rb', line 46

def with_rtl!(options = {})
  self.replace with_rtl(options)
end

#with_rtl_html(options = {}) ⇒ Object

Evaluate the direction of text in an HTML blob by first removing the HTML tags, then checking the plain text, then returning a wrapped blob of HTML



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rtl_that_string/rtl_that_string.rb', line 52

def with_rtl_html(options = {})
  plain_text = self.gsub(STRIP_TAGS_REGEX, '')

  case @@direction_detector.direction(plain_text)
  when StringDirection::RTL
    rtl_html_borders
  when StringDirection::BIDI
    plain_text.majority_rtl? ? bidi_html_borders : self
  else
    self
  end
end