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.24 by root, Mon Jun 21 22:28:12 2010 UTC

1#
2# This file is part of Deliantra, the Roguelike Realtime MMORPG.
3#
4# Copyright (©) 2005,2006,2007,2008,2009,2010 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
23package cf::pod;
24
25use common::sense;
26
27use Pod::POM;
28
29our $indent;
30our $level;
31our @result;
32
33package cf::pod::AsParagraphs;
34
35use common::sense;
36
37use base "Pod::POM::View";
38
39my %E = (
40 "<" => "E<lt>",
41 ">" => "E<gt>",
42);
43
44sub aspod($) {
45 local $_ = $_[0];
46
47 s/[<>]/$E{$1}/g;
48
49 $_
50}
51
52sub 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
72sub view_seq_text {
73 my $text = $_[1];
74 $text =~ s/\s+/ /g;
75 aspod $text
76}
77
78sub view_seq_link {
79 my (undef, $link) = @_;
80
81 my $text = $link =~ s/^(.*)\|// ? $1 : $link;
82
83 if ($link =~ /http:/) {
84 "U<" . (aspod $link) . ">"
85 } else {
86 ()
87 }
88}
89
90sub view_item {
91 push @result, {
92 type => "item",
93 indent => $indent * 8,
94 level => $level,
95 };
96 my $title = $_[1]->title->present ($_[0]);
97 $result[-1]{markup} = $title if length $title;
98 $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title;
99 local $level = $level + 1;
100 $_[1]->content->present ($_[0]);
101 ()
102}
103
104sub view_verbatim {
105 push @result, {
106 type => "verbatim",
107 indent => $indent * 16,
108 level => $level,
109 markup => $_[1],
110 };
111 ()
112}
113
114sub view_textblock {
115 push @result, {
116 indent => $indent * 16,
117 level => $level,
118 markup => flatten $_[1],
119 };
120 ()
121}
122
123sub view_head1 {
124 push @result, {
125 type => "head1",
126 indent => $indent * 16,
127 level => $level,
128 };
129 my $title = $_[1]->title->present ($_[0]);
130 $result[-1]{markup} = $title if length $title;
131 $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title;
132 local $level = $level + 1;
133 $_[1]->content->present ($_[0]);
134 ()
135};
136
137sub view_head2 {
138 push @result, {
139 type => "head2",
140 indent => $indent * 16,
141 level => $level,
142 };
143 my $title = $_[1]->title->present ($_[0]);
144 $result[-1]{markup} = $title if length $title;
145 $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title;
146 local $level = $level + 1;
147 $_[1]->content->present ($_[0]);
148 ()
149};
150
151sub view_head3 {
152 push @result, {
153 type => "head3",
154 indent => $indent * 16,
155 level => $level,
156 };
157 my $title = $_[1]->title->present ($_[0]);
158 $result[-1]{markup} = $title if length $title;
159 $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title;
160 local $level = $level + 1;
161 $_[1]->content->present ($_[0]);
162 ()
163};
164
165sub view_over {
166 local $indent = $indent + $_[1]->indent;
167 push @result, { indent => $indent };
168 $_[1]->content->present ($_[0]);
169 ()
170}
171
172sub view_for {
173 if ($_[1]->format eq "image") {
174# push @result, {
175# indent => $indent * 16,
176# level => $level,
177# markup => (::special image => "pod/" . $_->text),
178# };
179 }
180 ()
181}
182
183sub view_begin {
184 ()
185}
186
187sub view {
188 my ($self, $type, $item) = @_;
189
190 $item->content->present ($self);
191}
192
193#############################################################################
194
195package cf::pod;
196
197sub pom_as_paragraphs($) {
198 my ($pom) = @_;
199
200 # we suckers use global variables, unfortunately.
201 my $guard = cf::lock_acquire "cf::pod::as_paragraphs";
202
203 local $indent = 0;
204 local $level = 1;
205 local @result = ( { } );
206
207 $pom->present ("cf::pod::AsParagraphs");
208
209 [grep $_->{index} || exists $_->{markup}, @result]
210}
211
212sub load_pod($) {
213 my ($path) = @_;
214
215 Coro::Storable::thaw cf::cache "cf::pod::as_paragraphs/$path" => [$path],
216 8 => sub {
217 my ($src) = @_;
218
219 cf::fork_call {
220 Coro::Storable::blocking_nfreeze
221 pom_as_paragraphs
222 +(Pod::POM->new->parse_text ($src->[0]))
223 }
224 };
225}
226
227# format as cfpod-style text
228sub as_cfpod($) {
229 my ($pars) = @_;
230
231 my $res;
232
233 for my $par (@$pars) {
234 if ($par->{type} =~ /^head\d+$/) {
235 $res .= "B<$par->{markup}>\n\n";
236 } elsif ($par->{type} eq "verbatim") {
237 $res .= "$par->{markup}\n\n";
238 } elsif ($par->{type} eq "item") {
239 $res .= "* I<$par->{markup}>\n\n";
240 } else {
241 $res .= "$par->{markup}\n\n";
242 }
243 }
244
245 $res
246}
247
2481;
249

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines