Class: Soywiki::Renamer
Constant Summary
collapse
- RELATIVE_LINK_REGEX =
/(\A|\s)([A-Z][a-z]+[A-Z0-9]\w*)/
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from PathHelper
#ensure_path, #in_repo, #repo_path=, #repo_relative
Constructor Details
#initialize(repo_path, old_name, new_name) ⇒ Renamer
Returns a new instance of Renamer.
8
9
10
11
12
13
14
15
|
# 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
|
Instance Attribute Details
#memo ⇒ Object
Returns the value of attribute memo.
4
5
6
|
# File 'lib/soywiki/renamer.rb', line 4
def memo
@memo
end
|
#new_name ⇒ Object
Returns the value of attribute new_name.
3
4
5
|
# File 'lib/soywiki/renamer.rb', line 3
def new_name
@new_name
end
|
#old_name ⇒ Object
Returns the value of attribute old_name.
3
4
5
|
# File 'lib/soywiki/renamer.rb', line 3
def old_name
@old_name
end
|
#repo_path ⇒ Object
Returns the value of attribute repo_path.
3
4
5
|
# File 'lib/soywiki/renamer.rb', line 3
def repo_path
@repo_path
end
|
Instance Method Details
#absolutize_unqualified_outbound_links ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/soywiki/renamer.rb', line 89
def absolutize_unqualified_outbound_links
memorize "Absolutizing unqualified inbound links"
target_file = ensure_path(in_repo(new_name).to_s.to_file_path)
if target_file.exist?
text = target_file.read
begin
matches = text.scan(RELATIVE_LINK_REGEX).map {|x| x[1]}.
select {|match| match.strip != "" }.
select {|match| in_repo("#{namespace(:old)}/#{match}").exist? }
puts memorize(" - In file #{target_file}: matches: #{matches.inspect}")
text = text.gsub(RELATIVE_LINK_REGEX) do |match|
if matches.include?($2)
res = "#$1#{namespace(:old)}.#{$2}"
memorize " - In file #{target_file}: #{$2} -> #{res.strip}"
res
else
memorize " - 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
|
#change_all_absolute_links ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/soywiki/renamer.rb', line 57
def change_all_absolute_links
memorize "- Updating all absolute links"
grep_for_files(page_title(:old), repo_path).each do |file|
text = File.read(file)
begin
regex = /\b#{page_title(:old)}\b/
matches = text.scan(regex)
text = text.gsub(regex, page_title(:new))
File.open(file, 'w') {|f| f.puts text}
report file, page_title(:old), page_title(:new)
rescue
puts "Error processing #{file}: #$!"
end
end
end
|
#change_unqualified_inbound_links_in_same_namespace ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/soywiki/renamer.rb', line 73
def change_unqualified_inbound_links_in_same_namespace
memorize "- Updating unqualified inbound links"
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' + page_title(:new))
File.open(file, 'w') {|f| f.puts text}
report file, short_page_title(:old), new_name
rescue
puts "Error processing #{file}: #$!"
end
end
end
|
#grep_for_files(search, where, ignore = /(\.swp|\.swo)$/) ⇒ Object
50
51
52
53
54
55
|
# 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) ⇒ Object
39
40
41
42
43
44
|
# 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) ⇒ Object
17
18
19
20
21
|
# 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) ⇒ Object
23
24
25
26
27
|
# 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 ⇒ Object
46
47
48
|
# File 'lib/soywiki/renamer.rb', line 46
def print_report
puts @memo.join("\n")
end
|
#rename ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/soywiki/renamer.rb', line 117
def rename
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
else
change_unqualified_inbound_links_in_same_namespace
absolutize_unqualified_outbound_links
end
change_all_absolute_links
end
|
#report(file, oldname, newname) ⇒ Object
35
36
37
|
# File 'lib/soywiki/renamer.rb', line 35
def report(file, oldname, newname)
@memo << " - In #{file}: #{oldname} -> #{newname}"
end
|
#short_page_title(query = nil) ⇒ Object
29
30
31
32
33
|
# 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
|