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; # 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 | # 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_code = |
32 | sub load_docwiki { |
22 | *view_seq_bold = sub { "<b>$_[1]</b>" }; |
33 | *wiki = Storable::retrieve $_[0]; |
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 | } |
34 | } |
33 | |
35 | |
34 | sub view_item { |
36 | sub goto_document($) { |
35 | ("\t" x ($indent / 4)) |
37 | $goto_document->(split /\//, $_[0]); |
36 | . $_[1]->title->present ($_[0]) |
|
|
37 | . "\n\n" |
|
|
38 | . $_[1]->content->present ($_[0]) |
|
|
39 | } |
38 | } |
40 | |
39 | |
41 | sub view_verbatim { |
40 | sub is_prefix_of($@) { |
42 | (join "", |
41 | my ($node, @path) = @_; |
43 | map +("\t" x ($indent / 2)) . "<tt>$_</tt>\n", |
|
|
44 | split /\n/, CFClient::asxml $_[1]) |
|
|
45 | . "\n" |
|
|
46 | } |
|
|
47 | |
42 | |
48 | sub view_textblock { |
43 | return 1 unless @path; |
49 | ("\t" x ($indent / 2)) . "$_[1]\n\n" |
|
|
50 | } |
|
|
51 | |
44 | |
52 | sub view_head1 { |
45 | my $kw = pop @path; |
53 | "\n\n<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" |
|
|
54 | . $_[1]->content->present ($_[0]) |
|
|
55 | }; |
|
|
56 | |
46 | |
57 | sub view_head2 { |
47 | $node = $node->[N_PARENT] |
58 | "\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" |
48 | or return 0; |
59 | . $_[1]->content->present ($_[0]) |
|
|
60 | }; |
|
|
61 | |
49 | |
62 | sub view_head3 { |
50 | return scalar grep $_ eq $kw, @{ $node->[N_KW] }; |
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 | } |
51 | } |
71 | |
52 | |
72 | package CFClient::Pod::AsParagraphs; |
53 | sub find(@) { |
|
|
54 | my (@path) = @_; |
73 | |
55 | |
74 | use strict; |
56 | return unless @path; |
75 | |
57 | |
76 | use base "Pod::POM::View"; |
58 | my $kw = pop @path; |
77 | |
59 | |
78 | *view_seq_code = |
60 | my %res = map +($_, $_), |
79 | *view_seq_bold = sub { "<b>$_[1]</b>" }; |
61 | grep { is_prefix_of $_, @path } |
80 | *view_seq_italic = sub { "<i>$_[1]</i>" }; |
62 | map @$_, |
81 | *view_seq_space = |
63 | $kw eq "*" ? values %wiki |
82 | *view_seq_link = |
64 | : $wiki{$kw} || (); |
83 | *view_seq_index = sub { CFClient::asxml $_[1] }; |
|
|
84 | |
65 | |
85 | sub view_seq_text { |
66 | values %res |
86 | my $text = $_[1]; |
|
|
87 | $text =~ s/\s+/ /g; |
|
|
88 | CFClient::asxml $text |
|
|
89 | } |
67 | } |
90 | |
68 | |
91 | sub view_item { |
69 | sub full_path_of($) { |
92 | push @result, { |
70 | my ($node) = @_; |
93 | indent => $indent * 8, |
|
|
94 | text => $_[1]->title->present ($_[0]) . "\n\n", |
|
|
95 | }; |
|
|
96 | $_[1]->content->present ($_[0]); |
|
|
97 | () |
|
|
98 | } |
|
|
99 | |
71 | |
100 | sub view_verbatim { |
72 | my @path; |
101 | push @result, { |
|
|
102 | indent => $indent * 16, |
|
|
103 | text => "<tt>" . (CFClient::asxml $_[1]) . "</tt>", |
|
|
104 | }; |
|
|
105 | () |
|
|
106 | } |
|
|
107 | |
73 | |
108 | sub view_textblock { |
74 | while ($node) { |
109 | push @result, { |
75 | unshift @path, $node; |
110 | indent => $indent * 16, |
76 | $node = $node->[N_PARENT]; |
111 | text => "$_[1]\n", |
|
|
112 | }; |
|
|
113 | () |
|
|
114 | } |
|
|
115 | |
|
|
116 | sub view_head1 { |
|
|
117 | push @result, { |
|
|
118 | indent => $indent * 16, |
|
|
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 | |
|
|
125 | sub view_head2 { |
|
|
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 | |
|
|
134 | sub view_head3 { |
|
|
135 | push @result, { |
|
|
136 | indent => $indent * 16, |
|
|
137 | text => "\n\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n", |
|
|
138 | }; |
|
|
139 | $_[1]->content->present ($_[0]); |
|
|
140 | () |
|
|
141 | }; |
|
|
142 | |
|
|
143 | sub view_over { |
|
|
144 | local $indent = $indent + $_[1]->indent; |
|
|
145 | push @result, { indent => $indent }; |
|
|
146 | $_[1]->content->present ($_[0]); |
|
|
147 | () |
|
|
148 | } |
|
|
149 | |
|
|
150 | sub view_for { |
|
|
151 | if ($_[1]->format eq "image") { |
|
|
152 | push @result, { |
|
|
153 | indent => $indent * 16, |
|
|
154 | text => "\x{fffc}", |
|
|
155 | obj => [new CFClient::UI::Image path => "pod/" . $_[1]->text], |
|
|
156 | }; |
|
|
157 | } |
77 | } |
158 | () |
|
|
159 | } |
|
|
160 | |
78 | |
161 | sub view { |
79 | @path |
162 | my ($self, $type, $item) = @_; |
|
|
163 | |
|
|
164 | $item->content->present ($self); |
|
|
165 | } |
80 | } |
166 | |
81 | |
167 | package CFClient::Pod; |
82 | sub full_path($) { |
|
|
83 | join "/", map $_->[N_KW][0], &full_path_of |
|
|
84 | } |
168 | |
85 | |
169 | my $pod_cache = CFClient::db_table "pod_cache"; |
86 | sub section_of($) { |
|
|
87 | my ($node) = @_; |
170 | |
88 | |
171 | sub load($$$$) { |
89 | my $doc = $node->[N_DOC]; |
172 | my ($path, $filtertype, $filterversion, $filtercb) = @_; |
90 | my $par = $node->[N_PAR]; |
|
|
91 | my $lvl = $node->[N_LEVEL]; |
173 | |
92 | |
174 | stat $path |
93 | my @res; |
175 | or die "$path: $!"; |
|
|
176 | |
94 | |
177 | my $phash = join ",", $filterversion, $VERSION, (stat _)[7,9]; |
95 | do { |
|
|
96 | my $p = $doc->[$par]; |
178 | |
97 | |
179 | my ($chash, $pom) = eval { @{ Storable::thaw $pod_cache->get ("$path/$filtertype") } }; |
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; |
180 | |
105 | |
181 | return $pom if $chash eq $phash; |
106 | @res |
|
|
107 | } |
182 | |
108 | |
183 | my $pod = do { |
109 | sub section(@) { |
184 | local $/; |
110 | map section_of $_, &find |
185 | open my $pod, "<:utf8", $_[0] |
111 | } |
186 | or die "$_[0]: $!"; |
112 | |
187 | <$pod> |
113 | sub thaw_section(\@\%) { |
|
|
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; |
|
|
121 | |
|
|
122 | $_[1]{$type}($_, @arg) |
|
|
123 | }ogex; |
188 | }; |
124 | } |
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 | } |
125 | } |
198 | |
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 | |
199 | sub as_xml($) { |
153 | sub as_label(@) { |
200 | my ($pom) = @_; |
154 | thaw_section @_, %as_label; |
201 | |
155 | |
202 | local $indent = 0; |
156 | my $text = |
|
|
157 | join "\n", |
|
|
158 | map +("\xa0" x ($_->{indent} / 4)) . $_->{markup}, |
|
|
159 | @_; |
203 | |
160 | |
204 | $pom->present ("CFClient::Pod::AsXML") |
161 | $text =~ s/^\s+//; |
205 | } |
162 | $text =~ s/\s+$//; |
206 | |
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 | |
207 | sub as_paragraphs($) { |
196 | sub as_paragraphs(@) { |
208 | my ($pom) = @_; |
197 | thaw_section @_, %as_paragraphs; |
209 | |
198 | |
210 | local @result = ( { } ); |
199 | @_ |
211 | local $indent = 0; |
|
|
212 | |
|
|
213 | $pom->present ("CFClient::Pod::AsParagraphs"); |
|
|
214 | |
|
|
215 | [grep exists $_->{text}, @result] |
|
|
216 | } |
200 | } |
217 | |
201 | |
|
|
202 | sub section_paragraphs(@) { |
|
|
203 | as_paragraphs §ion |
|
|
204 | } |
|
|
205 | |
|
|
206 | sub section_label(@) { |
|
|
207 | as_label §ion |
|
|
208 | } |
|
|
209 | |
|
|
210 | 1 |