Module: RtlThatString::Base

Included in:
SafeBuffer, String
Defined in:
lib/rtl_that_string/base.rb

Constant Summary collapse

RTL_START =
"\u202F".freeze
RTL_END =

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

Instance Method Summary collapse

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.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rtl_that_string/base.rb', line 29

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

  case direction
  when StringDirection::RTL
    rtl_string_borders
  when StringDirection::BIDI
    bidi_string_borders
  else
    self
  end
end

#with_rtl!(options = {}) ⇒ Object

Modifies self with the result from ‘with_rtl`



43
44
45
# File 'lib/rtl_that_string/base.rb', line 43

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



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rtl_that_string/base.rb', line 49

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

  case plain_text.direction
  when StringDirection::RTL
    rtl_html_borders
  when StringDirection::BIDI
    bidi_html_borders
  else
    self
  end
end