Class: HTML::Pipeline::WikiLinkFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/html/pipeline/wiki_link/wiki_link_filter.rb

Overview

An ‘HTML::Pipeline` filter class that detects wiki-style links and converts them to HTML links.

Instance Method Summary collapse

Constructor Details

#initialize(doc, context = nil, result = nil) ⇒ WikiLinkFilter

Initializes a new instance of the ‘WikiLinkFilter` class.

Parameters:

  • doc

    Document to filter.

  • context (defaults to: nil)

    Parameters for the filter.

  • result (defaults to: nil)

    Results extracted from the filter.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/html/pipeline/wiki_link/wiki_link_filter.rb', line 17

def initialize(doc, context = nil, result = nil)
  super(doc, context, result)

  @base_url = '/'
  @space_replacement = '_'
  
  if context
    @base_url = context[:base_url] if context[:base_url]
    @space_replacement = context[:space_replacement] if context[:space_replacement]
  end

  unless @base_url.empty? || @base_url =~ /\/$/
    @base_url += '/'
  end
end

Instance Method Details

#callString

Performs the translation and returns the updated text.

Returns:

  • (String)

    Updated text with translated wiki links.



36
37
38
39
40
41
42
43
# File 'lib/html/pipeline/wiki_link/wiki_link_filter.rb', line 36

def call
  html.gsub(/\[\[([^\]|]*)(\|([^\]]*))?\]\]/) do
    link = $1
    desc = $3 ? $3 : $1

    "<a href=\"#{to_link link}\">#{to_description desc}</a>"
  end
end