auto-linker 0.0.1
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.
- checksums.yaml +7 -0
- data/lib/auto-linker.rb +8 -0
- data/lib/auto-linker/config/instagram.rb +15 -0
- data/lib/auto-linker/config/twitter.rb +15 -0
- data/lib/auto-linker/linker.rb +55 -0
- data/lib/auto-linker/regex.rb +11 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 73d0705bcdf4bd6e6004012b4cf7456b6dc6390e
|
4
|
+
data.tar.gz: c7ab11f3b86f6c3ee06fe7341e3752bdb88dcefc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5d7d860e3e8d7dea5cd0473797b0346f88ee84e4e24eea1d3b5a9c7d18d316f4f5f4d16e043b7b6f40621e1cdad1b2408bc68aae8c31b1b49d54713e6c96b7c8
|
7
|
+
data.tar.gz: be5c45b7e84b19a9bea0f9f08f0cfa3764daa086b6267718d3166670d77168d266b6ce02e34eeacfcf1200ca2b2bc5afb15e7d4c99f567bd191330cd763c48df
|
data/lib/auto-linker.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
class Autolinker
|
2
|
+
module Config
|
3
|
+
INSTAGRAM = {
|
4
|
+
:handle_class => "linked-url handle".freeze,
|
5
|
+
:hashtag_class => "linked-url hashtag".freeze,
|
6
|
+
:url_class => "linked-url url".freeze,
|
7
|
+
:email_class => "linked-url email".freeze,
|
8
|
+
|
9
|
+
:handle_url_base => "https://instagram.com/".freeze,
|
10
|
+
:hashtag_url_base => "https://instagram.com/explore/tags/".freeze,
|
11
|
+
|
12
|
+
:target => "_blank".freeze
|
13
|
+
}.freeze
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class Autolinker
|
2
|
+
module Config
|
3
|
+
TWITTER = {
|
4
|
+
:handle_class => "linked-url handle".freeze,
|
5
|
+
:hashtag_class => "linked-url hashtag".freeze,
|
6
|
+
:url_class => "linked-url url".freeze,
|
7
|
+
:email_class => "linked-url email".freeze,
|
8
|
+
|
9
|
+
:handle_url_base => "https://twitter.com/".freeze,
|
10
|
+
:hashtag_url_base => "https://twitter.com/#!/search?q=%23".freeze,
|
11
|
+
|
12
|
+
:target => "_blank".freeze
|
13
|
+
}.freeze
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
class Autolinker
|
2
|
+
attr_reader :config
|
3
|
+
attr_reader :text
|
4
|
+
|
5
|
+
#--
|
6
|
+
# Class Methods
|
7
|
+
#++
|
8
|
+
|
9
|
+
# Top level method used to replace handles, hashtags, url and emails with clickable links.
|
10
|
+
def self.parse(text, default_config = Config::INSTAGRAM, config = {})
|
11
|
+
Autolinker.new(default_config, config).parse(text)
|
12
|
+
end
|
13
|
+
|
14
|
+
#--
|
15
|
+
# Instance Methods
|
16
|
+
#++
|
17
|
+
|
18
|
+
# Merges the default_config and config together
|
19
|
+
def initialize(default_config = Config::INSTAGRAM, config = {})
|
20
|
+
@config = default_config.merge(config)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Replaces handles, hashtags, url and emails with clickable links.
|
24
|
+
def parse(text)
|
25
|
+
return '' unless text
|
26
|
+
|
27
|
+
@text = text
|
28
|
+
replace_url
|
29
|
+
replace_handle
|
30
|
+
replace_email
|
31
|
+
replace_hashtag
|
32
|
+
@text
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
# Replaces handles with clickable links
|
37
|
+
def replace_handle
|
38
|
+
@text.gsub!(Regex::HANDLE, '<a href="' << @config[:handle_url_base] << '\1" class="' << @config[:handle_class] << '" target="' << @config[:target] << '">@\1</a>')
|
39
|
+
end
|
40
|
+
|
41
|
+
# Replaces urls with clickable links
|
42
|
+
def replace_url
|
43
|
+
@text.gsub!(Regex::URL, '<a href="http://\1" class="' << @config[:url_class] << '" target="' << @config[:target] << '">\1</a>')
|
44
|
+
end
|
45
|
+
|
46
|
+
# Replaces emails with clickable links
|
47
|
+
def replace_email
|
48
|
+
@text.gsub!(Regex::EMAIL, '<a href="mailto:\1" class="' << @config[:email_class] << '" target="' << @config[:target] << '">\1</a>')
|
49
|
+
end
|
50
|
+
|
51
|
+
# Replaces hashtags with clickable links
|
52
|
+
def replace_hashtag
|
53
|
+
@text.gsub!(Regex::HASHTAG, '<a href="' << @config[:hashtag_url_base] << '\1" class="' << @config[:hashtag_class] << '" target="' << @config[:target] << '">#\1</a>')
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class Autolinker
|
2
|
+
module Regex extend self
|
3
|
+
HANDLE = /(?:\s|^)(?:@)(\w+)/i
|
4
|
+
|
5
|
+
HASHTAG = /(?:\s|^)(?:#(?!\d+(?:\s|$)))(\w+)(?=\s|$)/i
|
6
|
+
|
7
|
+
URL = /(?:https?:\/\/)?([\da-z\.-]+\.[a-z\.]{2,6}[\/\w \#\.-]*)+\/?/i
|
8
|
+
|
9
|
+
EMAIL = /([a-z0-9_\.-]+@[\da-z\.-]+\.[a-z\.]{2,6})/i
|
10
|
+
end
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: auto-linker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- François Lehoux
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-12 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Automatically replace mentions, hashtags, urls and emails with links
|
14
|
+
for multiple services (twitter, instagram, facebook, ...)
|
15
|
+
email:
|
16
|
+
- f.lehoux2@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/auto-linker.rb
|
22
|
+
- lib/auto-linker/config/instagram.rb
|
23
|
+
- lib/auto-linker/config/twitter.rb
|
24
|
+
- lib/auto-linker/linker.rb
|
25
|
+
- lib/auto-linker/regex.rb
|
26
|
+
homepage: https://github.com/flehoux/auto-linker
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 2.3.0
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: Replace values with links for multiple services
|
50
|
+
test_files: []
|