class Soywiki::Renamer

Constants

Attributes

memo[R]
new_name[R]
old_name[R]
repo_path[R]

Public Class Methods

new(repo_path, old_name, new_name) click to toggle source
# File lib/soywiki/renamer.rb, line 8
def initialize(repo_path, old_name, new_name)
  @repo_path = ensure_path(repo_path)
  @old_path = ensure_path(old_name)
  @new_path = ensure_path(new_name)
  @old_name = repo_relative(@old_path).to_s
  @new_name = repo_relative(@new_path).to_s
  @memo = ["Updating inbound and outbound links..."]
end

Public Instance Methods

grep_for_files(search, where, ignore=/(\.swp|\.swo)$/) click to toggle source
# File lib/soywiki/renamer.rb, line 50
def grep_for_files(search, where, ignore=/(\.swp|\.swo)$/)
  cmd = "grep -rlF '#{search}' #{where}"
  puts cmd
  files = `#{cmd}`.strip.split(/\n/)
  ignore ? files.select { |f| f !~ ignore } : files
end
memorize(message) click to toggle source
# File lib/soywiki/renamer.rb, line 39
def memorize(message)
  @memo ||= []
  @memo << message if message.is_a?(String)
  @memo += message if message.is_a?(Array)
  message
end
namespace(query=nil) click to toggle source
# File lib/soywiki/renamer.rb, line 17
def namespace(query=nil)
  self.instance_variable_get("@#{query}_name").namespace
rescue
  nil
end
page_title(query=nil) click to toggle source
# File lib/soywiki/renamer.rb, line 23
def page_title(query=nil)
  self.instance_variable_get("@#{query}_name").to_page_title
rescue
  nil
end
print_report() click to toggle source
rename() click to toggle source
# File lib/soywiki/renamer.rb, line 117
def rename
  # Three other cases to cover, involving namespaces:
  #
  # Case 1: newname is in same namespace as oldname
  #
  # In the directory for OldName's namespace, change all unqualified references to
  # OldName to NewName

  if namespace(:old) == namespace(:new)
    memorize "- Updating unqualified links in same namespace"
    grep_for_files(short_page_title(:old), in_repo(namespace(:old))).each do |file|
      text = File.read(file)
      begin
        text = text.gsub(/(\A|\s)(#{short_page_title(:old)})\b/, '\1' + short_page_title(:new))
        File.open(file, 'w') {|f| f.puts text}
        report file, short_page_title(:old), short_page_title(:new)
      rescue
        puts "Error processing #{file}: #$!"
      end
    end
    # Case 2: newname is in different namespace from oldname
    # oldname.namespace != newname.namespace
  else
    # In the directory for OldName's namespace, change all unqualified references to
    # OldName to newnamespace.NewName (i.e. NewName).
    change_unqualified_inbound_links_in_same_namespace
    # And in the renamed file, change all unqualified references to
    # PageName to oldnamespace.PageName
    absolutize_unqualified_outbound_links
  end

  # Finally,
  change_all_absolute_links
end
report(file, oldname, newname) click to toggle source
# File lib/soywiki/renamer.rb, line 35
def report(file, oldname, newname)
  @memo <<  "  - In #{file}: #{oldname} -> #{newname}"
end
short_page_title(query=nil) click to toggle source
# File lib/soywiki/renamer.rb, line 29
def short_page_title(query=nil)
  self.instance_variable_get("@#{query}_name").short_page_title
rescue
  nil
end