Sha256: a277a797b84cd352aee996e0806323894a00c7e67172c1b980590acd0e09dc7f

Contents?: true

Size: 1018 Bytes

Versions: 2

Compression:

Stored size: 1018 Bytes

Contents

# frozen_string_literal: true
require 'strscan'

module Decant
  module PathUtils
    # Generate a regular expression to strip a matching extension from a string.
    # Supports similar shell-like pattern syntax as +Dir.glob+ including +.*+
    # to remove any extension and +.{a,b}+ to remove either an +.a+ or +.b+
    # extension. Used internally by {Content#slug}.
    #
    # @param pattern [String]
    #
    # @return [Regexp]
    #
    # @raise [RegexpError] if the regular expression cannot be generated, for
    #   instance if +pattern+ includes unbalanced shell-like expansion brackets
    def self.delete_ext_regexp(pattern)
      scanner = StringScanner.new(pattern)
      regexp = String.new

      while (ch = scanner.getch)
        regexp << case ch
        when '.' then '\.'
        when '{' then '(?:'
        when ',' then '|'
        when '}' then ')'
        when '*' then '[^\.]+'
        else
          ch
        end
      end

      regexp << '$'

      Regexp.new(regexp)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
decant-0.3.0 lib/decant/path_utils.rb
decant-0.2.0 lib/decant/path_utils.rb