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