Class: Metanorma::Collection::Util::DisambigFiles

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/collection/util/disambig_files.rb

Instance Method Summary collapse

Constructor Details

#initializeDisambigFiles

Returns a new instance of DisambigFiles.



5
6
7
# File 'lib/metanorma/collection/util/disambig_files.rb', line 5

def initialize
  @seen_filenames = []
end

Instance Method Details

#disambiguate_filename(base) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/metanorma/collection/util/disambig_files.rb', line 23

def disambiguate_filename(base)
  m = /^(?<start>.+\.)(?!0)(?<num>\d+)\.(?<suff>[^.]*)$/.match(base) ||
    /^(?<start>.+\.)(?<suff>[^.]*)/.match(base) ||
    /^(?<start>.+)$/.match(base)
  i = m.names.include?("num") ? m["num"].to_i + 1 : 1
  while @seen_filenames.include? base = "#{m['start']}#{i}.#{m['suff']}"
    i += 1
  end
  base
end

#source2dest_filename(name, disambig: true, preserve_dirs: false) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/metanorma/collection/util/disambig_files.rb', line 13

def source2dest_filename(name, disambig: true, preserve_dirs: false)
  n = strip_root(name)
  dir = preserve_dirs ? "." : File.dirname(n)
  base = preserve_dirs ? n : File.basename(n)
  disambig && @seen_filenames.include?(base) and
    base = disambiguate_filename(base)
  @seen_filenames << base
  dir == "." ? base : File.join(dir, base)
end

#strip_root(name) ⇒ Object



9
10
11
# File 'lib/metanorma/collection/util/disambig_files.rb', line 9

def strip_root(name)
  name.sub(%r{^(\./)?(\.\./)+}, "")
end