ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf/pod.pm
Revision: 1.13
Committed: Sun Aug 31 09:03:31 2008 UTC (15 years, 10 months ago) by root
Branch: MAIN
Changes since 1.12: +7 -8 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.2 package cf::pod;
2    
3 root 1.3 use Pod::POM;
4    
5 root 1.2 our $indent;
6     our $level;
7     our @result;
8    
9     package cf::pod::AsParagraphs;
10    
11     use strict;
12    
13     use base "Pod::POM::View";
14    
15     sub asxml($) {
16     local $_ = $_[0];
17    
18 root 1.13 s/</E<lt>/g;
19     s/>/E<gt>/g;
20 root 1.2
21     $_
22     }
23    
24 root 1.5 sub flatten($) {
25     local $_ = $_[0];
26    
27     s/<[^>]+>//g;
28     s/^\s+//;
29     s/\s+$//;
30     s/\s+/ /g;
31    
32     $_
33     }
34    
35 root 1.2 *view_seq_file =
36     *view_seq_code =
37 root 1.13 *view_seq_bold = sub { "B<$_[1]>" };
38     *view_seq_italic = sub { "I<$_[1]>" };
39 root 1.2 *view_seq_zero = sub { };
40     *view_seq_space = sub { my $text = $_[1]; $text =~ s/ /\xa0/g; $text };
41     *view_seq_index = sub { push @{ $result[-1]{index} }, $_[1]; "" };
42    
43     sub view_seq_text {
44     my $text = $_[1];
45     $text =~ s/\s+/ /g;
46 root 1.5 asxml $text
47 root 1.2 }
48    
49     sub view_seq_link {
50     my (undef, $link) = @_;
51    
52     my $text = $link =~ s/^(.*)\|// ? $1 : $link;
53    
54     if ($link =~ /http:/) {
55 root 1.13 "U<" . (asxml $link) . ">"
56 root 1.2 } else {
57     ()
58     }
59     }
60    
61     sub view_item {
62     push @result, {
63 root 1.5 type => "item",
64 root 1.2 indent => $indent * 8,
65     level => $level,
66     };
67     my $title = $_[1]->title->present ($_[0]);
68 root 1.5 $result[-1]{markup} = $title if length $title;
69     $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title;
70 root 1.2 local $level = $level + 1;
71     $_[1]->content->present ($_[0]);
72     ()
73     }
74    
75     sub view_verbatim {
76     push @result, {
77 root 1.5 type => "verbatim",
78 root 1.2 indent => $indent * 16,
79     level => $level,
80 root 1.13 markup => $_[1],
81 root 1.2 };
82     ()
83     }
84    
85     sub view_textblock {
86     push @result, {
87     indent => $indent * 16,
88     level => $level,
89 root 1.6 markup => flatten $_[1],
90 root 1.2 };
91     ()
92     }
93    
94     sub view_head1 {
95     push @result, {
96 root 1.5 type => "head1",
97 root 1.2 indent => $indent * 16,
98     level => $level,
99     };
100     my $title = $_[1]->title->present ($_[0]);
101 root 1.5 $result[-1]{markup} = $title if length $title;
102     $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title;
103 root 1.2 local $level = $level + 1;
104     $_[1]->content->present ($_[0]);
105     ()
106     };
107    
108     sub view_head2 {
109     push @result, {
110 root 1.5 type => "head2",
111 root 1.2 indent => $indent * 16,
112     level => $level,
113     };
114     my $title = $_[1]->title->present ($_[0]);
115 root 1.5 $result[-1]{markup} = $title if length $title;
116     $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title;
117 root 1.2 local $level = $level + 1;
118     $_[1]->content->present ($_[0]);
119     ()
120     };
121    
122     sub view_head3 {
123     push @result, {
124 root 1.5 type => "head3",
125 root 1.2 indent => $indent * 16,
126     level => $level,
127     };
128     my $title = $_[1]->title->present ($_[0]);
129 root 1.5 $result[-1]{markup} = $title if length $title;
130     $title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title;
131 root 1.2 local $level = $level + 1;
132     $_[1]->content->present ($_[0]);
133     ()
134     };
135    
136     sub view_over {
137     local $indent = $indent + $_[1]->indent;
138     push @result, { indent => $indent };
139     $_[1]->content->present ($_[0]);
140     ()
141     }
142    
143     sub view_for {
144     if ($_[1]->format eq "image") {
145     # push @result, {
146     # indent => $indent * 16,
147     # level => $level,
148     # markup => (::special image => "pod/" . $_->text),
149     # };
150     }
151     ()
152     }
153    
154     sub view_begin {
155     ()
156     }
157    
158     sub view {
159     my ($self, $type, $item) = @_;
160    
161     $item->content->present ($self);
162     }
163    
164     #############################################################################
165    
166     package cf::pod;
167    
168 root 1.3 sub pom_as_paragraphs($) {
169 root 1.2 my ($pom) = @_;
170    
171     # we suckers use global variables, unfortunately.
172     my $guard = cf::lock_acquire "cf::pod::as_paragraphs";
173    
174     local $indent = 0;
175     local $level = 1;
176     local @result = ( { } );
177    
178     $pom->present ("cf::pod::AsParagraphs");
179    
180     [grep $_->{index} || exists $_->{markup}, @result]
181     }
182    
183 root 1.3 sub load_pod($) {
184     my ($path) = @_;
185    
186 root 1.4 Coro::Storable::thaw cf::cache "cf::pod::as_paragraphs/$path" => [$path],
187 root 1.13 3 => sub {
188 root 1.4 my ($src) = @_;
189 root 1.12
190 root 1.5 cf::fork_call {
191 root 1.4 my $pod = $src->[0];
192     utf8::decode $pod;
193 root 1.12 Coro::Storable::blocking_nfreeze pom_as_paragraphs +(Pod::POM->new->parse_text ($pod))
194 root 1.4 }
195     };
196 root 1.3 }
197    
198 root 1.8 # format as cfpod-style text
199 root 1.11 sub as_cfpod($) {
200 root 1.6 my ($pars) = @_;
201    
202     my $res;
203    
204     for my $par (@$pars) {
205     if ($par->{type} =~ /^head\d+$/) {
206 root 1.8 $res .= "B<$par->{markup}>\n\n";
207 root 1.6 } elsif ($par->{type} eq "verbatim") {
208     $res .= "\n$par->{markup}\n\n";
209     } elsif ($par->{type} eq "item") {
210 root 1.8 $res .= "\n* I<$par->{markup}>\n\n";
211 root 1.6 } else {
212 root 1.8 $res .= "$par->{markup}\n\n";
213 root 1.6 }
214     }
215    
216     $res
217     }
218    
219 root 1.2 1;
220