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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4342beb840df0ee1900b30f38834af4369f59fb5
4
- data.tar.gz: 8f9de833dd817c836b675069eacb469cb1913355
3
+ metadata.gz: 776df8d7a9be3cdd18ae750affd047db2054a54d
4
+ data.tar.gz: de1b56e9e926fd136d4f21b83d5f2bd32aca3945
5
5
  SHA512:
6
- metadata.gz: b91d57f519796838e79edd29bc909f0fa438044a008c402304f0898dcd1e9e552979feaad3f834da1c5d676055a109f9986db83cecdecc122fcae3c7eaad385c
7
- data.tar.gz: 0df629319595bbe552627f3f3edc29b54ec1809230878bb4aef6cae25bde2bc28d829e9422d546cee3ba1776f5a27e79cf693dada354bd7a30e51b41d762cca5
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.html_safe
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 - A special option that will remove the enclosing <p>
32
- # tags when set to true.
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
- if options[:inline]
38
- rendered.gsub(/<\/?p>/, '').html_safe
39
- else
40
- rendered.html_safe
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>)/, '&nbsp;\1')
47
+ rendered = rendered.gsub(/<\/?p>/, '') if options[:inline]
48
+ rendered.html_safe
42
49
  end
43
50
 
44
51
  protected
@@ -1,3 +1,3 @@
1
1
  module TextHelpers
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
@@ -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&nbsp;lookup"
13
+ @nb_global_text = "Global&nbsp;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 "*#{@scoped_text}*", @helper.text(:test_key)
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>#{@scoped_text}</em></p>\n", @helper.html(:test_key)
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>#{@scoped_text}</em>\n", @helper.html(:test_key, inline: true)
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? (#{@global_text})", @helper.text(:interpolated_key)
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 @global_text, @helper.text(:test_key)
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.5
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-14 00:00:00.000000000 Z
11
+ date: 2013-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport