App-Chronicle

 view release on metacpan or  search on metacpan

lib/Chronicle/Plugin/Snippets/Meta.pm  view on Meta::CPAN

The C<on_initiate> method is automatically invoked just before any
C<on_generate> methods which might be present.

This method updates the global variables, which are made available to
all loaded templates to define meta variables such as C<chronicle_version>
containing the current release number, and C<build_date> and C<build_time> containing the date and time of when the blog was (re)build (respectivley).

=cut

sub on_initiate
{
    my ( $self, %args ) = (@_);

    my $dbh    = $args{ 'dbh' };
    my $config = $args{ 'config' };


    #
    #  The chronicle version
    #
    $Chronicle::GLOBAL_TEMPLATE_VARS{ "chronicle_version" } =
      $Chronicle::VERSION;

    #
    #  The homepage for Chronicle
    #
    $Chronicle::GLOBAL_TEMPLATE_VARS{ "chronicle_link" } =
      "https://steve.fi/Software/chronicle";

    my $time = time;

    #
    #  The chronicle build date
    #
    (  $Chronicle::GLOBAL_TEMPLATE_VARS{ "build_date" },
       $Chronicle::GLOBAL_TEMPLATE_VARS{ "build_date_loc" } )
      = format_datetime( $config, 'meta_date_format', '%e %b %Y', $time );

    #
    #  The chronicle build time
    #
    (  $Chronicle::GLOBAL_TEMPLATE_VARS{ "build_time" },
       $Chronicle::GLOBAL_TEMPLATE_VARS{ "build_time_loc" } )
      = format_datetime( $config, 'meta_time_format', '%X', $time );

    #
    #  The username
    #
    if ( $ENV{ 'USER' } )
    {

        #
        #  Set the username
        #
        $Chronicle::GLOBAL_TEMPLATE_VARS{ "build_username" } = $ENV{ 'USER' };


        #
        #  If we can expand that into a full-name then do so
        #
        my ( $name,    $passwd, $uid, $gid,   $quota,
             $comment, $gcos,   $dir, $shell, $expire )
          = getpwnam( $ENV{ 'USER' } );

        #
        #  Did we get a GCOS field?  If so strip the trailing "," and
        # set if it is non-empty
        #
        if ($gcos)
        {
            $gcos =~ s/,+$//g;

            $Chronicle::GLOBAL_TEMPLATE_VARS{ "build_fullname" } = $gcos
              if ( $gcos && length($gcos) );
        }
    }


    #
    #  Now try to populate the hostname of the build-system too.
    #
    #  Again we start from the environment, then run `hostname` if the
    # environment isn't set.
    #
    my $hostname = $ENV{ 'HOSTNAME' };
    if ( !$hostname )
    {

        #
        #  If not we'll look for a hostname via the Sys::Hostname module.
        #
        $hostname = Sys::Hostname::hostname();

        #
        #  The previous line will have probably returned a short-name.
        #
        #  Try to expand it into FQDN.  If that fails - well at least we tried.
        #
        my @values = ( gethostbyname($hostname) );
        if ( scalar @values )
        {
            $hostname = $values[0];
        }
    }

    if ($hostname)
    {

        #
        #  If the hostname is qualified
        #
        if ( $hostname =~ /^([^.]+)\.(.*)/ )
        {
            my $short  = $1;
            my $domain = $2;

            #
            #  Set the long/short versions
            #
            $Chronicle::GLOBAL_TEMPLATE_VARS{ "build_host_long" }  = $hostname;
            $Chronicle::GLOBAL_TEMPLATE_VARS{ "build_host_short" } = $short;



( run in 1.157 second using v1.01-cache-2.11-cpan-ceb78f64989 )