html-pipeline-issue_references 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +66 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/html-pipeline-issue_references.gemspec +26 -0
- data/lib/html/pipeline/issue_reference_filter.rb +97 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c6263fd984baa3f1bd97ac973dc28b917d319768
|
4
|
+
data.tar.gz: cd6c1e8d6b2a1bf522ae38ba7d7c27d2673dc036
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 43f96285939e7b36ded7828013a2ee60b1ee04e1775d6491f45ace5062908f8a7742b2e19288c4c0698791954f477d7713e9d4827701b4c2c4c17b1344da17fb
|
7
|
+
data.tar.gz: db9d0d29803b15adc46a5ea64550a4ce0f11104b2890ed954a5bd36e50f7fdf11a8daf7be4f53a0d825f5f8673e76fdda858ea3d0ee67f3097df26ebc92b6805
|
data/.gitignore
ADDED
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 Derrick Reimer
|
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,66 @@
|
|
1
|
+
# HTML::Pipeline::IssueReferences
|
2
|
+
|
3
|
+
An HTML::Pipeline filter for auto-linking GitHub issue references.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'html-pipeline-issue_references'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install html-pipeline-issue_references
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Suppose you have some text that contains references to GitHub issues, like this:
|
24
|
+
|
25
|
+
```
|
26
|
+
Fixes rails/rails#123
|
27
|
+
```
|
28
|
+
|
29
|
+
This filter will automatically transform the GitHub-style issue references into
|
30
|
+
a bonafide hyperlink to the actual issue. For example, this code:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
require "html/pipeline"
|
34
|
+
require "html/pipeline/issue_references"
|
35
|
+
|
36
|
+
pipeline = HTML::Pipeline.new [
|
37
|
+
HTML::Pipeline::IssueReferenceFilter
|
38
|
+
]
|
39
|
+
|
40
|
+
result = pipeline.call("Fixes rails/rails#123", {
|
41
|
+
base_url: "https://github.com",
|
42
|
+
repository: "foo/bar"
|
43
|
+
})
|
44
|
+
|
45
|
+
puts result[:output].to_html
|
46
|
+
```
|
47
|
+
|
48
|
+
will output this:
|
49
|
+
|
50
|
+
```
|
51
|
+
Fixes <a href='https://github.com/rails/rails/issues/123' class='issue-reference'>rails/rails#123</a>
|
52
|
+
```
|
53
|
+
|
54
|
+
## Development
|
55
|
+
|
56
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
57
|
+
|
58
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
59
|
+
|
60
|
+
## Contributing
|
61
|
+
|
62
|
+
1. Fork it ( https://github.com/codetree/html-pipeline-issue_references/fork )
|
63
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
64
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
65
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
66
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "html/pipeline/issue_references"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,26 @@
|
|
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/issue_reference_filter'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "html-pipeline-issue_references"
|
8
|
+
spec.version = HTML::Pipeline::IssueReferenceFilter::VERSION
|
9
|
+
spec.authors = ["Derrick Reimer"]
|
10
|
+
spec.email = ["derrickreimer@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{An HTML::Pipeline filter for auto-linking GitHub issue references}
|
13
|
+
spec.description = spec.summary
|
14
|
+
spec.homepage = "https://github.com/codetree/html-pipeline-issue_references"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "html-pipeline", "~> 2.0"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require "html/pipeline"
|
2
|
+
require "set"
|
3
|
+
|
4
|
+
module HTML
|
5
|
+
class Pipeline
|
6
|
+
class IssueReferenceFilter < Filter
|
7
|
+
VERSION = "0.1.0".freeze
|
8
|
+
|
9
|
+
REPOSITORY_NAME = /[a-z0-9][a-z0-9\-]*\/[a-z0-9][a-z0-9\-_]*/ix
|
10
|
+
|
11
|
+
# Match references of the form:
|
12
|
+
#
|
13
|
+
# - #123
|
14
|
+
# - codetree/feedback#123
|
15
|
+
# - GH-123
|
16
|
+
#
|
17
|
+
# See http://rubular.com/r/evB7RlvUfI
|
18
|
+
SHORT_PATTERN = /
|
19
|
+
(?:^|\W) # beginning of string or non-word char
|
20
|
+
(
|
21
|
+
(?:(#{REPOSITORY_NAME})? # repository name (optional)
|
22
|
+
\#|(?:GH\-))(\d+) # issue number
|
23
|
+
)
|
24
|
+
(?=
|
25
|
+
\.+[ \t\W]| # dots followed by space or non-word character
|
26
|
+
\.+$| # dots at end of line
|
27
|
+
[^0-9a-zA-Z_.]| # non-word character except dot
|
28
|
+
$ # end of line
|
29
|
+
)
|
30
|
+
/ix
|
31
|
+
|
32
|
+
# Match references of the form:
|
33
|
+
#
|
34
|
+
# - https://github.com/codetree/feedback/issues/123
|
35
|
+
# - https://github.com/codetree/feedback/pulls/123
|
36
|
+
URL_PATTERN = /
|
37
|
+
(?:^|\W) # beginning of string or non-word char
|
38
|
+
https:\/\/github.com\/
|
39
|
+
(#{REPOSITORY_NAME}) # repository name
|
40
|
+
\/(?:issues|pulls)\/
|
41
|
+
(\d+) # issue number
|
42
|
+
(?=
|
43
|
+
\.+[ \t\W]| # dots followed by space or non-word character
|
44
|
+
\.+$| # dots at end of line
|
45
|
+
[^0-9a-zA-Z_.]| # non-word character except dot
|
46
|
+
$ # end of line
|
47
|
+
)
|
48
|
+
/ix
|
49
|
+
|
50
|
+
# Don't look for mentions in text nodes that are children of these elements
|
51
|
+
IGNORE_PARENTS = %w(pre code a style).to_set
|
52
|
+
|
53
|
+
def self.issue_references_in(text)
|
54
|
+
text.gsub SHORT_PATTERN do |match|
|
55
|
+
repository = $2
|
56
|
+
number = $3
|
57
|
+
yield match, repository, number
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def default_repo
|
62
|
+
context[:repository]
|
63
|
+
end
|
64
|
+
|
65
|
+
def call
|
66
|
+
doc.search('.//text()').each do |node|
|
67
|
+
content = node.to_html
|
68
|
+
next if has_ancestor?(node, IGNORE_PARENTS)
|
69
|
+
html = issue_reference_filter(content, base_url, default_repo)
|
70
|
+
next if html == content
|
71
|
+
node.replace(html)
|
72
|
+
end
|
73
|
+
|
74
|
+
doc
|
75
|
+
end
|
76
|
+
|
77
|
+
def issue_reference_filter(text, base_url = '/', default_repo = nil)
|
78
|
+
self.class.issue_references_in(text) do |match, referenced_repo, number|
|
79
|
+
repo = referenced_repo || default_repo
|
80
|
+
leading_whitespace = match.start_with?(' ') ? ' ' : ''
|
81
|
+
repo ? leading_whitespace + link_to_issue(repo, number, default_repo) : match
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def link_to_issue(repo, number, default_repo)
|
86
|
+
content = "##{number}"
|
87
|
+
content = "#{repo}#{content}" if repo != default_repo
|
88
|
+
|
89
|
+
url = base_url.dup
|
90
|
+
url << "/" unless url =~ /[\/~]\z/
|
91
|
+
url << "#{repo}/issues/#{number}"
|
92
|
+
|
93
|
+
"<a href='#{url}' class='issue-reference'>#{content}</a>"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: html-pipeline-issue_references
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Derrick Reimer
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-25 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: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.8'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.8'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: An HTML::Pipeline filter for auto-linking GitHub issue references
|
56
|
+
email:
|
57
|
+
- derrickreimer@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .travis.yml
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- html-pipeline-issue_references.gemspec
|
71
|
+
- lib/html/pipeline/issue_reference_filter.rb
|
72
|
+
homepage: https://github.com/codetree/html-pipeline-issue_references
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.3.0
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: An HTML::Pipeline filter for auto-linking GitHub issue references
|
96
|
+
test_files: []
|