gr_autolink 1.0.10 → 1.0.11

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.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,7 @@
1
+ === 1.0.11 / 2012-04-17
2
+
3
+ * adding ability to truncate the URL as link text to specified length
4
+
1
5
  === 1.0.10 / 2012-04-02
2
6
 
3
7
  * bug fix: fix the ...yes/no bug mentioned below
data/lib/gr_autolink.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module GrAutolink
2
- VERSION = '1.0.10'
2
+ VERSION = '1.0.11'
3
3
 
4
4
  class Railtie < ::Rails::Railtie
5
5
  initializer 'gr_autolink' do |app|
@@ -15,6 +15,8 @@ module GrAutolink
15
15
  # text given is sanitized, you can override this behaviour setting the
16
16
  # <tt>:sanitize</tt> option to false, or you can add options to the sanitization of
17
17
  # the text using the <tt>:sanitize_options</tt> option hash.
18
+ # An optional flag <tt>:max_link_length</tt> can be passed to let the function truncate
19
+ # the url text to the specified amount of characters
18
20
  #
19
21
  # ==== Examples
20
22
  # auto_link("Go to http://www.rubyonrails.org and say hello to david@loudthinking.com")
@@ -35,6 +37,8 @@ module GrAutolink
35
37
  # # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>.
36
38
  # Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
37
39
  #
40
+ # auto_link("Visit http://www.verylonglink.com/p/r/t/y/u/i/o/p/a.html", :max_link_length=>20)
41
+ # # => "Visit <a href=\"http://www.verylonglink.com/p/r/t/y/u/i/o/p/a.html\">http://www.verylo...</a>"
38
42
  #
39
43
  # You can still use <tt>auto_link</tt> with the old API that accepts the
40
44
  # +link+ as its optional second parameter and the +html_options+ hash
@@ -129,6 +133,10 @@ module GrAutolink
129
133
  href = escape_once(href)
130
134
  end
131
135
 
136
+ if options[:max_link_length]
137
+ link_text = truncate(link_text, :length=>options[:max_link_length])
138
+ end
139
+
132
140
  content_tag(:a, link_text, link_attributes.merge('href' => href), !!options[:sanitize]) + punctuation.reverse.join('')
133
141
  end
134
142
  end
@@ -1,5 +1,4 @@
1
- # encoding: utf-8
2
-
1
+ # -*- coding: utf-8 -*-
3
2
  require "minitest/autorun"
4
3
  require "rails"
5
4
  require "gr_autolink/helpers"
@@ -330,6 +329,15 @@ class TestGrAutolink < MiniTest::Unit::TestCase
330
329
  def test_auto_link_retains_non_html_safeness_on_strings_with_urls_to_autolink_sanitize_false
331
330
  assert !auto_link("my link is http://123.abc.org", :sanitize => false).html_safe?
332
331
  end
332
+
333
+ def test_auto_link_truncation_option
334
+ link_text = "http://www.a1234567890.com"
335
+ input = "my link needs truncation #{link_text} and so forth"
336
+ expected_truncation = "my link needs truncation <a href=\"#{link_text}\">http://www.a...</a> and so forth"
337
+ expected_without_truncation = "my link needs truncation <a href=\"#{link_text}\">#{link_text}</a> and so forth"
338
+ assert auto_link(input, :max_link_length=>15) == expected_truncation
339
+ assert auto_link(input, :max_link_length=>link_text.size+1) == expected_without_truncation
340
+ end
333
341
 
334
342
  private
335
343
  def generate_result(link_text, href = nil)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gr_autolink
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.10
4
+ version: 1.0.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,11 +12,11 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2012-04-02 00:00:00.000000000 Z
15
+ date: 2012-04-18 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: rails
19
- requirement: &2152851320 !ruby/object:Gem::Requirement
19
+ requirement: &2160991140 !ruby/object:Gem::Requirement
20
20
  none: false
21
21
  requirements:
22
22
  - - ~>
@@ -24,10 +24,10 @@ dependencies:
24
24
  version: '3.1'
25
25
  type: :runtime
26
26
  prerelease: false
27
- version_requirements: *2152851320
27
+ version_requirements: *2160991140
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rdoc
30
- requirement: &2152850720 !ruby/object:Gem::Requirement
30
+ requirement: &2160990480 !ruby/object:Gem::Requirement
31
31
  none: false
32
32
  requirements:
33
33
  - - ~>
@@ -35,10 +35,10 @@ dependencies:
35
35
  version: '3.10'
36
36
  type: :development
37
37
  prerelease: false
38
- version_requirements: *2152850720
38
+ version_requirements: *2160990480
39
39
  - !ruby/object:Gem::Dependency
40
40
  name: hoe
41
- requirement: &2152850180 !ruby/object:Gem::Requirement
41
+ requirement: &2160989780 !ruby/object:Gem::Requirement
42
42
  none: false
43
43
  requirements:
44
44
  - - ~>
@@ -46,7 +46,7 @@ dependencies:
46
46
  version: '2.16'
47
47
  type: :development
48
48
  prerelease: false
49
- version_requirements: *2152850180
49
+ version_requirements: *2160989780
50
50
  description: ! "This is an adaptation of the extraction of the `auto_link` method
51
51
  from rails\nthat is the rails_autolink gem. The `auto_link`\nmethod was removed
52
52
  from Rails in version Rails 3.1. This gem is meant to\nbridge the gap for people