Class: Mato::HtmlFilters::MentionLink
- Inherits:
-
Object
- Object
- Mato::HtmlFilters::MentionLink
- Includes:
- Concerns::HtmlNodeCheckable
- Defined in:
- lib/mato/html_filters/mention_link.rb
Constant Summary collapse
- MENTION_PATTERN =
e.g. @foo
/\@[a-zA-Z0-9_\-]+\b/
Instance Attribute Summary collapse
- #link_builder ⇒ Proc readonly
- #pattern ⇒ Regexp readonly
Instance Method Summary collapse
- #call(doc) ⇒ Object
-
#initialize(pattern = MENTION_PATTERN, &link_builder) ⇒ MentionLink
constructor
A new instance of MentionLink.
Methods included from Concerns::HtmlNodeCheckable
Constructor Details
#initialize(pattern = MENTION_PATTERN, &link_builder) ⇒ MentionLink
Returns a new instance of MentionLink.
20 21 22 23 |
# File 'lib/mato/html_filters/mention_link.rb', line 20 def initialize(pattern = MENTION_PATTERN, &link_builder) @pattern = pattern @link_builder = link_builder end |
Instance Attribute Details
#link_builder ⇒ Proc (readonly)
14 15 16 |
# File 'lib/mato/html_filters/mention_link.rb', line 14 def link_builder @link_builder end |
#pattern ⇒ Regexp (readonly)
11 12 13 |
# File 'lib/mato/html_filters/mention_link.rb', line 11 def pattern @pattern end |
Instance Method Details
#call(doc) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mato/html_filters/mention_link.rb', line 26 def call(doc) candidate_map = {} candidates = [] doc.xpath('.//text()').each do |text_node| next if has_ancestor?(text_node, 'a', 'code', 'pre') candidate_html = text_node.content.gsub(pattern) do |mention| "<span class='mention-candidate'>#{mention}</span>" end next if text_node.content == candidate_html candidate_fragment = text_node.replace(candidate_html) candidate_fragment.css('span').each do |mention_element| (candidate_map[mention_element.child.content] ||= []) << mention_element end candidates << candidate_fragment end unless candidate_map.empty? link_builder.call(candidate_map) # cleanup candidates.each do |candidate_fragment| candidate_fragment.css('span.mention-candidate').each do |node| node.replace(node.child.content) end end end end |