Sha256: 6c4cb35154e63fa5ca15a45596954bd0ea64f7cd9038a6e396d02133b8d90f5e

Contents?: true

Size: 1.03 KB

Versions: 8

Compression:

Stored size: 1.03 KB

Contents

module Metanorma
  class Collection
    module Util
      class DisambigFiles
        def initialize
          @seen_filenames = []
        end

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

        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

        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
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
metanorma-2.0.7 lib/metanorma/collection/util/disambig_files.rb
metanorma-2.0.6 lib/metanorma/collection/util/disambig_files.rb
metanorma-2.0.5 lib/metanorma/collection/util/disambig_files.rb
metanorma-2.0.4 lib/metanorma/collection/util/disambig_files.rb
metanorma-2.0.3 lib/metanorma/collection/util/disambig_files.rb
metanorma-2.0.2 lib/metanorma/collection/util/disambig_files.rb
metanorma-2.0.1 lib/metanorma/collection/util/disambig_files.rb
metanorma-2.0.0 lib/metanorma/collection/util/disambig_files.rb