it 0.0.1.planned → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +6 -0
- data/README.textile +63 -6
- data/Rakefile +5 -0
- data/lib/it.rb +28 -0
- data/lib/it/helper.rb +83 -0
- data/lib/it/link.rb +18 -0
- data/lib/it/tag.rb +25 -0
- data/spec/it/helper_spec.rb +104 -0
- data/spec/it/link_spec.rb +42 -0
- data/spec/it/tag_spec.rb +68 -0
- data/spec/it_spec.rb +40 -0
- data/spec/spec_helper.rb +6 -0
- metadata +45 -9
data/Gemfile
ADDED
data/README.textile
CHANGED
@@ -1,12 +1,69 @@
|
|
1
|
-
h1. What is
|
1
|
+
h1. What is *it*?
|
2
2
|
|
3
|
-
|
3
|
+
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.
|
4
4
|
|
5
|
-
|
5
|
+
<pre><code>en:
|
6
|
+
copy: "If you are already registered, %{login_link}!"
|
7
|
+
copy_login_link: "please sign in"</code></pre>
|
8
|
+
|
9
|
+
<pre><code><%=raw t("copy", login: link_to(t("copy_login_link"), login_path)) %></code></pre>
|
10
|
+
|
11
|
+
Wouldn't it be much nicer and easier to understand for your translator to have the whole copy in single label? *it* lets you do that:
|
12
|
+
|
13
|
+
<pre><code>en:
|
14
|
+
copy: "If you are already registered, %{login_link:please sign in}!"</code></pre>
|
15
|
+
|
16
|
+
<pre><code><%=it "copy", login_link: login_path %></code></pre>
|
17
|
+
|
18
|
+
You may have noticed in the example above, that *it* doesn't require @raw@ anymore. Of course, all HTML in the translation gets properly escaped, so you don't have to worry about XSS.
|
19
|
+
|
20
|
+
h2. Installation
|
21
|
+
|
22
|
+
Just add the following line to your Gemfile & run @bundle install@:
|
23
|
+
|
24
|
+
<pre><code>gem 'it'</code></pre>
|
25
|
+
|
26
|
+
h2. Usage
|
27
|
+
|
28
|
+
You may have as many links inside your translations as you like, and normal interpolations are possible as well:
|
29
|
+
|
30
|
+
<pre><code>en:
|
31
|
+
copy: "Did you read the %{guide:Rails I18n guide}? It has more than %{advises} useful advises. You may fork the repo at {repo:github}."</code></pre>
|
32
|
+
|
33
|
+
<pre><code><%=it "copy", guide: It.link("http://guides.rubyonrails.org/i18n.html"), advices: 100, repo: It.link("https://github.com/lifo/docrails/tree/master/railties/guides") %></code></pre>
|
34
|
+
|
35
|
+
As you see above, unless the interpolation name is @link@ or starts with @_link@ or ends with @_link@, you need to call @It.link@ to create a link. The advantage of @It.link@: You may specify options like you would with @link_to@:
|
36
|
+
|
37
|
+
<pre><code><%=it "copy", guide: It.link("http://guides.rubyonrails.org/i18n.html", target: '_blank', class: "important") %></code></pre>
|
38
|
+
|
39
|
+
You may pass any kind of object accepted by @link_to@ as the link target, so your loved named routes like @article_path(:id => article.id)@ will all work fine.
|
40
|
+
|
41
|
+
Want to introduce some markup into your sentences? *it* will help you:
|
42
|
+
|
43
|
+
<pre><code>en:
|
44
|
+
advantages: There are %{b:many advantages} for registered users!</code></pre>
|
45
|
+
|
46
|
+
<pre><code><%=it "advantages", b: It.tag(:b, class: "red") %></code></pre>
|
47
|
+
|
48
|
+
Even nested interpolations are possible:
|
6
49
|
|
7
50
|
<pre><code>en:
|
8
|
-
copy: "
|
51
|
+
copy: "Want to contact %{user}%? %{link:send %{b:%{user} a message}}!"</code></pre>
|
52
|
+
|
53
|
+
<pre><code><%=it "copy", link: "mailto:igel@igels.net", user: 'iGEL', :b => It.tag(:b) %></code></pre>
|
54
|
+
|
55
|
+
h2. isit18.com?
|
56
|
+
|
57
|
+
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
|
58
|
+
|
59
|
+
h2. Abandoned & outdated?
|
60
|
+
|
61
|
+
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.
|
62
|
+
|
63
|
+
h2. Broken English?
|
64
|
+
|
65
|
+
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.
|
9
66
|
|
10
|
-
|
67
|
+
h2. Like it?
|
11
68
|
|
12
|
-
|
69
|
+
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/Rakefile
ADDED
data/lib/it.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'it/tag'
|
2
|
+
require 'it/link'
|
3
|
+
require 'it/helper'
|
4
|
+
|
5
|
+
ActiveSupport.on_load(:action_view) do
|
6
|
+
include It::Helper
|
7
|
+
end
|
8
|
+
|
9
|
+
# Namespace of the gem.
|
10
|
+
module It
|
11
|
+
# Creates a new link to be used in +it+.
|
12
|
+
#
|
13
|
+
# * +href+: The url for the link. You may specify it as a String or as a named route like +article_path+. It's not possible to specify
|
14
|
+
# a Hash like <code>{:controller => "articles", :action => "index"}</code> directly. Use the +url_for+ helper, if you would like to specify your
|
15
|
+
# links like that.
|
16
|
+
# * +options+: The options as an Hash. Use them like you would with +link_to+. <em>(optional)</em>
|
17
|
+
def self.link(href, options = {})
|
18
|
+
It::Link.new(href, options)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Creates a new tag to be used in +it+.
|
22
|
+
#
|
23
|
+
# * +tag_name+: The name of the tag as a Symbol or String.
|
24
|
+
# * +options+: The options will become attributes on the tag. <em>(optional)</em>
|
25
|
+
def self.tag(tag_name, options = {})
|
26
|
+
It::Tag.new(tag_name, options)
|
27
|
+
end
|
28
|
+
end
|
data/lib/it/helper.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module It
|
4
|
+
# The helper will be available in the views.
|
5
|
+
module Helper
|
6
|
+
# This helper method works just like +t+ (or +translate+ for long), but it allows to insert tags like links
|
7
|
+
# or spans in the result. The content of these tags can be written in line with the rest of the translation.
|
8
|
+
# Unless you need this functionality, use the normal +t+ method, which is faster.
|
9
|
+
#
|
10
|
+
# Like for normal translations, you specify interpolations with <code>%{</code> at the beginning and with
|
11
|
+
# <code>}</code> at the end. The new element is the <code>:</code>, which separates the name from the argument
|
12
|
+
# of this interpolation.
|
13
|
+
#
|
14
|
+
# # translation: "Already signed up? %{login_link:Sign in}!"
|
15
|
+
#
|
16
|
+
# <%=it("translation", :login_link => It.link(login_path))
|
17
|
+
#
|
18
|
+
# If your link doesn't require additional attributes and the name is +link+, starts with +link_+ or ends with +_link+,
|
19
|
+
# you may specify the argument as a String or your helper.
|
20
|
+
#
|
21
|
+
# # translation: "Already signed up? %{login_link:Sign in}!"
|
22
|
+
#
|
23
|
+
# <%=it("translation", :login_link => login_path)
|
24
|
+
#
|
25
|
+
# You may have as many tags inside of one translation as you like, and you even may nest them into each other. Also you
|
26
|
+
# may specify arguments to links and other tags.
|
27
|
+
#
|
28
|
+
# # translation: "The top contributor of %{wiki_link:our wiki} is currently %{user_link:%{b:%{name}}}. Thanks a lot, %{name}!"
|
29
|
+
#
|
30
|
+
# <%= it("translation", :wiki_link => wiki_path, :name => user.name, :b => It.tag(:b, :class => "user"), :user_link => It.link(user_path(user), :target => "_blank"))
|
31
|
+
#
|
32
|
+
# I recommend to limit the use of +it+ as much as possible. You could use it for <code><div></code> or <code><br /></code>, but I think,
|
33
|
+
# things seperated over multiple lines should go into different translations. Use it for inline tags like links, <code><span></code>,
|
34
|
+
# <code><b></code>, <code><i></code>, <code><em></code> or <code><strong></code>.
|
35
|
+
#
|
36
|
+
# It's currently not possible to specify your links as an Hash. Use the +url_for+ helper, if you want to specify your
|
37
|
+
# link target as an Hash. Also, it's not possible by default to use this helper from inside your controllers. Eighter
|
38
|
+
# you google how to use helpers inside your controllers or you push only the arguments for +it+ into the flash and parse
|
39
|
+
# it once you parse the flash in the view:
|
40
|
+
#
|
41
|
+
# # Controller:
|
42
|
+
# flash[:message] = ["articles.update.success", {:article_link => It.link(article_path(article))}]
|
43
|
+
#
|
44
|
+
# # View (or layout)
|
45
|
+
# <% if flash[:message].is_a?(Array) %>
|
46
|
+
# <%= it(flash[:message].first, flash[:message].last) %>
|
47
|
+
# <% else %>
|
48
|
+
# <%= flash[:message] %>
|
49
|
+
# <% end %>
|
50
|
+
#
|
51
|
+
def it(identifier, options = {})
|
52
|
+
options.stringify_keys!
|
53
|
+
# We want the escaped String, not an ActiveSupport::SafeBuffer
|
54
|
+
translation = String.new(h(t(identifier)))
|
55
|
+
|
56
|
+
# For deep nesting, we repeat the process until we have not interpolations anymore
|
57
|
+
while translation =~ /%\{[^{}}]+\}/
|
58
|
+
translation.gsub!(/%\{[^{}}]+\}/) do |interpolation|
|
59
|
+
token, label = interpolation[2..-2].split(":", 2)
|
60
|
+
|
61
|
+
# Convert tokens with String arguments into It::Links, if they are named link, link_* or *_link
|
62
|
+
if (token == "link" || token.ends_with?("_link") || token.starts_with?("link_")) && (options[token].is_a?(String) || options[token].is_a?(Hash))
|
63
|
+
options[token] = It::Link.new(options[token])
|
64
|
+
end
|
65
|
+
|
66
|
+
if !options.has_key?(token)
|
67
|
+
raise KeyError, "key{#{token}} not found"
|
68
|
+
elsif label && !options[token].is_a?(It::Tag)
|
69
|
+
raise ArgumentError, "key{#{token}} has an argument, so it cannot resolved with a #{options[token].class}"
|
70
|
+
elsif label # Normal tags
|
71
|
+
options[token].process(raw label)
|
72
|
+
elsif options[token].is_a?(It::Tag) # Empty tag
|
73
|
+
options[token].process
|
74
|
+
else # Normal interpolations, as I18n.t would do it.
|
75
|
+
h(options[token])
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
raw translation
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
data/lib/it/link.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module It
|
2
|
+
# A class for links
|
3
|
+
class Link < Tag
|
4
|
+
include ActionView::Helpers::UrlHelper
|
5
|
+
|
6
|
+
# See It.link for details. You can do everything there and save 6 characters.
|
7
|
+
def initialize(href, options = {})
|
8
|
+
raise TypeError, "Invalid href given" unless href.is_a?(Hash) || href.is_a?(String)
|
9
|
+
super(:a, options)
|
10
|
+
@href = href
|
11
|
+
end
|
12
|
+
|
13
|
+
# Will be called from inside the helper to return the tag with the given content.
|
14
|
+
def process(content)
|
15
|
+
link_to(content, @href, @options)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/it/tag.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module It
|
2
|
+
# A generic class for html tags.
|
3
|
+
class Tag
|
4
|
+
include ActionView::Helpers::TagHelper
|
5
|
+
|
6
|
+
attr_reader :tag_name, :options
|
7
|
+
|
8
|
+
# See It.tag for details. You can do everything there and save 6 characters.
|
9
|
+
def initialize(tag_name, options = {})
|
10
|
+
raise TypeError, "tag_name must be specified as a String or Symbol" unless tag_name.is_a?(String) || tag_name.is_a?(Symbol)
|
11
|
+
raise TypeError, "options must be specified as a Hash" unless options.is_a?(Hash)
|
12
|
+
@tag_name = tag_name.to_sym
|
13
|
+
@options = options.symbolize_keys
|
14
|
+
end
|
15
|
+
|
16
|
+
# Will be called from inside the helper to return the tag with the given content.
|
17
|
+
def process(content = nil) # :nodoc:
|
18
|
+
if content.nil?
|
19
|
+
tag(@tag_name, @options)
|
20
|
+
else
|
21
|
+
content_tag(@tag_name, content, @options)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'it'
|
5
|
+
|
6
|
+
describe It::Helper, "#it" do
|
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.")
|
10
|
+
|
11
|
+
@view = ActionView::Base.new
|
12
|
+
@controller = ActionController::Base.new
|
13
|
+
@view.controller = @controller
|
14
|
+
end
|
15
|
+
|
16
|
+
after do
|
17
|
+
I18n.locale = :en
|
18
|
+
end
|
19
|
+
|
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.'
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should insert the link into the German translation" do
|
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.'
|
27
|
+
end
|
28
|
+
|
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.'
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should parse other tags as well" do
|
34
|
+
@view.it("test1", :link => It.tag(:b, :class => "classy")).should == 'I\'m containing a <b class="classy">link to Rails</b> in the middle.'
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should mark the result as html safe" do
|
38
|
+
@view.it("test1", :link => It.link("http://www.rubyonrails.org")).html_safe?.should be_true
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should escape all html in the translation" do
|
42
|
+
I18n.backend.store_translations(:en, :test2 => "<a href=\"hax0r\"> & %{link:link -> Rails} in <b>the middle</b>.")
|
43
|
+
@view.it("test2", :link => It.link("http://www.rubyonrails.org")).should == '<a href="hax0r"> & <a href="http://www.rubyonrails.org">link -> Rails</a> in <b>the middle</b>.'
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should also work with 2 links" do
|
47
|
+
I18n.backend.store_translations(:en, :test3 => "I like %{link1:rails} and %{link2:github}.")
|
48
|
+
@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>.'
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should allow normal I18n interpolations" do
|
52
|
+
I18n.backend.store_translations(:en, :test4 => "I'm containing a %{link:link to Rails} in the %{position}.")
|
53
|
+
@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.'
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should allow Intergers as normal interpolation" do
|
57
|
+
I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
|
58
|
+
@view.it("test5", :name => 2).should == 'Hello 2.'
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should escape the HTML in normal interpolations" do
|
62
|
+
I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
|
63
|
+
@view.it("test5", :name => '<a href="http://evil.haxor.com">victim</a>').should == 'Hello <a href="http://evil.haxor.com">victim</a>.'
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should not escape html_safe interpolations" do
|
67
|
+
I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
|
68
|
+
@view.it("test5", :name => '<a href="http://www.rubyonrails.org">Rails</a>'.html_safe).should == 'Hello <a href="http://www.rubyonrails.org">Rails</a>.'
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should allow interpolations inside of links" do
|
72
|
+
I18n.backend.store_translations(:en, :test6 => "Did you read our %{link:nice %{article}}?")
|
73
|
+
@view.it("test6", :link => It.link("/article/2"), :article => "article").should == 'Did you read our <a href="/article/2">nice article</a>?'
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should raise a KeyError, if the key was not given" do
|
77
|
+
expect { @view.it("test1", :blubb => true) }.to raise_error(KeyError, "key{link} not found")
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should raise an ArgumentError, if a String was given for an interpolation with argument" do
|
81
|
+
I18n.backend.store_translations(:en, :test7 => "Sign up %{asdf:here}!")
|
82
|
+
expect { @view.it("test7", :asdf => "Heinz") }.to raise_error(ArgumentError, "key{asdf} has an argument, so it cannot resolved with a String")
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should allow Strings, if the interpolation name is link" do
|
86
|
+
I18n.backend.store_translations(:en, :test8 => "Sign up %{link:here}!")
|
87
|
+
@view.it("test8", :link => "/register").should == 'Sign up <a href="/register">here</a>!'
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should allow Strings, if the interpolation name ends with _link" do
|
91
|
+
I18n.backend.store_translations(:en, :test8 => "Sign up %{register_link:here}!")
|
92
|
+
@view.it("test8", :register_link => "/register").should == 'Sign up <a href="/register">here</a>!'
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should allow Strings, if the interpolation name starts with link_" do
|
96
|
+
I18n.backend.store_translations(:en, :test8 => "Sign up %{link_to_register:here}!")
|
97
|
+
@view.it("test8", :link_to_register => "/register").should == 'Sign up <a href="/register">here</a>!'
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should work with tags without arguments" do
|
101
|
+
I18n.backend.store_translations(:en, :test9 => "We can %{br} do line breaks")
|
102
|
+
@view.it("test9", :br => It.tag(:br)).should == 'We can <br /> do line breaks'
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'it'
|
5
|
+
|
6
|
+
describe It::Link, '.new' do
|
7
|
+
it "should accept a String as frist param" do
|
8
|
+
expect { It::Link.new("http://www.rubyonrails.org/") }.not_to raise_error
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should accept a Hash as first param" do
|
12
|
+
expect { It::Link.new({:controller => "articles", :action => "index"}) }.not_to raise_error
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should raise a TypeError if the first param is an Integer" do
|
16
|
+
expect { It::Link.new(1) }.to raise_error(TypeError)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should accept options as a Hash" do
|
20
|
+
expect { It::Link.new("http://www.rubyonrails.org/", {:id => "identity", :class => "classy"}) }.not_to raise_error
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should raise a TypeError, if the options are a String" do
|
24
|
+
expect { It::Link.new("http://www.rubyonrails.org/", "classy") }.to raise_error(TypeError)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should raise ArgumentError, if called with three params" do
|
28
|
+
expect { It::Link.new("http://www.rubyonrails.org/", {}, :blubb) }.to raise_error(ArgumentError)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe It::Link, '#tag_name' do
|
33
|
+
it "should always return a" do
|
34
|
+
It::Link.new("http://www.rubyonrails.org/").tag_name.should == :a
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe It::Link, '#process' do
|
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>'
|
41
|
+
end
|
42
|
+
end
|
data/spec/it/tag_spec.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'it'
|
5
|
+
|
6
|
+
describe It::Tag, '.new' do
|
7
|
+
it "should work with a parameter (Symbol)" do
|
8
|
+
expect { It::Tag.new(:b) }.not_to raise_error
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should work with a paramter (String)" do
|
12
|
+
expect { It::Tag.new("b") }.not_to raise_error
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should raise TypeError if called with an Integer" do
|
16
|
+
expect { It::Tag.new(1) }.to raise_error(TypeError)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should accept an options Hash" do
|
20
|
+
expect { It::Tag.new(:b, :class => "very_bold") }.not_to raise_error
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should raise a TypeError if called with a String" do
|
24
|
+
expect { It::Tag.new(:b, "very_bold") }.to raise_error(TypeError)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should raise an ArgumentError if called with three params" do
|
28
|
+
expect { It::Tag.new(:b, {}, :blubb) }.to raise_error(ArgumentError)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe It::Tag, '#tag_name' do
|
33
|
+
it "should return the tag as a Symbol if given as a Symbol" do
|
34
|
+
It::Tag.new(:i).tag_name.should == :i
|
35
|
+
end
|
36
|
+
|
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
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe It::Tag, '#options' do
|
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"}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe It::Tag, '#process' do
|
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>'
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should be marked as html safe" do
|
54
|
+
It::Tag.new(:i).process("some text").html_safe.should be_true
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should escape HTML" do
|
58
|
+
It::Tag.new(:i).process("some text & <b>html</b>").should == '<i>some text & <b>html</b></i>'
|
59
|
+
end
|
60
|
+
|
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>'
|
63
|
+
end
|
64
|
+
|
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" />'
|
67
|
+
end
|
68
|
+
end
|
data/spec/it_spec.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'it'
|
5
|
+
|
6
|
+
describe It, '.link' do
|
7
|
+
it "should return an It::Link object" do
|
8
|
+
It.link("https://www.github.com").class.should == It::Link
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should accept one param" do
|
12
|
+
expect { It.link("http://www.rubyonrails.org/") }.not_to raise_error
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should accept two params" do
|
16
|
+
expect { It.link("http://www.rubyonrails.org/", {:id => "identity", :class => "classy"}) }.not_to raise_error
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should raise ArgumentError, if called with three params" do
|
20
|
+
expect { It::Link.new("http://www.rubyonrails.org/", {}, :blubb) }.to raise_error(ArgumentError)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe It, '.tag' do
|
25
|
+
it "should return an It::Tag object" do
|
26
|
+
It.tag(:b).class.should == It::Tag
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should work with a param" do
|
30
|
+
expect { It.tag(:b) }.not_to raise_error
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should accept two params" do
|
34
|
+
expect { It.tag(:b, :class => "very_bold") }.not_to raise_error
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should raise an ArgumentError if called with three params" do
|
38
|
+
expect { It.tag(:b, {}, :blubb) }.to raise_error(ArgumentError)
|
39
|
+
end
|
40
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: it
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
version: 0.0
|
4
|
+
prerelease:
|
5
|
+
version: 0.2.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Johannes Barre
|
@@ -10,9 +10,30 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-07-
|
14
|
-
dependencies:
|
15
|
-
|
13
|
+
date: 2011-07-23 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: actionpack
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 3.0.0
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rspec
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 2.6.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
16
37
|
description:
|
17
38
|
email: igel@igels.net
|
18
39
|
executables: []
|
@@ -24,7 +45,18 @@ extra_rdoc_files:
|
|
24
45
|
files:
|
25
46
|
- MIT-LICENSE
|
26
47
|
- README.textile
|
27
|
-
|
48
|
+
- Rakefile
|
49
|
+
- Gemfile
|
50
|
+
- lib/it.rb
|
51
|
+
- lib/it/helper.rb
|
52
|
+
- lib/it/tag.rb
|
53
|
+
- lib/it/link.rb
|
54
|
+
- spec/it/helper_spec.rb
|
55
|
+
- spec/it/link_spec.rb
|
56
|
+
- spec/it/tag_spec.rb
|
57
|
+
- spec/it_spec.rb
|
58
|
+
- spec/spec_helper.rb
|
59
|
+
homepage: https://github.com/igel/it
|
28
60
|
licenses: []
|
29
61
|
|
30
62
|
post_install_message:
|
@@ -43,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
75
|
requirements:
|
44
76
|
- - ">="
|
45
77
|
- !ruby/object:Gem::Version
|
46
|
-
version:
|
78
|
+
version: 1.3.6
|
47
79
|
requirements: []
|
48
80
|
|
49
81
|
rubyforge_project:
|
@@ -51,5 +83,9 @@ rubygems_version: 1.8.5
|
|
51
83
|
signing_key:
|
52
84
|
specification_version: 3
|
53
85
|
summary: A helper for links and other html tags in your translations
|
54
|
-
test_files:
|
55
|
-
|
86
|
+
test_files:
|
87
|
+
- spec/it/helper_spec.rb
|
88
|
+
- spec/it/link_spec.rb
|
89
|
+
- spec/it/tag_spec.rb
|
90
|
+
- spec/it_spec.rb
|
91
|
+
- spec/spec_helper.rb
|