Module: Decant::PathUtils

Defined in:
lib/decant/path_utils.rb

Class Method Summary collapse

Class Method Details

.delete_ext_regexp(pattern) ⇒ Regexp

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.

Parameters:

  • pattern (String)

Returns:

  • (Regexp)

Raises:

  • (RegexpError)

    if the regular expression cannot be generated, for instance if pattern includes unbalanced shell-like expansion brackets



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/decant/path_utils.rb', line 17

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