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