Sha256: 7d5acbabb4c1822a2ecbb11a46ccf95424b8fb5c8b08ab44dcbbe7c0a14b3385

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

# encoding: utf-8
module HTML
  class Pipeline

    class CustomLinksFilter < Filter

      LF_REGEXP = /\[\[\[([ '\.:\-\p{Word}]+)\]\]\]/
      WP_REGEXP = /\[\[([ '\.+:!\-\(\)\p{Word}]+)\]\]/

      LF_TITLE = "Lien du wiki interne LinuxFr.org"
      WP_TITLE = "Définition Wikipédia"

      # Don't look for links in text nodes that are children of these elements
      IGNORE_PARENTS = %w(pre code a).to_set

      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

      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

      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

    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
html-pipeline-linuxfr-0.14.4 lib/html/pipeline/custom_links_filter.rb
html-pipeline-linuxfr-0.14.3 lib/html/pipeline/custom_links_filter.rb
html-pipeline-linuxfr-0.14.2 lib/html/pipeline/custom_links_filter.rb
html-pipeline-linuxfr-0.14.1 lib/html/pipeline/custom_links_filter.rb