text_helpers 0.7.1 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of text_helpers might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/.travis.yml +2 -1
- data/CHANGELOG.md +95 -0
- data/Rakefile +1 -0
- data/lib/text_helpers.rb +2 -2
- data/lib/text_helpers/translation.rb +10 -9
- data/lib/text_helpers/version.rb +1 -1
- data/test/lib/text_helpers/translation_test.rb +42 -5
- data/test/lib/text_helpers/version_test.rb +1 -1
- data/text_helpers.gemspec +1 -1
- metadata +9 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: af6ec054fd5d8245c92d4cb73518c2a54237f2a0be69c409eacb3c3a1fb258d5
|
4
|
+
data.tar.gz: 705a6cb053a6497f473d0a50a8782c5f3ffa00352c82a3ae050406e87b8bcec8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0282c042d36297f1d3a7e60db481cfd0253645da0791112a58f89f1b289420e717d66f7e20a6d9a8b6d512720700470c7b452ad889a3f5e816929291c729c7b6'
|
7
|
+
data.tar.gz: 8ad38d3fe70e5ad3f33dbc871f8f5cb2d19c890d76609908bf397db2de7487baa27b4eddd5c8db924f553bdc3adcac7ce6126c64c0d7ade904f1d7b0f7f846ba
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
# Change Log
|
2
|
+
|
3
|
+
## [1.1.1] - 2021-02-02
|
4
|
+
|
5
|
+
### Changes
|
6
|
+
|
7
|
+
- Bump minimum redcarpet version for security (thanks to @n00dle)
|
8
|
+
|
9
|
+
## [1.1.0] - 2020-12-02
|
10
|
+
|
11
|
+
### New Features
|
12
|
+
|
13
|
+
- Add `rel=noopener` to external links (thanks to @n00dle)
|
14
|
+
|
15
|
+
## [1.0.1] - 2020-12-02
|
16
|
+
|
17
|
+
### Changes
|
18
|
+
|
19
|
+
- Support cascading backends by default
|
20
|
+
|
21
|
+
## [1.0.0] - 2020-03-27
|
22
|
+
|
23
|
+
### Changes
|
24
|
+
|
25
|
+
- **(Breaking)** Drop explicit support for Ruby versions below 2.6
|
26
|
+
|
27
|
+
### Bug Fixes
|
28
|
+
|
29
|
+
- Properly support translations using I18n::Pluralization backend (thanks to @jhanggi)
|
30
|
+
|
31
|
+
## [0.7.2] - 2019-07-06
|
32
|
+
|
33
|
+
### Bug Fixes
|
34
|
+
|
35
|
+
- Improve orphaned text matcher targeting
|
36
|
+
|
37
|
+
## [0.7.1] - 2019-07-05
|
38
|
+
|
39
|
+
### Bug Fixes
|
40
|
+
|
41
|
+
- Prevent unnecessary non-breaking spaces in generated HTML
|
42
|
+
|
43
|
+
### Changes
|
44
|
+
|
45
|
+
- Improve `NotImplementedError` messaging (thanks to @stephen-puiszis)
|
46
|
+
- Update README with spec setup instructions (thanks to @jhanggi)
|
47
|
+
|
48
|
+
## [0.7.0] - 2016-05-06
|
49
|
+
|
50
|
+
### New features
|
51
|
+
|
52
|
+
- Automatically load entire locale directory via Railtie
|
53
|
+
|
54
|
+
## [0.6.1] - 2015-10-01
|
55
|
+
|
56
|
+
### Changes
|
57
|
+
|
58
|
+
- Bump version requirement for Redcarpet gem to address memory leaks
|
59
|
+
|
60
|
+
## [0.6.0] - 2015-06-15
|
61
|
+
|
62
|
+
### New Features
|
63
|
+
|
64
|
+
- Apply `target=_blank` attribute to anchor tags pointed at external URLs
|
65
|
+
|
66
|
+
## [0.5.3] - 2015-02-25
|
67
|
+
|
68
|
+
### Changes
|
69
|
+
|
70
|
+
- Update spec helpers for compatibility with RSpec 3 (thanks to @gabrielg)
|
71
|
+
|
72
|
+
## [0.5.2] - 2015-02-03
|
73
|
+
|
74
|
+
### Changes
|
75
|
+
|
76
|
+
- Support cascading backends for interpolated keys (thanks to @gabrielg)
|
77
|
+
|
78
|
+
## [0.5.1] - 2015-01-23
|
79
|
+
|
80
|
+
### New Features
|
81
|
+
|
82
|
+
- Add RSpec helpers (thanks to @gabrielg)
|
83
|
+
|
84
|
+
[1.1.1]: https://github.com/ahorner/text-helpers/compare/v1.1.0...v1.1.1
|
85
|
+
[1.1.0]: https://github.com/ahorner/text-helpers/compare/v1.0.1...v1.1.0
|
86
|
+
[1.0.1]: https://github.com/ahorner/text-helpers/compare/v1.0.0...v1.0.1
|
87
|
+
[1.0.0]: https://github.com/ahorner/text-helpers/compare/v0.7.2...v1.0.0
|
88
|
+
[0.7.2]: https://github.com/ahorner/text-helpers/compare/v0.7.1...v0.7.2
|
89
|
+
[0.7.1]: https://github.com/ahorner/text-helpers/compare/v0.7.0...v0.7.1
|
90
|
+
[0.7.0]: https://github.com/ahorner/text-helpers/compare/v0.6.1...v0.7.0
|
91
|
+
[0.6.1]: https://github.com/ahorner/text-helpers/compare/v0.6.0...v0.6.1
|
92
|
+
[0.6.0]: https://github.com/ahorner/text-helpers/compare/v0.5.3...v0.6.0
|
93
|
+
[0.5.3]: https://github.com/ahorner/text-helpers/compare/v0.5.2...v0.5.3
|
94
|
+
[0.5.2]: https://github.com/ahorner/text-helpers/compare/v0.5.1...v0.5.2
|
95
|
+
[0.5.1]: https://github.com/ahorner/text-helpers/compare/v0.5.0...v0.5.1
|
data/Rakefile
CHANGED
data/lib/text_helpers.rb
CHANGED
@@ -5,10 +5,10 @@ require "text_helpers/railtie" if defined?(Rails)
|
|
5
5
|
module TextHelpers
|
6
6
|
# RaiseExceptionHandler just raises all exceptions, rather than swallowing
|
7
7
|
# MissingTranslation ones. It's cribbed almost verbatim from
|
8
|
-
#
|
8
|
+
# https://guides.rubyonrails.org/i18n.html#using-different-exception-handlers.
|
9
9
|
class RaiseExceptionHandler < I18n::ExceptionHandler
|
10
10
|
def call(exception, locale, key, options)
|
11
|
-
if exception.is_a?(I18n::MissingTranslation)
|
11
|
+
if exception.is_a?(I18n::MissingTranslation) && key.to_s != "i18n.plural.rule"
|
12
12
|
raise exception.to_exception
|
13
13
|
else
|
14
14
|
super
|
@@ -12,7 +12,7 @@ module TextHelpers
|
|
12
12
|
attributes = [
|
13
13
|
("href=\"#{link}\"" if link),
|
14
14
|
("title=\"#{title}\"" if title),
|
15
|
-
("target=\"_blank\"" if link
|
15
|
+
("target=\"_blank\" rel=\"noopener\"" if link.match?(PROTOCOL_MATCHER)),
|
16
16
|
]
|
17
17
|
|
18
18
|
"<a #{attributes.compact.join(" ")}>#{content}</a>"
|
@@ -22,7 +22,8 @@ module TextHelpers
|
|
22
22
|
|
23
23
|
module Translation
|
24
24
|
|
25
|
-
ORPHAN_MATCHER = /[ \t](?![^<]*>)(\S+\s*<\/(?:p|li)>)/.freeze
|
25
|
+
ORPHAN_MATCHER = /(\w+)[ \t](?![^<]*>)(\S+\s*<\/(?:p|li)>)/.freeze
|
26
|
+
KEYPATH_MATCHER = /!([\w.\/]+)!/.freeze
|
26
27
|
|
27
28
|
# Public: Get the I18n localized text for the passed key.
|
28
29
|
#
|
@@ -34,17 +35,17 @@ module TextHelpers
|
|
34
35
|
# Returns a String resulting from the I18n lookup.
|
35
36
|
def text(key, options = {})
|
36
37
|
options = html_safe_options(options)
|
37
|
-
text = I18n.t(key, {
|
38
|
+
text = I18n.t(key, **{
|
38
39
|
scope: self.translation_scope,
|
39
|
-
default: "!#{key}!"
|
40
|
+
default: "!#{key}!",
|
41
|
+
cascade: true,
|
40
42
|
}.merge(options)).strip
|
41
43
|
|
42
|
-
interpolation_options = options
|
43
|
-
interpolation_options[:cascade] = true unless interpolation_options.has_key?(:cascade)
|
44
|
+
interpolation_options = { cascade: true }.merge(options)
|
44
45
|
|
45
46
|
# Interpolate any keypaths (e.g., `!some.lookup.path/key!`) found in the text.
|
46
|
-
while text
|
47
|
-
text = text.gsub(
|
47
|
+
while text.match?(KEYPATH_MATCHER) do
|
48
|
+
text = text.gsub(KEYPATH_MATCHER) { |match| I18n.t($1, **interpolation_options) }
|
48
49
|
end
|
49
50
|
|
50
51
|
text = smartify(text) if options.fetch(:smart, true)
|
@@ -65,7 +66,7 @@ module TextHelpers
|
|
65
66
|
def html(key, options = {})
|
66
67
|
rendered = markdown(text(key, options.merge(smart: false)))
|
67
68
|
|
68
|
-
rendered = options[:orphans] ? rendered : rendered.gsub(ORPHAN_MATCHER, ' \
|
69
|
+
rendered = options[:orphans] ? rendered : rendered.gsub(ORPHAN_MATCHER, '\1 \2')
|
69
70
|
rendered = rendered.gsub(/<\/?p>/, '') if options[:inline]
|
70
71
|
rendered.html_safe
|
71
72
|
end
|
data/lib/text_helpers/version.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "../../test_helper"
|
2
2
|
|
3
3
|
describe TextHelpers::Translation do
|
4
4
|
before do
|
@@ -32,6 +32,7 @@ describe TextHelpers::Translation do
|
|
32
32
|
test_key: "*#{@scoped_text}*",
|
33
33
|
list_key: "* #{@scoped_text}",
|
34
34
|
single_word_list_key: "* #{@single_word_text}",
|
35
|
+
prerendered_html_key: "<ul>\n <li> Get everything you ever wanted</li>\n <li> Practically-guaranteed</li>\n </ul>",
|
35
36
|
interpolated_key: "Global? (!test_key!)",
|
36
37
|
interpolated_scoped_key: "Global? (!test_scoped_key!)",
|
37
38
|
interpol_arg_key: "Interpolate global? (!interpolated_key!)",
|
@@ -86,6 +87,12 @@ describe TextHelpers::Translation do
|
|
86
87
|
assert_equal expected, @helper.html(:single_word_list_key)
|
87
88
|
end
|
88
89
|
|
90
|
+
it "correctly handles orphans in HTML with erratic whitespace" do
|
91
|
+
expected = "<ul>\n <li> Get everything you ever wanted</li>\n <li> Practically-guaranteed</li>\n </ul>\n"
|
92
|
+
|
93
|
+
assert_equal expected, @helper.html(:prerendered_html_key)
|
94
|
+
end
|
95
|
+
|
89
96
|
it "does not modify HTML tags" do
|
90
97
|
expected = "<p><a href=\"mailto:#{@email_address}\">#{@email_address}</a></p>\n"
|
91
98
|
assert_equal expected, @helper.html(:email_key)
|
@@ -112,12 +119,12 @@ describe TextHelpers::Translation do
|
|
112
119
|
assert_equal "<em>#{@scoped_text}</em>\n", @helper.html(:test_key, inline: true, orphans: true)
|
113
120
|
end
|
114
121
|
|
115
|
-
it "renders internal links without a target" do
|
122
|
+
it "renders internal links without a target or rel" do
|
116
123
|
assert_equal "<a href=\"/internal/path\">Internal link</a>\n", @helper.html(:internal_link, inline: true)
|
117
124
|
end
|
118
125
|
|
119
|
-
it "renders external links with target='_blank'" do
|
120
|
-
assert_equal "<a href=\"http://external.com\" target=\"_blank\">External link</a>\n", @helper.html(:external_link, inline: true)
|
126
|
+
it "renders external links with target='_blank' and rel='noopener'" do
|
127
|
+
assert_equal "<a href=\"http://external.com\" target=\"_blank\" rel=\"noopener\">External link</a>\n", @helper.html(:external_link, inline: true)
|
121
128
|
end
|
122
129
|
|
123
130
|
it "interpolates values wrapped in !!" do
|
@@ -164,10 +171,32 @@ describe TextHelpers::Translation do
|
|
164
171
|
assert_equal "This is what <b>Han</b> Solo said", @helper.text(:argument_key, user: "<b>Han</b> Solo".html_safe)
|
165
172
|
end
|
166
173
|
|
167
|
-
it "correctly handles
|
174
|
+
it "correctly handles pluralized keys" do
|
168
175
|
assert_equal "A single piece of text", @helper.text(:pluralized_key, count: 1)
|
169
176
|
assert_equal "2 pieces of text", @helper.text(:pluralized_key, count: 2)
|
170
177
|
end
|
178
|
+
|
179
|
+
describe "when the pluralization backend is configured and the exception handler is enabled" do
|
180
|
+
before do
|
181
|
+
@original_backend = I18n.backend
|
182
|
+
new_backend = @original_backend.dup
|
183
|
+
new_backend.extend(I18n::Backend::Pluralization)
|
184
|
+
I18n.backend = new_backend
|
185
|
+
|
186
|
+
@original_exception_handler = I18n.exception_handler
|
187
|
+
I18n.exception_handler = TextHelpers::RaiseExceptionHandler.new
|
188
|
+
end
|
189
|
+
|
190
|
+
after do
|
191
|
+
I18n.backend = @original_backend
|
192
|
+
I18n.exception_handler = @original_exception_handler
|
193
|
+
end
|
194
|
+
|
195
|
+
it "correctly handles pluralized keys" do
|
196
|
+
assert_equal "A single piece of text", @helper.text(:pluralized_key, count: 1)
|
197
|
+
assert_equal "2 pieces of text", @helper.text(:pluralized_key, count: 2)
|
198
|
+
end
|
199
|
+
end
|
171
200
|
end
|
172
201
|
|
173
202
|
describe "when no valid scope is provided" do
|
@@ -213,6 +242,14 @@ describe TextHelpers::Translation do
|
|
213
242
|
I18n.backend = @original_backend
|
214
243
|
end
|
215
244
|
|
245
|
+
it "cascades the requested key by default" do
|
246
|
+
I18n.backend.store_translations(:en, {test_scoped_key: "a translation"})
|
247
|
+
assert_equal "a translation", @helper.text(:test_scoped_key, scope: "some.unnecessary.scope")
|
248
|
+
|
249
|
+
I18n.backend.store_translations(:en, {some: {test_scoped_key: "a scoped translation"}})
|
250
|
+
assert_equal "a scoped translation", @helper.text(:test_scoped_key, scope: "some.unnecessary.scope")
|
251
|
+
end
|
252
|
+
|
216
253
|
it "cascades the interpolated key by default" do
|
217
254
|
I18n.backend.store_translations(:en, {test_scoped_key: "a translation"})
|
218
255
|
|
data/text_helpers.gemspec
CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |gem|
|
|
24
24
|
|
25
25
|
gem.add_dependency("activesupport")
|
26
26
|
gem.add_dependency("i18n", ">=0.6.8")
|
27
|
-
gem.add_dependency("redcarpet", ">=3.
|
27
|
+
gem.add_dependency("redcarpet", ">=3.5.1")
|
28
28
|
|
29
29
|
gem.add_development_dependency("rake")
|
30
30
|
gem.add_development_dependency("railties")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: text_helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Horner
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 3.
|
47
|
+
version: 3.5.1
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 3.
|
54
|
+
version: 3.5.1
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -89,6 +89,7 @@ extra_rdoc_files: []
|
|
89
89
|
files:
|
90
90
|
- ".gitignore"
|
91
91
|
- ".travis.yml"
|
92
|
+
- CHANGELOG.md
|
92
93
|
- Gemfile
|
93
94
|
- LICENSE
|
94
95
|
- README.md
|
@@ -108,7 +109,7 @@ homepage: https://github.com/ahorner/text-helpers
|
|
108
109
|
licenses:
|
109
110
|
- MIT
|
110
111
|
metadata: {}
|
111
|
-
post_install_message:
|
112
|
+
post_install_message:
|
112
113
|
rdoc_options: []
|
113
114
|
require_paths:
|
114
115
|
- lib
|
@@ -123,9 +124,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
124
|
- !ruby/object:Gem::Version
|
124
125
|
version: '0'
|
125
126
|
requirements: []
|
126
|
-
|
127
|
-
|
128
|
-
signing_key:
|
127
|
+
rubygems_version: 3.1.4
|
128
|
+
signing_key:
|
129
129
|
specification_version: 4
|
130
130
|
summary: TextHelpers is a gem which supplies some basic utilities for text localization
|
131
131
|
in Rails projects. The library is intended to make it simple to keep your application's
|