Pod/SimpleBridge.pm untested

filter: Statementstmt 0.0% 0 / 237 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 84 MC/DCmcdc 0.0% 0 / 44 Subroutinesub 0.0% 0 / 41 Podpod 0.0% 0 / 32 totaltotal 0.0% 0 / 520 CC 42 SCAR 39.9
CC
42
Cov
0%
tag_for
132.0
process
110.0
_common_heading
110.0
CRAP 54.0
1  
2 require 5;
3 use strict;
4 package Tk::Pod::SimpleBridge;
5 # Interface between Tk::Pod and Pod::Simple
6  
7 use vars qw($VERSION);
8 $VERSION = '5.07';
9  
10 BEGIN { # Make a DEBUG constant very first thing...
11 if(defined &DEBUG) {
12 } elsif(($ENV{'TKPODDEBUG'} || '') =~ m/^(\d+)/) { # untaint
13 my $debug = $1;
14 *DEBUG = sub () { $debug };
15 } else {
16 *DEBUG = sub () {0};
17 }
18 }
19  
20 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21  
22 use Pod::Simple::PullParser;
23 use Tk::Pod::Styles;
24 use vars qw(@ISA);
25 @ISA = qw(Tk::Pod::Styles);
26  
27 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28 sub no_op {return}
29  
30 sub process { # main routine: non-handler
31 my ($w,$file_or_textref, $title) = @_; # window, filename or string ref, title (optional)
32  
33 my $p = $w->{'pod_parser'} = Pod::Simple::PullParser->new;
34 $p->set_source($file_or_textref);
35 my $file = !ref $file_or_textref && $file_or_textref;
36  
37 $w->toplevel->Busy;
38 $w->init_styles;
39  
40 my $process_no;
41 $w->{ProcessNo}++;
42 $process_no = $w->{ProcessNo};
43  
44 $w->{'sections'} = [];
45 $w->{'pod_tag'} = '10000'; # counter
46 #XXX my $style_stack = $w->{'style_stack'} ||= []; # || is probably harmful
47 my $style_stack = $w->{'style_stack'} = [];
48  
49 my @pod_marks;
50  
51 DEBUG and $file and warn "Pull-parsing $file (process number $process_no)\n";
52 $w->{'pod_title'} = $p->get_short_title || $title || $file;
53  
54 my($token, $tagname, $style);
55 my $last_update = Tk::timeofday();
56 my $current_line;
57 while($token = $p->get_token) {
58  
59 DEBUG > 7 and warn " t:", $token->dump, "\n";
60 if($token->can("attr_hash") && exists $token->attr_hash->{start_line}) {
61 $current_line = $token->attr_hash->{start_line};
62 }
63  
64 if($token->is_text) {
65 DEBUG > 10 and warn " ->pod_text( ", $token->text, ",", $current_line, ")\n";
66 $w->pod_text( $token, $current_line );
67  
68 } elsif($token->is_start) {
69 ($tagname = $token->tagname ) =~ tr/-:./__/;
70 $style = "style_" . $tagname;
71 $tagname = "pod_start_" . $tagname;
72 DEBUG > 7 and warn " ->$tagname & ->$style\n";
73 push @pod_marks, $w->index('end -1c');
74 # Yes, save the start-point for every element,
75 # for feeding to its end-tag event.
76  
77 if( $w->can($style) ) {
78 push @$style_stack, $w->$style($token);
79 DEBUG > 5 and warn "Style stack after adding ->$style: ",
80 join("|", map join('.',@$_), @{ $w->{'style_stack'} } ), "\n";
81 }
82  
83 &{ $w->can($tagname) || next }( $w, $token );
84 DEBUG > 10 and warn " back from ->$tagname\n";
85  
86 } elsif($token->is_end) {
87 ($tagname = $token->tagname ) =~ tr/-:./__/;
88 $style = "style_" . $tagname;
89 $tagname = "pod_end_" . $tagname;
90  
91 DEBUG > 7 and warn " ->$tagname & $style\n";
92  
93 &{ $w->can($tagname) || \&no_op }( $w, $token, pop(@pod_marks) );
94 # the output of that pop() is the start-point of this element
95 DEBUG > 10 and warn " back from ->$tagname\n";
96  
97 if( $w->can($style) ) {
98 pop @$style_stack;
99 DEBUG > 5 and warn "Style stack after popping results of ->$style: ",
100 join("|", map join('.',@$_), @{ $w->{'style_stack'} } ), "\n";
101 }
102 }
103  
104 if (Tk::timeofday() > $last_update+0.5) { # XXX make configurable
105 $w->update;
106 $last_update = Tk::timeofday();
107 do { warn "ABORT!"; return } if $w->{ProcessNo} != $process_no;
108 }
109  
110 }
111  
112 undef $p;
113 delete $w->{'pod_parser'};
114 DEBUG and $file and warn "Done rendering $file\n";
115  
116 $w->parent->add_section_menu if $w->parent->can('add_section_menu');
117 $w->Callback('-poddone', $file);
118 # set (invisible) insertion cursor to top of file
119 $w->markSet(insert => '@0,0');
120 $w->toplevel->Unbusy;
121 }
122  
123 ###########################################################################
124  
125 sub pod_text {
126 my($w, $t, $current_line) = @_;
127 if( $w->{'pod_in_X'} ) {
128 # no-op
129 } else {
130 # Emit it with whatever styles are in effect.
131  
132 my %attributes = (map @$_, @{ $w->{'style_stack'} } );
133 DEBUG > 4 and warn "Inserting <", $t->text, "> with attributes: ",
134 join('/', %attributes), "\n";
135  
136 my $startpoint = $w->index('end -1c');
137 $w->insert( 'end -1c', $t->text, "start_line_" . $current_line );
138
139 $w->tag(
140 'add',
141 $w->tag_for(\%attributes),
142 $startpoint => 'end -1c'
143 );
144 }
145 return;
146 }
147  
148 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
149  
150 sub pod_start_Document {
151 $_[0]->toplevel->title( "Tkpod: " . $_[0]->{'pod_title'} . " (loading)");
152 $_[0]->toplevel->update;
153 # XXX Is it bad form to manipulate the top level?
154 return;
155 }
156  
157 sub pod_end_Document {
158 $_[0]->toplevel->title( "Tkpod: " . $_[0]->{'pod_title'});
159 $_[0]->toplevel->update;
160 # XXX Is it bad form to manipulate the top level?
161 return;
162 }
163  
164 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
165  
166 sub nlnl { $_[0]->insert( 'end -1c', "\n\n" ); $_[0]; }
167 sub nl { $_[0]->insert( 'end -1c', "\n" ); $_[0]; }
168  
169 sub fake_unget_bold_text {
170 require Pod::Simple::PullParserStartToken;
171 require Pod::Simple::PullParserTextToken;
172 require Pod::Simple::PullParserEndToken;
173  
174 $_[0]{'pod_parser'}->unget_token(
175 Pod::Simple::PullParserStartToken->new('B'),
176 Pod::Simple::PullParserTextToken->new($_[1]),
177 Pod::Simple::PullParserEndToken->new('B'),
178 );
179 }
180  
181 sub pod_start_item_bullet {
182 $_[0]->fake_unget_bold_text('* ');
183 }
184 sub pod_start_item_number {
185 $_[0]->fake_unget_bold_text($_[1]->attr('number') . '. ');
186 }
187  
188 sub pod_end_Para { $_[0]->_indent($_[2]); $_[0]->nlnl }
189 sub pod_end_Verbatim { $_[0]->_indent($_[2]); $_[0]->nlnl }
190 sub pod_end_item_bullet { $_[0]->_indent($_[2]); $_[0]->nlnl }
191 sub pod_end_item_number { $_[0]->_indent($_[2]); $_[0]->nlnl }
192 sub pod_end_item_text { $_[0]->_indent($_[2]); $_[0]->nl }
193  
194 sub pod_end_over_text { $_[0]->nl } # XXX ok?
195  
196 sub _indent {
197 my ($w, $start) = @_;
198  
199 my $indent = 0;
200 foreach my $s (@{ $w->{'style_stack'} }) {
201 $indent += $s->[1] if @$s and $s->[0] eq 'indent';
202 # yes, indent is special -- it always has to be first
203 }
204 $indent = 0 if $indent < 0;
205
206 DEBUG > 5 and warn "Style stack giving indent of $indent for $start: ",
207 join("|", map join('.',@$_), @{ $w->{'style_stack'} } ), "\n";
208
209 my $tag = "Indent" . ($indent+0);
210 unless (exists $w->{'pod_indent_tag_known'}{$tag}) {
211 $w->{'pod_indent_tag_known'}{$tag} = 1;
212
213 $indent *= 8; # XXX Why 8?
214
215 $w->tag('configure' => $tag,
216 '-lmargin2' => $indent . 'p',
217 '-rmargin' => $indent . 'p',
218 '-lmargin1' => $indent . 'p'
219 );
220 }
221 $w->tag('add', $tag, $start, 'end -1c');
222 DEBUG > 3 and warn "Applying $tag to $start\n";
223 return;
224 }
225  
226  
227 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
228 # All we need for X<...>, I think:
229 sub pod_start_X { $_[0]{'pod_in_X'}++; return; }
230 sub pod_end_X { $_[0]{'pod_in_X'}--; return; }
231 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
232  
233 sub tag_for {
234 my($w, $attr) = @_;
235 my $canonical_form =
236 join( '~', map {; $_, $attr->{$_}}
237 sort
238 grep $_ ne 'indent',
239 keys %$attr
240 ) || 'nihil';
241
242 return
243 $w->{'known_tags'}{$canonical_form} ||=
244 do {
245 # initialize and return a new tagname
246 DEBUG and warn "Making a tag for $canonical_form\n";
247 $attr->{'family'} = 'times' unless exists $attr->{'family'};
248 $attr->{'weight'} = 'medium' unless exists $attr->{'weight'};
249 $attr->{'slant'} = 'r' unless exists $attr->{'slant'};
250 $attr->{'size'} = 10 unless exists $attr->{'size'};
251 $attr->{'spacing'} = '*' unless exists $attr->{'spacing'};
252 $attr->{'slant'} = substr( $attr->{'slant'},0,1 );
253
254 my $font_name = join ' ',
255 $attr->{'family'},
256 $attr->{'size'},
257 ($attr->{'weight'} ne 'medium') ? 'bold' : (),
258 ($attr->{'slant'} ne 'r' ) ? 'italic' : (),
259 ;
260
261 DEBUG and warn "Defining new tag $canonical_form with font $font_name\n";
262
263 $w->tagConfigure(
264 $canonical_form,
265 '-font' => $font_name,
266 ('none' eq ($attr->{'wrap'} || '')) ? ('-wrap' => 'none') : (),
267 $attr->{'underline'} ? ('-underline' => 'true') : (),
268 (map { defined $attr->{$_} ? ("-$_" => $attr->{$_}) : () } qw(background borderwidth relief lmargin1 rmargin)),
269 );
270 DEBUG > 10 and sleep 1;
271 $canonical_form;
272 }
273 ;
274 }
275  
276 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
277  
278 sub pod_start_L {
279 push @{ $_[0]->{'pod_L_attr_stack'} }, $_[1]->attr_hash;
280 }
281  
282 sub pod_end_L {
283 my $w = $_[0];
284 my $attr = pop @{ $w->{'pod_L_attr_stack'} };
285  
286 #$w->tag('add', 'L' , $_[2], 'end -1c');
287
288 my $tag = # make a unique identifier for this guy:
289 join "__", '!',
290 map defined($_) ? $_ : '',
291 @$attr{'type', 'to', 'section'};
292 #"!" . $attr->{'to'}
293 ;
294 $tag =~ tr/ /_/;
295 DEBUG > 2 and warn "Link-tag <$tag>\n";
296
297 my $to = $attr->{'to'} ; # might be undef!
298 my $section = $attr->{'section'}; # might be undef!
299
300 my $methodname;
301 if($attr->{'type'} eq 'pod') {
302 #$methodname = defined($to) ? 'Link' : 'Link_my_section';
303 $methodname = 'Link';
304 } elsif($attr->{'type'} eq 'url') {
305 $methodname = 'Link_url'
306 } elsif($attr->{'type'} eq 'man') {
307 $methodname = 'Link_man'
308 } else {
309 DEBUG and warn "Unknown link-type $$attr{'type'}!\n";
310 }
311  
312 $section = '' . $section if defined $section and ref $section;
313  
314 if(!defined $methodname) {
315 DEBUG > 2 and warn "No method for $$attr{'type'} links.\n";
316 } elsif($w->can($methodname)) {
317 DEBUG > 2 and warn "Binding $tag to $methodname\n";
318 $w->tag('bind', $tag, '<ButtonRelease-1>',
319 [$w, $methodname, 'reuse', Tk::Ev('@%x,%y'), $to, $section]);
320 $w->tag('bind', $tag, '<Shift-ButtonRelease-1>',
321 [$w, $methodname, 'new', Tk::Ev('@%x,%y'), $to, $section]);
322 $w->tag('bind', $tag, '<ButtonRelease-2>',
323 [$w, $methodname, 'new', Tk::Ev('@%x,%y'), $to, $section]);
324 $w->tag('bind', $tag, '<Enter>' => [$w, 'EnterLink']);
325 $w->tag('bind', $tag, '<Leave>' => [$w, 'LeaveLink']);
326 $w->tag('configure', $tag, '-underline' => 1, '-foreground' => 'blue' );
327 } else {
328 DEBUG > 2 and warn "Can't bind $tag to $methodname\n";
329 # green for no-good
330 $w->tag('configure', $tag, '-underline' => 1, '-foreground' => 'darkgreen' );
331 }
332 $w->tag('add', $tag, $_[2] ,'end -1c');
333 $w->tag('add', 'pod_link', $_[2] ,'end -1c'); # needed for ButtonRelease-2 hack, see Pod/Text.pm
334  
335 return;
336 }
337  
338 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
339  
340 sub pod_start_head1 { $_[0]->_common_heading('head1'); }
341 sub pod_start_head2 { $_[0]->_common_heading('head2'); }
342 sub pod_start_head3 { $_[0]->_common_heading('head3'); }
343 sub pod_start_head4 { $_[0]->_common_heading('head4'); }
344  
345 sub pod_end_head1 { $_[0]->nlnl }
346 sub pod_end_head2 { $_[0]->nlnl }
347 sub pod_end_head3 { $_[0]->nlnl }
348 sub pod_end_head4 { $_[0]->nlnl }
349  
350 sub _common_heading {
351 my $w = $_[0];
352 my $p = $w->{'pod_parser'};
353 my $end_tag = $_[1];
354
355 my @to_put_back;
356 my $text = '';
357 my $token;
358 my $in_X = 0;
359 while($token = $p->get_token) {
360 push @to_put_back, $token;
361 if( $token->is_end ) {
362 last if $token->is_tag($end_tag);
363 --$in_X if $token->is_tag('X');
364 } elsif($token->is_start) {
365 ++$in_X if $token->is_tag('X');
366 } elsif($token->is_text) {
367 $text .= $token->text unless $in_X;
368 }
369 last if @to_put_back > 40; # too complex a heading!
370 }
371  
372 if(length $text) {
373 my $level;
374 $end_tag =~ m/(\d+)$/ or die "WHAAAT? $end_tag!?";
375 $level = $1;
376 push @{$w->{'sections'}}, [$level, $text, $w->index('end')];
377 DEBUG and warn "Noting section heading head$level \"$text\".\n";
378 }
379  
380 $p->unget_token(@to_put_back);
381 return;
382 }
383  
384 # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
385  
386 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
387 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
388 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
389 1;
390 __END__