Class: Metanorma::Util::DisambigFiles

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

Instance Method Summary collapse

Constructor Details

#initializeDisambigFiles

Returns a new instance of DisambigFiles.



78
79
80
# File 'lib/metanorma/util.rb', line 78

def initialize
  @seen_filenames = []
end

Instance Method Details

#disambiguate_filename(base) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/metanorma/util.rb', line 97

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



86
87
88
89
90
91
92
93
94
95
# File 'lib/metanorma/util.rb', line 86

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



82
83
84
# File 'lib/metanorma/util.rb', line 82

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