Skip to content

Instantly share code, notes, and snippets.

@kch
Created February 4, 2010 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kch/295207 to your computer and use it in GitHub Desktop.
Save kch/295207 to your computer and use it in GitHub Desktop.
Converts Yojimbo's export format for password and serial number records into CSV.
#!/usr/bin/env ruby1.9
# encoding: UTF-8
# rb-yconvert by Caio Chassot
# because yconvert by Steven Frank is PHP and this cannot stand.
# http://stevenf.com/downloads/yconvert.php.txt
#
# Converts Yojimbo's export format for password and serial number
# records into CSV.
#
# Requires ruby 1.9 or macruby, because 1.8 is passé.
#
# Usage: yconvert.rb < path-to-exported-yojimbo-files > output.csv
#
($stderr.puts "Usage: #{File.basename $0} <directory>"; exit) if ARGV.length != 1
require 'csv'
require 'pathname'
CSV $stdout do |out|
Pathname.new(ARGV[0]).children.select(&:file?).map(&:readlines).each do |lines|
ix_comments = lines.index { |s| s.sub! /^Comments: /, '' }
comments = lines.slice!(ix_comments..-1).join.strip
lines.map! { |s| s.split(": ", 2).last.chomp } << comments
out << lines
end
end
#!/usr/bin/php
<?php
#
# yconvert by Steven Frank <stevenf@panic.com>
# http://stevenf.com/
#
# Converts Yojimbo's export format for password and serial number
# records into CSV.
#
# Usage: ./yconvert.php <path-to-exported-yojimbo-files> >output.csv
#
if ( count($argv) == 1 )
{
print "Usage: {$argv[0]} <directory>\n";
exit;
}
$srcdir = $argv[1];
$dir = opendir($srcdir);
while ( $path = readdir($dir) )
{
if ( $path{0} == '.' )
continue;
$content = file_get_contents("$srcdir/$path", "r");
$lines = preg_split("/\n/", $content);
$field = 0;
$inComments = false;
$comment = '';
foreach ( $lines as $line )
{
preg_match("/(.*?): (.*)$/", $line, $matches);
if ( $matches[1] == 'Comments' )
{
$comment = "{$matches[2]}\n";
$inComments = true;
continue;
}
if ( $inComments )
{
$comment .= "$line\n";
}
else
{
if ( $field > 0 )
print ",";
print "\"{$matches[2]}\"";
}
++$field;
}
$comment = trim($comment);
print ",\"$comment\"";
print "\n";
}
closedir($dir);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment