class HTML::Pipeline::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.

Constants

IGNORE_PARENTS

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

MentionLogins

List of username logins that, when mentioned, link to the blog post about @mentions instead of triggering a real mention.

MentionPattern

Pattern used to extract @mentions from text.

Public Class Methods

mentioned_logins_in(text) { |match, login, include?(downcase)| ... } 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/@mention_filter.rb, line 30
def self.mentioned_logins_in(text)
  text.gsub MentionPattern do |match|
    login = $1
    yield match, login, MentionLogins.include?(login.downcase)
  end
end

Public Instance Methods

call() click to toggle source
# File lib/html/pipeline/@mention_filter.rb, line 62
def call
  result[:mentioned_usernames] ||= []

  doc.search('text()').each do |node|
    content = node.to_html
    next if !content.include?('@')
    next if has_ancestor?(node, IGNORE_PARENTS)
    html = mention_link_filter(content, base_url, info_url)
    next if html == content
    node.replace(html)
  end
  doc
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/@mention_filter.rb, line 78
def info_url
  context[:info_url] || nil
end