ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf/pod.pm
Revision: 1.32
Committed: Sat Nov 17 23:40:02 2018 UTC (5 years, 7 months ago) by root
Branch: MAIN
CVS Tags: HEAD
Changes since 1.31: +1 -0 lines
Log Message:
copyright update 2018

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