anchored 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2d18bbdc9897556c387977f828f4e4420750510c
4
+ data.tar.gz: 8480d3332a06d0bf5a0be06f8194a3f7f0816e37
5
+ SHA512:
6
+ metadata.gz: 503d0303621f0ed29ddd3d861d64468c64c6dbe8ea682c60a127e1ad48d327101fdd4cda483dd0f885971e098ef04bc487f1c73d7e1b07dc95b856f881f77a5d
7
+ data.tar.gz: 3eaa07621c056fd651ca4a5577f8a2a8d7e7c25a42a494bc75ab5e8d82857914a482e1cb18a94b14c2ad60cfea2397cb456a7c2de76a271f80a5e351c9df79c7
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Neighborland, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,84 @@
1
+ [![Gem Version](https://badge.fury.io/rb/anchored.svg)][gem]
2
+ [![Build Status](https://travis-ci.org/neighborland/anchored.svg?branch=master)][build]
3
+
4
+ [gem]: http://rubygems.org/gems/anchored
5
+ [build]: https://travis-ci.org/neighborland/anchored
6
+
7
+ # Anchored
8
+
9
+ Ruby auto-linking of only URLs, with no HTML sanitization.
10
+
11
+ Copied from https://github.com/tenderlove/rails_autolink.
12
+
13
+ ## Install
14
+
15
+ Add to your Gemfile:
16
+
17
+ ```ruby
18
+ gem "anchored"
19
+ ```
20
+
21
+ Or:
22
+
23
+ ```sh
24
+ $ gem install anchored
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ```
30
+ require "anchored"
31
+
32
+ Anchored::Linker.auto_link("text")
33
+ => "text"
34
+
35
+
36
+ # Wrap URLs in anchors:
37
+
38
+ Anchored::Linker.auto_link("hello www.google.com.")
39
+ => "hello <a href='http://www.google.com'>www.google.com</a>."
40
+
41
+
42
+ # Set anchor attributes. Alter the link text with a block:
43
+
44
+ text = "Welcome to http://www.dogedogedoge.com/."
45
+ Anchored::Linker.auto_link(text, target: "_blank") do |text|
46
+ text[0...12] + "..."
47
+ end
48
+ # => "Welcome to <a href=\"http://www.dogedogedoge.com/\" target=\"_blank\">http://dogedo...</a>."
49
+
50
+
51
+ # Remove the target attribute when a URL matches a domain:
52
+
53
+ text = "Hello http://www.example.com/."
54
+ Anchored::Linker.auto_link(text, target: "_blank", domain: "example.com")
55
+ # => "Hello <a href=\"http://www.example.com/\">http://example.com</a>."
56
+ ```
57
+
58
+ Anchored does not sanitize html. Be sure to use something else for that.
59
+
60
+ ## Differences from `rails_autolink`
61
+
62
+ * No HTML sanitization
63
+ * No email auto-linking
64
+ * No dependencies on rails
65
+ * Dropped support for uncommon protocols (gopher, etc)
66
+ * Option to remove target attribute when URL matches a domain
67
+
68
+ ## Development
69
+
70
+ Run `bin/setup` to install dependencies. Then, run `rake test` to run the tests.
71
+
72
+ You can also run `bin/console` for an irb prompt that will allow you to experiment.
73
+
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at https://github.com/neighborland/anchored.
78
+ This project is intended to be a safe, welcoming space for collaboration, and contributors
79
+ are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
80
+
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "anchored/version"
4
+ require "anchored/linker"
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Anchored
4
+ module Linker
5
+ extend self
6
+
7
+ def auto_link(text, options = {}, &block)
8
+ return "" if text.to_s.empty?
9
+ auto_link_urls(text, options, &block)
10
+ end
11
+
12
+ # remove_target_if_local("http://same.com/x", "same.com", target: "_blank")
13
+ # => <a href="http://same.com/x">http://same.com/x</a>
14
+ #
15
+ # remove_target_if_local("http://same.com/x", "different.com", target: "_blank")
16
+ # => <a href="http://same.com/x" target="_blank">http://same.com/x</a>
17
+ #
18
+ # modifies options in place
19
+ def remove_target_if_local(href, domain, options)
20
+ return unless options[:target]
21
+ options.delete(:target) if href.include?("//#{domain}")
22
+ end
23
+
24
+ private
25
+
26
+ AUTO_LINK_RE = %r{(?: ((?:ftp|http|https):)// | www\. )[^\s<\u00A0"]+}ix
27
+
28
+ # regexps for determining context, used high-volume
29
+ AUTO_LINK_CRE = [/<[^>]+$/, /^[^>]*>/, /<a\b.*?>/i, /<\/a>/i].freeze
30
+
31
+ PUNCTUATION_RE = /[^\p{Word}\/-=&]$/
32
+
33
+ BRACKETS = { "]" => "[", ")" => "(", "}" => "{" }.freeze
34
+
35
+ # Turns all urls into clickable links. If a block is given, each url
36
+ # is yielded and the result is used as the link text.
37
+ def auto_link_urls(text, options = {})
38
+ text.gsub(AUTO_LINK_RE) do
39
+ match = Regexp.last_match
40
+ href = match[0]
41
+ scheme = match[1]
42
+ punctuation = []
43
+
44
+ if auto_linked?(match)
45
+ # do not change string; URL is already linked
46
+ href
47
+ else
48
+ # don't include trailing punctuation character as part of the URL
49
+ while href.sub!(PUNCTUATION_RE, "")
50
+ punctuation.push Regexp.last_match(0)
51
+ if (opening = BRACKETS[punctuation.last]) && href.scan(opening).size > href.scan(punctuation.last).size
52
+ href << punctuation.pop
53
+ break
54
+ end
55
+ end
56
+
57
+ link_text = block_given? ? yield(href) : href
58
+ href = "http://" + href unless scheme
59
+
60
+ # content_tag(:a, link_text, html.merge(href: href)) + punctuation.reverse.join('')
61
+ anchor_tag(href, link_text, options) + punctuation.reverse.join
62
+ end
63
+ end
64
+ end
65
+
66
+ # Detects already linked context or position in the middle of a tag
67
+ # Note: this changes the current Regexp
68
+ def auto_linked?(match)
69
+ left = match.pre_match
70
+ right = match.post_match
71
+ (left =~ AUTO_LINK_CRE[0] && right =~ AUTO_LINK_CRE[1]) ||
72
+ (left.rindex(AUTO_LINK_CRE[2]) && Regexp.last_match.post_match !~ AUTO_LINK_CRE[3])
73
+ end
74
+
75
+ def anchor_attrs(options)
76
+ options.map { |k, v| %(#{k}="#{v}") }.unshift("").join(" ")
77
+ end
78
+
79
+ def anchor_tag(href, text, options)
80
+ if (domain = options.delete(:domain))
81
+ remove_target_if_local href, domain, options
82
+ end
83
+ %(<a href="#{href}"#{anchor_attrs(options)}>#{text}</a>)
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Anchored
4
+ VERSION = "1.0.0"
5
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: anchored
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tee Parham
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.10'
55
+ description: Ruby auto linker based on rails_autolink. It wraps links in text with
56
+ HTML anchors.
57
+ email:
58
+ - tee@neighborland.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - LICENSE.txt
64
+ - README.md
65
+ - lib/anchored.rb
66
+ - lib/anchored/linker.rb
67
+ - lib/anchored/version.rb
68
+ homepage: https://github.com/neighborland/anchored
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.6.10
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Ruby auto linker
92
+ test_files: []