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