html-pipeline-hashtag 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.editorconfig +21 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +62 -0
- data/Rakefile +6 -0
- data/html-pipeline-hashtag.gemspec +23 -0
- data/lib/html/pipeline/hashtag.rb +2 -0
- data/lib/html/pipeline/hashtag/hashtag_filter.rb +70 -0
- data/lib/html/pipeline/hashtag/version.rb +7 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 99e9948fdc73821d691dff56564bd95ba226cb22
|
4
|
+
data.tar.gz: 1376e1747eb940625c622414ee3f461cdfb533df
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fdffdae4b8dfda259d5b0cff90f0ac8463325ba17af51a3ead9570824c0a36475ac1344c1ec97c46735299bb85da0dc10a1c4a2c5f119e7817e9cca5863b1902
|
7
|
+
data.tar.gz: 8eb869e289967a69541a1944b641539e86624a83da12f01182f5ed17c90d0d84f2956ba5dcb2b8040dfee88c3c1aaefa47e2678313b8f9370e27adb528bba8c7
|
data/.editorconfig
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# EditorConfig helps developers define and maintain consistent
|
2
|
+
# coding styles between different editors and IDEs
|
3
|
+
# editorconfig.org
|
4
|
+
|
5
|
+
root = true
|
6
|
+
|
7
|
+
|
8
|
+
[*]
|
9
|
+
|
10
|
+
# Change these settings to your own preference
|
11
|
+
indent_style = space
|
12
|
+
indent_size = 2
|
13
|
+
|
14
|
+
# We recommend you to keep these unchanged
|
15
|
+
end_of_line = lf
|
16
|
+
charset = utf-8
|
17
|
+
trim_trailing_whitespace = true
|
18
|
+
insert_final_newline = true
|
19
|
+
|
20
|
+
[*.md]
|
21
|
+
trim_trailing_whitespace = false
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.1
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 German Antsiferov
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# Hashtag filter for html-pipeline
|
2
|
+
|
3
|
+
An [HTML::Pipeline](https://github.com/jch/html-pipeline) filter for [hashtags](https://en.wikipedia.org/wiki/Hashtag).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'html-pipeline-hashtag'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install html-pipeline-hashtag
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Example:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'html/pipeline'
|
27
|
+
require 'html/pipeline/hashtag/hashtag_filter'
|
28
|
+
|
29
|
+
filters = [
|
30
|
+
HTML::Pipeline::MarkdownFilter,
|
31
|
+
HTML::Pipeline::HashtagFilter
|
32
|
+
]
|
33
|
+
|
34
|
+
pipeline = HTML::Pipeline.new filters
|
35
|
+
|
36
|
+
input = "Hello #world!"
|
37
|
+
|
38
|
+
context = {
|
39
|
+
:tag_url => '/tags/%{tag}'
|
40
|
+
}
|
41
|
+
|
42
|
+
pipeline.call(input, context)
|
43
|
+
# => <p>Hello <a href="/tags/world" target="_blank" class="hashtag">#world</a>!</p>
|
44
|
+
|
45
|
+
```
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
def extract_tags(*args)
|
49
|
+
tags = []
|
50
|
+
HTML::Pipeline::HashtagFilter.hashtags_in(*args) { |hashtag, tag| tags << tag }
|
51
|
+
|
52
|
+
tags
|
53
|
+
end
|
54
|
+
|
55
|
+
extract_tags("Hello #world! #welcome")
|
56
|
+
# => ["world", "welcome"]
|
57
|
+
```
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mr-dxdy/html-pipeline-hashtag.
|
62
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'html/pipeline/hashtag/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "html-pipeline-hashtag"
|
8
|
+
spec.version = Html::Pipeline::Hashtag::VERSION
|
9
|
+
spec.authors = ["German Antsiferov"]
|
10
|
+
spec.email = ["dxdy@bk.ru"]
|
11
|
+
|
12
|
+
spec.summary = %q{HTML Pipeline filter that replaces hashtags with links.}
|
13
|
+
spec.description = %q{HTML Pipeline filter that replaces hashtags with links.}
|
14
|
+
spec.homepage = "https://github.com/mr-dxdy/html-pipeline-hashtag"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'html-pipeline', '~> 1.11.0'
|
22
|
+
spec.add_development_dependency "rspec", '~> 3.2.0'
|
23
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
require 'html/pipeline'
|
4
|
+
|
5
|
+
module HTML
|
6
|
+
class Pipeline
|
7
|
+
# HTML filter that replaces @hashtag with link. Hashtags within <pre>,
|
8
|
+
# <code>, and <a> elements are ignored.
|
9
|
+
#
|
10
|
+
# Context options:
|
11
|
+
# :tag_url - Used to construct links to search by hashtag.
|
12
|
+
# Example: http://localhost/search?q=%{tag}
|
13
|
+
# :hashtag_pattern - Used to provide a custom regular expression to
|
14
|
+
# indentify hashtags
|
15
|
+
#
|
16
|
+
class HashtagFilter < Filter
|
17
|
+
def self.hashtags_in(text, hashtag_pattern = HashtagPattern)
|
18
|
+
text.gsub hashtag_pattern do |match|
|
19
|
+
tag = $1
|
20
|
+
yield match, tag
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
HashtagPattern = /#([\p{L}\w\-]+)/
|
25
|
+
|
26
|
+
IGNORE_PARENTS = %w(pre code a style).to_set
|
27
|
+
|
28
|
+
def call
|
29
|
+
search_text_nodes(doc).each do |node|
|
30
|
+
content = node.to_html
|
31
|
+
|
32
|
+
next if !content.include?('#')
|
33
|
+
next if has_ancestor?(node, IGNORE_PARENTS)
|
34
|
+
|
35
|
+
html = hashtag_link_filter(content, hashtag_pattern)
|
36
|
+
node.replace(html)
|
37
|
+
end
|
38
|
+
|
39
|
+
doc
|
40
|
+
end
|
41
|
+
|
42
|
+
def tag_url
|
43
|
+
context[:tag_url] || default_tag_url
|
44
|
+
end
|
45
|
+
|
46
|
+
def default_tag_url
|
47
|
+
'/tags/%{tag}'
|
48
|
+
end
|
49
|
+
|
50
|
+
def hashtag_pattern
|
51
|
+
context[:hashtag_pattern] || HashtagPattern
|
52
|
+
end
|
53
|
+
|
54
|
+
def hashtag_link_filter(text, hashtag_pattern)
|
55
|
+
self.class.hashtags_in(text, hashtag_pattern) do |match, tag|
|
56
|
+
link_to_hashtag(tag)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def link_to_hashtag(tag)
|
61
|
+
url = tag_url % { tag: tag }
|
62
|
+
link_pattern % { url: url, tag: tag }
|
63
|
+
end
|
64
|
+
|
65
|
+
def link_pattern
|
66
|
+
"<a href='%{url}' target='_blank' class='hashtag'>#%{tag}</a>"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: html-pipeline-hashtag
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- German Antsiferov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: html-pipeline
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.11.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.11.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.2.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.2.0
|
41
|
+
description: HTML Pipeline filter that replaces hashtags with links.
|
42
|
+
email:
|
43
|
+
- dxdy@bk.ru
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".editorconfig"
|
49
|
+
- ".gitignore"
|
50
|
+
- ".rspec"
|
51
|
+
- ".ruby-version"
|
52
|
+
- ".travis.yml"
|
53
|
+
- Gemfile
|
54
|
+
- LICENSE.txt
|
55
|
+
- README.md
|
56
|
+
- Rakefile
|
57
|
+
- html-pipeline-hashtag.gemspec
|
58
|
+
- lib/html/pipeline/hashtag.rb
|
59
|
+
- lib/html/pipeline/hashtag/hashtag_filter.rb
|
60
|
+
- lib/html/pipeline/hashtag/version.rb
|
61
|
+
homepage: https://github.com/mr-dxdy/html-pipeline-hashtag
|
62
|
+
licenses: []
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.4.5
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: HTML Pipeline filter that replaces hashtags with links.
|
84
|
+
test_files: []
|