[tpm] Freemind to Perl

J. Bobby Lopez jbl at jbldata.com
Thu May 28 06:33:49 PDT 2009


Hey all,

Not sure if anyone here uses Freemind [
http://freemind.sourceforge.net/wiki/index.php/Main_Page], but I use it
quite a bit for brainstorming and as an outliner.

One of the better uses of it for me is to hammer an idea for a perl hash
tree very quickly.  The problem is that once I have the hash tree exactly
the way I want it in Freemind, I have to manually re-create the hash tree in
perl source, with all the required formatting.

This is no longer the case, as I've written a quick and dirty
"freemind2perl" script (below) which takes a Freemind mindmap file, and
converts it into a perl hash tree automagically.  Not sure it will work with
all versions of Freemind, but their mindmap files are XML based, and they
haven't really changed it across versions.

Just run this script with 'freemind2perl.pl file.mm'.  It requires the
"XML::Simple" perl module to be installed.

Again, not sure if anyone here uses Freemind, but I figured I'd share.

#--------------------------------------------------------------------------------
#!/usr/bin/perl -w

use strict;

use XML::Simple;
use Data::Dumper;

my $xml = new XML::Simple;
my $mm_file = shift;
my $data = $xml->XMLin("$mm_file");
my $clean;

sub prep_clean
{
    my $data = shift;
    my $clean;

    foreach my $key ( keys %{ $data } )
    {
        if ( $key eq "TEXT" )
        {
            $clean->{$data->{$key}} = 1;
        }

        if ( $key eq "node" )
        {
            if ( ref( $data->{$key} ) eq "HASH" )
            {
                $clean->{$data->{'TEXT'}} = prep_clean(\%{$data->{$key}});
            }

            if ( ref( $data->{$key} ) eq "ARRAY" )
            {
                my $sub_hashes = {};
                for ( my $i = 0; $i <= $#{$data->{$key}}; $i++)
                {
                    foreach my $sub_hash ( \%{ $data->{$key}[$i] } )
                    {
                        my $subout = prep_clean( $sub_hash );
                        $sub_hashes = { %$sub_hashes, %$subout };
                    }
                }
                $clean->{$data->{'TEXT'}} = $sub_hashes;
            }
        }
    }
    return $clean;
}

$clean = prep_clean( \%{ $data->{'node'} } );

print Dumper(\$clean);

exit;
#--------------------------------------------------------------------------------


-- 
J. Bobby Lopez
Web: http://jbldata.com/
Twitter: http://www.twitter.com/jbobbylopez
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20090528/3dd8d031/attachment.html>


More information about the toronto-pm mailing list