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



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

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) ⇒ Object



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

def source2dest_filename(name, disambig = true)
  n = strip_root(name)
  dir = File.dirname(n)
  base = File.basename(n)
  if disambig && @seen_filenames.include?(base)
    base = disambiguate_filename(base)
  end
  @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