ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf/pod.pm
(Generate patch)

Comparing deliantra/server/lib/cf/pod.pm (file contents):
Revision 1.1 by root, Tue Mar 6 19:02:36 2007 UTC vs.
Revision 1.21 by root, Tue Nov 3 23:44:21 2009 UTC

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
24package cf::pod;
25
26use common::sense;
27
28use Pod::POM;
29
30our $indent;
31our $level;
32our @result;
33
34package cf::pod::AsParagraphs;
35
36use common::sense;
37
38use base "Pod::POM::View";
39
40my %E = (
41 "<" => "E<lt>",
42 ">" => "E<gt>",
43);
44
45sub aspod($) {
46 local $_ = $_[0];
47
48 s/[<>]/$E{$1}/g;
49
50 $_
51}
52
53sub 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
73sub view_seq_text {
74 my $text = $_[1];
75 $text =~ s/\s+/ /g;
76 aspod $text
77}
78
79sub 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
91sub 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
105sub view_verbatim {
106 push @result, {
107 type => "verbatim",
108 indent => $indent * 16,
109 level => $level,
110 markup => $_[1],
111 };
112 ()
113}
114
115sub view_textblock {
116 push @result, {
117 indent => $indent * 16,
118 level => $level,
119 markup => flatten $_[1],
120 };
121 ()
122}
123
124sub 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
138sub 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
152sub 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
166sub view_over {
167 local $indent = $indent + $_[1]->indent;
168 push @result, { indent => $indent };
169 $_[1]->content->present ($_[0]);
170 ()
171}
172
173sub 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
184sub view_begin {
185 ()
186}
187
188sub view {
189 my ($self, $type, $item) = @_;
190
191 $item->content->present ($self);
192}
193
194#############################################################################
195
196package cf::pod;
197
198sub 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
213sub 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
229sub 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
2491;
250

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines