jekyll-issue-mentions 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b953ba404a6d187573847e00b59b7b1a2de31c6d
4
+ data.tar.gz: 9fe7280e38a240006de05b0d7f3f4cd6dd3f4fa2
5
+ SHA512:
6
+ metadata.gz: 0f4b6b5bbc0feaf6409ed460c51470fbecc369c57822389ebc9e230293949f43ee95a3c620a0bfa1c21f0df7f6945d6a025fa37cff580ea125fe1f3a069e048c
7
+ data.tar.gz: a2f0f2e4a693dcd31e74f0cad0f1dc9f43eaafd79c0780c70c24c9e7c12e2ec7aec2597d7e69563510655a26ef67d0671c3d83a206e9a4b8f2b9304ca46c2820
@@ -0,0 +1,104 @@
1
+ require 'set'
2
+
3
+ module HTML
4
+ class Pipeline
5
+ # HTML filter that replaces #mention mentions with links to Github issue. Mentions within <pre>,
6
+ # <code>, <style> and <a> elements are ignored.
7
+ #
8
+ # Context options:
9
+ # :base_url - Used to construct links to issue page for each mention.
10
+ # :issueid_pattern - Used to provide a custom regular expression to
11
+ # identify issue ids
12
+ #
13
+ class IssueMentionFilter < Filter
14
+ # Public: Find issue #mention in text. See
15
+ # IssueMentionFilter#mention_link_filter.
16
+ #
17
+ # IssueMentionFilter.mentioned_issues_in(text) do |match, issueid|
18
+ # "<a href=...>#{issueid}</a>"
19
+ # end
20
+ #
21
+ # text - String text to search.
22
+ #
23
+ # Yields the String match, the String issueid. The yield's return replaces the match in
24
+ # the original text.
25
+ #
26
+ # Returns a String replaced with the return of the block.
27
+ def self.mentioned_issues_in(text, issueid_pattern=IssueidPattern)
28
+ text.gsub MentionPatterns[issueid_pattern] do |match|
29
+ issueid = $1
30
+ yield match, issueid
31
+ end
32
+ end
33
+
34
+
35
+ # Hash that contains all of the mention patterns used by the pipeline
36
+ MentionPatterns = Hash.new do |hash, key|
37
+ hash[key] = /
38
+ (?:^|\W) # beginning of string or non-word char
39
+ \#((?>#{key})) # #issueid
40
+ (?!\/) # without a trailing slash
41
+ (?=
42
+ \.+[ \t\W]| # dots followed by space or non-word character
43
+ \.+$| # dots at end of line
44
+ [^0-9a-zA-Z_.]| # non-word character except dot
45
+ $ # end of line
46
+ )
47
+ /ix
48
+ end
49
+
50
+ # Default pattern used to extract issueid from text. The value can be
51
+ # overriden by providing the issueid_pattern variable in the context.
52
+ IssueidPattern = /[0-9][0-9-]*/
53
+
54
+ # Don't look for mentions in text nodes that are children of these elements
55
+ IGNORE_PARENTS = %w(pre code a style).to_set
56
+
57
+ def call
58
+ result[:mentioned_issueids] ||= []
59
+
60
+ doc.search('text()').each do |node|
61
+ content = node.to_html
62
+ next if !content.include?('#')
63
+ next if has_ancestor?(node, IGNORE_PARENTS)
64
+ html = mention_link_filter(content, base_url, issueid_pattern)
65
+ next if html == content
66
+ node.replace(html)
67
+ end
68
+ doc
69
+ end
70
+
71
+ def issueid_pattern
72
+ context[:issueid_pattern] || IssueidPattern
73
+ end
74
+
75
+ # Replace issue #mentions in text with links to the mentioned
76
+ # issue's page.
77
+ #
78
+ # text - String text to replace #mention issueids in.
79
+ # base_url - The base URL used to construct issue page URLs.
80
+ # issueid_pattern - Regular expression used to identify issueid in
81
+ # text
82
+ #
83
+ # Returns a string with #issueid replaced with links. All links have a
84
+ # 'issue-mention' class name attached for styling.
85
+ def mention_link_filter(text, base_url='/', issueid_pattern)
86
+ self.class.mentioned_issues_in(text, issueid_pattern) do |match, issueid|
87
+ link = link_to_mentioned_issue(issueid)
88
+ link ? match.sub("\##{issueid}", link) : match
89
+ end
90
+ end
91
+
92
+ def link_to_mentioned_issue(issueid)
93
+ result[:mentioned_issueids] |= [issueid]
94
+
95
+ url = base_url.dup
96
+ url << "/" unless url =~ /[\/~]\z/
97
+
98
+ "<a href='#{url << issueid}' class='issue-mention'>" +
99
+ "\##{issueid}" +
100
+ "</a>"
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,46 @@
1
+ require 'jekyll'
2
+ require 'html/pipeline'
3
+ require 'issue_mention_filter'
4
+
5
+ module Jekyll
6
+ class IssueMentions < Jekyll::Generator
7
+ safe true
8
+
9
+ def initialize(config = Hash.new)
10
+ @filter = HTML::Pipeline::IssueMentionFilter.new(nil, {:base_url => base_url(config['jekyll-issue-mentions']) })
11
+ end
12
+
13
+ def generate(site)
14
+ site.pages.each { |page| mentionify page if html_page?(page) }
15
+ site.posts.each { |post| mentionify post }
16
+ site.docs_to_write.each { |doc| mentionify doc }
17
+ end
18
+
19
+ def mentionify(page)
20
+ return unless page.content.include?('#')
21
+ page.content = @filter.mention_link_filter(page.content, @filter.base_url, @filter.issueid_pattern)
22
+ end
23
+
24
+ def html_page?(page)
25
+ page.html? || page.url.end_with?('/')
26
+ end
27
+
28
+ def base_url(configs)
29
+ case configs
30
+ when nil, NilClass
31
+ raise ArgumentError.new("Your jekyll-issue-mentions config has to configured.")
32
+ when String
33
+ configs.to_s
34
+ when Hash
35
+ base_url = configs['base_url']
36
+ if base_url.nil?
37
+ raise ArgumentError.new("Your jekyll-issue-mentions is missing base_url configuration.")
38
+ else
39
+ base_url
40
+ end
41
+ else
42
+ raise ArgumentError.new("Your jekyll-issue-mentions config has to either be a string or a hash! It's a #{configs.class} right now.")
43
+ end
44
+ end
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-issue-mentions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Harish Shetty
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
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: html-pipeline
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.9.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.9.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rdoc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: shoulda
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email: support@workato.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - lib/issue_mention_filter.rb
104
+ - lib/jekyll-issue-mentions.rb
105
+ homepage: https://github.com/workato/jekyll-issue-mentions
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.4.6
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: "#issueid support for your Jekyll site"
129
+ test_files: []