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