module RtlThatString

Constants

BDE_CLOSE
BDE_OPEN
BDO_CLOSE
BDO_OPEN

Html wrapping tags www.w3.org/International/questions/qa-bidi-unicode-controls#correspondance

BIDI_END
BIDI_START
LTR_MARK
RTL_MARK

String borders www.w3.org/International/questions/qa-bidi-unicode-controls#pairedcontrols

STRIP_TAGS_REGEX
VERSION

Public Class Methods

included(base) click to toggle source
# File lib/rtl_that_string/rtl_that_string.rb, line 20
def self.included(base)
  @@direction_detector ||= StringDirection::Detector.new
end

Public Instance Methods

with_rtl(options = {}) click to toggle source

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.

# 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 = {}) click to toggle source

Modifies self with the result from ‘with_rtl`

# 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 = {}) click to toggle source

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

# 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

Protected Instance Methods

majority_rtl?() click to toggle source
# File lib/rtl_that_string/rtl_that_string.rb, line 67
def majority_rtl?
  dir_counts = split(' ').each_with_object({}) do |token, h|
    dir = @@direction_detector.direction(token)
    h[dir] ||= 1
    h[dir] += 1
  end

  dir_counts[StringDirection::RTL] > dir_counts[StringDirection::LTR]
end

Private Instance Methods

bidi_html_borders() click to toggle source
# File lib/rtl_that_string/rtl_that_string.rb, line 91
def bidi_html_borders
  prepend(BDE_OPEN).concat(BDE_CLOSE)
end
bidi_string_borders() click to toggle source
# File lib/rtl_that_string/rtl_that_string.rb, line 83
def bidi_string_borders
  prepend(BIDI_START).concat(BIDI_END)
end
rtl_html_borders() click to toggle source
# File lib/rtl_that_string/rtl_that_string.rb, line 87
def rtl_html_borders
  prepend(BDO_OPEN).concat(BDO_CLOSE)
end
rtl_string_borders() click to toggle source
# File lib/rtl_that_string/rtl_that_string.rb, line 79
def rtl_string_borders
  prepend(RTL_MARK).concat(LTR_MARK)
end