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 |
sub asxml($) { |
16 |
local $_ = $_[0]; |
17 |
|
18 |
s/</E<lt>/g; |
19 |
s/>/E<gt>/g; |
20 |
|
21 |
$_ |
22 |
} |
23 |
|
24 |
sub flatten($) { |
25 |
local $_ = $_[0]; |
26 |
|
27 |
s/<[^>]+>//g; |
28 |
s/^\s+//; |
29 |
s/\s+$//; |
30 |
s/\s+/ /g; |
31 |
|
32 |
$_ |
33 |
} |
34 |
|
35 |
*view_seq_file = |
36 |
*view_seq_code = |
37 |
*view_seq_bold = sub { "B<$_[1]>" }; |
38 |
*view_seq_italic = sub { "I<$_[1]>" }; |
39 |
*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 |
asxml $text |
47 |
} |
48 |
|
49 |
sub view_seq_link { |
50 |
my (undef, $link) = @_; |
51 |
|
52 |
my $text = $link =~ s/^(.*)\|// ? $1 : $link; |
53 |
|
54 |
if ($link =~ /http:/) { |
55 |
"U<" . (asxml $link) . ">" |
56 |
} else { |
57 |
() |
58 |
} |
59 |
} |
60 |
|
61 |
sub view_item { |
62 |
push @result, { |
63 |
type => "item", |
64 |
indent => $indent * 8, |
65 |
level => $level, |
66 |
}; |
67 |
my $title = $_[1]->title->present ($_[0]); |
68 |
$result[-1]{markup} = $title if length $title; |
69 |
$title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title; |
70 |
local $level = $level + 1; |
71 |
$_[1]->content->present ($_[0]); |
72 |
() |
73 |
} |
74 |
|
75 |
sub view_verbatim { |
76 |
push @result, { |
77 |
type => "verbatim", |
78 |
indent => $indent * 16, |
79 |
level => $level, |
80 |
markup => $_[1], |
81 |
}; |
82 |
() |
83 |
} |
84 |
|
85 |
sub view_textblock { |
86 |
push @result, { |
87 |
indent => $indent * 16, |
88 |
level => $level, |
89 |
markup => flatten $_[1], |
90 |
}; |
91 |
() |
92 |
} |
93 |
|
94 |
sub view_head1 { |
95 |
push @result, { |
96 |
type => "head1", |
97 |
indent => $indent * 16, |
98 |
level => $level, |
99 |
}; |
100 |
my $title = $_[1]->title->present ($_[0]); |
101 |
$result[-1]{markup} = $title if length $title; |
102 |
$title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title; |
103 |
local $level = $level + 1; |
104 |
$_[1]->content->present ($_[0]); |
105 |
() |
106 |
}; |
107 |
|
108 |
sub view_head2 { |
109 |
push @result, { |
110 |
type => "head2", |
111 |
indent => $indent * 16, |
112 |
level => $level, |
113 |
}; |
114 |
my $title = $_[1]->title->present ($_[0]); |
115 |
$result[-1]{markup} = $title if length $title; |
116 |
$title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title; |
117 |
local $level = $level + 1; |
118 |
$_[1]->content->present ($_[0]); |
119 |
() |
120 |
}; |
121 |
|
122 |
sub view_head3 { |
123 |
push @result, { |
124 |
type => "head3", |
125 |
indent => $indent * 16, |
126 |
level => $level, |
127 |
}; |
128 |
my $title = $_[1]->title->present ($_[0]); |
129 |
$result[-1]{markup} = $title if length $title; |
130 |
$title = flatten $title; unshift @{ $result[-1]{index} }, $title if length $title; |
131 |
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 |
sub pom_as_paragraphs($) { |
169 |
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 |
sub load_pod($) { |
184 |
my ($path) = @_; |
185 |
|
186 |
Coro::Storable::thaw cf::cache "cf::pod::as_paragraphs/$path" => [$path], |
187 |
3 => sub { |
188 |
my ($src) = @_; |
189 |
|
190 |
cf::fork_call { |
191 |
my $pod = $src->[0]; |
192 |
utf8::decode $pod; |
193 |
Coro::Storable::blocking_nfreeze pom_as_paragraphs +(Pod::POM->new->parse_text ($pod)) |
194 |
} |
195 |
}; |
196 |
} |
197 |
|
198 |
# format as cfpod-style text |
199 |
sub as_cfpod($) { |
200 |
my ($pars) = @_; |
201 |
|
202 |
my $res; |
203 |
|
204 |
for my $par (@$pars) { |
205 |
if ($par->{type} =~ /^head\d+$/) { |
206 |
$res .= "B<$par->{markup}>\n\n"; |
207 |
} elsif ($par->{type} eq "verbatim") { |
208 |
$res .= "\n$par->{markup}\n\n"; |
209 |
} elsif ($par->{type} eq "item") { |
210 |
$res .= "\n* I<$par->{markup}>\n\n"; |
211 |
} else { |
212 |
$res .= "$par->{markup}\n\n"; |
213 |
} |
214 |
} |
215 |
|
216 |
$res |
217 |
} |
218 |
|
219 |
1; |
220 |
|