Skip to content
This repository
tag: v4.5.3
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 56 lines (45 sloc) 1.1 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
# encoding: utf-8
#
# THINK Does it also remove diaritics, like べ to へ?
#
module Picky

  module CharacterSubstituters

    # Substitutes Umlauts like
    # ä, ö, ü => ae, oe, ue.
    # (and more, see specs)
    #
    class WestEuropean

      def initialize
        @chars = ActiveSupport::Multibyte.proxy_class
      end

      # Substitutes occurrences of certain characters
      # (like Umlauts) with ASCII representations of them.
      #
      # Examples:
      # ä -> ae
      # Ö -> Oe
      # ß -> ss
      # ç -> c
      #
      # (See the associated spec for all examples)
      #
      def substitute text
        trans = @chars.new(text).normalize :kd

        # Substitute special cases.
        #
        trans.gsub! 'ß', 'ss'

        # Substitute umlauts (of A,O,U,a,o,u).
        #
        trans.gsub! /([AOUaou])\314\210/u, '\1e'

        # Get rid of ecutes, graves etc.
        #
        trans.unpack('U*').select { |cp|
          cp < 0x0300 || cp > 0x035F
        }.pack 'U*'
      end

      def to_s
        self.class.name
      end

    end

  end

end
Something went wrong with that request. Please try again.