ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf/pod.pm
Revision: 1.31
Committed: Wed Nov 21 12:47:04 2012 UTC (11 years, 7 months ago) by root
Branch: MAIN
CVS Tags: rel-3_1
Changes since 1.30: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #
2 # This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 #
4 # Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 #
6 # Deliantra is free software: you can redistribute it and/or modify it under
7 # the terms of the Affero GNU General Public License as published by the
8 # Free Software Foundation, either version 3 of the License, or (at your
9 # option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the Affero GNU General Public License
17 # and the GNU General Public License along with this program. If not, see
18 # <http://www.gnu.org/licenses/>.
19 #
20 # The authors can be reached via e-mail to <support@deliantra.net>
21 #
22
23 package cf::pod;
24
25 use common::sense;
26
27 use Pod::POM;
28
29 our $indent;
30 our $level;
31 our @result;
32
33 package cf::pod::AsParagraphs;
34
35 use common::sense;
36
37 use base "Pod::POM::View";
38
39 my %E = (
40 "<" => "E<lt>",
41 ">" => "E<gt>",
42 );
43
44 sub aspod($) {
45 local $_ = $_[0];
46
47 s/[<>]/$E{$1}/g;
48
49 $_
50 }
51
52 sub flatten($) {
53 local $_ = $_[0];
54
55 s/^\s+//;
56 s/\s+$//;
57 s/\s+/ /g;
58
59 $_
60 }
61
62 *view_seq_file = sub { "C<$_[1]>" };
63 *view_seq_code = sub { "C<$_[1]>" };
64 *view_seq_bold = sub { "B<$_[1]>" };
65 *view_seq_italic = sub { "I<$_[1]>" };
66 *view_seq_T = sub { "T<$_[1]>" };
67 *view_seq_G = sub { "G<$_[1]>" };
68 *view_seq_zero = sub { "Z<>" };
69 *view_seq_space = sub { my $text = $_[1]; $text =~ s/ /\xa0/g; $text };
70 *view_seq_index = sub { push @{ $result[-1]{index} }, $_[1]; "" };
71 #view_seq_entity
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 aspod $text
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_ng($) {
199 my ($pom) = @_;
200
201 # we suckers use global variables, unfortunately.
202 local $indent = 0;
203 local $level = 1;
204 local @result = ( { } );
205
206 $pom->present ("cf::pod::AsParagraphs");
207
208 [grep $_->{index} || exists $_->{markup}, @result]
209 }
210
211 sub pom_as_paragraphs($) {
212 my ($pom) = @_;
213
214 my $guard = cf::lock_acquire "cf::pod::as_paragraphs";
215
216 $pom->pom_as_paragraphs_ng
217 }
218
219 sub load_pod($) {
220 my ($path) = @_;
221
222 Coro::Storable::thaw cf::cache "cf::pod::as_paragraphs/$path" => [$path],
223 9 => sub {
224 my ($src) = @_;
225
226 cf::fork_call {
227 Coro::Storable::blocking_nfreeze
228 pom_as_paragraphs_ng
229 +(Pod::POM->new->parse_text ($src->[0]))
230 }
231 };
232 }
233
234 # format as cfpod-style text
235 sub as_cfpod($) {
236 my ($pars) = @_;
237
238 my $res;
239
240 for my $par (@$pars) {
241 if ($par->{type} =~ /^head\d+$/) {
242 $res .= "B<$par->{markup}>\n\n";
243 } elsif ($par->{type} eq "verbatim") {
244 $res .= "$par->{markup}\n\n";
245 } elsif ($par->{type} eq "item") {
246 $res .= "* I<$par->{markup}>\n\n";
247 } else {
248 $res .= "$par->{markup}\n\n";
249 }
250 }
251
252 $res
253 }
254
255 1;
256