Sha256: a1086877c09fb7ea73a265f745c97295fb9b5e14e6f69a64b111c8b6c746d9a8

Contents?: true

Size: 1.46 KB

Versions: 14

Compression:

Stored size: 1.46 KB

Contents

# encoding: utf-8
require "active_support/core_ext/string/inflections"

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) do
          word = $1
          slug = word.parameterize
          "<a href=\"#{base_url}/#{slug}\" title=\"#{LF_TITLE}\">#{word}</a>"
        end
      end

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

    end

  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
html-pipeline-linuxfr-0.14.22 lib/html/pipeline/custom_links_filter.rb
html-pipeline-linuxfr-0.14.21 lib/html/pipeline/custom_links_filter.rb
html-pipeline-linuxfr-0.14.20 lib/html/pipeline/custom_links_filter.rb
html-pipeline-linuxfr-0.14.19 lib/html/pipeline/custom_links_filter.rb
html-pipeline-linuxfr-0.14.18 lib/html/pipeline/custom_links_filter.rb
html-pipeline-linuxfr-0.14.17 lib/html/pipeline/custom_links_filter.rb
html-pipeline-linuxfr-0.14.16 lib/html/pipeline/custom_links_filter.rb
html-pipeline-linuxfr-0.14.15 lib/html/pipeline/custom_links_filter.rb
html-pipeline-linuxfr-0.14.14 lib/html/pipeline/custom_links_filter.rb
html-pipeline-linuxfr-0.14.13 lib/html/pipeline/custom_links_filter.rb
html-pipeline-linuxfr-0.14.12 lib/html/pipeline/custom_links_filter.rb
html-pipeline-linuxfr-0.14.11 lib/html/pipeline/custom_links_filter.rb
html-pipeline-linuxfr-0.14.10 lib/html/pipeline/custom_links_filter.rb
html-pipeline-linuxfr-0.14.9 lib/html/pipeline/custom_links_filter.rb