#!/usr/bin/env ruby -w
require 'rexml/document'
class TomboyDirectory < Hash
attr_reader :directory
def initialize(directory)
raise "#{directory} is not a directory" unless File.lstat(directory).directory?
@directory = directory
for fn in Dir.glob "#{directory}/*.note"
doc = REXML::Document.new File.read fn
self[doc.elements['note/title'].text] = File.basename(fn)
end
end
end
if __FILE__ == $0
raise "No left-directory (source) specified" if ARGV[0].nil?
raise "No right-directory (target) specified" if ARGV[1].nil?
left = TomboyDirectory.new ARGV[0]
right = TomboyDirectory.new ARGV[1]
# Find notes (by title) in both Tomboy directories that don't have the same UUID.
# Then, rename them so they do.
right.reject { |t, u| (not left.key? t) or left[t] == u } \
.inject({}) { |m, (t, u)| m.update({u => left[t]}) } \
.inject({}) { |m, (orig, new)| m.update({File.join(right.directory, orig) => File.join(right.directory, new)}) } \
.each { |orig, new| raise "Right-side note collision" if File.exists? new
File.rename(orig, new) }
end
Refactorings
No refactoring yet !
Learning Ruby, and learning it's functional support...