Class: HTML::Pipeline::IssueMentionFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/issue_mention_filter.rb

Overview

HTML filter that replaces #mention mentions with links to Github issue. Mentions within <pre>, <code>, <style> and <a> elements are ignored.

Context options:

:base_url - Used to construct links to issue page for each mention.
:issueid_pattern - Used to provide a custom regular expression to
                    identify issue ids

Constant Summary collapse

MentionPatterns =

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

Hash.new do |hash, key|
  hash[key] = /
    (?:^|\W)                    # beginning of string or non-word char
    \#((?>#{key}))              # #issueid
    (?!\/)                      # without a trailing slash
    (?=
      \.+[ \t\W]|               # dots followed by space or non-word character
      \.+$|                     # dots at end of line
      [^0-9a-zA-Z_.]|           # non-word character except dot
      $                         # end of line
    )
  /ix
end
IssueidPattern =

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

/[0-9][0-9-]*/
IGNORE_PARENTS =

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

%w(pre code a style).to_set

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.mentioned_issues_in(text, issueid_pattern = IssueidPattern) ⇒ Object

Public: Find issue #mention in text. See IssueMentionFilter#mention_link_filter.

IssueMentionFilter.mentioned_issues_in(text) do |match, issueid|
  "<a href=...>#{issueid}</a>"
end

text - String text to search.

Yields the String match, the String issueid. The yield’s return replaces the match in the original text.

Returns a String replaced with the return of the block.



27
28
29
30
31
32
# File 'lib/issue_mention_filter.rb', line 27

def self.mentioned_issues_in(text, issueid_pattern=IssueidPattern)
  text.gsub MentionPatterns[issueid_pattern] do |match|
    issueid = $1
    yield match, issueid
  end
end

Instance Method Details

#callObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/issue_mention_filter.rb', line 57

def call
  result[:mentioned_issueids] ||= []

  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, issueid_pattern)
    next if html == content
    node.replace(html)
  end
  doc
end

#issueid_patternObject



71
72
73
# File 'lib/issue_mention_filter.rb', line 71

def issueid_pattern
  context[:issueid_pattern] || IssueidPattern
end


92
93
94
95
96
97
98
99
100
101
# File 'lib/issue_mention_filter.rb', line 92

def link_to_mentioned_issue(issueid)
  result[:mentioned_issueids] |= [issueid]

  url = base_url.dup
  url << "/" unless url =~ /[\/~]\z/

  "<a href='#{url << issueid}' class='issue-mention'>" +
  "\##{issueid}" +
  "</a>"
end

Replace issue #mentions in text with links to the mentioned issue’s page.

text - String text to replace #mention issueids in. base_url - The base URL used to construct issue page URLs. issueid_pattern - Regular expression used to identify issueid in

text

Returns a string with #issueid replaced with links. All links have a ‘issue-mention’ class name attached for styling.



85
86
87
88
89
90
# File 'lib/issue_mention_filter.rb', line 85

def mention_link_filter(text, base_url='/', issueid_pattern)
  self.class.mentioned_issues_in(text, issueid_pattern) do |match, issueid|
    link = link_to_mentioned_issue(issueid)
    link ? match.sub("\##{issueid}", link) : match
  end
end