1 |
package CFPlus::Pod; |
2 |
|
3 |
use strict; |
4 |
|
5 |
use Storable; |
6 |
|
7 |
use CFPlus::UI; |
8 |
|
9 |
our $VERSION = 1; |
10 |
|
11 |
our %wiki; |
12 |
|
13 |
*wiki = Storable::retrieve CFPlus::find_rcfile "docwiki.pst"; |
14 |
|
15 |
sub is_prefix_of($@) { |
16 |
my ($node, @path) = @_; |
17 |
|
18 |
return 1 unless @path; |
19 |
|
20 |
my $kw = pop @path; |
21 |
|
22 |
$node = $node->{parent} |
23 |
or return 0; |
24 |
|
25 |
return ! ! grep $_ eq $kw, @{ $node->{kw} }; |
26 |
} |
27 |
|
28 |
sub find(@) { |
29 |
my (@path) = @_; |
30 |
|
31 |
return unless @path; |
32 |
|
33 |
my $kw = pop @path; |
34 |
|
35 |
# TODO: make sure results are unique |
36 |
|
37 |
grep { is_prefix_of $_, @path } |
38 |
map @$_, |
39 |
$kw eq "*" ? @wiki{sort keys %wiki} |
40 |
: grep $_, $wiki{$kw} |
41 |
} |
42 |
|
43 |
sub full_path_of($) { |
44 |
my ($node) = @_; |
45 |
|
46 |
my $path = $node->{kw}[0]; |
47 |
$path = "$node->{kw}[0]/$path" while $node = $node->{parent}; |
48 |
$path |
49 |
} |
50 |
|
51 |
sub section_of($) { |
52 |
my ($node) = @_; |
53 |
|
54 |
my $doc = $node->{doc}; |
55 |
my $par = $node->{par}; |
56 |
my $lvl = $node->{level}; |
57 |
|
58 |
my @res; |
59 |
|
60 |
do { |
61 |
my $p = $doc->[$par]; |
62 |
|
63 |
my %para = ( |
64 |
markup => $p->{markup}, |
65 |
indent => $p->{indent}, |
66 |
); |
67 |
|
68 |
for (@{ $p->{widget} || [] }) { |
69 |
my ($class, @args) = @$_; |
70 |
push @{ $para{widget} }, $class->new (@args); |
71 |
} |
72 |
|
73 |
push @res, \%para; |
74 |
} while $doc->[++$par]{level} > $lvl; |
75 |
|
76 |
@res |
77 |
} |
78 |
|
79 |
sub section(@) { |
80 |
map section_of $_, &find |
81 |
} |
82 |
|
83 |
sub as_label(@) { |
84 |
join "\n", |
85 |
map +("\xa0" x ($_->{indent} / 4)) . $_->{markup}, |
86 |
@_ |
87 |
} |
88 |
|
89 |
1 |