1 | package CFPlus::Pod; |
1 | package CFPlus::Pod; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | |
4 | |
5 | use Pod::POM; |
5 | use Storable; |
6 | |
6 | |
7 | use CFPlus; |
7 | our $VERSION = 1; |
8 | use CFPlus::UI; |
|
|
9 | |
8 | |
10 | our $VERSION = 1.02; # bump if resultant formatting changes |
9 | our %wiki; |
11 | |
10 | |
12 | our @result; |
11 | my $MA_BEG = "\x{fcd0}"; |
13 | our $indent; |
12 | my $MA_SEP = "\x{fcd1}"; |
|
|
13 | my $MA_END = "\x{fcd2}"; |
14 | |
14 | |
15 | package CFPlus::Pod::AsMarkup; |
15 | *wiki = Storable::retrieve CFPlus::find_rcfile "docwiki.pst"; |
16 | |
16 | |
17 | use strict; |
17 | sub is_prefix_of($@) { |
|
|
18 | my ($node, @path) = @_; |
18 | |
19 | |
19 | use base "Pod::POM::View::Text"; |
20 | return 1 unless @path; |
20 | |
21 | |
21 | *view_seq_file = |
22 | my $kw = pop @path; |
22 | *view_seq_code = |
|
|
23 | *view_seq_bold = sub { "<b>$_[1]</b>" }; |
|
|
24 | *view_seq_italic = sub { "<i>$_[1]</i>" }; |
|
|
25 | *view_seq_space = |
|
|
26 | *view_seq_link = sub { CFPlus::asxml $_[1] }; |
|
|
27 | *view_seq_zero = |
|
|
28 | *view_seq_index = sub { }; |
|
|
29 | |
23 | |
30 | sub view_seq_text { |
24 | $node = $node->{parent} |
31 | my $text = $_[1]; |
25 | or return 0; |
32 | $text =~ s/\s+/ /g; |
26 | |
33 | CFPlus::asxml $text |
27 | return ! ! grep $_ eq $kw, @{ $node->{kw} }; |
34 | } |
28 | } |
35 | |
29 | |
36 | sub view_item { |
30 | sub find(@) { |
37 | ("\t" x ($indent / 4)) |
31 | my (@path) = @_; |
38 | . $_[1]->title->present ($_[0]) |
32 | |
39 | . "\n\n" |
33 | return unless @path; |
40 | . $_[1]->content->present ($_[0]) |
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} |
41 | } |
43 | } |
42 | |
44 | |
43 | sub view_verbatim { |
45 | sub full_path_of($) { |
44 | (join "", |
46 | my ($node) = @_; |
45 | map +("\t" x ($indent / 2)) . "<tt>$_</tt>\n", |
47 | |
46 | split /\n/, CFPlus::asxml $_[1]) |
48 | my $path = $node->{kw}[0]; |
47 | . "\n" |
49 | $path = "$node->{kw}[0]/$path" while $node = $node->{parent}; |
|
|
50 | $path |
48 | } |
51 | } |
49 | |
52 | |
50 | sub view_textblock { |
53 | sub section_of($) { |
51 | ("\t" x ($indent / 2)) . "$_[1]\n" |
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 |
52 | } |
74 | } |
53 | |
75 | |
54 | sub view_head1 { |
76 | sub section(@) { |
55 | "\n\n<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" |
77 | map section_of $_, &find |
56 | . $_[1]->content->present ($_[0]) |
|
|
57 | }; |
|
|
58 | |
|
|
59 | sub view_head2 { |
|
|
60 | "\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" |
|
|
61 | . $_[1]->content->present ($_[0]) |
|
|
62 | }; |
|
|
63 | |
|
|
64 | sub view_head3 { |
|
|
65 | "\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" |
|
|
66 | . $_[1]->content->present ($_[0]) |
|
|
67 | }; |
|
|
68 | |
|
|
69 | sub view_over { |
|
|
70 | local $indent = $indent + $_[1]->indent; |
|
|
71 | $_[1]->content->present ($_[0]) |
|
|
72 | } |
78 | } |
73 | |
79 | |
74 | package CFPlus::Pod::AsParagraphs; |
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; |
75 | |
88 | |
76 | use strict; |
89 | $_[1]{$type}($_, @arg) |
77 | |
90 | }ogex; |
78 | use base "Pod::POM::View"; |
91 | } |
79 | |
|
|
80 | *view_seq_file = |
|
|
81 | *view_seq_code = |
|
|
82 | *view_seq_bold = sub { "<b>$_[1]</b>" }; |
|
|
83 | *view_seq_italic = sub { "<i>$_[1]</i>" }; |
|
|
84 | *view_seq_zero = sub { }; |
|
|
85 | *view_seq_space = sub { my $text = $_[1]; $text =~ s/ / /g; $text }; |
|
|
86 | *view_seq_index = sub { warn "index<@_>\n"; $result[-1]{index}{$_[1]} = undef }; |
|
|
87 | |
|
|
88 | sub view_seq_text { |
|
|
89 | my $text = $_[1]; |
|
|
90 | $text =~ s/\s+/ /g; |
|
|
91 | CFPlus::asxml $text |
|
|
92 | } |
92 | } |
93 | |
93 | |
94 | sub view_seq_link { |
94 | my %as_label = ( |
95 | my (undef, $link) = @_; |
95 | image => sub { |
|
|
96 | my ($par, $path) = @_; |
96 | |
97 | |
97 | # TODO: |
98 | "<small>img</small>" |
98 | # http://... |
99 | }, |
99 | # ref |
100 | link => sub { |
100 | # pod/ref |
101 | my ($par, $link) = @_; |
101 | |
102 | |
102 | "<u>" . (CFPlus::asxml $_[1]) . "</u>"; |
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 |
103 | } |
119 | } |
104 | |
120 | |
105 | sub view_item { |
121 | my %as_paragraphs = ( |
106 | push @result, { |
122 | image => sub { |
107 | indent => $indent * 8, |
123 | my ($par, $path) = @_; |
108 | markup => $_[1]->title->present ($_[0]) . "\n\n", |
124 | |
|
|
125 | push @{ $par->{widget} }, new CFPlus::UI::Image path => $path; |
|
|
126 | |
|
|
127 | "\x{FFFC}" |
109 | }; |
128 | }, |
110 | $_[1]->content->present ($_[0]); |
129 | link => sub { |
111 | () |
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 | @_ |
112 | } |
140 | } |
113 | |
141 | |
114 | sub view_verbatim { |
142 | sub section_paragraphs(@) { |
115 | push @result, { |
143 | as_paragraphs §ion |
116 | indent => $indent * 16, |
|
|
117 | markup => "<tt>" . (CFPlus::asxml $_[1]) . "</tt>\n", |
|
|
118 | }; |
|
|
119 | () |
|
|
120 | } |
144 | } |
121 | |
145 | |
122 | sub view_textblock { |
146 | sub section_label(@) { |
123 | push @result, { |
147 | as_label §ion |
124 | indent => $indent * 16, |
|
|
125 | markup => "$_[1]\n", |
|
|
126 | }; |
|
|
127 | () |
|
|
128 | } |
148 | } |
129 | |
149 | |
130 | sub view_head1 { |
150 | 1 |
131 | push @result, { |
|
|
132 | indent => $indent * 16, |
|
|
133 | markup => "\n\n<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n", |
|
|
134 | }; |
|
|
135 | $_[1]->content->present ($_[0]); |
|
|
136 | () |
|
|
137 | }; |
|
|
138 | |
|
|
139 | sub view_head2 { |
|
|
140 | push @result, { |
|
|
141 | indent => $indent * 16, |
|
|
142 | markup => "\n\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n", |
|
|
143 | }; |
|
|
144 | $_[1]->content->present ($_[0]); |
|
|
145 | () |
|
|
146 | }; |
|
|
147 | |
|
|
148 | sub view_head3 { |
|
|
149 | push @result, { |
|
|
150 | indent => $indent * 16, |
|
|
151 | markup => "\n\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n", |
|
|
152 | }; |
|
|
153 | $_[1]->content->present ($_[0]); |
|
|
154 | () |
|
|
155 | }; |
|
|
156 | |
|
|
157 | sub view_over { |
|
|
158 | local $indent = $indent + $_[1]->indent; |
|
|
159 | push @result, { indent => $indent }; |
|
|
160 | $_[1]->content->present ($_[0]); |
|
|
161 | () |
|
|
162 | } |
|
|
163 | |
|
|
164 | sub view_for { |
|
|
165 | if ($_[1]->format eq "image") { |
|
|
166 | push @result, { |
|
|
167 | indent => $indent * 16, |
|
|
168 | markup => "\x{fffc}", |
|
|
169 | widget => [new CFPlus::UI::Image path => "pod/" . $_[1]->text], |
|
|
170 | }; |
|
|
171 | } |
|
|
172 | () |
|
|
173 | } |
|
|
174 | |
|
|
175 | sub view { |
|
|
176 | my ($self, $type, $item) = @_; |
|
|
177 | |
|
|
178 | $item->content->present ($self); |
|
|
179 | } |
|
|
180 | |
|
|
181 | package CFPlus::Pod; |
|
|
182 | |
|
|
183 | my $pod_cache = CFPlus::db_table "pod_cache"; |
|
|
184 | |
|
|
185 | sub load($$$$) { |
|
|
186 | my ($path, $filtertype, $filterversion, $filtercb) = @_; |
|
|
187 | |
|
|
188 | stat $path |
|
|
189 | or die "$path: $!"; |
|
|
190 | |
|
|
191 | my $phash = join ",", $filterversion, $VERSION, (stat _)[7,9]; |
|
|
192 | |
|
|
193 | my ($chash, $pom) = eval { |
|
|
194 | local $SIG{__DIE__}; |
|
|
195 | @{ Storable::thaw $pod_cache->get ("$path/$filtertype") } |
|
|
196 | }; |
|
|
197 | |
|
|
198 | return $pom if $chash eq $phash; |
|
|
199 | |
|
|
200 | my $pod = do { |
|
|
201 | local $/; |
|
|
202 | open my $pod, "<:utf8", $_[0] |
|
|
203 | or die "$_[0]: $!"; |
|
|
204 | <$pod> |
|
|
205 | }; |
|
|
206 | |
|
|
207 | #utf8::downgrade $pod; |
|
|
208 | |
|
|
209 | $pom = $filtercb->(Pod::POM->new->parse_text ($pod)); |
|
|
210 | |
|
|
211 | $pod_cache->put ("$path/$filtertype" => Storable::nfreeze [$phash, $pom]); |
|
|
212 | |
|
|
213 | $pom |
|
|
214 | } |
|
|
215 | |
|
|
216 | sub section($$) { |
|
|
217 | my ($pod, $section) = @_; |
|
|
218 | } |
|
|
219 | |
|
|
220 | sub as_markup($) { |
|
|
221 | my ($pom) = @_; |
|
|
222 | |
|
|
223 | local $indent = 0; |
|
|
224 | |
|
|
225 | $pom->present ("CFPlus::Pod::AsMarkup") |
|
|
226 | } |
|
|
227 | |
|
|
228 | sub as_paragraphs($) { |
|
|
229 | my ($pom) = @_; |
|
|
230 | |
|
|
231 | local @result = ( { } ); |
|
|
232 | local $indent = 0; |
|
|
233 | |
|
|
234 | $pom->present ("CFPlus::Pod::AsParagraphs"); |
|
|
235 | |
|
|
236 | [grep exists $_->{markup}, @result] |
|
|
237 | } |
|
|
238 | |
|
|
239 | sub pod_paragraphs($) { |
|
|
240 | load CFPlus::find_rcfile "pod/$_[0].pod", |
|
|
241 | pod_paragraphs => 1, sub { as_paragraphs $_[0] }; |
|
|
242 | } |
|
|
243 | |
|
|