it 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 540cf86712cced87828cbb22246c440c4fff172b
4
+ data.tar.gz: 58c4b692810db1b1732ad21d31394c5cec10b2a1
5
+ SHA512:
6
+ metadata.gz: f16fe37fd4a51ee176710ed9efec3e6874a3c1d86daefc128d48c718b81766701977aa1a5e180447a70ec344d4463b6f4fbf7081c0bc271973733d60ff07ce15
7
+ data.tar.gz: 9a98bc3f607fb8d6903c2ed5fae6b0aec9e416f36fa1897f75474176a2c3819326ed81a7d933c7a47abc7820edd18333ec803145ccdc18c97971f06dc844924d
data/Gemfile CHANGED
@@ -1,6 +1,3 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
- group :development, :test do
4
- gem 'actionpack'
5
- gem 'rspec-rails'
6
- end
3
+ gemspec
@@ -1,3 +1,5 @@
1
+ "!https://codeclimate.com/github/iGEL/it.png!":https://codeclimate.com/github/iGEL/it "!https://travis-ci.org/iGEL/it.png!":https://travis-ci.org/iGEL/it (Specs pass on Ruby 1.8.7, 1.9.2, 1.9.3, 2.0.0, Rubinius and Jruby)
2
+
1
3
  h1. What is *it*?
2
4
 
3
5
  I18n is baked right into Rails, and it's great. But if you want to place markup or links inside your translated copies, things get a little messy. You need to specify the label of your links separately from the rest of the copy. Writing HTML in your translations is even worse.
@@ -65,10 +67,6 @@ Plain mail:
65
67
 
66
68
  The @%s@ will be replaced with the label, in the example with Rails. You could provide any other string containing @%s@. The default is just @%s@, so it will return only the label itself.
67
69
 
68
- h2. isit18.com?
69
-
70
- Well, the specs pass on 1.8.7, but I never tried it in a real app. Report any issues on "github":https://github.com/iGEL/it/issues
71
-
72
70
  h2. Abandoned & outdated?
73
71
 
74
72
  Everybody hates old and outdated gems, which don't work on with the latest Rails or Ruby version or haven't been updated for ages. Sadly, rubygems is full of such gems. If you improved *it* send me a pull request. If I have not time to support the lib anymore, I will happily hand the project over to a new maintainer.
@@ -76,7 +74,3 @@ Everybody hates old and outdated gems, which don't work on with the latest Rails
76
74
  h2. Broken English?
77
75
 
78
76
  I'm not a native speaker, so you'll probably find some spelling errors or clumsy sentences above. If you do, please send me a message.
79
-
80
- h2. Like it?
81
-
82
- Like it? Flattr it! !http://api.flattr.com/button/flattr-badge-large.png!:http://flattr.com/thing/351483/it-HTML-tags-for-your-translations
data/lib/it.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'it/parser'
2
+ require 'it/interpolation'
1
3
  require 'it/tag'
2
4
  require 'it/link'
3
5
  require 'it/plain'
@@ -12,7 +14,7 @@ module It
12
14
  # It outside of your views. See documentation at Helper#it
13
15
  def self.it(identifier, options = {})
14
16
  options.stringify_keys!
15
- process(I18n.t(identifier, :locale => options["locale"]), options)
17
+ Parser.new(I18n.t(identifier, :locale => options["locale"]), options).process
16
18
  end
17
19
 
18
20
  # Creates a new link to be used in +it+.
@@ -40,39 +42,4 @@ module It
40
42
  def self.tag(tag_name, options = {})
41
43
  It::Tag.new(tag_name, options)
42
44
  end
43
-
44
- private
45
- def self.process(string, options)
46
- # Handle pluralization
47
- string = I18n.backend.send(:pluralize, options["locale"] || I18n.locale, string, options["count"]) if string.is_a?(Hash) && options["count"]
48
-
49
- # We want the escaped String, not an ActiveSupport::SafeBuffer
50
- translation = String.new(ERB::Util.h(string))
51
-
52
- # For deep nesting, we repeat the process until we have no interpolations anymore
53
- while translation =~ /%\{[^{}}]+\}/
54
- translation.gsub!(/%\{[^{}}]+\}/) do |interpolation|
55
- token, label = interpolation[2..-2].split(":", 2)
56
-
57
- # Convert tokens with String arguments into It::Links, if they are named link, link_* or *_link
58
- if (token == "link" || token.ends_with?("_link") || token.starts_with?("link_")) && (options[token].is_a?(String) || options[token].is_a?(Hash))
59
- options[token] = It::Link.new(options[token])
60
- end
61
-
62
- if !options.has_key?(token)
63
- raise KeyError, "key{#{token}} not found"
64
- elsif label && !options[token].is_a?(It::Tag)
65
- raise ArgumentError, "key{#{token}} has an argument, so it cannot resolved with a #{options[token].class}"
66
- elsif label # Normal tags
67
- options[token].process(label.html_safe)
68
- elsif options[token].is_a?(It::Tag) # Empty tag
69
- options[token].process
70
- else # Normal interpolations, as I18n.t would do it.
71
- ERB::Util.h(options[token])
72
- end
73
- end
74
- end
75
-
76
- translation.html_safe
77
- end
78
45
  end
@@ -40,7 +40,7 @@ module It
40
40
  #
41
41
  def it(identifier, options = {})
42
42
  options.stringify_keys!
43
- It.process(t(identifier, :locale => options["locale"]), options)
43
+ It::Parser.new(t(identifier, :locale => options["locale"]), options).process
44
44
  end
45
45
  end
46
46
  end
@@ -0,0 +1,40 @@
1
+ module It
2
+ class Interpolation
3
+ attr_accessor :key, :label, :values
4
+
5
+ def initialize(string, values)
6
+ self.key, self.label = string[2..-2].split(':', 2)
7
+ self.values = values
8
+
9
+ convert_links
10
+ end
11
+
12
+ def process
13
+ check_input_values
14
+
15
+ if label # Normal tags
16
+ values[key].process(label.html_safe)
17
+ elsif values[key].is_a?(It::Tag) # Empty tag
18
+ values[key].process
19
+ else # Normal interpolations, as I18n.t would do it.
20
+ ERB::Util.h(values[key])
21
+ end
22
+ end
23
+
24
+ private
25
+ # Convert keys with String arguments into It::Links, if they are named link, link_* or *_link
26
+ def convert_links
27
+ if key =~ /(\Alink\Z|_link\Z|\Alink_)/ && values[key].is_a?(String)
28
+ self.values[key] = It::Link.new(values[key])
29
+ end
30
+ end
31
+
32
+ def check_input_values
33
+ if !values.has_key?(key)
34
+ raise KeyError, "key{#{key}} not found"
35
+ elsif label && !values[key].is_a?(It::Tag)
36
+ raise ArgumentError, "key{#{key}} has an argument, so it cannot resolved with a #{values[key].class}"
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,38 @@
1
+ module It
2
+ class Parser
3
+ attr_accessor :string, :options
4
+
5
+ INTERPOLATION_REGEXP = /%\{[^{}}]+\}/
6
+
7
+ def initialize(string, options)
8
+ self.string = string
9
+ self.options = options
10
+ end
11
+
12
+ def process
13
+ handle_pluralization
14
+ escape_string
15
+
16
+ # For deep nesting, we repeat the process until we have no interpolations anymore
17
+ while has_interpolation?
18
+ self.string.gsub!(INTERPOLATION_REGEXP) do |interpolation|
19
+ Interpolation.new(interpolation, options).process
20
+ end
21
+ end
22
+ string.html_safe
23
+ end
24
+
25
+ private
26
+ def has_interpolation?
27
+ string =~ INTERPOLATION_REGEXP
28
+ end
29
+
30
+ def handle_pluralization
31
+ self.string = I18n.backend.send(:pluralize, (options['locale'] || I18n.locale), string, options['count']) if string.is_a?(Hash) && options['count']
32
+ end
33
+
34
+ def escape_string
35
+ self.string = String.new(ERB::Util.h(string))
36
+ end
37
+ end
38
+ end
@@ -5,8 +5,8 @@ require 'it'
5
5
 
6
6
  describe It::Helper, "#it" do
7
7
  before do
8
- I18n.backend.store_translations(:en, :test1 => "I'm containing a %{link:link to Rails} in the middle.")
9
- I18n.backend.store_translations(:de, :test1 => "Ich enthalte einen %{link:Link zu Rails} in der Mitte.")
8
+ I18n.backend.store_translations(:en, :test1 => "I have a %{link:link to Rails} in the middle.")
9
+ I18n.backend.store_translations(:de, :test1 => "Ich habe einen %{link:Link zu Rails} in der Mitte.")
10
10
 
11
11
  @view = ActionView::Base.new
12
12
  @controller = ActionController::Base.new
@@ -18,63 +18,63 @@ describe It::Helper, "#it" do
18
18
  end
19
19
 
20
20
  it "should insert the link into the string" do
21
- @view.it("test1", :link => It.link("http://www.rubyonrails.org")).should == 'I\'m containing a <a href="http://www.rubyonrails.org">link to Rails</a> in the middle.'
21
+ expect(@view.it("test1", :link => It.link("http://www.rubyonrails.org"))).to eq('I have a <a href="http://www.rubyonrails.org">link to Rails</a> in the middle.')
22
22
  end
23
23
 
24
24
  it "should insert the link into the German translation" do
25
25
  I18n.locale = :de
26
- @view.it("test1", :link => It.link("http://www.rubyonrails.org")).should == 'Ich enthalte einen <a href="http://www.rubyonrails.org">Link zu Rails</a> in der Mitte.'
26
+ expect(@view.it("test1", :link => It.link("http://www.rubyonrails.org"))).to eq('Ich habe einen <a href="http://www.rubyonrails.org">Link zu Rails</a> in der Mitte.')
27
27
  end
28
28
 
29
29
  it "should allow link options to be set" do
30
- @view.it("test1", :link => It.link("http://www.rubyonrails.org", :target => "_blank")).should == 'I\'m containing a <a href="http://www.rubyonrails.org" target="_blank">link to Rails</a> in the middle.'
30
+ expect(@view.it("test1", :link => It.link("http://www.rubyonrails.org", :target => "_blank"))).to eq('I have a <a href="http://www.rubyonrails.org" target="_blank">link to Rails</a> in the middle.')
31
31
  end
32
32
 
33
33
  it "should support the plain thing" do
34
- @view.it("test1", :link => It.plain("%s[http://www.rubyonrails.org]")).should == 'I\'m containing a link to Rails[http://www.rubyonrails.org] in the middle.'
34
+ expect(@view.it("test1", :link => It.plain("%s[http://www.rubyonrails.org]"))).to eq('I have a link to Rails[http://www.rubyonrails.org] in the middle.')
35
35
  end
36
36
 
37
37
  it "should parse other tags as well" do
38
- @view.it("test1", :link => It.tag(:b, :class => "classy")).should == 'I\'m containing a <b class="classy">link to Rails</b> in the middle.'
38
+ expect(@view.it("test1", :link => It.tag(:b, :class => "classy"))).to eq('I have a <b class="classy">link to Rails</b> in the middle.')
39
39
  end
40
40
 
41
41
  it "should mark the result as html safe" do
42
- @view.it("test1", :link => It.link("http://www.rubyonrails.org")).html_safe?.should be_true
42
+ expect(@view.it("test1", :link => It.link("http://www.rubyonrails.org")).html_safe?).to be_true
43
43
  end
44
44
 
45
45
  it "should escape all html in the translation" do
46
46
  I18n.backend.store_translations(:en, :test2 => "<a href=\"hax0r\"> & %{link:link -> Rails} in <b>the middle</b>.")
47
- @view.it("test2", :link => It.link("http://www.rubyonrails.org")).should == '&lt;a href=&quot;hax0r&quot;&gt; &amp; <a href="http://www.rubyonrails.org">link -&gt; Rails</a> in &lt;b&gt;the middle&lt;/b&gt;.'
47
+ expect(@view.it("test2", :link => It.link("http://www.rubyonrails.org"))).to eq('&lt;a href=&quot;hax0r&quot;&gt; &amp; <a href="http://www.rubyonrails.org">link -&gt; Rails</a> in &lt;b&gt;the middle&lt;/b&gt;.')
48
48
  end
49
49
 
50
50
  it "should also work with 2 links" do
51
51
  I18n.backend.store_translations(:en, :test3 => "I like %{link1:rails} and %{link2:github}.")
52
- @view.it("test3", :link1 => It.link("http://www.rubyonrails.org"), :link2 => It.link("http://www.github.com")).should == 'I like <a href="http://www.rubyonrails.org">rails</a> and <a href="http://www.github.com">github</a>.'
52
+ expect(@view.it("test3", :link1 => It.link("http://www.rubyonrails.org"), :link2 => It.link("http://www.github.com"))).to eq('I like <a href="http://www.rubyonrails.org">rails</a> and <a href="http://www.github.com">github</a>.')
53
53
  end
54
54
 
55
55
  it "should allow normal I18n interpolations" do
56
- I18n.backend.store_translations(:en, :test4 => "I'm containing a %{link:link to Rails} in the %{position}.")
57
- @view.it("test4", :link => It.link("http://www.rubyonrails.org"), :position => "middle").should == 'I\'m containing a <a href="http://www.rubyonrails.org">link to Rails</a> in the middle.'
56
+ I18n.backend.store_translations(:en, :test4 => "I have a %{link:link to Rails} in the %{position}.")
57
+ expect(@view.it("test4", :link => It.link("http://www.rubyonrails.org"), :position => "middle")).to eq('I have a <a href="http://www.rubyonrails.org">link to Rails</a> in the middle.')
58
58
  end
59
59
 
60
- it "should allow Intergers as normal interpolation" do
60
+ it "should allow Integers as normal interpolation" do
61
61
  I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
62
- @view.it("test5", :name => 2).should == 'Hello 2.'
62
+ expect(@view.it("test5", :name => 2)).to eq('Hello 2.')
63
63
  end
64
64
 
65
65
  it "should escape the HTML in normal interpolations" do
66
66
  I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
67
- @view.it("test5", :name => '<a href="http://evil.haxor.com">victim</a>').should == 'Hello &lt;a href=&quot;http://evil.haxor.com&quot;&gt;victim&lt;/a&gt;.'
67
+ expect(@view.it("test5", :name => '<a href="http://evil.haxor.com">victim</a>')).to eq('Hello &lt;a href=&quot;http://evil.haxor.com&quot;&gt;victim&lt;/a&gt;.')
68
68
  end
69
69
 
70
70
  it "should not escape html_safe interpolations" do
71
71
  I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
72
- @view.it("test5", :name => '<a href="http://www.rubyonrails.org">Rails</a>'.html_safe).should == 'Hello <a href="http://www.rubyonrails.org">Rails</a>.'
72
+ expect(@view.it("test5", :name => '<a href="http://www.rubyonrails.org">Rails</a>'.html_safe)).to eq('Hello <a href="http://www.rubyonrails.org">Rails</a>.')
73
73
  end
74
74
 
75
75
  it "should allow interpolations inside of links" do
76
76
  I18n.backend.store_translations(:en, :test6 => "Did you read our %{link:nice %{article}}?")
77
- @view.it("test6", :link => It.link("/article/2"), :article => "article").should == 'Did you read our <a href="/article/2">nice article</a>?'
77
+ expect(@view.it("test6", :link => It.link("/article/2"), :article => "article")).to eq('Did you read our <a href="/article/2">nice article</a>?')
78
78
  end
79
79
 
80
80
  it "should raise a KeyError, if the key was not given" do
@@ -88,32 +88,33 @@ describe It::Helper, "#it" do
88
88
 
89
89
  it "should allow Strings, if the interpolation name is link" do
90
90
  I18n.backend.store_translations(:en, :test8 => "Sign up %{link:here}!")
91
- @view.it("test8", :link => "/register").should == 'Sign up <a href="/register">here</a>!'
91
+ expect(@view.it("test8", :link => "/register")).to eq('Sign up <a href="/register">here</a>!')
92
92
  end
93
93
 
94
94
  it "should allow Strings, if the interpolation name ends with _link" do
95
95
  I18n.backend.store_translations(:en, :test8 => "Sign up %{register_link:here}!")
96
- @view.it("test8", :register_link => "/register").should == 'Sign up <a href="/register">here</a>!'
96
+ expect(@view.it("test8", :register_link => "/register")).to eq('Sign up <a href="/register">here</a>!')
97
97
  end
98
98
 
99
99
  it "should allow Strings, if the interpolation name starts with link_" do
100
100
  I18n.backend.store_translations(:en, :test8 => "Sign up %{link_to_register:here}!")
101
- @view.it("test8", :link_to_register => "/register").should == 'Sign up <a href="/register">here</a>!'
101
+ expect(@view.it("test8", :link_to_register => "/register")).to eq('Sign up <a href="/register">here</a>!')
102
102
  end
103
103
 
104
104
  it "should work with tags without arguments" do
105
105
  I18n.backend.store_translations(:en, :test9 => "We can %{br} do line breaks")
106
- @view.it("test9", :br => It.tag(:br)).should == 'We can <br /> do line breaks'
106
+ expect(@view.it("test9", :br => It.tag(:br))).to eq('We can <br /> do line breaks')
107
107
  end
108
108
 
109
109
  it 'should support dot-prefixed keys' do
110
110
  I18n.backend.store_translations(:en, :widgets => { :show => { :all_widgets => "See %{widgets_link:all widgets}" } })
111
- @view.instance_variable_set(:"@_virtual_path", "widgets/show")
112
- @view.it('.all_widgets', :widgets_link => '/widgets').should == 'See <a href="/widgets">all widgets</a>'
111
+ @view.instance_variable_set(:"@_virtual_path", "widgets/show") # For Rails 3.0
112
+ @view.instance_variable_set(:"@virtual_path", "widgets/show") # For Rails 3.1, 3.2 and probably 4.0
113
+ expect(@view.it('.all_widgets', :widgets_link => '/widgets')).to eq('See <a href="/widgets">all widgets</a>')
113
114
  end
114
115
 
115
116
  it 'should support the locale option' do
116
- @view.it('test1', :locale => "de", :link => It.link("http://www.rubyonrails.org")).should == 'Ich enthalte einen <a href="http://www.rubyonrails.org">Link zu Rails</a> in der Mitte.'
117
+ expect(@view.it('test1', :locale => "de", :link => It.link("http://www.rubyonrails.org"))).to eq('Ich habe einen <a href="http://www.rubyonrails.org">Link zu Rails</a> in der Mitte.')
117
118
  end
118
119
 
119
120
  context "With a pluralized translation" do
@@ -122,15 +123,15 @@ describe It::Helper, "#it" do
122
123
  end
123
124
 
124
125
  it 'should work with count = 0' do
125
- @view.it("test10", :count => 0, :link => "/messages").should == 'You have zero messages.'
126
+ expect(@view.it("test10", :count => 0, :link => "/messages")).to eq('You have zero messages.')
126
127
  end
127
128
 
128
129
  it 'should work with count = 1' do
129
- @view.it("test10", :count => 1, :link => "/messages").should == 'You have <a href="/messages">one message</a>.'
130
+ expect(@view.it("test10", :count => 1, :link => "/messages")).to eq('You have <a href="/messages">one message</a>.')
130
131
  end
131
132
 
132
133
  it 'should work with count > 1' do
133
- @view.it("test10", :count => 2, :link => "/messages").should == 'You have <a href="/messages">2 messages</a>.'
134
+ expect(@view.it("test10", :count => 2, :link => "/messages")).to eq('You have <a href="/messages">2 messages</a>.')
134
135
  end
135
136
  end
136
137
  end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+ require 'it'
3
+
4
+ describe It::Interpolation do
5
+ subject(:interpolation) { It::Interpolation.new('%{key:label}', { 'key' => It.tag(:b) }) }
6
+
7
+ describe '.new' do
8
+ it 'throws an error with one argument' do
9
+ expect { It::Interpolation.new('%{key:label}') }.to raise_error(ArgumentError)
10
+ end
11
+
12
+ it 'extracts the key from the string' do
13
+ expect(interpolation.key).to eq('key')
14
+ end
15
+
16
+ it 'extracts the label from the string' do
17
+ expect(interpolation.label).to eq('label')
18
+ end
19
+
20
+ it 'assigns nil to the label if not present' do
21
+ expect(It::Interpolation.new('%{key}', {}).label).to be_nil
22
+ end
23
+
24
+ it 'converts string values to a It::Link if the key is named link' do
25
+ interpolation = It::Interpolation.new('%{link:label}', 'link' => 'http://github.com')
26
+
27
+ expect(interpolation.values['link']).to be_kind_of(It::Link)
28
+ end
29
+
30
+ it 'converts string values to a It::Link if the key starts with link_' do
31
+ interpolation = It::Interpolation.new('%{link_github:label}', 'link_github' => 'http://github.com')
32
+
33
+ expect(interpolation.values['link_github']).to be_kind_of(It::Link)
34
+ end
35
+
36
+ it 'converts string values to a It::Link if the key ends with _link' do
37
+ interpolation = It::Interpolation.new('%{github_link:label}', 'github_link' => 'http://github.com')
38
+
39
+ expect(interpolation.values['github_link']).to be_kind_of(It::Link)
40
+ end
41
+ end
42
+
43
+ describe '#process' do
44
+ it 'interpolates the string' do
45
+ expect(interpolation.process).to eq('<b>label</b>')
46
+ end
47
+
48
+ it 'interpolates the string to an empty tag if no label is present' do
49
+ interpolation.label = nil
50
+
51
+ expect(interpolation.process).to eq('<b />')
52
+ end
53
+
54
+ it 'does the normal interpolation if the value is not a Tag and no label is present' do
55
+ interpolation.label = nil
56
+ interpolation.values = { 'key' => 'string'}
57
+
58
+ expect(interpolation.process).to eq('string')
59
+ end
60
+
61
+ it 'escapes HTML in the normal interpolation' do
62
+ interpolation.label = nil
63
+ interpolation.values = { 'key' => '<b>hallo</b>'}
64
+
65
+ expect(interpolation.process).to eq('&lt;b&gt;hallo&lt;/b&gt;')
66
+ end
67
+
68
+ it 'raises an KeyError if the requested key was not provided' do
69
+ interpolation.values = {}
70
+
71
+ expect { interpolation.process }.to raise_error(KeyError)
72
+ end
73
+
74
+ it 'raises an ArgumentError, if a string should be interpolated with a label' do
75
+ interpolation.values = { 'key' => 'string'}
76
+
77
+ expect { interpolation.process }.to raise_error(ArgumentError)
78
+ end
79
+ end
80
+ end
@@ -31,12 +31,12 @@ end
31
31
 
32
32
  describe It::Link, '#tag_name' do
33
33
  it "should always return a" do
34
- It::Link.new("http://www.rubyonrails.org/").tag_name.should == :a
34
+ expect(It::Link.new("http://www.rubyonrails.org/").tag_name).to eq(:a)
35
35
  end
36
36
  end
37
37
 
38
38
  describe It::Link, '#process' do
39
39
  it "should return a link with the options set and the content as label" do
40
- It::Link.new("http://www.rubyonrails.org", :target => "_blank").process("Rails").should == '<a href="http://www.rubyonrails.org" target="_blank">Rails</a>'
40
+ expect(It::Link.new("http://www.rubyonrails.org", :target => "_blank").process("Rails")).to eq('<a href="http://www.rubyonrails.org" target="_blank">Rails</a>')
41
41
  end
42
- end
42
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ require 'it'
3
+
4
+ describe It::Parser do
5
+ describe '#process' do
6
+ it 'calls the Interpolation as required' do
7
+ values = {'b' => It.tag(:b), 'link' => '/messages'}
8
+ parser = It::Parser.new('You have %{b:%{link:new messages}}!', values)
9
+
10
+ return1 = double('It::Interpolation', :process => '<a href="/messages">new messages</a>')
11
+ It::Interpolation.should_receive(:new).with('%{link:new messages}', values).and_return(return1)
12
+
13
+ return2 = double('It::Interpolation', :process => '<b><a href="/messages">new messages</a></b>')
14
+ It::Interpolation.should_receive(:new).with('%{b:<a href="/messages">new messages</a>}', values).and_return(return2)
15
+
16
+ expect(parser.process).to eq('You have <b><a href="/messages">new messages</a></b>!')
17
+ end
18
+
19
+ it 'escapes HTML in the string and the labels' do
20
+ parser = It::Parser.new('It is a <b>save</b> %{word:<i>world</i>}', 'word' => It.tag(:i))
21
+
22
+ expect(parser.process).to eq('It is a &lt;b&gt;save&lt;/b&gt; <i>&lt;i&gt;world&lt;/i&gt;</i>')
23
+ end
24
+
25
+ it 'marks the result as html safe' do
26
+ parser = It::Parser.new('test', {})
27
+
28
+ expect(parser.process).to be_html_safe
29
+ end
30
+
31
+ it 'delegates pluralization to I18n' do
32
+ I18n.backend.stub(:pluralize).with('en', {:other => 'You have %{count} messages'}, 2) { 'This is the pluralized string' }
33
+ parser = It::Parser.new({:other => 'You have %{count} messages'}, 'locale' => 'en', 'count' => 2)
34
+
35
+ expect(parser.process).to eq('This is the pluralized string')
36
+ end
37
+ end
38
+ end
@@ -11,7 +11,7 @@ describe It::Plain, '.new' do
11
11
  end
12
12
 
13
13
  it "should raise ArgumentError with 2 params" do
14
- expect { It::Plain.new("asdf", "asdf")}.to raise_error(ArgumentError, "wrong number of arguments (2 for 1)")
14
+ expect { It::Plain.new("asdf", "asdf")}.to raise_error(ArgumentError)
15
15
  end
16
16
 
17
17
  it "should raise a TypeError, if the first param is not a String" do
@@ -21,10 +21,10 @@ end
21
21
 
22
22
  describe It::Plain, '#process' do
23
23
  it "should return 'Ruby on Rails', if no param was given" do
24
- It::Plain.new.process("Ruby on Rails").should == 'Ruby on Rails'
24
+ expect(It::Plain.new.process("Ruby on Rails")).to eq('Ruby on Rails')
25
25
  end
26
26
 
27
27
  it "should return 'Ruby on Rails[http://www.rubyonrails.org/]'" do
28
- It::Plain.new("%s[http://www.rubyonrails.org/]").process("Ruby on Rails").should == 'Ruby on Rails[http://www.rubyonrails.org/]'
28
+ expect(It::Plain.new("%s[http://www.rubyonrails.org/]").process("Ruby on Rails")).to eq('Ruby on Rails[http://www.rubyonrails.org/]')
29
29
  end
30
- end
30
+ end
@@ -31,38 +31,38 @@ end
31
31
 
32
32
  describe It::Tag, '#tag_name' do
33
33
  it "should return the tag as a Symbol if given as a Symbol" do
34
- It::Tag.new(:i).tag_name.should == :i
34
+ expect(It::Tag.new(:i).tag_name).to eq(:i)
35
35
  end
36
36
 
37
37
  it "should return the tag_name as a Symbol if given as a String" do
38
- It::Tag.new("i").tag_name.should == :i
38
+ expect(It::Tag.new("i").tag_name).to eq(:i)
39
39
  end
40
40
  end
41
41
 
42
42
  describe It::Tag, '#options' do
43
43
  it "should return the options with symbolized keys" do
44
- It::Tag.new(:i, "id" => "cool", :class => "classy").options.should == {:id => "cool", :class => "classy"}
44
+ expect(It::Tag.new(:i, "id" => "cool", :class => "classy").options).to eq({:id => "cool", :class => "classy"})
45
45
  end
46
46
  end
47
47
 
48
48
  describe It::Tag, '#process' do
49
49
  it "should return a tag with the options as attributes and the param as content" do
50
- It::Tag.new(:i, "id" => "cool", "class" => "classy").process("some text").should == '<i class="classy" id="cool">some text</i>'
50
+ expect(It::Tag.new(:i, "id" => "cool", "class" => "classy").process("some text")).to eq('<i class="classy" id="cool">some text</i>')
51
51
  end
52
52
 
53
53
  it "should be marked as html safe" do
54
- It::Tag.new(:i).process("some text").html_safe.should be_true
54
+ expect(It::Tag.new(:i).process("some text").html_safe).to be_true
55
55
  end
56
56
 
57
57
  it "should escape HTML" do
58
- It::Tag.new(:i).process("some text & <b>html</b>").should == '<i>some text &amp; &lt;b&gt;html&lt;/b&gt;</i>'
58
+ expect(It::Tag.new(:i).process("some text & <b>html</b>")).to eq('<i>some text &amp; &lt;b&gt;html&lt;/b&gt;</i>')
59
59
  end
60
60
 
61
61
  it "should not escape strings marked as HTML safe" do
62
- It::Tag.new(:i).process("some text & <b>html</b>".html_safe).should == '<i>some text & <b>html</b></i>'
62
+ expect(It::Tag.new(:i).process("some text & <b>html</b>".html_safe)).to eq('<i>some text & <b>html</b></i>')
63
63
  end
64
64
 
65
65
  it "should return an empty tag, if no content is provided" do
66
- It::Tag.new(:br, "id" => "cool").process.should == '<br id="cool" />'
66
+ expect(It::Tag.new(:br, "id" => "cool").process).to eq('<br id="cool" />')
67
67
  end
68
- end
68
+ end
@@ -5,14 +5,14 @@ require 'it'
5
5
 
6
6
  describe It, '.it' do
7
7
  it "should translate inside the controller as well" do
8
- I18n.backend.store_translations(:en, :test1 => "I'm containing a %{link:link to Rails} in the middle.")
9
- It.it("test1", :link => It.link("http://www.rubyonrails.org")).should == 'I\'m containing a <a href="http://www.rubyonrails.org">link to Rails</a> in the middle.'
8
+ I18n.backend.store_translations(:en, :test1 => "I have a %{link:link to Rails} in the middle.")
9
+ expect(It.it("test1", :link => It.link("http://www.rubyonrails.org"))).to eq('I have a <a href="http://www.rubyonrails.org">link to Rails</a> in the middle.')
10
10
  end
11
11
  end
12
12
 
13
13
  describe It, '.link' do
14
14
  it "should return an It::Link object" do
15
- It.link("https://www.github.com").class.should == It::Link
15
+ expect(It.link("https://www.github.com").class).to eq(It::Link)
16
16
  end
17
17
 
18
18
  it "should accept one param" do
@@ -30,7 +30,7 @@ end
30
30
 
31
31
  describe It, '.tag' do
32
32
  it "should return an It::Tag object" do
33
- It.tag(:b).class.should == It::Tag
33
+ expect(It.tag(:b).class).to eq(It::Tag)
34
34
  end
35
35
 
36
36
  it "should work with a param" do
@@ -48,7 +48,7 @@ end
48
48
 
49
49
  describe It, '.plain' do
50
50
  it "should return an It::Plain object" do
51
- It.plain.class.should == It::Plain
51
+ expect(It.plain.class).to eq(It::Plain)
52
52
  end
53
53
 
54
54
  it "should work without params" do
@@ -3,4 +3,11 @@ require 'action_pack'
3
3
  require 'action_controller'
4
4
  require 'action_view'
5
5
 
6
- $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
7
+
8
+ RSpec.configure do |config|
9
+
10
+ config.expect_with :rspec do |c|
11
+ c.syntax = :expect
12
+ end
13
+ end
metadata CHANGED
@@ -1,117 +1,112 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: it
3
- version: !ruby/object:Gem::Version
4
- hash: 17
5
- prerelease:
6
- segments:
7
- - 0
8
- - 2
9
- - 3
10
- version: 0.2.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.4
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Johannes Barre
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2011-12-15 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
11
+ date: 2013-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
21
14
  name: actionpack
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 7
29
- segments:
30
- - 3
31
- - 0
32
- - 0
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
33
19
  version: 3.0.0
34
20
  type: :runtime
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
37
28
  name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.11'
34
+ type: :development
38
35
  prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
40
- none: false
41
- requirements:
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
42
38
  - - ~>
43
- - !ruby/object:Gem::Version
44
- hash: 23
45
- segments:
46
- - 2
47
- - 6
48
- - 0
49
- version: 2.6.0
39
+ - !ruby/object:Gem::Version
40
+ version: '2.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
50
48
  type: :development
51
- version_requirements: *id002
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
52
55
  description:
53
56
  email: igel@igels.net
54
57
  executables: []
55
-
56
58
  extensions: []
57
-
58
- extra_rdoc_files:
59
+ extra_rdoc_files:
59
60
  - README.textile
60
- files:
61
+ files:
61
62
  - MIT-LICENSE
62
63
  - README.textile
63
64
  - Rakefile
64
65
  - Gemfile
66
+ - spec/spec_helper.rb
67
+ - spec/it_spec.rb
68
+ - spec/it/link_spec.rb
69
+ - spec/it/helper_spec.rb
70
+ - spec/it/parser_spec.rb
71
+ - spec/it/tag_spec.rb
72
+ - spec/it/plain_spec.rb
73
+ - spec/it/interpolation_spec.rb
65
74
  - lib/it.rb
75
+ - lib/it/interpolation.rb
66
76
  - lib/it/helper.rb
67
- - lib/it/tag.rb
77
+ - lib/it/parser.rb
68
78
  - lib/it/link.rb
69
79
  - lib/it/plain.rb
70
- - spec/it/helper_spec.rb
71
- - spec/it/link_spec.rb
72
- - spec/it/tag_spec.rb
73
- - spec/it/plain_spec.rb
74
- - spec/it_spec.rb
75
- - spec/spec_helper.rb
80
+ - lib/it/tag.rb
76
81
  homepage: https://github.com/igel/it
77
82
  licenses: []
78
-
83
+ metadata: {}
79
84
  post_install_message:
80
85
  rdoc_options: []
81
-
82
- require_paths:
86
+ require_paths:
83
87
  - lib
84
- required_ruby_version: !ruby/object:Gem::Requirement
85
- none: false
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- hash: 3
90
- segments:
91
- - 0
92
- version: "0"
93
- required_rubygems_version: !ruby/object:Gem::Requirement
94
- none: false
95
- requirements:
96
- - - ">="
97
- - !ruby/object:Gem::Version
98
- hash: 23
99
- segments:
100
- - 1
101
- - 3
102
- - 6
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
103
97
  version: 1.3.6
104
98
  requirements: []
105
-
106
99
  rubyforge_project:
107
- rubygems_version: 1.8.10
100
+ rubygems_version: 2.0.0.rc.2
108
101
  signing_key:
109
- specification_version: 3
102
+ specification_version: 4
110
103
  summary: A helper for links and other html tags in your translations
111
- test_files:
112
- - spec/it/helper_spec.rb
104
+ test_files:
105
+ - spec/it_spec.rb
113
106
  - spec/it/link_spec.rb
107
+ - spec/it/helper_spec.rb
108
+ - spec/it/parser_spec.rb
114
109
  - spec/it/tag_spec.rb
115
110
  - spec/it/plain_spec.rb
116
- - spec/it_spec.rb
111
+ - spec/it/interpolation_spec.rb
117
112
  - spec/spec_helper.rb