text_helpers 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/text_helpers/translation.rb +17 -10
- data/lib/text_helpers/version.rb +1 -1
- data/test/lib/text_helpers/translation_test.rb +20 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 776df8d7a9be3cdd18ae750affd047db2054a54d
|
4
|
+
data.tar.gz: de1b56e9e926fd136d4f21b83d5f2bd32aca3945
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 297235af410582b99cd195e5cd9d5c0e6a7d75bf2605ba5919cb863a0bd30f761c9aa8d987ebb074660a0d3dfd44e8b905d5573ad39cb544226d889086a9e82c
|
7
|
+
data.tar.gz: 8ab0165dcd09fc8a1f60d20f7a1b7f2f47331e2e65c20204ae397b2a60e4fafe2550b131491df80674b26e10eb4c2d7991129186703d8703c5d845cf3ac6316e
|
@@ -10,6 +10,9 @@ module TextHelpers
|
|
10
10
|
#
|
11
11
|
# key - The desired I18n lookup key.
|
12
12
|
# options - A Hash of options to pass through to the lookup.
|
13
|
+
# :orphans - A special option that will prevent the insertion of
|
14
|
+
# non-breaking space characters at the end of the text
|
15
|
+
# when set to true.
|
13
16
|
#
|
14
17
|
# Returns a String resulting from the I18n lookup.
|
15
18
|
def text(key, options = {})
|
@@ -19,26 +22,30 @@ module TextHelpers
|
|
19
22
|
}.merge(options))
|
20
23
|
|
21
24
|
# Interpolate any keypaths (e.g., `!some.lookup.path/key!`) found in the text.
|
22
|
-
text.strip.gsub(/!([\w._\/]+)!/) do |match|
|
25
|
+
final_text = text.strip.gsub(/!([\w._\/]+)!/) do |match|
|
23
26
|
I18n.t($1)
|
24
|
-
end
|
27
|
+
end
|
28
|
+
final_text = final_text.sub(/\s(\S+)\Z/, ' \1') unless options[:orphans]
|
29
|
+
final_text.html_safe
|
25
30
|
end
|
26
31
|
|
27
32
|
# Public: Get an HTML representation of the rendered markdown for the passed I18n key.
|
28
33
|
#
|
29
34
|
# key - The desired I18n lookup key.
|
30
35
|
# options - A Hash of options to pass through to the lookup.
|
31
|
-
# :inline
|
32
|
-
#
|
36
|
+
# :inline - A special option that will remove the enclosing <p>
|
37
|
+
# tags when set to true.
|
38
|
+
# :orphans - A special option that will prevent the insertion of
|
39
|
+
# non-breaking space characters at the end of each
|
40
|
+
# paragraph when set to true.
|
33
41
|
#
|
34
42
|
# Returns a String containing the localized text rendered via Markdown
|
35
43
|
def html(key, options = {})
|
36
|
-
rendered = GitHub::Markdown.render(text(key, options))
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
end
|
44
|
+
rendered = GitHub::Markdown.render(text(key, options.merge(orphans: true)))
|
45
|
+
|
46
|
+
rendered = options[:orphans] ? rendered : rendered.sub(/\s(\S+\s*<\/p>)/, ' \1')
|
47
|
+
rendered = rendered.gsub(/<\/?p>/, '') if options[:inline]
|
48
|
+
rendered.html_safe
|
42
49
|
end
|
43
50
|
|
44
51
|
protected
|
data/lib/text_helpers/version.rb
CHANGED
@@ -9,6 +9,9 @@ describe TextHelpers::Translation do
|
|
9
9
|
@scoped_text = "Scoped lookup"
|
10
10
|
@global_text = "Global lookup"
|
11
11
|
|
12
|
+
@nb_scoped_text = "Scoped lookup"
|
13
|
+
@nb_global_text = "Global lookup"
|
14
|
+
|
12
15
|
I18n.backend.store_translations :en, {
|
13
16
|
test_key: @global_text,
|
14
17
|
test: {
|
@@ -26,19 +29,31 @@ describe TextHelpers::Translation do
|
|
26
29
|
end
|
27
30
|
|
28
31
|
it "looks up the text for the key in a scope derived from the call stack" do
|
29
|
-
assert_equal "*#{@
|
32
|
+
assert_equal "*#{@nb_scoped_text}*", @helper.text(:test_key)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "allows orphaned text with :orphans" do
|
36
|
+
assert_equal "*#{@scoped_text}*", @helper.text(:test_key, orphans: true)
|
30
37
|
end
|
31
38
|
|
32
39
|
it "converts the text to HTML via Markdown" do
|
33
|
-
assert_equal "<p><em>#{@
|
40
|
+
assert_equal "<p><em>#{@nb_scoped_text}</em></p>\n", @helper.html(:test_key)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "allows orphaned text with :orphans" do
|
44
|
+
assert_equal "<p><em>#{@scoped_text}</em></p>\n", @helper.html(:test_key, orphans: true)
|
34
45
|
end
|
35
46
|
|
36
47
|
it "removes the enclosing paragraph with :inline" do
|
37
|
-
assert_equal "<em>#{@
|
48
|
+
assert_equal "<em>#{@nb_scoped_text}</em>\n", @helper.html(:test_key, inline: true)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "correctly combines :orphans and :inline options" do
|
52
|
+
assert_equal "<em>#{@scoped_text}</em>\n", @helper.html(:test_key, inline: true, orphans: true)
|
38
53
|
end
|
39
54
|
|
40
55
|
it "interpolates values wrapped in !!" do
|
41
|
-
assert_equal "Global? (#{@
|
56
|
+
assert_equal "Global? (#{@nb_global_text})", @helper.text(:interpolated_key)
|
42
57
|
end
|
43
58
|
end
|
44
59
|
|
@@ -50,7 +65,7 @@ describe TextHelpers::Translation do
|
|
50
65
|
end
|
51
66
|
|
52
67
|
it "defaults to a globally-defined value for the key" do
|
53
|
-
assert_equal @
|
68
|
+
assert_equal @nb_global_text, @helper.text(:test_key)
|
54
69
|
end
|
55
70
|
end
|
56
71
|
end
|
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: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Horner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-12-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|