1 |
package CFPlus::Pod; |
2 |
|
3 |
use strict; |
4 |
|
5 |
use Storable; |
6 |
|
7 |
our $VERSION = 1; |
8 |
|
9 |
our %wiki; |
10 |
|
11 |
my $MA_BEG = "\x{fcd0}"; |
12 |
my $MA_SEP = "\x{fcd1}"; |
13 |
my $MA_END = "\x{fcd2}"; |
14 |
|
15 |
*wiki = Storable::retrieve CFPlus::find_rcfile "docwiki.pst"; |
16 |
|
17 |
sub is_prefix_of($@) { |
18 |
my ($node, @path) = @_; |
19 |
|
20 |
return 1 unless @path; |
21 |
|
22 |
my $kw = pop @path; |
23 |
|
24 |
$node = $node->{parent} |
25 |
or return 0; |
26 |
|
27 |
return ! ! grep $_ eq $kw, @{ $node->{kw} }; |
28 |
} |
29 |
|
30 |
sub find(@) { |
31 |
my (@path) = @_; |
32 |
|
33 |
return unless @path; |
34 |
|
35 |
my $kw = pop @path; |
36 |
|
37 |
# TODO: make sure results are unique |
38 |
|
39 |
grep { is_prefix_of $_, @path } |
40 |
map @$_, |
41 |
$kw eq "*" ? @wiki{sort keys %wiki} |
42 |
: grep $_, $wiki{$kw} |
43 |
} |
44 |
|
45 |
sub full_path_of($) { |
46 |
my ($node) = @_; |
47 |
|
48 |
my $path = $node->{kw}[0]; |
49 |
$path = "$node->{kw}[0]/$path" while $node = $node->{parent}; |
50 |
$path |
51 |
} |
52 |
|
53 |
sub section_of($) { |
54 |
my ($node) = @_; |
55 |
|
56 |
my $doc = $node->{doc}; |
57 |
my $par = $node->{par}; |
58 |
my $lvl = $node->{level}; |
59 |
|
60 |
my @res; |
61 |
|
62 |
do { |
63 |
my $p = $doc->[$par]; |
64 |
|
65 |
if (length $p->{markup}) { |
66 |
push @res, { |
67 |
markup => $p->{markup}, |
68 |
indent => $p->{indent}, |
69 |
}; |
70 |
} |
71 |
} while $doc->[++$par]{level} > $lvl; |
72 |
|
73 |
@res |
74 |
} |
75 |
|
76 |
sub section(@) { |
77 |
map section_of $_, &find |
78 |
} |
79 |
|
80 |
sub thaw_section(\@\%) { |
81 |
for (@{$_[0]}) { |
82 |
$_->{markup} =~ s{ |
83 |
$MA_BEG |
84 |
([^$MA_END]+) |
85 |
$MA_END |
86 |
}{ |
87 |
my ($type, @arg) = split /$MA_SEP/o, $1; |
88 |
|
89 |
$_[1]{$type}($_, @arg) |
90 |
}ogex; |
91 |
} |
92 |
} |
93 |
|
94 |
my %as_label = ( |
95 |
image => sub { |
96 |
my ($par, $path) = @_; |
97 |
|
98 |
"<small>img</small>" |
99 |
}, |
100 |
link => sub { |
101 |
my ($par, $link) = @_; |
102 |
|
103 |
"<big>" . (CFPlus::asxml $link) . "</big>" |
104 |
}, |
105 |
); |
106 |
|
107 |
sub as_label(@) { |
108 |
thaw_section @_, %as_label; |
109 |
|
110 |
my $text = |
111 |
join "\n", |
112 |
map +("\xa0" x ($_->{indent} / 4)) . $_->{markup}, |
113 |
@_; |
114 |
|
115 |
$text =~ s/^\s+//; |
116 |
$text =~ s/\s+$//; |
117 |
|
118 |
$text |
119 |
} |
120 |
|
121 |
my %as_paragraphs = ( |
122 |
image => sub { |
123 |
my ($par, $path) = @_; |
124 |
|
125 |
push @{ $par->{widget} }, new CFPlus::UI::Image path => $path; |
126 |
|
127 |
"\x{FFFC}" |
128 |
}, |
129 |
link => sub { |
130 |
my ($par, $link) = @_; |
131 |
|
132 |
"<big>" . (CFPlus::asxml $link) . "</big>" |
133 |
}, |
134 |
); |
135 |
|
136 |
sub as_paragraphs(@) { |
137 |
thaw_section @_, %as_paragraphs; |
138 |
|
139 |
@_ |
140 |
} |
141 |
|
142 |
sub section_paragraphs(@) { |
143 |
as_paragraphs §ion |
144 |
} |
145 |
|
146 |
sub section_label(@) { |
147 |
as_label §ion |
148 |
} |
149 |
|
150 |
1 |