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.2 by root, Wed Apr 18 12:43:54 2007 UTC vs.
Revision 1.26 by root, Tue Jan 3 11:25:33 2012 UTC

1#
2# This file is part of Deliantra, the Roguelike Realtime MMORPG.
3#
4# Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 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
1package cf::pod; 23package cf::pod;
24
25use common::sense;
26
27use Pod::POM;
2 28
3our $indent; 29our $indent;
4our $level; 30our $level;
5our @result; 31our @result;
6 32
7package cf::pod::AsParagraphs; 33package cf::pod::AsParagraphs;
8 34
9use strict; 35use common::sense;
10 36
11use base "Pod::POM::View"; 37use base "Pod::POM::View";
12 38
39my %E = (
40 "<" => "E<lt>",
41 ">" => "E<gt>",
42);
43
13sub asxml($) { 44sub aspod($) {
14 local $_ = $_[0]; 45 local $_ = $_[0];
15 46
16 s/&/&amp;/g; 47 s/[<>]/$E{$1}/g;
17 s/>/&gt;/g;
18 s/</&lt;/g;
19 48
20 $_ 49 $_
21} 50}
22 51
23*view_seq_file = 52sub flatten($) {
24*view_seq_code = 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]>" };
25*view_seq_bold = sub { "<b>$_[1]</b>" }; 64*view_seq_bold = sub { "B<$_[1]>" };
26*view_seq_italic = sub { "<i>$_[1]</i>" }; 65*view_seq_italic = sub { "I<$_[1]>" };
66*view_seq_T = sub { "T<$_[1]>" };
67*view_seq_G = sub { "G<$_[1]>" };
27*view_seq_zero = sub { }; 68*view_seq_zero = sub { "Z<>" };
28*view_seq_space = sub { my $text = $_[1]; $text =~ s/ /\xa0/g; $text }; 69*view_seq_space = sub { my $text = $_[1]; $text =~ s/ /\xa0/g; $text };
29*view_seq_index = sub { push @{ $result[-1]{index} }, $_[1]; "" }; 70*view_seq_index = sub { push @{ $result[-1]{index} }, $_[1]; "" };
30 71
31sub view_seq_text { 72sub view_seq_text {
32 my $text = $_[1]; 73 my $text = $_[1];
33 $text =~ s/\s+/ /g; 74 $text =~ s/\s+/ /g;
34 ::asxml $text 75 aspod $text
35} 76}
36 77
37sub view_seq_link { 78sub view_seq_link {
38 my (undef, $link) = @_; 79 my (undef, $link) = @_;
39 80
40 my $text = $link =~ s/^(.*)\|// ? $1 : $link; 81 my $text = $link =~ s/^(.*)\|// ? $1 : $link;
41 82
42 if ($link =~ /http:/) { 83 if ($link =~ /http:/) {
43 "<u>" . (::asxml $link) . "</u>" 84 "U<" . (aspod $link) . ">"
44 } else { 85 } else {
45 () 86 ()
46 } 87 }
47} 88}
48 89
49sub view_item { 90sub view_item {
50 push @result, { 91 push @result, {
92 type => "item",
51 indent => $indent * 8, 93 indent => $indent * 8,
52 level => $level, 94 level => $level,
53 }; 95 };
54 my $title = $_[1]->title->present ($_[0]); 96 my $title = $_[1]->title->present ($_[0]);
55 $result[-1]{markup} = "$title\n" if length $title; 97 $result[-1]{markup} = $title if length $title;
56 $title = ::flatten $title; unshift @{ $result[-1]{index} }, $title if length $title; 98 $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title;
57 local $level = $level + 1; 99 local $level = $level + 1;
58 $_[1]->content->present ($_[0]); 100 $_[1]->content->present ($_[0]);
59 () 101 ()
60} 102}
61 103
62sub view_verbatim { 104sub view_verbatim {
63 push @result, { 105 push @result, {
106 type => "verbatim",
64 indent => $indent * 16, 107 indent => $indent * 16,
65 level => $level, 108 level => $level,
66 markup => "<tt>" . (::asxml $_[1]) . "</tt>\n", 109 markup => $_[1],
67 }; 110 };
68 () 111 ()
69} 112}
70 113
71sub view_textblock { 114sub view_textblock {
72 push @result, { 115 push @result, {
73 indent => $indent * 16, 116 indent => $indent * 16,
74 level => $level, 117 level => $level,
75 markup => "$_[1]\n", 118 markup => flatten $_[1],
76 }; 119 };
77 () 120 ()
78} 121}
79 122
80sub view_head1 { 123sub view_head1 {
81 push @result, { 124 push @result, {
125 type => "head1",
82 indent => $indent * 16, 126 indent => $indent * 16,
83 level => $level, 127 level => $level,
84 }; 128 };
85 my $title = $_[1]->title->present ($_[0]); 129 my $title = $_[1]->title->present ($_[0]);
86 $result[-1]{markup} = "\n\n<h1>$title</h1>\n" if length $title; 130 $result[-1]{markup} = $title if length $title;
87 $title = ::flatten $title; unshift @{ $result[-1]{index} }, $title if length $title; 131 $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title;
88 local $level = $level + 1; 132 local $level = $level + 1;
89 $_[1]->content->present ($_[0]); 133 $_[1]->content->present ($_[0]);
90 () 134 ()
91}; 135};
92 136
93sub view_head2 { 137sub view_head2 {
94 push @result, { 138 push @result, {
139 type => "head2",
95 indent => $indent * 16, 140 indent => $indent * 16,
96 level => $level, 141 level => $level,
97 }; 142 };
98 my $title = $_[1]->title->present ($_[0]); 143 my $title = $_[1]->title->present ($_[0]);
99 $result[-1]{markup} = "\n\n<h2>$title</h2>\n" if length $title; 144 $result[-1]{markup} = $title if length $title;
100 $title = ::flatten $title; unshift @{ $result[-1]{index} }, $title if length $title; 145 $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title;
101 local $level = $level + 1; 146 local $level = $level + 1;
102 $_[1]->content->present ($_[0]); 147 $_[1]->content->present ($_[0]);
103 () 148 ()
104}; 149};
105 150
106sub view_head3 { 151sub view_head3 {
107 push @result, { 152 push @result, {
153 type => "head3",
108 indent => $indent * 16, 154 indent => $indent * 16,
109 level => $level, 155 level => $level,
110 }; 156 };
111 my $title = $_[1]->title->present ($_[0]); 157 my $title = $_[1]->title->present ($_[0]);
112 $result[-1]{markup} = "\n\n<h3>$title</h3>\n" if length $title; 158 $result[-1]{markup} = $title if length $title;
113 $title = ::flatten $title; unshift @{ $result[-1]{index} }, $title if length $title; 159 $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title;
114 local $level = $level + 1; 160 local $level = $level + 1;
115 $_[1]->content->present ($_[0]); 161 $_[1]->content->present ($_[0]);
116 () 162 ()
117}; 163};
118 164
146 192
147############################################################################# 193#############################################################################
148 194
149package cf::pod; 195package cf::pod;
150 196
151sub as_paragraphs($) { 197sub pom_as_paragraphs($) {
152 my ($pom) = @_; 198 my ($pom) = @_;
153 199
154 # we suckers use global variables, unfortunately. 200 # we suckers use global variables, unfortunately.
155 my $guard = cf::lock_acquire "cf::pod::as_paragraphs"; 201 my $guard = cf::lock_acquire "cf::pod::as_paragraphs";
156 202
161 $pom->present ("cf::pod::AsParagraphs"); 207 $pom->present ("cf::pod::AsParagraphs");
162 208
163 [grep $_->{index} || exists $_->{markup}, @result] 209 [grep $_->{index} || exists $_->{markup}, @result]
164} 210}
165 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
1661; 2481;
167 249

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines