Class: HTMLPipeline::NodeFilter::TeamMentionFilter

Inherits:
HTMLPipeline::NodeFilter show all
Defined in:
lib/html_pipeline/node_filter/team_mention_filter.rb

Overview

HTML filter that replaces @org/team mentions with links. Mentions within <pre>, <code>, <a>, <style>, and <script> elements are ignored.

Context options:

:base_url - Used to construct links to team profile pages for each
            mention.
:team_pattern - Used to provide a custom regular expression to
                    identify team names

Constant Summary collapse

TEAM_PATTERN =

Default pattern used to extract team names from text. The value can be overridden by providing the team_pattern variable in the context. To properly link the mention, should be in the format of /@(1)/(2)/.

%r{
  (?<=^|\W)                  # beginning of string or non-word char
  @([a-z0-9][a-z0-9-]*)      # @organization
    (?:/|&\#47;?)             # dividing slash
    ([a-z0-9][a-z0-9\-_]*)   # team
    \b
}ix
IGNORE_PARENTS =

Don’t look for mentions in text nodes that are children of these elements

["pre", "code", "a", "style", "script"]
SELECTOR =
Selma::Selector.new(match_text_within: "*", ignore_text_within: IGNORE_PARENTS)

Instance Attribute Summary

Attributes inherited from HTMLPipeline::NodeFilter

#context

Attributes inherited from Filter

#context, #result

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HTMLPipeline::NodeFilter

call, #html, #initialize, #reset!

Methods inherited from Filter

#base_url, #call, call, #has_ancestor?, #initialize, #needs, #validate

Constructor Details

This class inherits a constructor from HTMLPipeline::NodeFilter

Class Method Details

.mentioned_teams_in(text, team_pattern = TEAM_PATTERN) ⇒ Object

Public: Find @org/team mentions in text. See TeamMentionFilter#team_mention_link_filter.

TeamMentionFilter.mentioned_teams_in(text) do |match, org, team|
  "<a href=...>#{team}</a>"
end

text - String text to search.

Yields the String match, org name, and team name. The yield’s return replaces the match in the original text.

Returns a String replaced with the return of the block.



31
32
33
34
35
36
37
# File 'lib/html_pipeline/node_filter/team_mention_filter.rb', line 31

def mentioned_teams_in(text, team_pattern = TEAM_PATTERN)
  text.gsub(team_pattern) do |match|
    org = Regexp.last_match(1)
    team = Regexp.last_match(2)
    yield match, org, team
  end
end

Instance Method Details

#after_initializeObject



56
57
58
# File 'lib/html_pipeline/node_filter/team_mention_filter.rb', line 56

def after_initialize
  result[:mentioned_teams] = []
end

#handle_text_chunk(text) ⇒ Object



64
65
66
67
68
69
# File 'lib/html_pipeline/node_filter/team_mention_filter.rb', line 64

def handle_text_chunk(text)
  content = text.to_s
  return unless content.include?("@")

  text.replace(mention_link_filter(content, base_url: base_url, team_pattern: team_pattern), as: :html)
end


93
94
95
96
97
98
99
100
101
102
# File 'lib/html_pipeline/node_filter/team_mention_filter.rb', line 93

def link_to_mentioned_team(base_url, org, team)
  result[:mentioned_teams] |= [team]

  url = base_url.dup
  url << "/" unless %r{[/~]\z}.match?(url)

  "<a href=\"#{url << org}/#{team}\" class=\"team-mention\">" \
    "@#{org}/#{team}" \
    "</a>"
end

Replace @org/team mentions in text with links to the mentioned team’s page.

text - String text to replace @mention team names in. base_url - The base URL used to construct team page URLs. team_pattern - Regular expression used to identify teams in text

Returns a string with @team mentions replaced with links. All links have a ‘team-mention’ class name attached for styling.



84
85
86
87
88
89
90
91
# File 'lib/html_pipeline/node_filter/team_mention_filter.rb', line 84

def mention_link_filter(text, base_url: "/", team_pattern: TEAM_PATTERN)
  self.class.mentioned_teams_in(text, team_pattern) do |match, org, team|
    link = link_to_mentioned_team(base_url, org, team)
    seperator = %r{/|&\#47;?}

    link ? match.sub(/@#{org}#{seperator}#{team}/, link) : match
  end
end

#selectorObject



60
61
62
# File 'lib/html_pipeline/node_filter/team_mention_filter.rb', line 60

def selector
  SELECTOR
end

#team_patternObject



71
72
73
# File 'lib/html_pipeline/node_filter/team_mention_filter.rb', line 71

def team_pattern
  context[:team_pattern] || TEAM_PATTERN
end