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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5638ffbca9252a4443a1f4202d4338fe33249137
4
- data.tar.gz: 46b88271fba26138bbaa86a17bad8062e3d2e818
3
+ metadata.gz: b7d544ff42bdaed0ad76d2b225dc17eda21a8ce9
4
+ data.tar.gz: 7ef282549e3dbcdaa6df7fec6b8aa9dccf0d0ba6
5
5
  SHA512:
6
- metadata.gz: 9670294c39de66cbe696aacc67d93d40b714d30a5cb58cca4caf0edaecf5c7c449ad5f9e4e731e914783e3327ad148888f5a1a71a8ec70a5bf0599fbe8b95fec
7
- data.tar.gz: ff3001f09b0f2390ff676d0453fb08e9eaa5eb14dacd14695e9357391ce27bcdf54303cd455b02443279a2d42d433f301fcfe5af389a75655e643fe573552810
6
+ metadata.gz: 9d8d7f78298b42446740ec56ffafbad473c0f7c361a370d9c4b571421f3c278da68f50094df93d004003a855f03aad00f8e0c821c9c1809cd50b135e2a835e76
7
+ data.tar.gz: 90c5eac937732170879a89d1c59d9cedda236375251e4e867344a21afe9bca021a70d9ae6e954cefdc4761b21aa04daab33fcc76abf5e2dfffa4e9a9021ec780
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
1
  # CHANGELOG
2
2
 
3
3
  ## Unreleased
4
+
5
+ ## 1.0.1 - 2015.10.15
6
+
7
+ - Linkify after markdown generated html. [#3](https://github.com/jollygoodcode/html-pipeline-linkify_github/pull/3/)
8
+
9
+ ## 1.0.0 - 2015.10.04
10
+
11
+ Released v1.0.0.
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
@@ -7,6 +7,7 @@ group :development do
7
7
  gem "rake"
8
8
  gem "bundler"
9
9
  gem "pry"
10
+ gem "github-markdown"
10
11
  end
11
12
 
12
13
  group :test do
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 before your markdown 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::LinkifyGitHubFilter,
32
- HTML::Pipeline::MarkdownFilter
31
+ HTML::Pipeline::MarkdownFilter,
32
+ HTML::Pipeline::LinkifyGitHubFilter
33
33
  ]
34
34
 
35
- result = pipeline.call <<-CODE
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
- CODE
39
+ MARKDOWN
40
40
 
41
- puts result[:output]
41
+ puts result[:output].to_html
42
42
  ```
43
43
 
44
44
  prints:
45
45
 
46
- ```markdown
47
- [rails/rails#21862](https://github.com/rails/rails/pull/21862)
48
- [rails/rails#21843](https://github.com/rails/rails/issues/21843)
49
- [rails/rails@`67597e1`](https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864)
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
- ## Credits
56
+ ## Contributing
65
57
 
66
- A huge THANK YOU to all our [contributors](https://github.com/jollygoodcode/twemoji/graphs/contributors)! :heart:
58
+ Please see the [CONTRIBUTING.md](/CONTRIBUTING.md) file.
67
59
 
68
- This project is maintained by [Jolly Good Code](http://www.jollygoodcode.com).
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
- MIT License. See [LICENSE](LICENSE) for details.
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
- # [rails/rails#21862](https://github.com/rails/rails/pull/21862)
16
- # [rails/rails#21843](https://github.com/rails/rails/issues/21843)
17
- # [rails/rails@`67597e`](https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864)
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 < TextFilter
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
- return @text unless @text.include?(GITHUB_URL)
26
+ doc.search("a").each do |element|
27
+ next if element["href"].to_s.empty?
28
+
29
+ text = element.content
33
30
 
34
- replace_pull_request_links if has_pull_request_link?
35
- replace_issue_links if has_issue_link?
36
- replace_commit_links if has_commit_link?
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
- @text
42
+ doc
39
43
  end
40
44
 
41
45
  private
42
46
 
43
- def has_pull_request_link?
44
- @text.include?(PULL)
47
+ def is_a_pull_request_link?(text)
48
+ text.include?("/pull/".freeze)
45
49
  end
46
50
 
47
- def has_issue_link?
48
- @text.include?(ISSUES)
51
+ def is_a_issue_link?(text)
52
+ text.include?("/issues/".freeze)
49
53
  end
50
54
 
51
- def has_commit_link?
52
- @text.include?(COMMIT)
55
+ def is_a_commit_link?(text)
56
+ text.include?("/commit/".freeze)
53
57
  end
54
58
 
55
- def replace_pull_request_links
56
- @text.gsub!(PULL_REQUEST_REGEXP) do
57
- pull_request_markdown($1, $2, $3) if [$1, $2, $3].all?(&:present?)
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 replace_issue_links
62
- @text.gsub!(ISSUES_REGEXP) do
63
- issue_markdown($1, $2, $3) if [$1, $2, $3].all?(&:present?)
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 replace_commit_links
68
- @text.gsub!(COMMIT_REGEXP) do
69
- commit_markdown($1, $2, $3) if [$1, $2, $3].all?(&:present?)
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 pull_request_markdown(repo, owner, number)
74
- "[#{repo}/#{owner}##{number}](https://github.com/#{repo}/#{owner}/pull/#{number})"
77
+ def pull_request_shorthand(repo, owner, number)
78
+ "#{repo}/#{owner}##{number}"
75
79
  end
76
80
 
77
- def issue_markdown(repo, owner, number)
78
- "[#{repo}/#{owner}##{number}](https://github.com/#{repo}/#{owner}/issues/#{number})"
81
+ def issue_shorthand(repo, owner, number)
82
+ "#{repo}/#{owner}##{number}"
79
83
  end
80
84
 
81
- def commit_markdown(repo, owner, number)
82
- "[#{repo}/#{owner}@`#{number[0..6]}`](https://github.com/#{repo}/#{owner}/commit/#{number})"
85
+ def commit_shorthand(repo, owner, number)
86
+ "#{repo}/#{owner}@`#{number[0..5]}`"
83
87
  end
84
88
  end
85
89
  end
@@ -1,7 +1,7 @@
1
1
  module HTML
2
2
  class Pipeline
3
3
  module LinkifyGitHub
4
- VERSION = "1.0.0".freeze
4
+ VERSION = "1.0.1".freeze
5
5
  end
6
6
  end
7
7
  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
@@ -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(text)
7
- LinkifyGitHubFilter.to_html(text)
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 test_that_normal_url_will_not_linkify
15
- text = "https://www.deppbot.com"
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 test_that_linkify_pull_request_url_http
41
- text = "http://github.com/rails/rails/pull/21862"
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 test_that_linkify_pull_request_url_http_www
47
- text = "http://www.github.com/rails/rails/pull/21862"
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 test_that_linkify_commit_url_with_http
117
- text = "http://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864"
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
- assert_equal "[rails/rails@`67597e1`](https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864)", filter(text)
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 test_that_linkify_commit_url_with_http_www
123
- text = "http://www.github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864"
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
- assert_equal "[rails/rails@`67597e1`](https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864)", filter(text)
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 test_that_linkify_with_invalid_commit_url
129
- text = "http://www.github.com/rails/rails/commit/"
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
- assert_equal text, filter(text)
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.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-04 00:00:00.000000000 Z
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