App-Basis-ConvertText2

 view release on metacpan or  search on metacpan

lib/App/Basis/ConvertText2.pm  view on Meta::CPAN


        # strip out any HTML comments that may have come in from template
        $html =~ s/<!--.*?-->//gsm;

        $self->{output} = $html;
        $self->_store_cache( $cachefile, $html );
    }
    return $self->{output};
}

# ----------------------------------------------------------------------------

=item save_to_file

save the created html to a named file

B<Parameters>  
    filename    filename to store/convert stored HTML into
    pdfconvertor   indicate that we should use prince or wkhtmltopdf to create PDF

=cut

sub save_to_file {
    state $counter = 0;
    my $self = shift;
    my ( $filename, $pdfconvertor ) = @_;
    my ($format) = ( $filename =~ /\.(\w+)$/ );    # get last thing after a '.'
    if ( !$format ) {
        warn "Could not determine outpout file format, using PDF";
        $format = '.pdf';
    }

    my $f = $self->_md5id() . ".html";

    # have we got the parsed data
    my $cf = cachefile( $self->cache_dir, $f );
    if ( !$self->{output} ) {
        die "parse has not been run yet";
    }

    if ( !-f $cf ) {
        if ( !$self->use_cache() ) {

            # create a file name to store the output to
            $cf = "/tmp/" . get_program() . "$$." . $counter++;
        }

        # either update the cache, or create temp file
        path($cf)->spew_utf8( encode_utf8( $self->{output} ) );
    }

    my $outfile = $cf;
    $outfile =~ s/\.html$/.$format/i;

    # if the marked-up file is more recent than the converted one
    # then we need to convert it again
    if ( $format !~ /html/i ) {

        # as we can generate PDF using a number of convertors we should
        # always regenerate PDF output incase the convertor used is different
        if ( !-f $outfile || $format =~ /pdf/i || ( ( stat($cf) )[9] > ( stat($outfile) )[9] ) ) {
            $outfile = $self->_convert_file( $cf, $format, $pdfconvertor );

            # if we failed to convert, then clear the filename
            if ( !$outfile || !-f $outfile ) {
                $outfile = undef;
                debug( "ERROR", "failed to create output file from cached file $cf" );
            }
        }
    }

    my $status = 0;

    # now lets copy it to its final resting place
    if ($outfile) {
        try {
            $status = path($outfile)->copy($filename);
        }
        catch {
            say STDERR "$_ ";
            debug( "ERROR", "failed to copy $outfile to $filename" );
        };
    }
    return $status;
}

=back

=cut

# ----------------------------------------------------------------------------

1;

__END__



( run in 0.707 second using v1.01-cache-2.11-cpan-39bf76dae61 )