|
|
1 | # |
|
|
2 | # |
|
|
3 | # This file is part of Deliantra, the Roguelike Realtime MMORPG. |
|
|
4 | # |
|
|
5 | # Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
6 | # |
|
|
7 | # Deliantra is free software: you can redistribute it and/or modify it under |
|
|
8 | # the terms of the Affero GNU General Public License as published by the |
|
|
9 | # Free Software Foundation, either version 3 of the License, or (at your |
|
|
10 | # option) any later version. |
|
|
11 | # |
|
|
12 | # This program is distributed in the hope that it will be useful, |
|
|
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
15 | # GNU General Public License for more details. |
|
|
16 | # |
|
|
17 | # You should have received a copy of the Affero GNU General Public License |
|
|
18 | # and the GNU General Public License along with this program. If not, see |
|
|
19 | # <http://www.gnu.org/licenses/>. |
|
|
20 | # |
|
|
21 | # The authors can be reached via e-mail to <support@deliantra.net> |
|
|
22 | # |
|
|
23 | |
|
|
24 | package cf::pod; |
|
|
25 | |
|
|
26 | use common::sense; |
|
|
27 | |
|
|
28 | use Pod::POM; |
|
|
29 | |
|
|
30 | our $indent; |
|
|
31 | our $level; |
|
|
32 | our @result; |
|
|
33 | |
|
|
34 | package cf::pod::AsParagraphs; |
|
|
35 | |
|
|
36 | use common::sense; |
|
|
37 | |
|
|
38 | use base "Pod::POM::View"; |
|
|
39 | |
|
|
40 | my %E = ( |
|
|
41 | "<" => "E<lt>", |
|
|
42 | ">" => "E<gt>", |
|
|
43 | ); |
|
|
44 | |
|
|
45 | sub aspod($) { |
|
|
46 | local $_ = $_[0]; |
|
|
47 | |
|
|
48 | s/[<>]/$E{$1}/g; |
|
|
49 | |
|
|
50 | $_ |
|
|
51 | } |
|
|
52 | |
|
|
53 | sub flatten($) { |
|
|
54 | local $_ = $_[0]; |
|
|
55 | |
|
|
56 | s/^\s+//; |
|
|
57 | s/\s+$//; |
|
|
58 | s/\s+/ /g; |
|
|
59 | |
|
|
60 | $_ |
|
|
61 | } |
|
|
62 | |
|
|
63 | *view_seq_file = sub { "C<$_[1]>" }; |
|
|
64 | *view_seq_code = sub { "C<$_[1]>" }; |
|
|
65 | *view_seq_bold = sub { "B<$_[1]>" }; |
|
|
66 | *view_seq_italic = sub { "I<$_[1]>" }; |
|
|
67 | *view_seq_T = sub { "T<$_[1]>" }; |
|
|
68 | *view_seq_G = sub { "G<$_[1]>" }; |
|
|
69 | *view_seq_zero = sub { "Z<>" }; |
|
|
70 | *view_seq_space = sub { my $text = $_[1]; $text =~ s/ /\xa0/g; $text }; |
|
|
71 | *view_seq_index = sub { push @{ $result[-1]{index} }, $_[1]; "" }; |
|
|
72 | |
|
|
73 | sub view_seq_text { |
|
|
74 | my $text = $_[1]; |
|
|
75 | $text =~ s/\s+/ /g; |
|
|
76 | aspod $text |
|
|
77 | } |
|
|
78 | |
|
|
79 | sub view_seq_link { |
|
|
80 | my (undef, $link) = @_; |
|
|
81 | |
|
|
82 | my $text = $link =~ s/^(.*)\|// ? $1 : $link; |
|
|
83 | |
|
|
84 | if ($link =~ /http:/) { |
|
|
85 | "U<" . (aspod $link) . ">" |
|
|
86 | } else { |
|
|
87 | () |
|
|
88 | } |
|
|
89 | } |
|
|
90 | |
|
|
91 | sub view_item { |
|
|
92 | push @result, { |
|
|
93 | type => "item", |
|
|
94 | indent => $indent * 8, |
|
|
95 | level => $level, |
|
|
96 | }; |
|
|
97 | my $title = $_[1]->title->present ($_[0]); |
|
|
98 | $result[-1]{markup} = $title if length $title; |
|
|
99 | $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title; |
|
|
100 | local $level = $level + 1; |
|
|
101 | $_[1]->content->present ($_[0]); |
|
|
102 | () |
|
|
103 | } |
|
|
104 | |
|
|
105 | sub view_verbatim { |
|
|
106 | push @result, { |
|
|
107 | type => "verbatim", |
|
|
108 | indent => $indent * 16, |
|
|
109 | level => $level, |
|
|
110 | markup => $_[1], |
|
|
111 | }; |
|
|
112 | () |
|
|
113 | } |
|
|
114 | |
|
|
115 | sub view_textblock { |
|
|
116 | push @result, { |
|
|
117 | indent => $indent * 16, |
|
|
118 | level => $level, |
|
|
119 | markup => flatten $_[1], |
|
|
120 | }; |
|
|
121 | () |
|
|
122 | } |
|
|
123 | |
|
|
124 | sub view_head1 { |
|
|
125 | push @result, { |
|
|
126 | type => "head1", |
|
|
127 | indent => $indent * 16, |
|
|
128 | level => $level, |
|
|
129 | }; |
|
|
130 | my $title = $_[1]->title->present ($_[0]); |
|
|
131 | $result[-1]{markup} = $title if length $title; |
|
|
132 | $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title; |
|
|
133 | local $level = $level + 1; |
|
|
134 | $_[1]->content->present ($_[0]); |
|
|
135 | () |
|
|
136 | }; |
|
|
137 | |
|
|
138 | sub view_head2 { |
|
|
139 | push @result, { |
|
|
140 | type => "head2", |
|
|
141 | indent => $indent * 16, |
|
|
142 | level => $level, |
|
|
143 | }; |
|
|
144 | my $title = $_[1]->title->present ($_[0]); |
|
|
145 | $result[-1]{markup} = $title if length $title; |
|
|
146 | $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title; |
|
|
147 | local $level = $level + 1; |
|
|
148 | $_[1]->content->present ($_[0]); |
|
|
149 | () |
|
|
150 | }; |
|
|
151 | |
|
|
152 | sub view_head3 { |
|
|
153 | push @result, { |
|
|
154 | type => "head3", |
|
|
155 | indent => $indent * 16, |
|
|
156 | level => $level, |
|
|
157 | }; |
|
|
158 | my $title = $_[1]->title->present ($_[0]); |
|
|
159 | $result[-1]{markup} = $title if length $title; |
|
|
160 | $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title; |
|
|
161 | local $level = $level + 1; |
|
|
162 | $_[1]->content->present ($_[0]); |
|
|
163 | () |
|
|
164 | }; |
|
|
165 | |
|
|
166 | sub view_over { |
|
|
167 | local $indent = $indent + $_[1]->indent; |
|
|
168 | push @result, { indent => $indent }; |
|
|
169 | $_[1]->content->present ($_[0]); |
|
|
170 | () |
|
|
171 | } |
|
|
172 | |
|
|
173 | sub view_for { |
|
|
174 | if ($_[1]->format eq "image") { |
|
|
175 | # push @result, { |
|
|
176 | # indent => $indent * 16, |
|
|
177 | # level => $level, |
|
|
178 | # markup => (::special image => "pod/" . $_->text), |
|
|
179 | # }; |
|
|
180 | } |
|
|
181 | () |
|
|
182 | } |
|
|
183 | |
|
|
184 | sub view_begin { |
|
|
185 | () |
|
|
186 | } |
|
|
187 | |
|
|
188 | sub view { |
|
|
189 | my ($self, $type, $item) = @_; |
|
|
190 | |
|
|
191 | $item->content->present ($self); |
|
|
192 | } |
|
|
193 | |
|
|
194 | ############################################################################# |
|
|
195 | |
|
|
196 | package cf::pod; |
|
|
197 | |
|
|
198 | sub pom_as_paragraphs($) { |
|
|
199 | my ($pom) = @_; |
|
|
200 | |
|
|
201 | # we suckers use global variables, unfortunately. |
|
|
202 | my $guard = cf::lock_acquire "cf::pod::as_paragraphs"; |
|
|
203 | |
|
|
204 | local $indent = 0; |
|
|
205 | local $level = 1; |
|
|
206 | local @result = ( { } ); |
|
|
207 | |
|
|
208 | $pom->present ("cf::pod::AsParagraphs"); |
|
|
209 | |
|
|
210 | [grep $_->{index} || exists $_->{markup}, @result] |
|
|
211 | } |
|
|
212 | |
|
|
213 | sub load_pod($) { |
|
|
214 | my ($path) = @_; |
|
|
215 | |
|
|
216 | Coro::Storable::thaw cf::cache "cf::pod::as_paragraphs/$path" => [$path], |
|
|
217 | 7 => sub { |
|
|
218 | my ($src) = @_; |
|
|
219 | |
|
|
220 | cf::fork_call { |
|
|
221 | my $pod = $src->[0]; |
|
|
222 | utf8::decode $pod; |
|
|
223 | Coro::Storable::blocking_nfreeze pom_as_paragraphs +(Pod::POM->new->parse_text ($pod)) |
|
|
224 | } |
|
|
225 | }; |
|
|
226 | } |
|
|
227 | |
|
|
228 | # format as cfpod-style text |
|
|
229 | sub as_cfpod($) { |
|
|
230 | my ($pars) = @_; |
|
|
231 | |
|
|
232 | my $res; |
|
|
233 | |
|
|
234 | for my $par (@$pars) { |
|
|
235 | if ($par->{type} =~ /^head\d+$/) { |
|
|
236 | $res .= "B<$par->{markup}>\n\n"; |
|
|
237 | } elsif ($par->{type} eq "verbatim") { |
|
|
238 | $res .= "$par->{markup}\n\n"; |
|
|
239 | } elsif ($par->{type} eq "item") { |
|
|
240 | $res .= "* I<$par->{markup}>\n\n"; |
|
|
241 | } else { |
|
|
242 | $res .= "$par->{markup}\n\n"; |
|
|
243 | } |
|
|
244 | } |
|
|
245 | |
|
|
246 | $res |
|
|
247 | } |
|
|
248 | |
|
|
249 | 1; |
|
|
250 | |