Sha256: b58b757e2de6caf967ce7f4d3e7aa33046bdba90ed1b4917cd8c29d8530cf691

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

# -*- coding: utf-8; frozen_string_literal: true -*-
#
#--
# Copyright (C) 2009-2019 Thomas Leitner <t_leitner@gmx.at>
#
# This file is part of kramdown which is licensed under the MIT.
#++
#
# This file is based on code originally from the Stringex library and needs the data files from
# Stringex to work correctly.

module Kramdown
  module Utils

    # Provides the ability to tranliterate Unicode strings into plain ASCII ones.
    module Unidecoder

      gem 'stringex'
      path = $:.find do |dir|
        File.directory?(File.join(File.expand_path(dir), "stringex", "unidecoder_data"))
      end

      if path
        CODEPOINTS = Hash.new do |h, k|
          h[k] = YAML.load_file(File.join(path, "stringex", "unidecoder_data", "#{k}.yml"))
        end

        # Transliterate string from Unicode into ASCII.
        def self.decode(string)
          string.gsub(/[^\x00-\x7f]/u) do |codepoint|
            unpacked = codepoint.unpack1("U")
            CODEPOINTS[sprintf("x%02x", unpacked >> 8)][unpacked & 255]
          rescue StandardError
            "?"
          end
        end
      else
        def self.decode(string)
          string
        end
      end

    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kramdown-2.5.1 lib/kramdown/utils/unidecoder.rb
kramdown-2.5.0 lib/kramdown/utils/unidecoder.rb