File Coverage

blib/lib/Pod/Simple/HTMLBatch.pm
Criterion Covered Total %
statement 326 369 88.3
branch 83 164 50.6
condition 30 84 35.7
subroutine 41 42 97.6
pod 4 16 25.0
total 484 675 71.7


line stmt bran cond sub pod time code
1             package Pod::Simple::HTMLBatch;
2 2     2   218727 use strict;
  2         4  
  2         146  
3             our $VERSION = '3.48';
4             our @ISA = (); # Yup, we're NOT a subclass of Pod::Simple::HTML!
5              
6             # TODO: nocontents stylesheets. Strike some of the color variations?
7              
8 2     2   527 use Pod::Simple::HTML ();
  2         5  
  2         106  
9 2     2   44 BEGIN {*esc = \&Pod::Simple::HTML::esc }
10 2     2   14 use File::Spec ();
  2         3  
  2         36  
11              
12 2     2   1418 use Pod::Simple::Search;
  2         6  
  2         196  
13             our $SEARCH_CLASS ||= 'Pod::Simple::Search';
14              
15             BEGIN {
16 2 50   2   19 if(defined &DEBUG) { } # no-op
    50          
17 2         12499 elsif( defined &Pod::Simple::DEBUG ) { *DEBUG = \&Pod::Simple::DEBUG }
18 0         0 else { *DEBUG = sub () {0}; }
19             }
20              
21             our $SLEEPY;
22             $SLEEPY = 1 if !defined $SLEEPY and $^O =~ /mswin|mac/i;
23             # flag to occasionally sleep for $SLEEPY - 1 seconds.
24              
25             our $HTML_RENDER_CLASS ||= "Pod::Simple::HTML";
26             our $HTML_EXTENSION;
27              
28             #
29             # Methods beginning with "_" are particularly internal and possibly ugly.
30             #
31              
32             Pod::Simple::_accessorize( __PACKAGE__,
33             'verbose', # how verbose to be during batch conversion
34             'html_render_class', # what class to use to render
35             'search_class', # what to use to search for POD documents
36             'contents_file', # If set, should be the name of a file (in current directory)
37             # to write the list of all modules to
38             'index', # will set $htmlpage->index(...) to this (true or false)
39             'progress', # progress object
40             'contents_page_start', 'contents_page_end',
41              
42             'css_flurry', '_css_wad', 'javascript_flurry', '_javascript_wad',
43             'no_contents_links', # set to true to suppress automatic adding of << links.
44             '_contents',
45             );
46              
47             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
48             # Just so we can run from the command line more easily
49             sub go {
50 0 0   0 0 0 @ARGV == 2 or die sprintf(
51             "Usage: perl -M%s -e %s:go indirs outdir\n (or use \"\@INC\" for indirs)\n",
52             __PACKAGE__, __PACKAGE__,
53             );
54              
55 0 0 0     0 if(defined($ARGV[1]) and length($ARGV[1])) {
56 0         0 my $d = $ARGV[1];
57 0 0       0 -e $d or die "I see no output directory named \"$d\"\nAborting";
58 0 0       0 -d $d or die "But \"$d\" isn't a directory!\nAborting";
59 0 0       0 -w $d or die "Directory \"$d\" isn't writeable!\nAborting";
60             }
61              
62 0         0 __PACKAGE__->batch_convert(@ARGV);
63             }
64             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
65              
66              
67             sub new {
68 1   33 1 1 4504 my $new = bless {}, ref($_[0]) || $_[0];
69 1         4 $new->html_render_class($HTML_RENDER_CLASS);
70 1         3 $new->search_class($SEARCH_CLASS);
71 1         9 $new->verbose(1 + DEBUG);
72 1         2 $new->_contents([]);
73              
74 1         3 $new->index(1);
75              
76 1         2 $new-> _css_wad([]); $new->css_flurry(1);
  1         2  
77 1         2 $new->_javascript_wad([]); $new->javascript_flurry(1);
  1         2  
78              
79 1   33     4 $new->contents_file(
80             'index' . ($HTML_EXTENSION || $Pod::Simple::HTML::HTML_EXTENSION)
81             );
82              
83 1         5 $new->contents_page_start( join "\n", grep $_,
84             $Pod::Simple::HTML::Doctype_decl,
85             "",
86             "Perl Documentation",
87             $Pod::Simple::HTML::Content_decl,
88             "",
89             "\n\n

Perl Documentation

\n"
90             ); # override if you need a different title
91              
92              
93             $new->contents_page_end( sprintf(
94             "\n\n

Generated by %s v%s under Perl v%s\n
At %s GMT.

\n\n\n",
95             esc(
96             ref($new),
97             eval {$new->VERSION} || $VERSION,
98 1   33     35 $], scalar(gmtime($ENV{SOURCE_DATE_EPOCH} || time)),
      33        
99             )));
100              
101 1         3 return $new;
102             }
103              
104             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
105              
106             sub muse {
107 10     10 0 12 my $self = shift;
108 10 50       23 if($self->verbose) {
109 0         0 print 'T+', int(time() - $self->{'_batch_start_time'}), "s: ", @_, "\n";
110             }
111 10         15 return 1;
112             }
113              
114             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
115              
116             sub batch_convert {
117 1     1 1 5 my($self, $dirs, $outdir) = @_;
118 1   50     2 $self ||= __PACKAGE__; # tolerate being called as an optionless function
119 1 50       2 $self = $self->new unless ref $self; # tolerate being used as a class method
120              
121 1 50 33     7 if(!defined($dirs) or $dirs eq '' or $dirs eq '@INC' ) {
    50 33        
122 0         0 $dirs = '';
123             } elsif(ref $dirs) {
124             # OK, it's an explicit set of dirs to scan, specified as an arrayref.
125             } else {
126             # OK, it's an explicit set of dirs to scan, specified as a
127             # string like "/thing:/also:/whatever/perl" (":"-delim, as usual)
128             # or, under MSWin, like "c:/thing;d:/also;c:/whatever/perl" (";"-delim!)
129 0         0 require Config;
130 0   0     0 my $ps = quotemeta( $Config::Config{'path_sep'} || ":" );
131 0         0 $dirs = [ grep length($_), split qr/$ps/, $dirs ];
132             }
133              
134 1 50 33     4 $outdir = $self->filespecsys->curdir
135             unless defined $outdir and length $outdir;
136              
137 1         7 $self->_batch_convert_main($dirs, $outdir);
138             }
139              
140             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
141              
142             sub _batch_convert_main {
143 1     1   2 my($self, $dirs, $outdir) = @_;
144             # $dirs is either false, or an arrayref.
145             # $outdir is a pathspec.
146              
147 1   33     5 $self->{'_batch_start_time'} ||= time();
148              
149 1         35 $self->muse( "= ", scalar(localtime) );
150 1         3 $self->muse( "Starting batch conversion to \"$outdir\"" );
151              
152 1         2 my $progress = $self->progress;
153 1 50 33     4 if(!$progress and $self->verbose > 0 and $self->verbose() <= 5) {
      33        
154 0         0 require Pod::Simple::Progress;
155 0 0       0 $progress = Pod::Simple::Progress->new(
    0          
156             ($self->verbose < 2) ? () # Default omission-delay
157             : ($self->verbose == 2) ? 1 # Reduce the omission-delay
158             : 0 # Eliminate the omission-delay
159             );
160 0         0 $self->progress($progress);
161             }
162              
163 1 50       2 if($dirs) {
164 1         8 $self->muse(scalar(@$dirs), " dirs to scan: @$dirs");
165             } else {
166 0         0 $self->muse("Scanning \@INC. This could take a minute or two.");
167             }
168 1 50       4 my $mod2path = $self->find_all_pods($dirs ? $dirs : ());
169 1         62 $self->muse("Done scanning.");
170              
171 1         1 my $total = keys %$mod2path;
172 1 50       3 unless($total) {
173 0         0 $self->muse("No pod found. Aborting batch conversion.\n");
174 0         0 return $self;
175             }
176              
177 1 50       3 $progress and $progress->goal($total);
178 1 50       3 $self->muse("Now converting pod files to HTML.",
179             ($total > 25) ? " This will take a while more." : ()
180             );
181              
182 1         5 $self->_spray_css( $outdir );
183 1         4 $self->_spray_javascript( $outdir );
184              
185 1         6 $self->_do_all_batch_conversions($mod2path, $outdir);
186              
187             $progress and $progress->done(sprintf (
188 1 50       4 "Done converting %d files.", $self->{"__batch_conv_page_count"}
189             ));
190 1         4 return $self->_batch_convert_finish($outdir);
191 0         0 return $self;
192             }
193              
194              
195             sub _do_all_batch_conversions {
196 1     1   3 my($self, $mod2path, $outdir) = @_;
197 1         4 $self->{"__batch_conv_page_count"} = 0;
198              
199 1         9 foreach my $module (sort {lc($a) cmp lc($b)} keys %$mod2path) {
  21         26  
200 10         21 $self->_do_one_batch_conversion($module, $mod2path, $outdir);
201 10 50       22 sleep($SLEEPY - 1) if $SLEEPY;
202             }
203              
204 1         3 return;
205             }
206              
207             sub _batch_convert_finish {
208 1     1   3 my($self, $outdir) = @_;
209 1         4 $self->write_contents_file($outdir);
210 1         24 $self->muse("Done with batch conversion. $$self{'__batch_conv_page_count'} files done.");
211 1         31 $self->muse( "= ", scalar(localtime) );
212 1 50       3 $self->progress and $self->progress->done("All done!");
213 1         11 return;
214             }
215              
216             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
217              
218             sub _do_one_batch_conversion {
219 10     10   22 my($self, $module, $mod2path, $outdir, $outfile) = @_;
220              
221 10         10 my $retval;
222 10         16 my $total = scalar keys %$mod2path;
223 10         15 my $infile = $mod2path->{$module};
224 10         65 my @namelets = grep m/\S/, split "::", $module;
225             # this can stick around in the contents LoL
226 10         25 my $depth = scalar @namelets;
227 10 50       17 die "Contentless thingie?! $module $infile" unless @namelets; #sanity
228              
229 10   33     23 $outfile ||= do {
230 10         13 my @n = @namelets;
231 10   33     34 $n[-1] .= $HTML_EXTENSION || $Pod::Simple::HTML::HTML_EXTENSION;
232 10         15 $self->filespecsys->catfile( $outdir, @n );
233             };
234              
235 10         25 my $progress = $self->progress;
236              
237 10         20 my $page = $self->html_render_class->new;
238 10         12 if(DEBUG > 5) {
239             $self->muse($self->{"__batch_conv_page_count"} + 1, "/$total: ",
240             ref($page), " render ($depth) $module => $outfile");
241 0         0 } elsif(DEBUG > 2) {
242             $self->muse($self->{"__batch_conv_page_count"} + 1, "/$total: $module => $outfile")
243             }
244              
245             # Give each class a chance to init the converter:
246 10 50       45 $page->batch_mode_page_object_init($self, $module, $infile, $outfile, $depth)
247             if $page->can('batch_mode_page_object_init');
248             # Init for the index (TOC), too.
249 10 50       35 $self->batch_mode_page_object_init($page, $module, $infile, $outfile, $depth)
250             if $self->can('batch_mode_page_object_init');
251              
252             # Now get busy...
253 10         26 $self->makepath($outdir => \@namelets);
254              
255 10 50       29 $progress and $progress->reach($self->{"__batch_conv_page_count"}, "Rendering $module");
256              
257 10 50       64 if( $retval = $page->parse_from_file($infile, $outfile) ) {
258 10         13 ++ $self->{"__batch_conv_page_count"} ;
259 10         22 $self->note_for_contents_file( \@namelets, $infile, $outfile );
260             } else {
261 0         0 $self->muse("Odd, parse_from_file(\"$infile\", \"$outfile\") returned false.");
262             }
263              
264 10 50       38 $page->batch_mode_page_object_kill($self, $module, $infile, $outfile, $depth)
265             if $page->can('batch_mode_page_object_kill');
266             # The following isn't a typo. Note that it switches $self and $page.
267 10 50       24 $self->batch_mode_page_object_kill($page, $module, $infile, $outfile, $depth)
268             if $self->can('batch_mode_page_object_kill');
269              
270 10         8 DEBUG > 4 and printf STDERR "%s %sb < $infile %s %sb\n",
271             $outfile, -s $outfile, $infile, -s $infile
272             ;
273              
274 10         644 undef($page);
275 10         34 return $retval;
276             }
277              
278             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
279 28 50   28 0 351 sub filespecsys { $_[0]{'_filespecsys'} || 'File::Spec' }
280              
281             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
282              
283             sub note_for_contents_file {
284 10     10 0 20 my($self, $namelets, $infile, $outfile) = @_;
285              
286             # I think the infile and outfile parts are never used. -- SMB
287             # But it's handy to have them around for debugging.
288              
289 10 50       19 if( $self->contents_file ) {
290 10         17 my $c = $self->_contents();
291 10         26 push @$c,
292             [ join("::", @$namelets), $infile, $outfile, $namelets ]
293             # 0 1 2 3
294             ;
295 10         11 DEBUG > 3 and print STDERR "Noting @$c[-1]\n";
296             }
297 10         16 return;
298             }
299              
300             #_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
301              
302             sub write_contents_file {
303 1     1 0 3 my($self, $outdir) = @_;
304 1   50     3 my $outfile = $self->_contents_filespec($outdir) || return;
305              
306 1         5 $self->muse("Preparing list of modules for ToC");
307              
308 1         9 my($toplevel, # maps toplevelbit => [all submodules]
309             $toplevel_form_freq, # ends up being 'foo' => 'Foo'
310             ) = $self->_prep_contents_breakdown;
311              
312 1         2 my $Contents = eval { $self->_wopen($outfile) };
  1         17  
313 1 50       4 if( $Contents ) {
314 1         4 $self->muse( "Writing contents file $outfile" );
315             } else {
316 0         0 warn "Couldn't write-open contents file $outfile: $!\nAbort writing to $outfile at all";
317 0         0 return;
318             }
319              
320 1         3 $self->_write_contents_start( $Contents, $outfile, );
321 1         4 $self->_write_contents_middle( $Contents, $outfile, $toplevel, $toplevel_form_freq );
322 1         4 $self->_write_contents_end( $Contents, $outfile, );
323 1         7 return $outfile;
324             }
325              
326             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
327              
328             sub _write_contents_start {
329 1     1   4 my($self, $Contents, $outfile) = @_;
330 1   50     20 my $starter = $self->contents_page_start || '';
331              
332             {
333 1         2 my $css_wad = $self->_css_wad_to_markup(1);
  1         3  
334 1 50       4 if( $css_wad ) {
335 1         17 $starter =~ s{()}{\n$css_wad\n$1}i; # otherwise nevermind
336             }
337              
338 1         2 my $javascript_wad = $self->_javascript_wad_to_markup(1);
339 1 50       2 if( $javascript_wad ) {
340 1         11 $starter =~ s{()}{\n$javascript_wad\n$1}i; # otherwise nevermind
341             }
342             }
343              
344 1 50       15 unless(print $Contents $starter, "
\n" ) {
345 0         0 warn "Couldn't print to $outfile: $!\nAbort writing to $outfile at all";
346 0         0 close($Contents);
347 0         0 return 0;
348             }
349 1         3 return 1;
350             }
351              
352             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
353              
354             sub _write_contents_middle {
355 1     1   2 my($self, $Contents, $outfile, $toplevel2submodules, $toplevel_form_freq) = @_;
356              
357 1         8 foreach my $t (sort keys %$toplevel2submodules) {
358 5         10 my @downlines = sort {$a->[-1] cmp $b->[-1]}
359 6         4 @{ $toplevel2submodules->{$t} };
  6         14  
360              
361             printf $Contents qq[
%s
\n
\n],
362 6         9 esc( $t, $toplevel_form_freq->{$t} )
363             ;
364              
365 6         9 my($path, $name);
366 6         7 foreach my $e (@downlines) {
367 10         11 $name = $e->[0];
368 10   33     10 $path = join( "/", '.', esc( @{$e->[3]} ) )
  10         16  
369             . ($HTML_EXTENSION || $Pod::Simple::HTML::HTML_EXTENSION);
370 10         15 print $Contents qq{ }, esc($name), "  \n";
371             }
372 6         11 print $Contents "\n\n";
373             }
374 1         2 return 1;
375             }
376              
377             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
378              
379             sub _write_contents_end {
380 1     1   2 my($self, $Contents, $outfile) = @_;
381 1 50 50     4 unless(
382             print $Contents "\n",
383             $self->contents_page_end || '',
384             ) {
385 0         0 warn "Couldn't write to $outfile: $!";
386             }
387 1 50       45 close($Contents) or warn "Couldn't close $outfile: $!";
388 1         2 return 1;
389             }
390              
391             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
392              
393             sub _prep_contents_breakdown {
394 1     1   2 my($self) = @_;
395 1         2 my $contents = $self->_contents;
396 1         2 my %toplevel; # maps lctoplevelbit => [all submodules]
397             my %toplevel_form_freq; # ends up being 'foo' => 'Foo'
398             # (mapping anycase forms to most freq form)
399              
400 1         2 foreach my $entry (@$contents) {
401 10 100       23 my $toplevel =
402             $entry->[0] =~ m/^perl\w*$/ ? 'perl_core_docs'
403             # group all the perlwhatever docs together
404             : $entry->[3][0] # normal case
405             ;
406 10         18 ++$toplevel_form_freq{ lc $toplevel }{ $toplevel };
407 10         8 push @{ $toplevel{ lc $toplevel } }, $entry;
  10         13  
408 10         17 push @$entry, lc($entry->[0]); # add a sort-order key to the end
409             }
410              
411 1         5 foreach my $toplevel (sort keys %toplevel) {
412 6         6 my $fgroup = $toplevel_form_freq{$toplevel};
413             $toplevel_form_freq{$toplevel} =
414             (
415 6 0       13 sort { $fgroup->{$b} <=> $fgroup->{$a} or $a cmp $b }
  0         0  
416             keys %$fgroup
417             # This hash is extremely unlikely to have more than 4 members, so this
418             # sort isn't so very wasteful
419             )[0];
420             }
421              
422 1 50       5 return(\%toplevel, \%toplevel_form_freq) if wantarray;
423 0         0 return \%toplevel;
424             }
425              
426             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
427              
428             sub _contents_filespec {
429 1     1   2 my($self, $outdir) = @_;
430 1         3 my $outfile = $self->contents_file;
431 1 50       2 return unless $outfile;
432 1         3 return $self->filespecsys->catfile( $outdir, $outfile );
433             }
434              
435             #_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
436              
437             sub makepath {
438 10     10 0 14 my($self, $outdir, $namelets) = @_;
439 10 100       19 return unless @$namelets > 1;
440 5         12 for my $i (0 .. ($#$namelets - 1)) {
441 5         8 my $dir = $self->filespecsys->catdir( $outdir, @$namelets[0 .. $i] );
442 5 100       186 if(-e $dir) {
443 2 50       14 die "$dir exists but not as a directory!?" unless -d $dir;
444 2         5 next;
445             }
446 3         6 DEBUG > 3 and print STDERR " Making $dir\n";
447 3 50       254 mkdir $dir, 0777
448             or die "Can't mkdir $dir: $!\nAborting"
449             ;
450             }
451 5         10 return;
452             }
453              
454             #_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
455              
456             sub batch_mode_page_object_init {
457 10     10 0 12 my $self = shift;
458 10         18 my($page, $module, $infile, $outfile, $depth) = @_;
459              
460             # TODO: any further options to percolate onto this new object here?
461              
462 10         20 $page->default_title($module);
463 10         18 $page->index( $self->index );
464              
465 10         20 $page->html_css( $self-> _css_wad_to_markup($depth) );
466 10         21 $page->html_javascript( $self->_javascript_wad_to_markup($depth) );
467              
468 10         21 $self->add_header_backlink($page, $module, $infile, $outfile, $depth);
469 10         18 $self->add_footer_backlink($page, $module, $infile, $outfile, $depth);
470              
471              
472 10         13 return $self;
473             }
474              
475             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
476              
477             sub add_header_backlink {
478 10     10 0 16 my $self = shift;
479 10 50       15 return if $self->no_contents_links;
480 10         27 my($page, $module, $infile, $outfile, $depth) = @_;
481 10 50 50     18 $page->html_header_after_title( join '',
482             $page->html_header_after_title || '',
483              
484             qq[

485

            $self->url_up_to_contents($depth),
486             qq[" accesskey="1" title="All Documents"><<

\n],
487             )
488             if $self->contents_file
489             ;
490 10         17 return;
491             }
492              
493             sub add_footer_backlink {
494 10     10 0 12 my $self = shift;
495 10 50       14 return if $self->no_contents_links;
496 10         13 my($page, $module, $infile, $outfile, $depth) = @_;
497 10 50 50     14 $page->html_footer( join '',
498             qq[

499

            $self->url_up_to_contents($depth),
500             qq[" title="All Documents"><<

\n],
501              
502             $page->html_footer || '',
503             )
504             if $self->contents_file
505             ;
506 10         12 return;
507             }
508              
509             sub url_up_to_contents {
510 20     20 0 22 my($self, $depth) = @_;
511 20         18 --$depth;
512 20         30 return join '/', ('..') x $depth, esc($self->contents_file);
513             }
514              
515             #_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
516              
517             sub find_all_pods {
518 1     1 0 1 my($self, $dirs) = @_;
519             # You can override find_all_pods in a subclass if you want to
520             # do extra filtering or whatnot. But for the moment, we just
521             # pass to modnames2paths:
522 1         2 return $self->modnames2paths($dirs);
523             }
524              
525             #_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
526              
527             sub modnames2paths { # return a hashref mapping modulenames => paths
528 1     1 0 2 my($self, $dirs) = @_;
529              
530 1         1 my $m2p;
531             {
532 1         5 my $search = $self->search_class->new;
  1         2  
533 1         3 DEBUG and print STDERR "Searching via $search\n";
534 1         2 $search->verbose(1) if DEBUG > 10;
535 1 50       5 $search->progress( $self->progress->copy->goal(0) ) if $self->progress;
536 1         4 $search->shadows(0); # don't bother noting shadowed files
537 1 50       5 $search->inc( $dirs ? 0 : 1 );
538 1 50       3 $search->survey( $dirs ? @$dirs : () );
539 1         6 $m2p = $search->name2path;
540 1 50       32 die "What, no name2path?!" unless $m2p;
541             }
542              
543 1 50       4 $self->muse("That's odd... no modules found!") unless keys %$m2p;
544 1         2 if( DEBUG > 4 ) {
545             print STDERR "Modules found (name => path):\n";
546             foreach my $m (sort {lc($a) cmp lc($b)} keys %$m2p) {
547             print STDERR " $m $$m2p{$m}\n";
548             }
549             print STDERR "(total ", scalar(keys %$m2p), ")\n\n";
550 0         0 } elsif( DEBUG ) {
551             print STDERR "Found ", scalar(keys %$m2p), " modules.\n";
552             }
553 1         11 $self->muse( "Found ", scalar(keys %$m2p), " modules." );
554              
555             # return the Foo::Bar => /whatever/Foo/Bar.pod|pm hashref
556 1         4 return $m2p;
557             }
558              
559             #===========================================================================
560              
561             sub _wopen {
562             # this is abstracted out so that the daemon class can override it
563 13     13   24 my($self, $outpath) = @_;
564 13         55 require Symbol;
565 13         30 my $out_fh = Symbol::gensym();
566 13         122 DEBUG > 5 and print STDERR "Write-opening to $outpath\n";
567 13 50       1451 return $out_fh if open($out_fh, "> $outpath");
568 0         0 require Carp;
569 0         0 Carp::croak("Can't write-open $outpath: $!");
570             }
571              
572             #==========================================================================
573              
574             sub add_css {
575 11     11 1 17 my($self, $url, $is_default, $name, $content_type, $media, $_code) = @_;
576 11 50       15 return unless $url;
577 11 50       13 unless($name) {
578             # cook up a reasonable name based on the URL
579 0         0 $name = $url;
580 0 0 0     0 if( $name !~ m/\?/ and $name =~ m{([^/]+)$}s ) {
581 0         0 $name = $1;
582 0         0 $name =~ s/\.css//i;
583             }
584             }
585 11   50     31 $media ||= 'all';
586 11   50     24 $content_type ||= 'text/css';
587              
588 11         21 my $bunch = [$url, $name, $content_type, $media, $_code];
589 11 50       12 if($is_default) { unshift @{ $self->_css_wad }, $bunch }
  0         0  
  0         0  
590 11         10 else { push @{ $self->_css_wad }, $bunch }
  11         60  
591 11         22 return;
592             }
593              
594             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
595              
596             sub _spray_css {
597 1     1   2 my($self, $outdir) = @_;
598              
599 1 50       5 return unless $self->css_flurry();
600 1         6 $self->_gen_css_wad();
601              
602 1         3 my $lol = $self->_css_wad;
603 1         2 foreach my $chunk (@$lol) {
604 11         30 my $url = $chunk->[0];
605 11         12 my $outfile;
606 11 50 33     76 if( ref($chunk->[-1]) and $url =~ m{^(_[-a-z0-9_]+\.css$)} ) {
607 11         24 $outfile = $self->filespecsys->catfile( $outdir, "$1" );
608 11         20 DEBUG > 5 and print STDERR "Noting $$chunk[0] as a file I'll create.\n";
609             } else {
610 0         0 DEBUG > 5 and print STDERR "OK, noting $$chunk[0] as an external CSS.\n";
611             # Requires no further attention.
612 0         0 next;
613             }
614              
615             #$self->muse( "Writing autogenerated CSS file $outfile" );
616 11         14 my $Cssout = $self->_wopen($outfile);
617 11 50       24 print $Cssout ${$chunk->[-1]}
  11         43  
618             or warn "Couldn't print to $outfile: $!\nAbort writing to $outfile at all";
619 11         447 close($Cssout);
620 11         61 DEBUG > 5 and print STDERR "Wrote $outfile\n";
621             }
622              
623 1         2 return;
624             }
625              
626             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
627              
628             sub _css_wad_to_markup {
629 11     11   14 my($self, $depth) = @_;
630              
631 11 50       10 my @css = @{ $self->_css_wad || return '' };
  11         17  
632 11 50       36 return '' unless @css;
633              
634 11         15 my $rel = 'stylesheet';
635 11         12 my $out = '';
636              
637 11         11 --$depth;
638 11 100       20 my $uplink = $depth ? ('../' x $depth) : '';
639              
640 11         15 foreach my $chunk (@css) {
641 121 50 33     225 next unless $chunk and @$chunk;
642              
643 121         144 my( $url1, $url2, $title, $type, $media) = (
644             $self->_maybe_uplink( $chunk->[0], $uplink ),
645             esc(grep !ref($_), @$chunk)
646             );
647              
648 121         167 $out .= qq{\n};
649              
650 121         135 $rel = 'alternate stylesheet'; # alternates = all non-first iterations
651             }
652 11         41 return $out;
653             }
654              
655             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
656             sub _maybe_uplink {
657             # if the given URL looks relative, return the given uplink string --
658             # otherwise return emptystring
659 132     132   153 my($self, $url, $uplink) = @_;
660 132 50 33     406 ($url =~ m{^\./} or $url !~ m{[/\:]} )
661             ? $uplink
662             : ''
663             # qualify it, if/as needed
664             }
665              
666             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
667             sub _gen_css_wad {
668 1     1   1 my $self = $_[0];
669 1         5 my $css_template = $self->_css_template;
670 1         5 foreach my $variation (
671              
672             # Commented out for sake of concision:
673             #
674             # 011n=black_with_red_on_white
675             # 001n=black_with_yellow_on_white
676             # 101n=black_with_green_on_white
677             # 110=white_with_yellow_on_black
678             # 010=white_with_green_on_black
679             # 011=white_with_blue_on_black
680             # 100=white_with_red_on_black
681             '110n=blkbluw', # black_with_blue_on_white
682             '010n=blkmagw', # black_with_magenta_on_white
683             '100n=blkcynw', # black_with_cyan_on_white
684             '101=whtprpk', # white_with_purple_on_black
685             '001=whtnavk', # white_with_navy_blue_on_black
686             '010a=grygrnk', # grey_with_green_on_black
687             '010b=whtgrng', # white_with_green_on_grey
688             '101an=blkgrng', # black_with_green_on_grey
689             '101bn=grygrnw', # grey_with_green_on_white
690             ) {
691              
692 9         10 my $outname = $variation;
693 9 50 100     64 my($flipmode, @swap) = ( ($4 || ''), $1,$2,$3)
694             if $outname =~ s/^([012])([012])([[012])([a-z]*)=?//s;
695 9 100       19 @swap = () if '010' eq join '', @swap; # 010 is a swop-no-op!
696              
697 9         31 my $this_css =
698             "/* This file is autogenerated. Do not edit. $variation */\n\n"
699             . $css_template;
700              
701             # Only look at three-digitty colors, for now at least.
702 9 100       16 if( $flipmode =~ m/n/ ) {
703 5         22 $this_css =~ s/(#[0-9a-fA-F]{3})\b/_color_negate($1)/eg;
  110         104  
704 5         69 $this_css =~ s/\bthin\b/medium/g;
705             }
706 9 100       31 $this_css =~ s<#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])\b>
707 132         273 < join '', '#', ($1,$2,$3)[@swap] >eg if @swap;
708              
709 9 100       20 if( $flipmode =~ m/a/)
    100          
710 2         24 { $this_css =~ s/#fff\b/#999/gi } # black -> dark grey
711             elsif($flipmode =~ m/b/)
712 2         15 { $this_css =~ s/#000\b/#666/gi } # white -> light grey
713              
714 9         11 my $name = $outname;
715 9         8 $name =~ tr/-_/ /;
716 9         19 $self->add_css( "_$outname.css", 0, $name, 0, 0, \$this_css);
717             }
718              
719             # Now a few indexless variations:
720 1         4 my %variations = (
721             blkbluw => 'black_with_blue_on_white',
722             whtpurk => 'white_with_purple_on_black',
723             whtgrng => 'white_with_green_on_grey',
724             grygrnw => 'grey_with_green_on_white',
725             );
726 1         6 for (my ($outfile, $variation) = each %variations) {
727 2         8 my $this_css = join "\n",
728             "/* This file is autogenerated. Do not edit. $outfile */\n",
729             "\@import url(\"./_$variation.css\");",
730             ".indexgroup { display: none; }",
731             "\n",
732             ;
733 2         2 my $name = $outfile;
734 2         2 $name =~ tr/-_/ /;
735 2         4 $self->add_css( "_$outfile.css", 0, $name, 0, 0, \$this_css);
736             }
737              
738 1         3 return;
739             }
740              
741             sub _color_negate {
742 110     110   113 my $x = lc $_[0];
743 110         96 $x =~ tr[0123456789abcdef]
744             [fedcba9876543210];
745 110         229 return $x;
746             }
747              
748             #===========================================================================
749              
750             sub add_javascript {
751 1     1 1 3 my($self, $url, $content_type, $_code) = @_;
752 1 50       3 return unless $url;
753 1   50     1 push @{ $self->_javascript_wad }, [
  1         5  
754             $url, $content_type || 'text/javascript', $_code
755             ];
756 1         2 return;
757             }
758              
759             sub _spray_javascript {
760 1     1   3 my($self, $outdir) = @_;
761 1 50       9 return unless $self->javascript_flurry();
762 1         3 $self->_gen_javascript_wad();
763              
764 1         2 my $lol = $self->_javascript_wad;
765 1         4 foreach my $script (@$lol) {
766 1         2 my $url = $script->[0];
767 1         1 my $outfile;
768              
769 1 50 33     10 if( ref($script->[-1]) and $url =~ m{^(_[-a-z0-9_]+\.js$)} ) {
770 1         2 $outfile = $self->filespecsys->catfile( $outdir, "$1" );
771 1         2 DEBUG > 5 and print STDERR "Noting $$script[0] as a file I'll create.\n";
772             } else {
773 0         0 DEBUG > 5 and print STDERR "OK, noting $$script[0] as an external JavaScript.\n";
774 0         0 next;
775             }
776              
777             #$self->muse( "Writing JavaScript file $outfile" );
778 1         2 my $Jsout = $self->_wopen($outfile);
779              
780 1 50       3 print $Jsout ${$script->[-1]}
  1         20  
781             or warn "Couldn't print to $outfile: $!\nAbort writing to $outfile at all";
782 1         27 close($Jsout);
783 1         5 DEBUG > 5 and print STDERR "Wrote $outfile\n";
784             }
785              
786 1         2 return;
787             }
788              
789             sub _gen_javascript_wad {
790 1     1   1 my $self = $_[0];
791 1   50     3 my $js_code = $self->_javascript || return;
792 1         16 $self->add_javascript( "_podly.js", 0, \$js_code);
793 1         2 return;
794             }
795              
796             sub _javascript_wad_to_markup {
797 11     11   15 my($self, $depth) = @_;
798              
799 11 50       12 my @scripts = @{ $self->_javascript_wad || return '' };
  11         17  
800 11 50       17 return '' unless @scripts;
801              
802 11         13 my $out = '';
803              
804 11         16 --$depth;
805 11 100       23 my $uplink = $depth ? ('../' x $depth) : '';
806              
807 11         28 foreach my $s (@scripts) {
808 11 50 33     24 next unless $s and @$s;
809              
810 11         16 my( $url1, $url2, $type, $media) = (
811             $self->_maybe_uplink( $s->[0], $uplink ),
812             esc(grep !ref($_), @$s)
813             );
814              
815 11         26 $out .= qq{\n};
816             }
817 11         23 return $out;
818             }
819              
820             #===========================================================================
821              
822             our $CSS = <<'EOCSS';
823             /* For accessibility reasons, never specify text sizes in px/pt/pc/in/cm/mm */
824              
825             @media all { .hide { display: none; } }
826              
827             @media print {
828             .noprint, div.indexgroup, .backlinktop, .backlinkbottom { display: none }
829              
830             * {
831             border-color: black !important;
832             color: black !important;
833             background-color: transparent !important;
834             background-image: none !important;
835             }
836              
837             dl.superindex > dd {
838             word-spacing: .6em;
839             }
840             }
841              
842             @media aural, braille, embossed {
843             div.indexgroup { display: none; } /* Too noisy, don't you think? */
844             dl.superindex > dt:before { content: "Group "; }
845             dl.superindex > dt:after { content: " contains:"; }
846             .backlinktop a:before { content: "Back to contents"; }
847             .backlinkbottom a:before { content: "Back to contents"; }
848             }
849              
850             @media aural {
851             dl.superindex > dt { pause-before: 600ms; }
852             }
853              
854             @media screen, tty, tv, projection {
855             .noscreen { display: none; }
856              
857             a:link { color: #7070ff; text-decoration: underline; }
858             a:visited { color: #e030ff; text-decoration: underline; }
859             a:active { color: #800000; text-decoration: underline; }
860             body.contentspage a { text-decoration: none; }
861             a.u { color: #fff !important; text-decoration: none; }
862              
863             body.pod {
864             margin: 0 5px;
865             color: #fff;
866             background-color: #000;
867             }
868              
869             body.pod h1, body.pod h2, body.pod h3,
870             body.pod h4, body.pod h5, body.pod h6 {
871             font-family: Tahoma, Verdana, Helvetica, Arial, sans-serif;
872             font-weight: normal;
873             margin-top: 1.2em;
874             margin-bottom: .1em;
875             border-top: thin solid transparent;
876             /* margin-left: -5px; border-left: 2px #7070ff solid; padding-left: 3px; */
877             }
878              
879             body.pod h1 { border-top-color: #0a0; }
880             body.pod h2 { border-top-color: #080; }
881             body.pod h3 { border-top-color: #040; }
882             body.pod h4 { border-top-color: #010; }
883             body.pod h5 { border-top-color: #010; }
884             body.pod h6 { border-top-color: #010; }
885              
886             p.backlinktop + h1 { border-top: none; margin-top: 0em; }
887             p.backlinktop + h2 { border-top: none; margin-top: 0em; }
888             p.backlinktop + h3 { border-top: none; margin-top: 0em; }
889             p.backlinktop + h4 { border-top: none; margin-top: 0em; }
890             p.backlinktop + h5 { border-top: none; margin-top: 0em; }
891             p.backlinktop + h6 { border-top: none; margin-top: 0em; }
892              
893             body.pod dt {
894             font-size: 105%; /* just a wee bit more than normal */
895             }
896              
897             .indexgroup { font-size: 80%; }
898              
899             .backlinktop, .backlinkbottom {
900             margin-left: -5px;
901             margin-right: -5px;
902             background-color: #040;
903             border-top: thin solid #050;
904             border-bottom: thin solid #050;
905             }
906              
907             .backlinktop a, .backlinkbottom a {
908             text-decoration: none;
909             color: #080;
910             background-color: #000;
911             border: thin solid #0d0;
912             }
913             .backlinkbottom { margin-bottom: 0; padding-bottom: 0; }
914             .backlinktop { margin-top: 0; padding-top: 0; }
915              
916             body.contentspage {
917             color: #fff;
918             background-color: #000;
919             }
920              
921             body.contentspage h1 {
922             color: #0d0;
923             margin-left: 1em;
924             margin-right: 1em;
925             text-indent: -.9em;
926             font-family: Tahoma, Verdana, Helvetica, Arial, sans-serif;
927             font-weight: normal;
928             border-top: thin solid #fff;
929             border-bottom: thin solid #fff;
930             text-align: center;
931             }
932              
933             dl.superindex > dt {
934             font-family: Tahoma, Verdana, Helvetica, Arial, sans-serif;
935             font-weight: normal;
936             font-size: 90%;
937             margin-top: .45em;
938             /* margin-bottom: -.15em; */
939             }
940             dl.superindex > dd {
941             word-spacing: .6em; /* most important rule here! */
942             }
943             dl.superindex > a:link {
944             text-decoration: none;
945             color: #fff;
946             }
947              
948             .contentsfooty {
949             border-top: thin solid #999;
950             font-size: 90%;
951             }
952              
953             }
954              
955             /* The End */
956              
957             EOCSS
958              
959             #==========================================================================
960              
961             our $JAVASCRIPT = <<'EOJAVASCRIPT';
962              
963             // From http://www.alistapart.com/articles/alternate/
964              
965             function setActiveStyleSheet(title) {
966             var i, a, main;
967             for(i=0 ; (a = document.getElementsByTagName("link")[i]) ; i++) {
968             if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
969             a.disabled = true;
970             if(a.getAttribute("title") == title) a.disabled = false;
971             }
972             }
973             }
974              
975             function getActiveStyleSheet() {
976             var i, a;
977             for(i=0 ; (a = document.getElementsByTagName("link")[i]) ; i++) {
978             if( a.getAttribute("rel").indexOf("style") != -1
979             && a.getAttribute("title")
980             && !a.disabled
981             ) return a.getAttribute("title");
982             }
983             return null;
984             }
985              
986             function getPreferredStyleSheet() {
987             var i, a;
988             for(i=0 ; (a = document.getElementsByTagName("link")[i]) ; i++) {
989             if( a.getAttribute("rel").indexOf("style") != -1
990             && a.getAttribute("rel").indexOf("alt") == -1
991             && a.getAttribute("title")
992             ) return a.getAttribute("title");
993             }
994             return null;
995             }
996              
997             function createCookie(name,value,days) {
998             if (days) {
999             var date = new Date();
1000             date.setTime(date.getTime()+(days*24*60*60*1000));
1001             var expires = "; expires="+date.toGMTString();
1002             }
1003             else expires = "";
1004             document.cookie = name+"="+value+expires+"; path=/";
1005             }
1006              
1007             function readCookie(name) {
1008             var nameEQ = name + "=";
1009             var ca = document.cookie.split(';');
1010             for(var i=0 ; i < ca.length ; i++) {
1011             var c = ca[i];
1012             while (c.charAt(0)==' ') c = c.substring(1,c.length);
1013             if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
1014             }
1015             return null;
1016             }
1017              
1018             window.onload = function(e) {
1019             var cookie = readCookie("style");
1020             var title = cookie ? cookie : getPreferredStyleSheet();
1021             setActiveStyleSheet(title);
1022             }
1023              
1024             window.onunload = function(e) {
1025             var title = getActiveStyleSheet();
1026             createCookie("style", title, 365);
1027             }
1028              
1029             var cookie = readCookie("style");
1030             var title = cookie ? cookie : getPreferredStyleSheet();
1031             setActiveStyleSheet(title);
1032              
1033             // The End
1034              
1035             EOJAVASCRIPT
1036              
1037 1     1   3 sub _css_template { return $CSS }
1038 1     1   2 sub _javascript { return $JAVASCRIPT }
1039              
1040             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1041             1;
1042             __END__