html-pipeline-linkify_github 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/CONTRIBUTING.md +7 -0
- data/Gemfile +1 -0
- data/{LICENSE → LICENSE.md} +0 -0
- data/README.md +23 -22
- data/lib/html/pipeline/linkify_github.rb +43 -39
- data/lib/html/pipeline/linkify_github/version.rb +1 -1
- data/test/linkify_github_integration_test.rb +32 -0
- data/test/linkify_github_test.rb +26 -107
- data/test/test_helper.rb +17 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b7d544ff42bdaed0ad76d2b225dc17eda21a8ce9
|
4
|
+
data.tar.gz: 7ef282549e3dbcdaa6df7fec6b8aa9dccf0d0ba6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d8d7f78298b42446740ec56ffafbad473c0f7c361a370d9c4b571421f3c278da68f50094df93d004003a855f03aad00f8e0c821c9c1809cd50b135e2a835e76
|
7
|
+
data.tar.gz: 90c5eac937732170879a89d1c59d9cedda236375251e4e867344a21afe9bca021a70d9ae6e954cefdc4761b21aa04daab33fcc76abf5e2dfffa4e9a9021ec780
|
data/CHANGELOG.md
CHANGED
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
1. [Fork it](https://github.com/jollygoodcode/html-pipeline-linkify_github/fork)
|
4
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
5
|
+
3. Commit your changes (`git commit -am "Add some feature"`)
|
6
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
7
|
+
5. Create a new Pull Request
|
data/Gemfile
CHANGED
data/{LICENSE → LICENSE.md}
RENAMED
File without changes
|
data/README.md
CHANGED
@@ -21,52 +21,53 @@ Or install it yourself as:
|
|
21
21
|
|
22
22
|
## Usage
|
23
23
|
|
24
|
-
**Use `HTML::Pipeline::LinkifyGitHubFilter` filter
|
24
|
+
**Use `HTML::Pipeline::LinkifyGitHubFilter` filter after your markdown filter.**
|
25
25
|
|
26
26
|
```ruby
|
27
27
|
require "html/pipeline"
|
28
28
|
require "html/pipeline/linkify_github"
|
29
29
|
|
30
30
|
pipeline = HTML::Pipeline.new [
|
31
|
-
HTML::Pipeline::
|
32
|
-
HTML::Pipeline::
|
31
|
+
HTML::Pipeline::MarkdownFilter,
|
32
|
+
HTML::Pipeline::LinkifyGitHubFilter
|
33
33
|
]
|
34
34
|
|
35
|
-
result = pipeline.call <<-
|
35
|
+
result = pipeline.call <<-MARKDOWN.strip_heredoc
|
36
36
|
https://github.com/rails/rails/pull/21862
|
37
37
|
https://github.com/rails/rails/issues/21843
|
38
38
|
https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864
|
39
|
-
|
39
|
+
MARKDOWN
|
40
40
|
|
41
|
-
puts result[:output]
|
41
|
+
puts result[:output].to_html
|
42
42
|
```
|
43
43
|
|
44
44
|
prints:
|
45
45
|
|
46
|
-
```
|
47
|
-
|
48
|
-
|
49
|
-
|
46
|
+
```html
|
47
|
+
<p><a href="https://github.com/rails/rails/pull/21862">rails/rails#21862</a><br>
|
48
|
+
<a href="https://github.com/rails/rails/issues/21843">rails/rails#21843</a><br>
|
49
|
+
<a href="https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864">rails/rails@`67597e`</a></p>
|
50
50
|
```
|
51
51
|
|
52
|
-
## Contributing
|
53
|
-
|
54
|
-
1. [Fork it](https://github.com/jollygoodcode/html-pipeline-linkify_github/fork)
|
55
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
56
|
-
3. Commit your changes (`git commit -am "Add some feature"`)
|
57
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
58
|
-
5. Create a new Pull Request
|
59
|
-
|
60
52
|
## Notes
|
61
53
|
|
62
54
|
This RubyGem requires Ruby 2.0+ because [support for 1.9.3 is officially end](https://www.ruby-lang.org/en/news/2014/01/10/ruby-1-9-3-will-end-on-2015/).
|
63
55
|
|
64
|
-
##
|
56
|
+
## Contributing
|
65
57
|
|
66
|
-
|
58
|
+
Please see the [CONTRIBUTING.md](/CONTRIBUTING.md) file.
|
67
59
|
|
68
|
-
|
60
|
+
## Credits
|
61
|
+
|
62
|
+
A huge THANK YOU to all our [contributors](https://github.com/jollygoodcode/html-pipeline-linkify_github/graphs/contributors)! :heart:
|
69
63
|
|
70
64
|
## License
|
71
65
|
|
72
|
-
|
66
|
+
Please see the [LICENSE.md](/LICENSE.md) file.
|
67
|
+
|
68
|
+
## Maintained by Jolly Good Code
|
69
|
+
|
70
|
+
[![Jolly Good Code](https://cloud.githubusercontent.com/assets/1000669/9362336/72f9c406-46d2-11e5-94de-5060e83fcf83.jpg)](http://www.jollygoodcode.com)
|
71
|
+
|
72
|
+
We specialise in Agile practices and Ruby, and we love contributing to open source.
|
73
|
+
[Speak to us](http://www.jollygoodcode.com/#get-in-touch) about your next big idea, or [check out our projects](http://www.jollygoodcode.com/open-source).
|
@@ -6,80 +6,84 @@ module HTML
|
|
6
6
|
#
|
7
7
|
# For example:
|
8
8
|
#
|
9
|
-
# https://github.com/rails/rails/pull/21862
|
10
|
-
# https://github.com/rails/rails/issues/21843
|
11
|
-
# https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864
|
9
|
+
# <a href="https://github.com/rails/rails/pull/21862">https://github.com/rails/rails/pull/21862</a>
|
10
|
+
# <a href="https://github.com/rails/rails/issues/21843">https://github.com/rails/rails/issues/21843</a>
|
11
|
+
# <a href="https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864">https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864</a>
|
12
12
|
#
|
13
13
|
# =>
|
14
14
|
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
15
|
+
# <a href="https://github.com/rails/rails/pull/21862">rails/rails#21862</a>
|
16
|
+
# <a href="https://github.com/rails/rails/issues/21843">rails/rails#21843</a>
|
17
|
+
# <a href="https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864">rails/rails@`67597e`</a>
|
18
18
|
#
|
19
19
|
# This filter does not write any additional information to the context hash.
|
20
|
-
class LinkifyGitHubFilter <
|
21
|
-
GITHUB_URL = "github.com".freeze
|
22
|
-
PULL = "/pull/".freeze
|
23
|
-
ISSUES = "/issues/".freeze
|
24
|
-
COMMIT = "/commit/".freeze
|
25
|
-
|
20
|
+
class LinkifyGitHubFilter < Filter
|
26
21
|
PULL_REQUEST_REGEXP = %r{https?://(www.)?github.com/(?<owner>.+)/(?<repo>.+)/pull/(?<number>\d+)/?}.freeze
|
27
22
|
ISSUES_REGEXP = %r{https?://(www.)?github.com/(?<owner>.+)/(?<repo>.+)/issues/(?<number>\d+)/?}.freeze
|
28
23
|
COMMIT_REGEXP = %r{https?://(www.)?github.com/(?<owner>.+)/(?<repo>.+)/commit/(?<number>\w+)/?}.freeze
|
29
24
|
|
30
|
-
# Convert GitHub urls into friendly markdown.
|
31
25
|
def call
|
32
|
-
|
26
|
+
doc.search("a").each do |element|
|
27
|
+
next if element["href"].to_s.empty?
|
28
|
+
|
29
|
+
text = element.content
|
33
30
|
|
34
|
-
|
35
|
-
|
36
|
-
|
31
|
+
element.content = if is_a_pull_request_link? text
|
32
|
+
replace_pull_request_link(text)
|
33
|
+
elsif is_a_issue_link? text
|
34
|
+
replace_issue_link(text)
|
35
|
+
elsif is_a_commit_link? text
|
36
|
+
replace_commit_link(text)
|
37
|
+
else
|
38
|
+
text
|
39
|
+
end
|
40
|
+
end
|
37
41
|
|
38
|
-
|
42
|
+
doc
|
39
43
|
end
|
40
44
|
|
41
45
|
private
|
42
46
|
|
43
|
-
def
|
44
|
-
|
47
|
+
def is_a_pull_request_link?(text)
|
48
|
+
text.include?("/pull/".freeze)
|
45
49
|
end
|
46
50
|
|
47
|
-
def
|
48
|
-
|
51
|
+
def is_a_issue_link?(text)
|
52
|
+
text.include?("/issues/".freeze)
|
49
53
|
end
|
50
54
|
|
51
|
-
def
|
52
|
-
|
55
|
+
def is_a_commit_link?(text)
|
56
|
+
text.include?("/commit/".freeze)
|
53
57
|
end
|
54
58
|
|
55
|
-
def
|
56
|
-
|
57
|
-
|
59
|
+
def replace_pull_request_link(text)
|
60
|
+
text.match(PULL_REQUEST_REGEXP) do
|
61
|
+
pull_request_shorthand($1, $2, $3)
|
58
62
|
end
|
59
63
|
end
|
60
64
|
|
61
|
-
def
|
62
|
-
|
63
|
-
|
65
|
+
def replace_issue_link(text)
|
66
|
+
text.match(ISSUES_REGEXP) do
|
67
|
+
issue_shorthand($1, $2, $3)
|
64
68
|
end
|
65
69
|
end
|
66
70
|
|
67
|
-
def
|
68
|
-
|
69
|
-
|
71
|
+
def replace_commit_link(text)
|
72
|
+
text.match(COMMIT_REGEXP) do
|
73
|
+
commit_shorthand($1, $2, $3)
|
70
74
|
end
|
71
75
|
end
|
72
76
|
|
73
|
-
def
|
74
|
-
"
|
77
|
+
def pull_request_shorthand(repo, owner, number)
|
78
|
+
"#{repo}/#{owner}##{number}"
|
75
79
|
end
|
76
80
|
|
77
|
-
def
|
78
|
-
"
|
81
|
+
def issue_shorthand(repo, owner, number)
|
82
|
+
"#{repo}/#{owner}##{number}"
|
79
83
|
end
|
80
84
|
|
81
|
-
def
|
82
|
-
"
|
85
|
+
def commit_shorthand(repo, owner, number)
|
86
|
+
"#{repo}/#{owner}@`#{number[0..5]}`"
|
83
87
|
end
|
84
88
|
end
|
85
89
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "html/pipeline"
|
3
|
+
require "html/pipeline/linkify_github"
|
4
|
+
require "github/markdown"
|
5
|
+
|
6
|
+
class HTML::Pipeline::LinkifyGitHubIntegrationTest < Minitest::Test
|
7
|
+
def pipeline
|
8
|
+
@pipeline ||= HTML::Pipeline.new [
|
9
|
+
HTML::Pipeline::MarkdownFilter,
|
10
|
+
HTML::Pipeline::LinkifyGitHubFilter
|
11
|
+
]
|
12
|
+
end
|
13
|
+
def test_works_with_markdown_filter
|
14
|
+
result = pipeline.call <<-MARKDOWN.strip_heredoc
|
15
|
+
https://github.com/rails/rails/pull/21862
|
16
|
+
https://github.com/rails/rails/issues/21843
|
17
|
+
https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864
|
18
|
+
MARKDOWN
|
19
|
+
|
20
|
+
assert_equal "<p><a href=\"https://github.com/rails/rails/pull/21862\">rails/rails#21862</a><br>\n<a href=\"https://github.com/rails/rails/issues/21843\">rails/rails#21843</a><br>\n<a href=\"https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864\">rails/rails@`67597e`</a></p>",
|
21
|
+
result[:output].to_html
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_works_when_markdown_already_linkified
|
25
|
+
result = pipeline.call <<-MARKDOWN.strip_heredoc
|
26
|
+
- [rails/rails#21862](https://github.com/rails/rails/pull/21862)
|
27
|
+
MARKDOWN
|
28
|
+
|
29
|
+
assert_equal "<ul>\n<li><a href=\"https://github.com/rails/rails/pull/21862\">rails/rails#21862</a></li>\n</ul>",
|
30
|
+
result[:output].to_html
|
31
|
+
end
|
32
|
+
end
|
data/test/linkify_github_test.rb
CHANGED
@@ -3,131 +3,50 @@ require "test_helper"
|
|
3
3
|
class HTML::Pipeline::LinkifyGitHubTest < Minitest::Test
|
4
4
|
LinkifyGitHubFilter = HTML::Pipeline::LinkifyGitHubFilter
|
5
5
|
|
6
|
-
def filter(
|
7
|
-
LinkifyGitHubFilter.
|
6
|
+
def filter(html)
|
7
|
+
LinkifyGitHubFilter.call(html)
|
8
8
|
end
|
9
9
|
|
10
10
|
def test_that_it_has_a_version_number
|
11
11
|
assert HTML::Pipeline::LinkifyGitHub::VERSION
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
15
|
-
|
16
|
-
|
17
|
-
assert_equal text, filter(text)
|
18
|
-
end
|
19
|
-
|
20
|
-
# == Pull Request
|
21
|
-
|
22
|
-
def test_that_linkify_pull_request_url
|
23
|
-
text = "https://github.com/rails/rails/pull/21862"
|
24
|
-
|
25
|
-
assert_equal "[rails/rails#21862](https://github.com/rails/rails/pull/21862)", filter(text)
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_that_linkify_pull_request_url_with_end_tilde
|
29
|
-
text = "https://github.com/rails/rails/pull/21862/"
|
30
|
-
|
31
|
-
assert_equal "[rails/rails#21862](https://github.com/rails/rails/pull/21862)", filter(text)
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_that_linkify_pull_request_url_with_www
|
35
|
-
text = "https://www.github.com/rails/rails/pull/21862"
|
36
|
-
|
37
|
-
assert_equal "[rails/rails#21862](https://github.com/rails/rails/pull/21862)", filter(text)
|
14
|
+
def pull_request_url
|
15
|
+
"https://github.com/rails/rails/pull/21862"
|
38
16
|
end
|
39
17
|
|
40
|
-
def
|
41
|
-
|
42
|
-
|
43
|
-
assert_equal "[rails/rails#21862](https://github.com/rails/rails/pull/21862)", filter(text)
|
18
|
+
def issue_url
|
19
|
+
"https://github.com/rails/rails/issues/21843"
|
44
20
|
end
|
45
21
|
|
46
|
-
def
|
47
|
-
|
48
|
-
|
49
|
-
assert_equal "[rails/rails#21862](https://github.com/rails/rails/pull/21862)", filter(text)
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_that_linkify_with_invalid_pull_request_url
|
53
|
-
text = "https://github.com/rails/rails/pull/"
|
54
|
-
|
55
|
-
assert_equal text, filter(text)
|
56
|
-
end
|
57
|
-
|
58
|
-
# == Issue
|
59
|
-
|
60
|
-
def test_that_linkify_issue_url
|
61
|
-
text = "https://github.com/rails/rails/issues/21862"
|
62
|
-
|
63
|
-
assert_equal "[rails/rails#21862](https://github.com/rails/rails/issues/21862)", filter(text)
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_that_linkify_issue_url_with_end_tilde
|
67
|
-
text = "https://github.com/rails/rails/issues/21862/"
|
68
|
-
|
69
|
-
assert_equal "[rails/rails#21862](https://github.com/rails/rails/issues/21862)", filter(text)
|
70
|
-
end
|
71
|
-
|
72
|
-
def test_that_linkify_issue_url_with_www
|
73
|
-
text = "https://www.github.com/rails/rails/issues/21862"
|
74
|
-
|
75
|
-
assert_equal "[rails/rails#21862](https://github.com/rails/rails/issues/21862)", filter(text)
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_that_linkify_issue_url_http
|
79
|
-
text = "http://github.com/rails/rails/issues/21862"
|
80
|
-
|
81
|
-
assert_equal "[rails/rails#21862](https://github.com/rails/rails/issues/21862)", filter(text)
|
82
|
-
end
|
83
|
-
|
84
|
-
def test_that_linkify_issue_url_http_www
|
85
|
-
text = "http://www.github.com/rails/rails/issues/21862"
|
86
|
-
|
87
|
-
assert_equal "[rails/rails#21862](https://github.com/rails/rails/issues/21862)", filter(text)
|
88
|
-
end
|
89
|
-
|
90
|
-
def test_that_linkify_with_invalid_issue_url
|
91
|
-
text = "https://github.com/rails/rails/issues/"
|
92
|
-
|
93
|
-
assert_equal text, filter(text)
|
94
|
-
end
|
95
|
-
|
96
|
-
# == Commit
|
97
|
-
|
98
|
-
def test_that_linkify_commit_url
|
99
|
-
text = "https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864"
|
100
|
-
|
101
|
-
assert_equal "[rails/rails@`67597e1`](https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864)", filter(text)
|
102
|
-
end
|
103
|
-
|
104
|
-
def test_that_linkify_commit_url_with_end_tilde
|
105
|
-
text = "https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864/"
|
106
|
-
|
107
|
-
assert_equal "[rails/rails@`67597e1`](https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864)", filter(text)
|
108
|
-
end
|
109
|
-
|
110
|
-
def test_that_linkify_commit_url_with_www
|
111
|
-
text = "https://www.github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864"
|
112
|
-
|
113
|
-
assert_equal "[rails/rails@`67597e1`](https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864)", filter(text)
|
22
|
+
def commit_url
|
23
|
+
"https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864"
|
114
24
|
end
|
115
25
|
|
116
|
-
def
|
117
|
-
|
26
|
+
def test_linkify_github_pull_request_html_link
|
27
|
+
body = %Q(<a href="#{pull_request_url}">#{pull_request_url}</a>)
|
28
|
+
doc = Nokogiri::HTML::DocumentFragment.parse(body)
|
118
29
|
|
119
|
-
|
30
|
+
res = filter(doc)
|
31
|
+
assert_equal_html %Q(<a href="#{pull_request_url}">rails/rails#21862</a>),
|
32
|
+
res.to_html
|
120
33
|
end
|
121
34
|
|
122
|
-
def
|
123
|
-
|
35
|
+
def test_linkify_github_issue_html_link
|
36
|
+
body = %Q(<a href="#{issue_url}">#{issue_url}</a>)
|
37
|
+
doc = Nokogiri::HTML::DocumentFragment.parse(body)
|
124
38
|
|
125
|
-
|
39
|
+
res = filter(doc)
|
40
|
+
assert_equal_html %Q(<a href="#{issue_url}">rails/rails#21843</a>),
|
41
|
+
res.to_html
|
126
42
|
end
|
127
43
|
|
128
|
-
def
|
129
|
-
|
44
|
+
def test_linkify_github_commit_html_link
|
45
|
+
body = %Q(<a href="#{commit_url}">#{commit_url}</a>)
|
46
|
+
doc = Nokogiri::HTML::DocumentFragment.parse(body)
|
130
47
|
|
131
|
-
|
48
|
+
res = filter(doc)
|
49
|
+
assert_equal_html %Q(<a href="#{commit_url}">rails/rails@`67597e`</a>),
|
50
|
+
res.to_html
|
132
51
|
end
|
133
52
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,3 +1,20 @@
|
|
1
1
|
require "bundler/setup"
|
2
2
|
require "minitest/autorun"
|
3
3
|
require "html/pipeline/linkify_github"
|
4
|
+
|
5
|
+
module LinkifyGitHubTestExtensions
|
6
|
+
def assert_equal_html(expected, actual)
|
7
|
+
assert_equal Nokogiri::HTML::DocumentFragment.parse(expected).to_hash,
|
8
|
+
Nokogiri::HTML::DocumentFragment.parse(actual).to_hash
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Minitest::Test
|
13
|
+
include LinkifyGitHubTestExtensions
|
14
|
+
end
|
15
|
+
|
16
|
+
class String
|
17
|
+
def strip_heredoc
|
18
|
+
gsub(/^#{scan(/^[ \t]*(?=\S)/).min}/, "".freeze)
|
19
|
+
end
|
20
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: html-pipeline-linkify_github
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juanito Fatas
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: html-pipeline
|
@@ -34,13 +34,15 @@ files:
|
|
34
34
|
- ".gitignore"
|
35
35
|
- ".travis.yml"
|
36
36
|
- CHANGELOG.md
|
37
|
+
- CONTRIBUTING.md
|
37
38
|
- Gemfile
|
38
|
-
- LICENSE
|
39
|
+
- LICENSE.md
|
39
40
|
- README.md
|
40
41
|
- Rakefile
|
41
42
|
- html-pipeline-linkify_github.gemspec
|
42
43
|
- lib/html/pipeline/linkify_github.rb
|
43
44
|
- lib/html/pipeline/linkify_github/version.rb
|
45
|
+
- test/linkify_github_integration_test.rb
|
44
46
|
- test/linkify_github_test.rb
|
45
47
|
- test/test_helper.rb
|
46
48
|
homepage: https://github.com/juanitofatas/html-pipeline-linkify_github
|