App-Changelog2x

 view release on metacpan or  search on metacpan

bin/changelog2x  view on Meta::CPAN


# Get a App::Changelog2x instance to work with
$app = App::Changelog2x->new();

# Add ourself to the "credits" list:
$app->application_tokens("$cmd/$VERSION");

# The default root for the templates is in a changelog2x directory that sits
# below the installation of App::Changelog2x.pm. We get it through a pseudo-
# accessor in that package. We actually only need it to generate the list of
# available formats for the --help option. Actual resolving of template names
# that aren't absolute paths is handled within App::Changelog2x.
$app->xslt_path(@{$opts{templateroot}}) if $opts{templateroot};

# We actually needed that option *before* handling the "help" option, so that
# we can scan the template-root directory for available formats:
if ($opts{help})
{
    my @paths = $app->xslt_path;
    my $paths = join("\n\t", @paths);
    my @formats = formats_list @paths;
    my $plural = (1 < @paths) ? 's' : '';

    print "$USAGE
Type 'man $cmd' for full manual page and stylesheet options.

Template search path$plural:
\t$paths

Available values for the --template option:
\t@formats\n";

    exit 0;
}

# The default template is "html".
$opts{template} ||= 'html';

# If either of headcontentfile or bodycontentfile have been specified, they
# override the simpler options (if they were even set at all).
for my $part (qw(head body))
{
    if ($opts{"${part}contentfile"})
    {
        open(my $fh, "< $opts{'${part}contentfile'}") or
            die "Error opening $opts{'${part}contentfile'} for reading: $!";
        $opts{"${part}content"} = join('', <$fh>);
    }
}

# If the user specified a date format to use, set it
$app->date_format($opts{format}) if $opts{format};

# Set up the input/output sinks, allowing for '-' or no specification to
# default to STDIN/STDOUT
our ($ifh, $ofh);
if ($opts{input} && ($opts{input} ne '-'))
{
    open($ifh, "< $opts{input}") or
        die "Error opening $opts{output} for writing: $!";
    binmode $ifh; # In case someone tried to sneak a PerlIO layer in
}
else
{
    $ifh = \*STDIN;
}
if ($opts{output} && ($opts{output} ne '-'))
{
    open($ofh, "> $opts{output}") or
        die "Error opening $opts{output} for writing: $!";
}
else
{
    $ofh = \*STDOUT;
}

# Though the stylesheet processor will quietly ignore unknown parameters, I
# prefer to only pass through those params that are for the stylesheet. Drop
# the application parameters:
my %params = map { $opts{$_} ? ($_ => $opts{$_}) : () } (STYLESHEET_OPTS);
# And copy over any user-specified "extra" XSLT parameters:
if ($opts{xsltparam} and ref($opts{xsltparam}))
{
    for (keys %{$opts{xsltparam}})
    {
        $params{$_} = $opts{xsltparam}->{$_};
    }
}
$app->transform_changelog($ifh, $ofh, $opts{template}, \%params);

exit 0;

###############################################################################
#
#   Sub Name:       formats_list
#
#   Description:    List the available formats/stylesheets in the default
#                   directory of XSLT files.
#
#   Arguments:      NAME      IN/OUT  TYPE      DESCRIPTION
#                   @dirs     in      list      Directories to scan for files
#
#   Globals:        None.
#
#   Environment:    None.
#
#   Returns:        Success:    1
#                   Failure:    0
#
###############################################################################
sub formats_list
{
    my @dirs = @_;

    my (%seen, @list);

    foreach my $dir (@dirs)
    {
        opendir(my $dh, $dir) or die "Error opening $dir for reading: $!";

        @list = grep(/^changelog2.*\.xslt$/, readdir($dh));



( run in 2.570 seconds using v1.01-cache-2.11-cpan-524268b4103 )