Sha256: 13774ef042a3cafb878e80f11eea125f1e8ca3263e4d4b61837c99065415ef38

Contents?: true

Size: 1.3 KB

Versions: 8

Compression:

Stored size: 1.3 KB

Contents

# encoding: utf-8

module Nanoc::StringExtensions

  # Transforms string into an actual identifier
  #
  # @return [String] The identifier generated from the receiver
  def cleaned_identifier
    "/#{self}/".gsub(/^\/+|\/+$/, '/')
  end

  # Replaces Unicode characters with their ASCII decompositions if the
  # environment does not support Unicode.
  #
  # This method is not suited for general usage. If you need similar
  # functionality, consider using the Iconv library instead.
  #
  # @return [String] The decomposed string
  def make_compatible_with_env
    # Check whether environment supports Unicode
    # FIXME this is ugly, and there most likely are better ways to do this
    is_unicode_supported = %w( LC_ALL LC_CTYPE LANG ).any? { |e| ENV[e] =~ /UTF/ }
    return self if is_unicode_supported

    # Decompose if necessary
    # FIXME this decomposition is not generally usable
    self.gsub(/“|”/, '"').gsub(/‘|’/, '\'').gsub('…', '...').gsub('©', '(c)')
  end

  # Calculates the checksum for this string. Any change to this string will
  # result in a different checksum.
  #
  # @return [String] The checksum for this string
  #
  # @api private
  def checksum
    digest = Digest::SHA1.new
    digest.update(self)
    digest.hexdigest
  end

end

class String
  include Nanoc::StringExtensions
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
nanoc-3.3.7 lib/nanoc/base/core_ext/string.rb
nanoc-3.3.6 lib/nanoc/base/core_ext/string.rb
nanoc-3.3.5 lib/nanoc/base/core_ext/string.rb
nanoc-3.3.4 lib/nanoc/base/core_ext/string.rb
nanoc-3.3.3 lib/nanoc/base/core_ext/string.rb
nanoc-3.3.2 lib/nanoc/base/core_ext/string.rb
nanoc-3.3.1 lib/nanoc/base/core_ext/string.rb
nanoc-3.3.0 lib/nanoc/base/core_ext/string.rb