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