rtl_that_string 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d7752966a23a6506f024aae2c5478d7d061e3dfb
4
- data.tar.gz: 802bfd4e86b754ca146aa6b6a0498ee3cfa8b313
3
+ metadata.gz: 37d8425c91b11b96fac7e448aaef45b07c954639
4
+ data.tar.gz: 4061e2e831cd68a9395a87fde0d22bba1a23186a
5
5
  SHA512:
6
- metadata.gz: 23429f639ae92867ee120f567dd5c1f389a103d4a53cc298232be0e678ccf97c7c81b896a6bda811962d77d959f26401a800aa177a303d80a6bf32f7a4ff4034
7
- data.tar.gz: 6a78923725d71f68020cb966846b171c77756b9259cd04cfd812cf2a4d1f53798dd191388289b646803c1e7218152920de565004a2731876cef52fb6f2547324
6
+ metadata.gz: a999b66c40e1cedd1b0e98c9e46a871044487bfcdd703e043cb1e41bedcb1a0560310442bcf39ae4fc6d9bc9e972c8b5673e690464338a15d263f16ed671a73a
7
+ data.tar.gz: 763937007cf71f84ec3fdaee731de28fa86e72e1ea48108dc263d66cd36c9b71d4444945e86a4e7fb2088ee67c8087ecc919a5aea9319ac9c0b3f1664dddb276
data/Rakefile CHANGED
@@ -1,5 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
+ require "bump/tasks"
3
4
 
4
5
  RSpec::Core::RakeTask.new(:spec)
5
6
 
@@ -0,0 +1,90 @@
1
+ require 'string-direction'
2
+
3
+ module RtlThatString
4
+ # String borders
5
+ # http://www.w3.org/International/questions/qa-bidi-unicode-controls#pairedcontrols
6
+ RTL_MARK = "\u202F".freeze # RIGHT-TO-LEFT MARK
7
+ LTR_MARK = "\u202E".freeze # LEFT-TO-RIGHT MARK
8
+ BIDI_START = "\u202B".freeze # RIGHT-TO-LEFT EMBEDDING (RLE)
9
+ BIDI_END = "\u202C".freeze # POP DIRECTIONAL FORMATTING (PDF)
10
+
11
+ # Html wrapping tags
12
+ # http://www.w3.org/International/questions/qa-bidi-unicode-controls#correspondance
13
+ BDO_OPEN = '<bdo dir="rtl">'.freeze
14
+ BDO_CLOSE = '</bdo>'.freeze
15
+ BDE_OPEN = '<bde dir="rtl">'.freeze
16
+ BDE_CLOSE = '</bde>'.freeze
17
+
18
+ STRIP_TAGS_REGEX = /<.*?>/.freeze # Strip any html tag, with or without attributes
19
+
20
+ # Wrap plain text or html in direction control unicode characters or tags
21
+ #
22
+ # If the string is detected as all RTL characters it will be bordered with
23
+ # directional override characters. If it has mixed RTL and LTR it will get
24
+ # wrapped with bi-directional override characters.
25
+ #
26
+ # If the `html: true` option is passed the string will get wrapped in either
27
+ # a <bdo></bdo> or <bde></bde> tag.
28
+ def with_rtl(options = {})
29
+ return with_rtl_html(options) if options.delete(:html)
30
+
31
+ case direction
32
+ when StringDirection::RTL
33
+ rtl_string_borders
34
+ when StringDirection::BIDI
35
+ majority_rtl? ? bidi_string_borders : self
36
+ else
37
+ self
38
+ end
39
+ end
40
+
41
+ # Modifies self with the result from `with_rtl`
42
+ def with_rtl!(options = {})
43
+ self.replace with_rtl(options)
44
+ end
45
+
46
+ # Evaluate the direction of text in an HTML blob by first removing the HTML
47
+ # tags, then checking the plain text, then returning a wrapped blob of HTML
48
+ def with_rtl_html(options = {})
49
+ plain_text = self.gsub(STRIP_TAGS_REGEX, '')
50
+
51
+ case plain_text.direction
52
+ when StringDirection::RTL
53
+ rtl_html_borders
54
+ when StringDirection::BIDI
55
+ plain_text.majority_rtl? ? bidi_html_borders : self
56
+ else
57
+ self
58
+ end
59
+ end
60
+
61
+ protected
62
+
63
+ def majority_rtl?
64
+ dir_counts = split(' ').each_with_object({}) do |token, h|
65
+ dir = token.direction
66
+ h[dir] ||= 1
67
+ h[dir] += 1
68
+ end
69
+
70
+ dir_counts[StringDirection::RTL] > dir_counts[StringDirection::LTR]
71
+ end
72
+
73
+ private
74
+
75
+ def rtl_string_borders
76
+ prepend(RTL_MARK).concat(LTR_MARK)
77
+ end
78
+
79
+ def bidi_string_borders
80
+ prepend(BIDI_START).concat(BIDI_END)
81
+ end
82
+
83
+ def rtl_html_borders
84
+ prepend(BDO_OPEN).concat(BDO_CLOSE)
85
+ end
86
+
87
+ def bidi_html_borders
88
+ prepend(BDE_OPEN).concat(BDE_CLOSE)
89
+ end
90
+ end
@@ -1,3 +1,3 @@
1
1
  module RtlThatString
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,6 +1,4 @@
1
1
  require 'rtl_that_string/version'
2
- require 'rtl_that_string/string'
3
- require 'rtl_that_string/safe_buffer' if defined?(ActiveSupport)
2
+ require 'rtl_that_string/rtl_that_string'
4
3
 
5
- String.send :include, RtlThatString::String
6
- ActiveSupport::SafeBuffer.send :include, RtlThatString::SafeBuffer if defined?(ActiveSupport)
4
+ String.send :include, RtlThatString
@@ -30,4 +30,5 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency "bundler", "~> 1.10"
31
31
  spec.add_development_dependency "rake", "~> 10.0"
32
32
  spec.add_development_dependency "rspec"
33
+ spec.add_development_dependency "bump"
33
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rtl_that_string
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Craig Day
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-09-04 00:00:00.000000000 Z
11
+ date: 2015-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bump
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: Small library to wrap text/html in RTL markers/tags
56
70
  email:
57
71
  - cday@zendesk.com
@@ -69,9 +83,7 @@ files:
69
83
  - bin/console
70
84
  - bin/setup
71
85
  - lib/rtl_that_string.rb
72
- - lib/rtl_that_string/base.rb
73
- - lib/rtl_that_string/safe_buffer.rb
74
- - lib/rtl_that_string/string.rb
86
+ - lib/rtl_that_string/rtl_that_string.rb
75
87
  - lib/rtl_that_string/version.rb
76
88
  - rtl_that_string.gemspec
77
89
  homepage: https://github.com/craig-day/
@@ -1,80 +0,0 @@
1
- require 'string-direction'
2
-
3
- module RtlThatString
4
- module Base
5
- # String borders
6
- # http://www.w3.org/International/questions/qa-bidi-unicode-controls#pairedcontrols
7
- RTL_START = "\u202F".freeze # RIGHT-TO-LEFT MARK
8
- RTL_END = "\u202E".freeze # LEFT-TO-RIGHT MARK
9
- BIDI_START = "\u202B".freeze # RIGHT-TO-LEFT EMBEDDING (RLE)
10
- BIDI_END = "\u202C".freeze # POP DIRECTIONAL FORMATTING (PDF)
11
-
12
- # Html wrapping tags
13
- # http://www.w3.org/International/questions/qa-bidi-unicode-controls#correspondance
14
- BDO_OPEN = '<bdo dir="rtl">'.freeze
15
- BDO_CLOSE = '</bdo>'.freeze
16
- BDE_OPEN = '<bde dir="rtl">'.freeze
17
- BDE_CLOSE = '</bde>'.freeze
18
-
19
- STRIP_TAGS_REGEX = /<.*?>/.freeze # Strip any html tag, with or without attributes
20
-
21
- # Wrap plain text or html in direction control unicode characters or tags
22
- #
23
- # If the string is detected as all RTL characters it will be bordered with
24
- # directional override characters. If it has mixed RTL and LTR it will get
25
- # wrapped with bi-directional override characters.
26
- #
27
- # If the `html: true` option is passed the string will get wrapped in either
28
- # a <bdo></bdo> or <bde></bde> tag.
29
- def with_rtl(options = {})
30
- return with_rtl_html(options) if options.delete(:html)
31
-
32
- case direction
33
- when StringDirection::RTL
34
- rtl_string_borders
35
- when StringDirection::BIDI
36
- bidi_string_borders
37
- else
38
- self
39
- end
40
- end
41
-
42
- # Modifies self with the result from `with_rtl`
43
- def with_rtl!(options = {})
44
- self.replace with_rtl(options)
45
- end
46
-
47
- # Evaluate the direction of text in an HTML blob by first removing the HTML
48
- # tags, then checking the plain text, then returning a wrapped blob of HTML
49
- def with_rtl_html(options = {})
50
- plain_text = self.gsub(STRIP_TAGS_REGEX, '')
51
-
52
- case plain_text.direction
53
- when StringDirection::RTL
54
- rtl_html_borders
55
- when StringDirection::BIDI
56
- bidi_html_borders
57
- else
58
- self
59
- end
60
- end
61
-
62
- private
63
-
64
- def rtl_string_borders
65
- raise "must be implemented by submodule"
66
- end
67
-
68
- def bidi_string_borders
69
- raise "must be implemented by submodule"
70
- end
71
-
72
- def rtl_html_borders
73
- raise "must be implemented by submodule"
74
- end
75
-
76
- def bidi_html_borders
77
- raise "must be implemented by submodule"
78
- end
79
- end
80
- end
@@ -1,25 +0,0 @@
1
- require_relative './base'
2
-
3
- module RtlThatString
4
- module SafeBuffer
5
- include RtlThatString::Base
6
-
7
- private
8
-
9
- def rtl_string_borders
10
- prepend(RTL_START).concat(RTL_END)
11
- end
12
-
13
- def bidi_string_borders
14
- prepend(BIDI_START).concat(BIDI_END)
15
- end
16
-
17
- def rtl_html_borders
18
- prepend(BDO_OPEN).concat(BDO_CLOSE)
19
- end
20
-
21
- def bidi_html_borders
22
- prepend(BDE_OPEN).concat(BDE_CLOSE)
23
- end
24
- end
25
- end
@@ -1,25 +0,0 @@
1
- require_relative './base'
2
-
3
- module RtlThatString
4
- module String
5
- include RtlThatString::Base
6
-
7
- private
8
-
9
- def rtl_string_borders
10
- "#{RTL_START}#{self}#{RTL_END}"
11
- end
12
-
13
- def bidi_string_borders
14
- "#{BIDI_START}#{self}#{BIDI_END}"
15
- end
16
-
17
- def rtl_html_borders
18
- "#{BDO_OPEN}#{self}#{BDO_CLOSE}"
19
- end
20
-
21
- def bidi_html_borders
22
- "#{BDE_OPEN}#{self}#{BDE_CLOSE}"
23
- end
24
- end
25
- end