Class: HTML::Pipeline::CustomLinksFilter

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

Constant Summary collapse

LF_REGEXP =
/\[\[\[([ '\.:\-\p{Word}]+)\]\]\]/
WP_REGEXP =
/\[\[([ '\.+:!\-\(\)\p{Word}]+)\]\]/
LF_TITLE =
"Lien du wiki interne LinuxFr.org"
WP_TITLE =
"Définition Wikipédia"
IGNORE_PARENTS =

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

%w(pre code a).to_set

Instance Attribute Summary

Attributes inherited from Filter

#context, #result

Instance Method Summary collapse

Methods inherited from Filter

#base_url, call, #current_user, #doc, #has_ancestor?, #html, #initialize, #needs, #parse_html, #repository, to_document, to_html, #validate

Constructor Details

This class inherits a constructor from HTML::Pipeline::Filter

Instance Method Details

#callObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/html/pipeline/custom_links_filter.rb', line 16

def call
  doc.search('text()').each do |node|
    content = node.to_html
    next if !content.include?('[[')
    next if has_ancestor?(node, IGNORE_PARENTS)
    html = content
    html = process_internal_wiki_links html
    html = process_wikipedia_links html
    next if html == content
    node.replace(html)
  end
  doc
end


30
31
32
33
# File 'lib/html/pipeline/custom_links_filter.rb', line 30

def process_internal_wiki_links(text)
  base_url = "//#{context[:host]}/wiki"
  text.gsub(LF_REGEXP, "<a href=\"#{base_url}/\1\" title=\"#{LF_TITLE}\">\\1</a>")
end


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

def process_wikipedia_links(text)
  text.gsub(WP_REGEXP) do
    word = $1
    escaped = word.gsub(/\(|\)|'/) {|x| "\\#{x}" }
    parts = word.split(":")
    parts.shift if %w(de en es eo wikt).include?(parts.first)
    "<a href=\"http://fr.wikipedia.org/wiki/#{escaped}\" title=\"#{WP_TITLE}\")>#{parts.join ':'}</a>"
  end
end