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