Text::Thread - format threaded items to ascii tree


Text-Thread documentation Contained in the Text-Thread distribution.

Index


Code Index:

NAME

Top

Text::Thread - format threaded items to ascii tree

SYNOPSIS

Top

    use Text::Thread;

    my @tree = (
     { title => 'test1',
       child => [{ title => 'test2',
		   child => [{ title => 'test5' },
			     { title => 'test3'}]}]},
     { title => 'test4' } );

    my @list = Text::Thread::formatthread
        ('child', 'threadtitle', 'title', \@tree);

    print "$_->{threadtitle}\n" foreach @list;

DESCRIPTION

Top

Text::Thread formats a tree structure into a ascii tree, which is often used in threaded mail and netnews reader.

formatthread CHILD THREADTITLE TITLE TREE

format the given TREE. CHILD is the hash key for child nodes in the items in TREE. it could be either arrayref or hashref. THREADTITLE is the key for the output ascii tree in each node. TITLE is the thing to be put at the leaves of the tree.

BUGS

Top

It doesn't work if the depth of the tree is more than 32.

AUTHORS

Top

Chia-liang Kao <clkao@clkao.org>

COPYRIGHT

Top


Text-Thread documentation Contained in the Text-Thread distribution.
package Text::Thread;
use strict;
use warnings;

our $VERSION = '0.2';

no warnings  'closure';

# warning: this is lisp
sub formatthread {
    my ($c, $t, $ot, $tree) = @_;
    no warnings 'uninitialized';
    sub flat {
	my @child = ref($_[0]->{$c}) eq 'HASH' ?
	    values %{$_[0]->{$c}} : @{$_[0]->{$c}} if $_[0]->{$c};
	$_[1] |= (my $bit = 1 << $_[0]->{level});
	($_[0], map { my $last = $_ eq $child[-1];
		      $_->{$t} = (join('',map {
			  ($_[1] & 1 << $_) ? '| ' : '  '
		      }(0..$_[0]->{level}-1))).
			  ($last ? '`' : '|').'->'.
			      ($_->{$ot} eq $_[0]->{$ot}
			       ? '' : $_->{$ot});
		      $_->{level} = $_[0]->{level} + 1;
		      $_[1] ^= $bit if $last;
		      flat($_, $_[1]);
		  } @child)
    };
    map {$_->{$t} = $_->{$ot}; flat $_} @$tree;
}

1;