#!/usr/bin/env ruby # encoding: UTF-8 require 'soywiki' oldname, newname = *ARGV MEMO = ["Updating inbound and outbound links..."] def report(file, oldname, newname) MEMO << " - In #{file}: #{oldname.to_page_title} -> #{newname.to_page_title}" end def change_all_absolute_links(oldname, newname) MEMO << "- Updating all absolute links" target_files = `grep -rlF '#{oldname.to_page_title}' *`.strip.split(/\n/) target_files.select {|f| f !~ /(\.swp|\.swo)$/}.each do |file| text = File.read(file) begin regex = /\b#{oldname.to_page_title}\b/ matches = text.scan(regex) text = text.gsub(regex, newname.to_page_title) File.open(file, 'w') {|f| f.puts text} report file, oldname.to_page_title, newname.to_page_title rescue puts "Error processing #{file}: #$!" end end end def change_unqualified_inbound_links_in_same_namespace(oldname, newname) MEMO << "- Updating unqualified inbound links" target_files = `grep -rlF '#{oldname.short_page_title}' #{oldname.namespace}/*`.strip.split(/\n/) target_files.select {|f| f !~ /(\.swp|\.swo)$/}.each do |file| text = File.read(file) begin text = text.gsub(/(\A|\s)#{oldname.short_page_title}\b/, newname.to_page_title) File.open(file, 'w') {|f| f.puts text} report file, oldname.short_page_title, newname rescue puts "Error processing #{file}: #$!" end end end RELATIVE_LINK_REGEX = /(\A|\s)([A-Z][a-z]+[A-Z]\w*)/ def absolutize_unqualified_outbound_links(oldname, newname) MEMO << "Absolutizing unqualified inbound links" target_file = newname.to_file_path if File.exist?(target_file) text = File.read(target_file) begin matches = text.scan(RELATIVE_LINK_REGEX).map {|x| x[1]}. select {|match| match.strip != "" }. select {|match| File.exist?( oldname.namespace + "/#{match}" ) } puts MEMO << " - In file #{target_file}: matches: #{matches.inspect}" text = text.gsub(RELATIVE_LINK_REGEX) do |match| if matches.include?($2) res = "#$1#{oldname.namespace}.#{$2}" MEMO << " - In file #{target_file}: #{$2} -> #{res.strip}" res else MEMO << " - In file #{target_file}: skipping #{$2}" match end end File.open(target_file, 'w') {|f| f.puts text} rescue puts "Error processing #{target_file}: #$!" end end end # 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 referenecs to # OldName to NewName if oldname.namespace == newname.namespace MEMO << "- Updating unqualified links in same namespace" target_files = `grep -rlF '#{oldname.short_page_title}' #{oldname.namespace}/*`.strip.split(/\n/) target_files.select {|f| f !~ /(\.swp|\.swo)$/}.each do |file| text = File.read(file) begin text = text.gsub(/(\A|\s)(#{oldname.short_page_title})\b/, newname.short_page_title) File.open(file, 'w') {|f| f.puts text} report file, oldname.short_page_title, newname.short_page_title rescue puts "Error processing #{file}: #$!" end end end # Case 2: newname is in different namespace from oldname if oldname.namespace != newname.namespace # 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(oldname, newname) # And in the renamed file, change all unqualified references to # PageName to oldnamespace.PageName absolutize_unqualified_outbound_links(oldname, newname) end # Finally, change_all_absolute_links oldname, newname puts MEMO.join("\n")