App-Basis-ConvertText2

 view release on metacpan or  search on metacpan

bin/ct2  view on Meta::CPAN

my ( $story, $basedir );
if ( $opt{filename} eq '-' ) {
    $story = do { local $/; <STDIN> };
    $basedir = Path::Tiny->cwd;
}
else {
    $story   = path( $opt{filename} )->slurp_utf8;
    $basedir = path( $opt{filename} )->dirname;
}

show_usage("Bad markup file $opt{filename}") if ( !$story );

# anything in the replace hash will get replaced in the final document
my $replace = {

    # '%TITLE%'   => '',    # this comes from the first markdown level 1 header
    # '%DATE%' => strftime( "%Y-%m-%d", gmtime() ),    # :date in document overrides
    # '%COPYRIGHT%'   => $settings->{config}->get("copyright")        || '',    # :copyright in document overrides
    # '%AUTHOR%'      => $settings->{config}->get("author")           || '',    # :author in document overrides
    # '%PAGE_SIZE%'   => $settings->{config}->get("page/size")        || '',
    # '%ORIENTATION%' => $settings->{config}->get("page/orientation") || '',
    # '%KEYWORDS%' => '',   # get from document :keywords or :tags
    # '%SUMMARY%' => '',   # get from document :summary
    # '%REVISION%' => '',   # get from document :revision
};

# get any template from the stop of the story
my $settings;
my ($template) = ( $story =~ /^template:\s?(.*?)$/sm );

# document template overwritten by the command line option
$template = $opt{template} if ( $opt{template} );
$settings = read_settings( $template, $opt{config_dir} );

# add in template defaults if needed
$replace->{DATE} ||= strftime( "%Y-%m-%d", gmtime() );
$replace->{COPYRIGHT}   ||= $settings->{config}->get("copyright");
$replace->{AUTHOR}      ||= $settings->{config}->get("author");
$replace->{PAGE_SIZE}   ||= $settings->{config}->get("page/size");
$replace->{ORIENTATION} ||= $settings->{config}->get("page/orientation");

my $format = App::Basis::ConvertText2->new(
    name      => get_program(),
    basedir   => $basedir,
    use_cache => 1,
    cache_dir => $CACHE_DIR,
    template  => $settings->{template},
    replace   => $replace,
    verbose   => $opt{verbose},
    embed     => $opt{embed},
);
$format->clean_cache() if ( $opt{clean} );

# be in the same dir as the input file in case there are an files in plugins
# that act on relative paths
my $current = Path::Tiny->cwd;
chdir($basedir);

my $data = $format->parse($story);

# decide on output filename from any format keyword
# all the keywords are in UPPER-CASE
my $keywords = $format->replace;
if ( !$opt{output} && $keywords->{FORMAT} ) {

    # same name as input
    $opt{output} = $opt{filename};

    # change extension
    $opt{output} =~ s/\.md$/.$keywords->{FORMAT}/i;
}

if ( $opt{output} ) {
    my $pdfconvertor;

    if ( $opt{prince} ) {
        $pdfconvertor = 'prince';
    }
    elsif ($opt{wkhtmltopdf})  {
        $pdfconvertor = 'wkhtmltopdf';
    }

    my $status = $format->save_to_file( $opt{output}, $pdfconvertor);

    if ( $opt{verbose} && $status ) {
        say "Created $opt{output}";
    }
    elsif ( !$status ) {
        say "Failed to create $opt{output}";
    }
}
else {
    say STDERR "Ignoring $opt{filename}, could not determine a filename to output to, no :format option in file?" if ( $opt{verbose} );
}

# return to where we came from
chdir($current);



( run in 1.511 second using v1.01-cache-2.11-cpan-e1769b4cff6 )