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