class HTMLPipeline::NodeFilter::MentionFilter

HTML filter that replaces @user mentions with links. Mentions within <pre>, <code>, and <a> elements are ignored. Mentions that reference users that do not exist are ignored.

Context options:

:base_url - Used to construct links to user profile pages for each
            mention.
:info_url - Used to link to "more info" when someone mentions @mention
            or @mentioned.
:username_pattern - Used to provide a custom regular expression to
                    identify usernames

Constants

IGNORE_PARENTS

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

MENTION_PATTERNS

Hash that contains all of the mention patterns used by the pipeline

SELECTOR
USERNAME_PATTERN

Default pattern used to extract usernames from text. The value can be overriden by providing the username_pattern variable in the context.

Public Class Methods

mentioned_logins_in(text, username_pattern = USERNAME_PATTERN) { |match, login| ... } click to toggle source

Public: Find user @mentions in text. See MentionFilter#mention_link_filter.

MentionFilter.mentioned_logins_in(text) do |match, login, is_mentioned|
  "<a href=...>#{login}</a>"
end

text - String text to search.

Yields the String match, the String login name, and a Boolean determining if the match = “@mention”. The yield’s return replaces the match in the original text.

Returns a String replaced with the return of the block.

# File lib/html_pipeline/node_filter/mention_filter.rb, line 35
def mentioned_logins_in(text, username_pattern = USERNAME_PATTERN)
  text.gsub(MENTION_PATTERNS[username_pattern]) do |match|
    login = Regexp.last_match(1)
    yield match, login
  end
end

Public Instance Methods

after_initialize() click to toggle source
# File lib/html_pipeline/node_filter/mention_filter.rb, line 66
def after_initialize
  result[:mentioned_usernames] ||= []
end
handle_text_chunk(text) click to toggle source
# File lib/html_pipeline/node_filter/mention_filter.rb, line 74
def handle_text_chunk(text)
  content = text.to_s
  return unless content.include?("@")

  html = mention_link_filter(content, base_url: base_url, username_pattern: username_pattern)
  return if html == content

  text.replace(html, as: :html)
end
info_url() click to toggle source

The URL to provide when someone @mentions a “mention” name, such as @mention or @mentioned, that will give them more info on mentions.

# File lib/html_pipeline/node_filter/mention_filter.rb, line 86
def info_url
  context[:info_url] || nil
end
selector() click to toggle source
# File lib/html_pipeline/node_filter/mention_filter.rb, line 70
def selector
  SELECTOR
end
username_pattern() click to toggle source
# File lib/html_pipeline/node_filter/mention_filter.rb, line 90
def username_pattern
  context[:username_pattern] || USERNAME_PATTERN
end