Sha256: c390fac1167a700ca519376b065838c3cd9008d84f08668d010d2e8e700c9192

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

#!/usr/bin/env ruby
oldname, newname = *ARGV

class String
  def is_namespaced?
    self.split(".").size == 2
  end
end

target_files = `grep -lF '#{oldname}' *`.strip.split(/\n/)
target_files.each do |file|
  text = File.read(file)
  text.gsub!(/\b#{oldname}\b/, newname)
  File.open(file, 'w') {|f| f.puts text}
end

# deal with links without namespace

old_short = oldname.split(".")[1]
new_short = newname.split(".")[1]

if old_short && !new_short
  # where going from a namespaced name to a top-level name
  old_namespace = oldname.split(".")[0]
  target_files = `grep -lF '\<#{old_short}\>' *`.strip.split(/\n/)
  target_files.each do |file|
    text = File.read(file)
    text.gsub!(/\b#{old_short}\b/, newname)
    File.open(file, 'w') {|f| f.puts text}
  end
  # change all the .Wikiwords in the newname file to fully namespaced
  # links with namespace of newname.downcase
  if File.exist?(newname)
    text = File.read(newname)
    #text.gsub!(relative_wiki_word, "#{newname.downcase}.#{$1}")
    # ".Pear".gsub!(/\A\.([A-Z][a-z]+[A-Z]\w*)/, "TEST.#{$1}")
    text.gsub!(/([\A\s\n\b])\.([A-Z][a-z]+[A-Z]\w*)/, '\1' + newname.downcase + '.\2')
    File.open(newname, 'w') {|f| f.puts text}
  end
elsif !old_short && new_short
  # where going from a top-level name to a namespaced one 
  # no action needed
elsif !old_short && !new_short
  # no action needed
elsif old_short && new_short
  target_files = `grep -lF '#{old_short}' *`.strip.split(/\n/)
  puts target_files.inspect
  target_files.each do |file|
    text = File.read(file)
    text.gsub!(/\b#{old_short}\b/, new_short)
    File.open(file, 'w') {|f| f.puts text}
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
soywiki-0.0.1 bin/soywiki-rename