line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Thread; |
2
|
1
|
|
|
1
|
|
917
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
41
|
|
3
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
53
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.2'; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
14
|
no warnings 'closure'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
84
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Text::Thread - format threaded items to ascii tree |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 SYNOPSIS |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use Text::Thread; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my @tree = ( |
18
|
|
|
|
|
|
|
{ title => 'test1', |
19
|
|
|
|
|
|
|
child => [{ title => 'test2', |
20
|
|
|
|
|
|
|
child => [{ title => 'test5' }, |
21
|
|
|
|
|
|
|
{ title => 'test3'}]}]}, |
22
|
|
|
|
|
|
|
{ title => 'test4' } ); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my @list = Text::Thread::formatthread |
25
|
|
|
|
|
|
|
('child', 'threadtitle', 'title', \@tree); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
print "$_->{threadtitle}\n" foreach @list; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 DESCRIPTION |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
B formats a tree structure into a ascii tree, which is |
32
|
|
|
|
|
|
|
often used in threaded mail and netnews reader. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=over 4 |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=item formatthread CHILD THREADTITLE TITLE TREE |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
format the given TREE. CHILD is the hash key for child nodes in the |
39
|
|
|
|
|
|
|
items in TREE. it could be either arrayref or hashref. THREADTITLE is |
40
|
|
|
|
|
|
|
the key for the output ascii tree in each node. TITLE is the thing to |
41
|
|
|
|
|
|
|
be put at the leaves of the tree. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=back |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=cut |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# warning: this is lisp |
48
|
|
|
|
|
|
|
sub formatthread { |
49
|
1
|
|
|
1
|
1
|
391
|
my ($c, $t, $ot, $tree) = @_; |
50
|
1
|
|
|
1
|
|
4
|
no warnings 'uninitialized'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
421
|
|
51
|
|
|
|
|
|
|
sub flat { |
52
|
0
|
|
|
|
|
0
|
my @child = ref($_[0]->{$c}) eq 'HASH' ? |
53
|
6
|
50
|
|
6
|
0
|
20
|
values %{$_[0]->{$c}} : @{$_[0]->{$c}} if $_[0]->{$c}; |
|
2
|
100
|
|
|
|
4
|
|
54
|
6
|
|
|
|
|
9
|
$_[1] |= (my $bit = 1 << $_[0]->{level}); |
55
|
6
|
|
|
|
|
15
|
($_[0], map { my $last = $_ eq $child[-1]; |
|
4
|
|
|
|
|
7
|
|
56
|
2
|
50
|
|
|
|
12
|
$_->{$t} = (join('',map { |
57
|
4
|
100
|
|
|
|
17
|
($_[1] & 1 << $_) ? '| ' : ' ' |
|
|
50
|
|
|
|
|
|
58
|
|
|
|
|
|
|
}(0..$_[0]->{level}-1))). |
59
|
|
|
|
|
|
|
($last ? '`' : '|').'->'. |
60
|
|
|
|
|
|
|
($_->{$ot} eq $_[0]->{$ot} |
61
|
|
|
|
|
|
|
? '' : $_->{$ot}); |
62
|
4
|
|
|
|
|
8
|
$_->{level} = $_[0]->{level} + 1; |
63
|
4
|
100
|
|
|
|
7
|
$_[1] ^= $bit if $last; |
64
|
4
|
|
|
|
|
12
|
flat($_, $_[1]); |
65
|
|
|
|
|
|
|
} @child) |
66
|
|
|
|
|
|
|
}; |
67
|
1
|
|
|
|
|
2
|
map {$_->{$t} = $_->{$ot}; flat $_} @$tree; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
4
|
|
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
1; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 BUGS |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
It doesn't work if the depth of the tree is more than 32. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 AUTHORS |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Chia-liang Kao Eclkao@clkao.org> |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 COPYRIGHT |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Copyright 2001,2006 by Chia-liang kao Eclkao@clkao.org>. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
85
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
See L |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=cut |