package Pod::HtmlPsPdf::Book; use strict; use File::Copy; use Pod::HtmlPsPdf::Config (); use Pod::HtmlPsPdf::Common (); use Pod::HtmlPsPdf::RunTime (); use Pod::HtmlPsPdf::Chapter (); my $config = Pod::HtmlPsPdf::Config->new(); ######## sub new{ my $class = shift; # create the build directories if they are needed and missing my $rel_root = $config->get_param('rel_root'); my $split_root = $config->get_param('split_root'); my $ps_root = $config->get_param('ps_root'); my $dir_mode = $config->get_param('dir_mode'); mkdir $rel_root,$dir_mode unless -e $rel_root; mkdir $split_root, $dir_mode if $Pod::HtmlPsPdf::RunTime::options{split_html} and !-e $split_root ; mkdir $ps_root, $dir_mode if $Pod::HtmlPsPdf::RunTime::options{generate_ps} and !-e $ps_root ; my $rh_main_toc = ''; my $toc_file = $config->get_param('toc_file'); # it just saves me time when I change only a few files and don't # want to wait for everything to get rebuilt $rh_main_toc = ( Pod::HtmlPsPdf::RunTime::has_storable_module() and !$Pod::HtmlPsPdf::RunTime::options{makefile_mode} and -e $toc_file) ? Storable::retrieve($toc_file) : {}; # when we will finish to parse the pod files, %valid_anchors will # include all the valid anchors # $valid_anchors{target_page#anchor} = "title"; # when we will finish to parse the pod files, %links_to_check will # include all the crossreference links # $links_to_check{srcpage}[3] = "target_page#anchor"; my $self = bless { rh_valid_anchors => {}, rh_links_to_check => {}, some_modified => {}, # keep track of which type (html, split, ps) was modified. rh_main_toc => $rh_main_toc, # Main Table of Contents for index.html }, ref($class)||$class; return $self; } # end of sub new # ############## sub get_param{ my $self = shift; return () unless @_; return unless defined wantarray; my @values = map {defined $self->{$_} ? $self->{$_} : ''} @_; return wantarray ? @values : $values[0]; } # end of sub get_param ################### # These are used to check last modified times and determine when an action is required # Where these still fail is in checking for modified template files sub pod_newer { my ( $file, $dir_root, $tmpl ) = @_; my $src = $config->get_param('src_root') . "/$file.pod"; my $dest = $config->get_param( $dir_root ) . "/$file.html"; return "F:$dest" if $Pod::HtmlPsPdf::RunTime::options{rebuild_all}; return "C:$dest" unless -e $dest; return "U:$dest" if -M $src < -M $dest; # Check for template dependency my $template = $config->get_param( $tmpl ); if ( -M $template < -M $src ) { my $time = time; utime $time, $time, $src; return "T:$dest"; } return; } sub pod_newer_split { my $file = shift; my $src = $config->get_param('src_root') . "/$file.pod"; my $split_root = $config->get_param('split_root'); my $dest = "$split_root/$file"; return "F:$dest" if $Pod::HtmlPsPdf::RunTime::options{rebuild_all}; # Hack -- assumes index.html is same date as everything else return "C:$dest" unless -e "$dest/index.html"; return "U:$dest" if -M $src < -M "$dest/index.html"; # Check for template dependency my $template = $config->get_param( 'tmpl_page_split_html' ); if ( -M $template < -M $src ) { my $time = time; utime $time, $time, $src; return "T:$dest"; } return; } sub index_out_of_date { my ($self, $type, $root ) = @_; # Index file(s) get created if any pod file was built return 'Pod created: Updating' if $self->{some_modified}{$type}; # or if the destination doesn't exist my $rel_root = $config->get_param( $root ); return 'Create' if !-e "$rel_root/index.html"; return 'Create' if $type ne 'ps' && !-e "$rel_root/index_long.html"; # or if the version file is newer than the index file(s) # since the index file can print [VERSION] my $version_file = $config->get_param('version_file'); return 'Version changed: Updating' if -M $version_file < -M "$rel_root/index.html"; return 'Version changed: Updating' if $type ne 'ps' && -M $version_file < -M "$rel_root/index_long.html"; # Check for template dependency my $template = $config->get_param( $type eq 'ps' ? 'tmpl_index_ps' : 'tmpl_index_html' ); return 'Template changed: Updating' if -M $template < -M "$rel_root/index.html"; return 'Template changed: Updating' if $type ne 'ps' && -M $template < -M "$rel_root/index_long.html"; } #################### sub create_html_version { my $self = shift; print "+++ Processing the pod files \n"; my $src_root = $config->get_param('src_root'); my $verbose = $Pod::HtmlPsPdf::RunTime::options{verbose}; my $ra_ordered_srcs = $config->get_param('pod_files'); # strip the .pod extension my @basenames = map { s/\.pod$//; $_} @{$ra_ordered_srcs}; # Process each pod for (my $i=0; $i < @basenames; $i++) { my $file = "$basenames[$i].pod"; my $src_file = "$src_root/$file"; my $generate_ps = pod_newer( $basenames[$i], 'ps_root', 'tmpl_ps_html' ) if $Pod::HtmlPsPdf::RunTime::options{generate_ps}; my $generate_html = pod_newer( $basenames[$i], 'rel_root', 'tmpl_page_html' ); my $generate_split = pod_newer_split( $basenames[$i] ) if $Pod::HtmlPsPdf::RunTime::options{split_html}; unless ( $generate_ps || $generate_split || $generate_html ) { printf("--- %-22s: skipping (not modified)\n",$file); next; } $self->{some_modified}{html}++ if $generate_html; # to know if index needs to be rebuilt. $self->{some_modified}{ps}++ if $generate_ps; $self->{some_modified}{split}++ if $generate_split; my $rebuild_reason = join ', ', grep { $_ } $generate_ps, $generate_split, $generate_html; printf "+++ %-22s: processing ($rebuild_reason)\n",$file; # prev/next linking my $prev_page = ($i and defined $basenames[$i-1]) ? "$basenames[$i-1].html" : ''; my $next_page = ( defined $basenames[$i+1]) ? "$basenames[$i+1].html" : ''; my $curr_page = "$basenames[$i].html"; my $curr_page_index = $i+1; my $doc_root = "./"; $curr_page =~ s|^\./||; # strip the leading './' $doc_root .= "../" x ($curr_page =~ tr|/|/|); $doc_root =~ s|/$||; # remove the last '/' # adjust the links for prev/next for sources files relative to the # current doc if ($prev_page) { $prev_page =~ s|^\./||; # strip the leading './' $prev_page = "$doc_root/$prev_page"; } if ($next_page) { $next_page =~ s|^\./||; # strip the leading './' $next_page = "$doc_root/$next_page"; } # convert pod to html -- huh? When would this ever not be .pod? if ($file =~ /\.pod/) { my $chapter = Pod::HtmlPsPdf::Chapter->new( $self, $file, $src_root, $src_file, $verbose, $doc_root, $curr_page, $curr_page_index, $prev_page, $next_page, ); # podify $chapter->podify_items() if $Pod::HtmlPsPdf::RunTime::options{podify_items}; # pod2html $chapter->pod2html(); $chapter->parse_html(); $chapter->write_html_file() if $generate_html; # html for ps creation $chapter->write_ps_html_file() if $generate_ps; # html 2 split html -- this destroys the title/body/index # structures! Therefore it MUST come last, when we won't need # them anymore. Othewise you should revise this function to # use copy instead. $chapter->write_split_html_files() if $generate_split; } } # end of for (my $i=0; $i<@$ra_ordered_srcs; $i++) my $rel_root = $config->get_param('rel_root'); # create the index.html files based on the toc from each pod file. $self->write_index_html_file(); # copy non-pod files like images and stylesheets (if any are modified) $self->copy_the_rest($rel_root); } # end of sub create_html_version #################### sub copy_the_rest{ my $self = shift; my $target_root = shift; print "+++ Copying the non-pod files to $target_root\n"; my ($nonpod_files,$src_root) = $config->get_param(qw(nonpod_files src_root)); # Handle the rest of the files foreach my $file (@$nonpod_files) { # The nonpod files are relative to the current directory now. my $src = "$src_root/$file"; my $dst = "$target_root/$file"; my $base_name = File::Basename::basename $dst; my $forced = $Pod::HtmlPsPdf::RunTime::options{rebuild_all} ? ' / forced' : ''; if ( !$forced && -e $dst && -M $src > -M $dst ) { printf("--- %-22s: skipping (not modified)\n",$base_name); } else { $forced ||= 'modified'; Pod::HtmlPsPdf::Common::copy_file( $src, $dst ); printf("+++ %-22s: processing (%s)\n",$base_name, $forced); } } } # end of sub copy_the_rest # generate the PS version #################### sub create_ps_version{ my $self = shift; print "+++ Starting the Generation of a PostScript Book version\n"; $self->make_indexps_file(); # copy non-pod files like images and stylesheets # must copy these before PS gets generated my $ps_root = $config->get_param('ps_root'); $self->copy_the_rest($ps_root); print "+++ Generating a PostScript Book version\n"; my $html2ps_exec = $config->get_param('html2ps_exec'); my $html2ps_conf = $config->get_param('html2ps_conf'); my $ra_ordered_srcs = $config->get_param('pod_files'); my $out_name = $config->get_param('out_name'); my $command = "$html2ps_exec -f $html2ps_conf -o $ps_root/${out_name}.ps "; $command .= join " ", map {"$ps_root/$_.html"} "index", @$ra_ordered_srcs; print "Doing $command\n" if $Pod::HtmlPsPdf::RunTime::options{verbose}; print "Doing $command\n"; system $command; } # end of sub create_ps_version # generate the PDF version #################### sub create_pdf_version{ my $self = shift; print "+++ Converting from PS format to PDF format\n"; my $ps_root = $config->get_param('ps_root'); my $out_name = $config->get_param('out_name'); my $command = "ps2pdf $ps_root/$out_name.ps $ps_root/$out_name.pdf"; print "Doing $command\n" if $Pod::HtmlPsPdf::RunTime::options{verbose}; system $command; } # end of sub create_pdf_version # generate the PDF version #################### sub create_split_html_version{ my $self = shift; print "+++ Completing the Split HTML version\n"; # copy non-pod files like images and stylesheets my $split_root = $config->get_param('split_root'); $self->copy_the_rest($split_root); $self->write_split_index_html_file() } # end of sub create_split_html_version # Using the same template file create the long and the short index # html files ################### sub write_index_html_file{ my $self = shift; # Make sure there's something to do my $update_reason = $self->index_out_of_date( 'html', 'rel_root' ); unless ( $update_reason ) { print "--- HTML index files: (not modified)\n"; return; } my $ra_ordered_srcs = $config->get_param('pod_files'); my @ordered_srcs = @$ra_ordered_srcs; # build a complete toc my $main_long_toc = join "\n", map { s/$/.html/; $self->{rh_main_toc}->{$_} ||= '' } @ordered_srcs; my $main_short_toc = join "\n", map { $self->{rh_main_toc}->{$_} =~ s|
|$1|ism; $self->{rh_main_toc}->{$_} =~ s^
^^gism; $self->{rh_main_toc}->{$_} } @ordered_srcs; # $self->{rh_main_toc}->{$_} =~ s^||
^^gism;
# $self->{rh_main_toc}->{$_} =~ s^