1 | package CFClient::Pod; |
1 | package DC::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.03; |
8 | use CFClient::UI; |
|
|
9 | |
9 | |
10 | our $VERSION = 1.02; # 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::AsMarkup; |
17 | # nodes (order must stay as it is) |
|
|
18 | sub N_PARENT (){ 0 } |
|
|
19 | sub N_PAR (){ 1 } |
|
|
20 | sub N_LEVEL (){ 2 } |
|
|
21 | sub N_KW (){ 3 } |
|
|
22 | sub N_DOC (){ 4 } |
16 | |
23 | |
17 | use strict; |
24 | # paragraphs (order must stay as it is) |
|
|
25 | sub P_INDENT (){ 0 } |
|
|
26 | sub P_LEVEL (){ 1 } |
|
|
27 | sub P_MARKUP (){ 2 } |
|
|
28 | sub P_INDEX (){ 3 } |
18 | |
29 | |
19 | use base "Pod::POM::View::Text"; |
30 | our %wiki; |
20 | |
31 | |
21 | *view_seq_file = |
32 | sub load_docwiki { |
22 | *view_seq_code = |
33 | *wiki = Storable::retrieve $_[0]; |
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 { CFClient::asxml $_[1] }; |
|
|
27 | *view_seq_zero = |
|
|
28 | *view_seq_index = sub { }; |
|
|
29 | |
|
|
30 | sub view_seq_text { |
|
|
31 | my $text = $_[1]; |
|
|
32 | $text =~ s/\s+/ /g; |
|
|
33 | CFClient::asxml $text |
|
|
34 | } |
34 | } |
35 | |
35 | |
36 | sub view_item { |
36 | sub goto_document($) { |
37 | ("\t" x ($indent / 4)) |
37 | $goto_document->(split /\//, $_[0]); |
38 | . $_[1]->title->present ($_[0]) |
|
|
39 | . "\n\n" |
|
|
40 | . $_[1]->content->present ($_[0]) |
|
|
41 | } |
38 | } |
42 | |
39 | |
43 | sub view_verbatim { |
40 | sub is_prefix_of($@) { |
44 | (join "", |
41 | my ($node, @path) = @_; |
45 | map +("\t" x ($indent / 2)) . "<tt>$_</tt>\n", |
|
|
46 | split /\n/, CFClient::asxml $_[1]) |
|
|
47 | . "\n" |
|
|
48 | } |
|
|
49 | |
42 | |
50 | sub view_textblock { |
43 | return 1 unless @path; |
51 | ("\t" x ($indent / 2)) . "$_[1]\n" |
|
|
52 | } |
|
|
53 | |
44 | |
54 | sub view_head1 { |
45 | my $kw = pop @path; |
55 | "\n\n<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" |
|
|
56 | . $_[1]->content->present ($_[0]) |
|
|
57 | }; |
|
|
58 | |
46 | |
59 | sub view_head2 { |
47 | $node = $node->[N_PARENT] |
60 | "\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" |
48 | or return 0; |
61 | . $_[1]->content->present ($_[0]) |
|
|
62 | }; |
|
|
63 | |
49 | |
64 | sub view_head3 { |
50 | return scalar grep $_ eq $kw, @{ $node->[N_KW] }; |
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 | } |
51 | } |
73 | |
52 | |
74 | package CFClient::Pod::AsParagraphs; |
53 | sub find(@) { |
|
|
54 | my (@path) = @_; |
75 | |
55 | |
76 | use strict; |
56 | return unless @path; |
77 | |
57 | |
78 | use base "Pod::POM::View"; |
58 | my $kw = pop @path; |
79 | |
59 | |
80 | *view_seq_file = |
60 | my %res = map +($_, $_), |
81 | *view_seq_code = |
61 | grep { is_prefix_of $_, @path } |
82 | *view_seq_bold = sub { "<b>$_[1]</b>" }; |
62 | map @$_, |
83 | *view_seq_italic = sub { "<i>$_[1]</i>" }; |
63 | $kw eq "*" ? values %wiki |
84 | *view_seq_zero = sub { }; |
64 | : $wiki{$kw} || (); |
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 | |
65 | |
88 | sub view_seq_text { |
66 | values %res |
89 | my $text = $_[1]; |
|
|
90 | $text =~ s/\s+/ /g; |
|
|
91 | CFClient::asxml $text |
|
|
92 | } |
67 | } |
93 | |
68 | |
94 | sub view_seq_link { |
69 | sub full_path_of($) { |
95 | my (undef, $link) = @_; |
70 | my ($node) = @_; |
96 | |
71 | |
97 | # TODO: |
72 | my @path; |
98 | # http://... |
|
|
99 | # ref |
|
|
100 | # pod/ref |
|
|
101 | |
73 | |
102 | "<u>" . (CFClient::asxml $_[1]) . "</u>"; |
74 | while ($node) { |
103 | } |
75 | unshift @path, $node; |
104 | |
76 | $node = $node->[N_PARENT]; |
105 | sub view_item { |
|
|
106 | push @result, { |
|
|
107 | indent => $indent * 8, |
|
|
108 | markup => $_[1]->title->present ($_[0]) . "\n\n", |
|
|
109 | }; |
|
|
110 | $_[1]->content->present ($_[0]); |
|
|
111 | () |
|
|
112 | } |
|
|
113 | |
|
|
114 | sub view_verbatim { |
|
|
115 | push @result, { |
|
|
116 | indent => $indent * 16, |
|
|
117 | markup => "<tt>" . (CFClient::asxml $_[1]) . "</tt>\n", |
|
|
118 | }; |
|
|
119 | () |
|
|
120 | } |
|
|
121 | |
|
|
122 | sub view_textblock { |
|
|
123 | push @result, { |
|
|
124 | indent => $indent * 16, |
|
|
125 | markup => "$_[1]\n", |
|
|
126 | }; |
|
|
127 | () |
|
|
128 | } |
|
|
129 | |
|
|
130 | sub view_head1 { |
|
|
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 CFClient::UI::Image path => "pod/" . $_[1]->text], |
|
|
170 | }; |
|
|
171 | } |
77 | } |
172 | () |
|
|
173 | } |
|
|
174 | |
78 | |
175 | sub view { |
79 | @path |
176 | my ($self, $type, $item) = @_; |
|
|
177 | |
|
|
178 | $item->content->present ($self); |
|
|
179 | } |
80 | } |
180 | |
81 | |
181 | package CFClient::Pod; |
82 | sub full_path($) { |
182 | |
83 | join "/", map $_->[N_KW][0], &full_path_of |
183 | my $pod_cache = CFClient::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 | } |
84 | } |
215 | |
85 | |
|
|
86 | sub section_of($) { |
|
|
87 | my ($node) = @_; |
|
|
88 | |
|
|
89 | my $doc = $node->[N_DOC]; |
|
|
90 | my $par = $node->[N_PAR]; |
|
|
91 | my $lvl = $node->[N_LEVEL]; |
|
|
92 | |
|
|
93 | my @res; |
|
|
94 | |
|
|
95 | do { |
|
|
96 | my $p = $doc->[$par]; |
|
|
97 | |
|
|
98 | if (length $p->[P_MARKUP]) { |
|
|
99 | push @res, { |
|
|
100 | markup => $p->[P_MARKUP], |
|
|
101 | indent => $p->[P_INDENT], |
|
|
102 | }; |
|
|
103 | } |
|
|
104 | } while $doc->[++$par][P_LEVEL] > $lvl; |
|
|
105 | |
|
|
106 | @res |
|
|
107 | } |
|
|
108 | |
216 | sub section($$) { |
109 | sub section(@) { |
217 | my ($pod, $section) = @_; |
110 | map section_of $_, &find |
218 | } |
111 | } |
219 | |
112 | |
220 | sub as_markup($) { |
113 | sub thaw_section(\@\%) { |
221 | my ($pom) = @_; |
114 | for (@{$_[0]}) { |
|
|
115 | $_->{markup} =~ s{ |
|
|
116 | $MA_BEG |
|
|
117 | ([^$MA_END]+) |
|
|
118 | $MA_END |
|
|
119 | }{ |
|
|
120 | my ($type, @arg) = split /$MA_SEP/o, $1; |
222 | |
121 | |
223 | local $indent = 0; |
122 | $_[1]{$type}($_, @arg) |
224 | |
123 | }ogex; |
225 | $pom->present ("CFClient::Pod::AsMarkup") |
124 | } |
226 | } |
125 | } |
227 | |
126 | |
|
|
127 | my %as_common = ( |
|
|
128 | h1 => sub { |
|
|
129 | "\n\n<span foreground='#ffff00' size='x-large'>$_[1]</span>\n" |
|
|
130 | }, |
|
|
131 | h2 => sub { |
|
|
132 | "\n\n<span foreground='#ccccff' size='large'>$_[1]</span>\n" |
|
|
133 | }, |
|
|
134 | h3 => sub { |
|
|
135 | "\n\n<span size='large'>$_[1]</span>\n" |
|
|
136 | }, |
|
|
137 | ); |
|
|
138 | |
|
|
139 | my %as_label = ( |
|
|
140 | %as_common, |
|
|
141 | image => sub { |
|
|
142 | my ($par, $path) = @_; |
|
|
143 | |
|
|
144 | "<small>img</small>" |
|
|
145 | }, |
|
|
146 | link => sub { |
|
|
147 | my ($par, $text, $link) = @_; |
|
|
148 | |
|
|
149 | "<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>" |
|
|
150 | }, |
|
|
151 | ); |
|
|
152 | |
|
|
153 | sub as_label(@) { |
|
|
154 | thaw_section @_, %as_label; |
|
|
155 | |
|
|
156 | my $text = |
|
|
157 | join "\n", |
|
|
158 | map +("\xa0" x ($_->{indent} / 4)) . $_->{markup}, |
|
|
159 | @_; |
|
|
160 | |
|
|
161 | $text =~ s/^\s+//; |
|
|
162 | $text =~ s/\s+$//; |
|
|
163 | |
|
|
164 | $text |
|
|
165 | } |
|
|
166 | |
|
|
167 | my %as_paragraphs = ( |
|
|
168 | %as_common, |
|
|
169 | image => sub { |
|
|
170 | my ($par, $path, $flags) = @_; |
|
|
171 | |
|
|
172 | push @{ $par->{widget} }, new DC::UI::Image path => $path, |
|
|
173 | $flags & 1 ? (max_h => $::FONTSIZE) : (); |
|
|
174 | |
|
|
175 | "\x{fffc}" |
|
|
176 | }, |
|
|
177 | link => sub { |
|
|
178 | my ($par, $text, $link) = @_; |
|
|
179 | |
|
|
180 | push @{ $par->{widget} }, new DC::UI::Label |
|
|
181 | markup => "<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>", |
|
|
182 | fontsize => 0.8, |
|
|
183 | can_hover => 1, |
|
|
184 | can_events => 1, |
|
|
185 | padding_x => 0, |
|
|
186 | padding_y => 0, |
|
|
187 | tooltip => "Go to <i>" . (DC::asxml $link) . "</i>", |
|
|
188 | on_button_up => sub { |
|
|
189 | goto_document $link; |
|
|
190 | }; |
|
|
191 | |
|
|
192 | "\x{fffc}" |
|
|
193 | }, |
|
|
194 | ); |
|
|
195 | |
228 | sub as_paragraphs($) { |
196 | sub as_paragraphs(@) { |
229 | my ($pom) = @_; |
197 | thaw_section @_, %as_paragraphs; |
230 | |
198 | |
231 | local @result = ( { } ); |
199 | @_ |
232 | local $indent = 0; |
|
|
233 | |
|
|
234 | $pom->present ("CFClient::Pod::AsParagraphs"); |
|
|
235 | |
|
|
236 | [grep exists $_->{markup}, @result] |
|
|
237 | } |
200 | } |
238 | |
201 | |
239 | sub pod_paragraphs($) { |
202 | sub section_paragraphs(@) { |
240 | load CFClient::find_rcfile "pod/$_[0].pod", |
203 | as_paragraphs §ion |
241 | pod_paragraphs => 1, sub { as_paragraphs $_[0] }; |
|
|
242 | } |
204 | } |
243 | |
205 | |
|
|
206 | sub section_label(@) { |
|
|
207 | as_label §ion |
|
|
208 | } |
|
|
209 | |
|
|
210 | 1 |