ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/server/lib/cf/pod.pm
Revision: 1.28
Committed: Tue Nov 13 01:12:23 2012 UTC (13 years, 11 months ago) by root
Branch: MAIN
Changes since 1.27: +11 -5 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.21 #
2     # This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 root 1.27 #
4 root 1.26 # Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.27 #
6 root 1.21 # 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 root 1.27 #
11 root 1.21 # 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 root 1.27 #
16 root 1.21 # 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 root 1.27 #
20 root 1.21 # The authors can be reached via e-mail to <support@deliantra.net>
21     #
22    
23 root 1.2 package cf::pod;
24    
25 root 1.20 use common::sense;
26    
27 root 1.3 use Pod::POM;
28    
29 root 1.2 our $indent;
30     our $level;
31     our @result;
32    
33     package cf::pod::AsParagraphs;
34    
35 root 1.20 use common::sense;
36 root 1.2
37     use base "Pod::POM::View";
38    
39 root 1.15 my %E = (
40     "<" => "E<lt>",
41     ">" => "E<gt>",
42     );
43    
44 root 1.14 sub aspod($) {
45 root 1.2 local $_ = $_[0];
46    
47 root 1.15 s/[<>]/$E{$1}/g;
48 root 1.2
49     $_
50     }
51    
52 root 1.5 sub flatten($) {
53     local $_ = $_[0];
54    
55     s/^\s+//;
56     s/\s+$//;
57     s/\s+/ /g;
58    
59     $_
60     }
61    
62 root 1.17 *view_seq_file = sub { "C<$_[1]>" };
63     *view_seq_code = sub { "C<$_[1]>" };
64 root 1.13 *view_seq_bold = sub { "B<$_[1]>" };
65     *view_seq_italic = sub { "I<$_[1]>" };
66 root 1.19 *view_seq_T = sub { "T<$_[1]>" };
67     *view_seq_G = sub { "G<$_[1]>" };
68 root 1.14 *view_seq_zero = sub { "Z<>" };
69 root 1.2 *view_seq_space = sub { my $text = $_[1]; $text =~ s/ /\xa0/g; $text };
70     *view_seq_index = sub { push @{ $result[-1]{index} }, $_[1]; "" };
71    
72     sub view_seq_text {
73     my $text = $_[1];
74     $text =~ s/\s+/ /g;
75 root 1.14 aspod $text
76 root 1.2 }
77    
78     sub view_seq_link {
79     my (undef, $link) = @_;
80    
81     my $text = $link =~ s/^(.*)\|// ? $1 : $link;
82    
83     if ($link =~ /http:/) {
84 root 1.15 "U<" . (aspod $link) . ">"
85 root 1.2 } else {
86     ()
87     }
88     }
89    
90     sub view_item {
91     push @result, {
92 root 1.5 type => "item",
93 root 1.2 indent => $indent * 8,
94     level => $level,
95     };
96     my $title = $_[1]->title->present ($_[0]);
97 root 1.5 $result[-1]{markup} = $title if length $title;
98     $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title;
99 root 1.2 local $level = $level + 1;
100     $_[1]->content->present ($_[0]);
101     ()
102     }
103    
104     sub view_verbatim {
105     push @result, {
106 root 1.5 type => "verbatim",
107 root 1.2 indent => $indent * 16,
108     level => $level,
109 root 1.13 markup => $_[1],
110 root 1.2 };
111     ()
112     }
113    
114     sub view_textblock {
115     push @result, {
116     indent => $indent * 16,
117     level => $level,
118 root 1.6 markup => flatten $_[1],
119 root 1.2 };
120     ()
121     }
122    
123     sub view_head1 {
124     push @result, {
125 root 1.5 type => "head1",
126 root 1.2 indent => $indent * 16,
127     level => $level,
128     };
129     my $title = $_[1]->title->present ($_[0]);
130 root 1.5 $result[-1]{markup} = $title if length $title;
131     $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title;
132 root 1.2 local $level = $level + 1;
133     $_[1]->content->present ($_[0]);
134     ()
135     };
136    
137     sub view_head2 {
138     push @result, {
139 root 1.5 type => "head2",
140 root 1.2 indent => $indent * 16,
141     level => $level,
142     };
143     my $title = $_[1]->title->present ($_[0]);
144 root 1.5 $result[-1]{markup} = $title if length $title;
145     $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title;
146 root 1.2 local $level = $level + 1;
147     $_[1]->content->present ($_[0]);
148     ()
149     };
150    
151     sub view_head3 {
152     push @result, {
153 root 1.5 type => "head3",
154 root 1.2 indent => $indent * 16,
155     level => $level,
156     };
157     my $title = $_[1]->title->present ($_[0]);
158 root 1.5 $result[-1]{markup} = $title if length $title;
159     $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title;
160 root 1.2 local $level = $level + 1;
161     $_[1]->content->present ($_[0]);
162     ()
163     };
164    
165     sub view_over {
166     local $indent = $indent + $_[1]->indent;
167     push @result, { indent => $indent };
168     $_[1]->content->present ($_[0]);
169     ()
170     }
171    
172     sub 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    
183     sub view_begin {
184     ()
185     }
186    
187     sub view {
188     my ($self, $type, $item) = @_;
189    
190     $item->content->present ($self);
191     }
192    
193     #############################################################################
194    
195     package cf::pod;
196    
197 root 1.28 sub pom_as_paragraphs_ng($) {
198 root 1.2 my ($pom) = @_;
199    
200     local $indent = 0;
201     local $level = 1;
202     local @result = ( { } );
203    
204     $pom->present ("cf::pod::AsParagraphs");
205    
206     [grep $_->{index} || exists $_->{markup}, @result]
207     }
208    
209 root 1.28 sub pom_as_paragraphs($) {
210     my ($pom) = @_;
211    
212     # we suckers use global variables, unfortunately.
213     my $guard = cf::lock_acquire "cf::pod::as_paragraphs";
214    
215     $pom->pom_as_paragraphs_ng
216     }
217    
218 root 1.3 sub load_pod($) {
219     my ($path) = @_;
220    
221 root 1.4 Coro::Storable::thaw cf::cache "cf::pod::as_paragraphs/$path" => [$path],
222 root 1.24 8 => sub {
223 root 1.4 my ($src) = @_;
224 root 1.12
225 root 1.5 cf::fork_call {
226 root 1.24 Coro::Storable::blocking_nfreeze
227 root 1.28 pom_as_paragraphs_ng
228 root 1.24 +(Pod::POM->new->parse_text ($src->[0]))
229 root 1.4 }
230     };
231 root 1.3 }
232    
233 root 1.8 # format as cfpod-style text
234 root 1.11 sub as_cfpod($) {
235 root 1.6 my ($pars) = @_;
236    
237     my $res;
238    
239     for my $par (@$pars) {
240     if ($par->{type} =~ /^head\d+$/) {
241 root 1.8 $res .= "B<$par->{markup}>\n\n";
242 root 1.6 } elsif ($par->{type} eq "verbatim") {
243 root 1.16 $res .= "$par->{markup}\n\n";
244 root 1.6 } elsif ($par->{type} eq "item") {
245 root 1.16 $res .= "* I<$par->{markup}>\n\n";
246 root 1.6 } else {
247 root 1.8 $res .= "$par->{markup}\n\n";
248 root 1.6 }
249     }
250    
251     $res
252     }
253    
254 root 1.2 1;
255