html-pipeline 2.11.1 → 2.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6247333ffe74b1a26211f5563ceea18e43b355aa8b8fe5b0474355f7efa81f93
4
- data.tar.gz: 0f29d8cae966aa24960a57e874a73693e50040444d3f98d2c505b6bb6161c9e9
3
+ metadata.gz: d08ceab233a694f9315d1733a1ab2a9509adcb5ab6845a28ba5aa09865e110d9
4
+ data.tar.gz: 6b8eef1085941d9e65ea9747ed402f519c886b54d393a5cd66326d1f01d1d433
5
5
  SHA512:
6
- metadata.gz: 19b69c57d6b7df1efa7ad35c754b283242d46d61a6b8a8313ca27ea2929511934eecf4c019e744cd640704455a0ac0ec06f52bcb2929425f58153320b2e47be2
7
- data.tar.gz: 9fb8ca18eeb49d3150e7b88e024b89da94e88e8723861964e49a8873e0045e8886b49745deff0973b3cd731fb2488283a96fad7b23dec7d3fd9f424a087a75f9
6
+ metadata.gz: 825524c21337d775d83c8cd4b99a353b0c295d49b2447d2dfc6f4f7b762f43c4b69891f608813ad2e8402b739b904878b13f948d8910f04c8e14e42d3c4a4cd6
7
+ data.tar.gz: e5e2b9740e9915e6054404dcd28f86aa8f931b4585647380f6e2d4c5b4bff75644549b27d77080c9d95b87596bb4a55ebf759855c9f9ca2dc0cbe80ec2b0e3fa
data/README.md CHANGED
@@ -153,6 +153,7 @@ EmojiPipeline = Pipeline.new [
153
153
  ## Filters
154
154
 
155
155
  * `MentionFilter` - replace `@user` mentions with links
156
+ * `TeamMentionFilter` - replace `@org/team` mentions with links
156
157
  * `AbsoluteSourceFilter` - replace relative image urls with fully qualified versions
157
158
  * `AutolinkFilter` - auto_linking urls in HTML
158
159
  * `CamoFilter` - replace http image urls with [camo-fied](https://github.com/atmos/camo) https versions
data/lib/html/pipeline.rb CHANGED
@@ -38,6 +38,7 @@ module HTML
38
38
  autoload :ImageMaxWidthFilter, 'html/pipeline/image_max_width_filter'
39
39
  autoload :MarkdownFilter, 'html/pipeline/markdown_filter'
40
40
  autoload :MentionFilter, 'html/pipeline/@mention_filter'
41
+ autoload :TeamMentionFilter, 'html/pipeline/@team_mention_filter'
41
42
  autoload :PlainTextInputFilter, 'html/pipeline/plain_text_input_filter'
42
43
  autoload :SanitizationFilter, 'html/pipeline/sanitization_filter'
43
44
  autoload :SyntaxHighlightFilter, 'html/pipeline/syntax_highlight_filter'
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'set'
4
+
5
+ module HTML
6
+ class Pipeline
7
+ # HTML filter that replaces @org/team mentions with links. Mentions within
8
+ # <pre>, <code>, <a>, <style>, and <script> elements are ignored.
9
+ #
10
+ # Context options:
11
+ # :base_url - Used to construct links to team profile pages for each
12
+ # mention.
13
+ # :team_pattern - Used to provide a custom regular expression to
14
+ # identify team names
15
+ #
16
+ class TeamMentionFilter < Filter
17
+ # Public: Find @org/team mentions in text. See
18
+ # TeamMentionFilter#team_mention_link_filter.
19
+ #
20
+ # TeamMentionFilter.mentioned_teams_in(text) do |match, org, team|
21
+ # "<a href=...>#{team}</a>"
22
+ # end
23
+ #
24
+ # text - String text to search.
25
+ #
26
+ # Yields the String match, org name, and team name. The yield's
27
+ # return replaces the match in the original text.
28
+ #
29
+ # Returns a String replaced with the return of the block.
30
+ def self.mentioned_teams_in(text, team_pattern = TeamPattern)
31
+ text.gsub team_pattern do |match|
32
+ org = $1
33
+ team = $2
34
+ yield match, org, team
35
+ end
36
+ end
37
+
38
+ # Default pattern used to extract team names from text. The value can be
39
+ # overridden by providing the team_pattern variable in the context. To
40
+ # properly link the mention, should be in the format of /@(1)\/(2)/.
41
+ TeamPattern = /
42
+ (?<=^|\W) # beginning of string or non-word char
43
+ @([a-z0-9][a-z0-9-]*) # @organization
44
+ \/ # dividing slash
45
+ ([a-z0-9][a-z0-9\-_]*) # team
46
+ \b
47
+ /ix
48
+
49
+ # Don't look for mentions in text nodes that are children of these elements
50
+ IGNORE_PARENTS = %w[pre code a style script].to_set
51
+
52
+ def call
53
+ result[:mentioned_teams] ||= []
54
+
55
+ doc.search('.//text()').each do |node|
56
+ content = node.to_html
57
+ next unless content.include?('@')
58
+ next if has_ancestor?(node, IGNORE_PARENTS)
59
+ html = mention_link_filter(content, base_url, team_pattern)
60
+ next if html == content
61
+ node.replace(html)
62
+ end
63
+ doc
64
+ end
65
+
66
+ def team_pattern
67
+ context[:team_pattern] || TeamPattern
68
+ end
69
+
70
+ # Replace @org/team mentions in text with links to the mentioned team's
71
+ # page.
72
+ #
73
+ # text - String text to replace @mention team names in.
74
+ # base_url - The base URL used to construct team page URLs.
75
+ # team_pattern - Regular expression used to identify teams in text
76
+ #
77
+ # Returns a string with @team mentions replaced with links. All links have a
78
+ # 'team-mention' class name attached for styling.
79
+ def mention_link_filter(text, _base_url = '/', team_pattern = TeamPattern)
80
+ self.class.mentioned_teams_in(text, team_pattern) do |match, org, team|
81
+ link = link_to_mentioned_team(org, team)
82
+
83
+ link ? match.sub("@#{org}/#{team}", link) : match
84
+ end
85
+ end
86
+
87
+ def link_to_mentioned_team(org, team)
88
+ result[:mentioned_teams] |= [team]
89
+
90
+ url = base_url.dup
91
+ url << '/' unless url =~ /[\/~]\z/
92
+
93
+ "<a href='#{url << org}/#{team}' class='team-mention'>" \
94
+ "@#{org}/#{team}" \
95
+ '</a>'
96
+ end
97
+ end
98
+ end
99
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module HTML
4
4
  class Pipeline
5
- VERSION = '2.11.1'.freeze
5
+ VERSION = '2.12.0'.freeze
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html-pipeline
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.11.1
4
+ version: 2.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Tomayko
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-07-18 00:00:00.000000000 Z
13
+ date: 2019-08-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -62,6 +62,7 @@ files:
62
62
  - html-pipeline.gemspec
63
63
  - lib/html/pipeline.rb
64
64
  - lib/html/pipeline/@mention_filter.rb
65
+ - lib/html/pipeline/@team_mention_filter.rb
65
66
  - lib/html/pipeline/absolute_source_filter.rb
66
67
  - lib/html/pipeline/autolink_filter.rb
67
68
  - lib/html/pipeline/body_content.rb