Skip to content

Commit

Permalink
move renamer and expander classes into own files.
Browse files Browse the repository at this point in the history
  • Loading branch information
0robustus1 committed Jan 25, 2014
1 parent 21eca4e commit f4ded09
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 237 deletions.
86 changes: 1 addition & 85 deletions bin/soywiki-expand
Original file line number Original file line Diff line number Diff line change
@@ -1,90 +1,6 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
require 'soywiki' require 'soywiki'

require 'expander'
class Soywiki::Expander
# Takes any wiki link that stands alone on a line and expands it
# this is different from Soywiki::WIKI_WORD in that it requires ^\s* before the
# first letter

WIKI_LINK_PATTERN = /^\s*([a-z0-9]\w+\.)?[A-Z][a-z]+[A-Z0-9]\w*\s*$/

attr_reader :mode, :file, :processed_files
attr_reader :repo_path, :file_path
attr_reader :expanded_text

include PathHelper

def initialize(repo_path, mode, file)
@repo_path = ensure_path(repo_path)
@mode = mode
@file_path = ensure_path(file)
@file = repo_relative(file).to_s
@processed_files = []
end

def seamless?
mode == 'seamless'
end

def seamful?
mode == 'seamful'
end

def indent(text, level)
return text if seamless?
return text if level == 0
('|' * level) + ' ' + text
end

def divider
'+' + '-' * 78 + '+'
end

def register_in_expansion(text, inline=false)
@expanded_text ||= ''
full_text = inline ? text : text + "\n"
@expanded_text << full_text
end

def recursive_expand(file_path, name, level=0)
processed_files << file_path
lines = file_path.readlines
title = lines.shift # takes title
lines = lines.join.strip.split("\n")
if seamful?
register_in_expansion divider unless level == 0
register_in_expansion indent(title, level)
end
lines.each do |line|
# note that the wiki link must be alone on the line to be expanded
if line =~ WIKI_LINK_PATTERN
link = line.strip
if link =~ /(\A|\s)[A-Z]/ # short link in namespace (relative link)
link = [name.namespace, link].join('.')
end
link_file_path = in_repo(link.to_file_path)
if link_file_path.file? && !processed_files.include?(link_file_path)
recursive_expand(link_file_path, link, level + 1) # recursive call
elsif processed_files.include?(link_file_path)
register_in_expansion indent("#{link} [[already expanded]]", level)
elsif !link_file_path.file?
register_in_expansion indent("#{link} [[no file found]]", level)
else
register_in_expansion indent("#{link}", level)
end
else
register_in_expansion indent(line, level)
end
end
register_in_expansion divider if seamful? && level != 0
end

def expand
recursive_expand(file_path, file.to_page_title)
expanded_text
end

end


repo_path, mode, file = *ARGV repo_path, mode, file = *ARGV
expander = Soywiki::Expander.new(repo_path, mode, file) expander = Soywiki::Expander.new(repo_path, mode, file)
Expand Down
153 changes: 1 addition & 152 deletions bin/soywiki-rename
Original file line number Original file line Diff line number Diff line change
@@ -1,158 +1,7 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# encoding: UTF-8 # encoding: UTF-8
require 'soywiki' require 'soywiki'

require 'renamer'
class Soywiki::Renamer
attr_reader :repo_path, :old_name, :new_name
attr_reader :memo

include PathHelper

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

def namespace(query=nil)
self.instance_variable_get("@#{query}_name").namespace
rescue
nil
end

def page_title(query=nil)
self.instance_variable_get("@#{query}_name").to_page_title
rescue
nil
end

def short_page_title(query=nil)
self.instance_variable_get("@#{query}_name").short_page_title
rescue
nil
end

def report(file, oldname, newname)
@memo << " - In #{file}: #{oldname} -> #{newname}"
end

def memorize(message)
@memo ||= []
@memo << message if message.is_a?(String)
@memo += message if message.is_a?(Array)
message
end

def print_report
puts @memo.join("\n")
end

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

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

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

RELATIVE_LINK_REGEX = /(\A|\s)([A-Z][a-z]+[A-Z0-9]\w*)/

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

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

end


repo_path, old_name, new_name = *ARGV repo_path, old_name, new_name = *ARGV
repo_path = Pathname.new(repo_path) repo_path = Pathname.new(repo_path)
Expand Down
86 changes: 86 additions & 0 deletions lib/soywiki/expander.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,86 @@
module Soywiki
class Expander
# Takes any wiki link that stands alone on a line and expands it
# this is different from Soywiki::WIKI_WORD in that it requires ^\s* before the
# first letter

WIKI_LINK_PATTERN = /^\s*([a-z0-9]\w+\.)?[A-Z][a-z]+[A-Z0-9]\w*\s*$/

attr_reader :mode, :file, :processed_files
attr_reader :repo_path, :file_path
attr_reader :expanded_text

include PathHelper

def initialize(repo_path, mode, file)
@repo_path = ensure_path(repo_path)
@mode = mode
@file_path = ensure_path(file)
@file = repo_relative(file).to_s
@processed_files = []
end

def seamless?
mode == 'seamless'
end

def seamful?
mode == 'seamful'
end

def indent(text, level)
return text if seamless?
return text if level == 0
('|' * level) + ' ' + text
end

def divider
'+' + '-' * 78 + '+'
end

def register_in_expansion(text, inline=false)
@expanded_text ||= ''
full_text = inline ? text : text + "\n"
@expanded_text << full_text
end

def recursive_expand(file_path, name, level=0)
processed_files << file_path
lines = file_path.readlines
title = lines.shift # takes title
lines = lines.join.strip.split("\n")
if seamful?
register_in_expansion divider unless level == 0
register_in_expansion indent(title, level)
end
lines.each do |line|
# note that the wiki link must be alone on the line to be expanded
if line =~ WIKI_LINK_PATTERN
link = line.strip
if link =~ /(\A|\s)[A-Z]/ # short link in namespace (relative link)
link = [name.namespace, link].join('.')
end
link_file_path = in_repo(link.to_file_path)
if link_file_path.file? && !processed_files.include?(link_file_path)
recursive_expand(link_file_path, link, level + 1) # recursive call
elsif processed_files.include?(link_file_path)
register_in_expansion indent("#{link} [[already expanded]]", level)
elsif !link_file_path.file?
register_in_expansion indent("#{link} [[no file found]]", level)
else
register_in_expansion indent("#{link}", level)
end
else
register_in_expansion indent(line, level)
end
end
register_in_expansion divider if seamful? && level != 0
end

def expand
recursive_expand(file_path, file.to_page_title)
expanded_text
end

end
end
Loading

0 comments on commit f4ded09

Please sign in to comment.