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.



73
74
75
# File 'lib/metanorma/util.rb', line 73

def initialize
  @seen_filenames = []
end

Instance Method Details

#disambiguate_filename(base) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/metanorma/util.rb', line 92

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



81
82
83
84
85
86
87
88
89
90
# File 'lib/metanorma/util.rb', line 81

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



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

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