1 |
# |
2 |
# This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
# |
4 |
# Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
5 |
# Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 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 |
|
24 |
package cf::pod; |
25 |
|
26 |
use common::sense; |
27 |
|
28 |
use Pod::POM; |
29 |
|
30 |
our $indent; |
31 |
our $level; |
32 |
our @result; |
33 |
|
34 |
package cf::pod::AsParagraphs; |
35 |
|
36 |
use common::sense; |
37 |
|
38 |
use base "Pod::POM::View"; |
39 |
|
40 |
my %E = ( |
41 |
"<" => "E<lt>", |
42 |
">" => "E<gt>", |
43 |
); |
44 |
|
45 |
sub aspod($) { |
46 |
local $_ = $_[0]; |
47 |
|
48 |
s/[<>]/$E{$1}/g; |
49 |
|
50 |
$_ |
51 |
} |
52 |
|
53 |
sub 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 |
#view_seq_entity |
73 |
|
74 |
sub view_seq_text { |
75 |
my $text = $_[1]; |
76 |
$text =~ s/\s+/ /g; |
77 |
aspod $text |
78 |
} |
79 |
|
80 |
sub view_seq_link { |
81 |
my (undef, $link) = @_; |
82 |
|
83 |
my $text = $link =~ s/^(.*)\|// ? $1 : $link; |
84 |
|
85 |
if ($link =~ /http:/) { |
86 |
"U<" . (aspod $link) . ">" |
87 |
} else { |
88 |
aspod $text |
89 |
} |
90 |
} |
91 |
|
92 |
sub view_item { |
93 |
push @result, { |
94 |
type => "item", |
95 |
indent => $indent * 8, |
96 |
level => $level, |
97 |
}; |
98 |
my $title = $_[1]->title->present ($_[0]); |
99 |
$result[-1]{markup} = $title if length $title; |
100 |
$title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title; |
101 |
local $level = $level + 1; |
102 |
$_[1]->content->present ($_[0]); |
103 |
() |
104 |
} |
105 |
|
106 |
sub view_verbatim { |
107 |
push @result, { |
108 |
type => "verbatim", |
109 |
indent => $indent * 16, |
110 |
level => $level, |
111 |
markup => $_[1], |
112 |
}; |
113 |
() |
114 |
} |
115 |
|
116 |
sub view_textblock { |
117 |
push @result, { |
118 |
indent => $indent * 16, |
119 |
level => $level, |
120 |
markup => flatten $_[1], |
121 |
}; |
122 |
() |
123 |
} |
124 |
|
125 |
sub view_head1 { |
126 |
push @result, { |
127 |
type => "head1", |
128 |
indent => $indent * 16, |
129 |
level => $level, |
130 |
}; |
131 |
my $title = $_[1]->title->present ($_[0]); |
132 |
$result[-1]{markup} = $title if length $title; |
133 |
$title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title; |
134 |
local $level = $level + 1; |
135 |
$_[1]->content->present ($_[0]); |
136 |
() |
137 |
}; |
138 |
|
139 |
sub view_head2 { |
140 |
push @result, { |
141 |
type => "head2", |
142 |
indent => $indent * 16, |
143 |
level => $level, |
144 |
}; |
145 |
my $title = $_[1]->title->present ($_[0]); |
146 |
$result[-1]{markup} = $title if length $title; |
147 |
$title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title; |
148 |
local $level = $level + 1; |
149 |
$_[1]->content->present ($_[0]); |
150 |
() |
151 |
}; |
152 |
|
153 |
sub view_head3 { |
154 |
push @result, { |
155 |
type => "head3", |
156 |
indent => $indent * 16, |
157 |
level => $level, |
158 |
}; |
159 |
my $title = $_[1]->title->present ($_[0]); |
160 |
$result[-1]{markup} = $title if length $title; |
161 |
$title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title; |
162 |
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 |
sub pom_as_paragraphs_ng($) { |
200 |
my ($pom) = @_; |
201 |
|
202 |
# we suckers use global variables, unfortunately. |
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 |
|
212 |
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 |
sub load_pod($) { |
221 |
my ($path) = @_; |
222 |
|
223 |
Coro::Storable::thaw cf::cache "cf::pod::as_paragraphs/$path" => [$path], |
224 |
9 => sub { |
225 |
my ($src) = @_; |
226 |
|
227 |
cf::fork_call { |
228 |
Coro::Storable::blocking_nfreeze |
229 |
pom_as_paragraphs_ng |
230 |
+(Pod::POM->new->parse_text ($src->[0])) |
231 |
} |
232 |
}; |
233 |
} |
234 |
|
235 |
# format as cfpod-style text |
236 |
sub as_cfpod($) { |
237 |
my ($pars) = @_; |
238 |
|
239 |
my $res; |
240 |
|
241 |
for my $par (@$pars) { |
242 |
if ($par->{type} =~ /^head\d+$/) { |
243 |
$res .= "B<$par->{markup}>\n\n"; |
244 |
} elsif ($par->{type} eq "verbatim") { |
245 |
$res .= "$par->{markup}\n\n"; |
246 |
} elsif ($par->{type} eq "item") { |
247 |
$res .= "* I<$par->{markup}>\n\n"; |
248 |
} else { |
249 |
$res .= "$par->{markup}\n\n"; |
250 |
} |
251 |
} |
252 |
|
253 |
$res |
254 |
} |
255 |
|
256 |
1; |
257 |
|