Class: Mato::HtmlFilters::TokenLink

Inherits:
Object
  • Object
show all
Includes:
Concerns::HtmlNodeCheckable
Defined in:
lib/mato/html_filters/token_link.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::HtmlNodeCheckable

has_ancestor?

Constructor Details

#initialize(pattern, &builder) ⇒ TokenLink

Returns a new instance of TokenLink.

Parameters:

  • pattern (Regexp)
  • builder (Procc)

    link builder that takes



18
19
20
21
# File 'lib/mato/html_filters/token_link.rb', line 18

def initialize(pattern, &builder)
  @pattern = pattern
  @builder = builder
end

Instance Attribute Details

#builderProc (readonly)

Returns:

  • (Proc)


14
15
16
# File 'lib/mato/html_filters/token_link.rb', line 14

def builder
  @builder
end

#patternRegexp (readonly)

Returns:

  • (Regexp)


11
12
13
# File 'lib/mato/html_filters/token_link.rb', line 11

def pattern
  @pattern
end

Instance Method Details

#call(doc) ⇒ Object

Parameters:

  • doc (Nokogiri::HTML4::DocumentFragment)


24
25
26
27
28
29
30
# File 'lib/mato/html_filters/token_link.rb', line 24

def call(doc)
  doc.xpath('.//text()').each do |text_node|
    next if has_ancestor?(text_node, 'a', 'code')

    text_node.replace(text_node.content.gsub(pattern, &builder))
  end
end