<commit_before>package Combust::Template;
use strict;

use Template;
use Template::Parser;
use Template::Stash;

# use Template::Constants split /\|/, 'DEBUG_VARS|DEBUG_DIRS|DEBUG_STASH|DEBUG_PARSER|DEBUG_PROVIDER|DEBUG_SERVICE|DEBUG_CONTEXT';

use Carp qw(croak);

use Combust::Config;

use Combust::Template::Provider;
use Combust::Template::Filters;
use Combust::Template::Translator::POD;

my $config = Combust::Config->new();
my $root   = $config->root;

$Template::Config::STASH = 'Template::Stash::XS';

$Template::Stash::SCALAR_OPS->{rand} = sub {
    return int( rand(shift) );
};

sub new {
    my $class = shift;
    my $obj   = bless {}, $class;
    return $obj->_init(@_);
}

sub _init {
    my $self = shift; 
    my %args = ref $_[0] ? %{ $_[0] } : @_;

    my $parser = Template::Parser->new();

    my %provider_config = (
        PARSER      => $parser,
        COMPILE_EXT => '.ttc',
        COMPILE_DIR => $config->work_path . "/ctpl",
        UNICODE     => 1,
        ENCODING    => 'utf-8',

        #TOLERANT => 1,
        #RELATIVE => 1,
        CACHE_SIZE => 128,    # cache 128 templates
        EXTENSIONS => [
            {   extension  => "pod",
                translator => Combust::Template::Translator::POD->new()
            },
        ],

    );

    my $provider =
      Combust::Template::Provider->new( %provider_config,
        INCLUDE_PATH => [ sub { $self->get_include_path } ], );

    my %tt_config = (
        FILTERS =>
          { 'navigation' => [ \&Combust::Template::Filters::navigation_filter_factory, 1 ],
            $args{filters} ? %{$args{filters}} : ()
          },
        RELATIVE       => 1,
        LOAD_TEMPLATES => [$provider],

        #'LOAD_TEMPLATES' => [ $file, $http ],
        #PREFIX_MAP => {
        #               file => 0,
        #               http => 1,
        #		    default => 1,
        #	            },
        'PRE_PROCESS' => 'tpl/defaults',
        'PROCESS'     => 'tpl/wrapper',
        'PLUGIN_BASE' => 'Combust::Template::Plugin',
        # 'DEBUG'  => DEBUG_VARS | DEBUG_DIRS | DEBUG_STASH
        #             | DEBUG_PARSER | DEBUG_PROVIDER | DEBUG_SERVICE
        #             | DEBUG_CONTEXT,
    );

    if ( $config->template_timer ) {
        require Template::Timer;
        $tt_config{CONTEXT} = Template::Timer->new(%tt_config);
    }

    $self->{provider} = $provider;

    $self->{tt} = Template->new( \%tt_config )<commit_msg>Fix memory leak due to circular reference<commit_after>package Combust::Template;
use strict;

use Template;
use Template::Parser;
use Template::Stash;

# use Template::Constants split /\|/, 'DEBUG_VARS|DEBUG_DIRS|DEBUG_STASH|DEBUG_PARSER|DEBUG_PROVIDER|DEBUG_SERVICE|DEBUG_CONTEXT';

use Carp qw(croak);
use Scalar::Util;

use Combust::Config;

use Combust::Template::Provider;
use Combust::Template::Filters;
use Combust::Template::Translator::POD;

my $config = Combust::Config->new();
my $root   = $config->root;

$Template::Config::STASH = 'Template::Stash::XS';

$Template::Stash::SCALAR_OPS->{rand} = sub {
    return int( rand(shift) );
};

sub new {
    my $class = shift;
    my $obj   = bless {}, $class;
    return $obj->_init(@_);
}

sub _init {
    my $self = shift; 
    my %args = ref $_[0] ? %{ $_[0] } : @_;

    my $parser = Template::Parser->new();

    my %provider_config = (
        PARSER      => $parser,
        COMPILE_EXT => '.ttc',
        COMPILE_DIR => $config->work_path . "/ctpl",
        UNICODE     => 1,
        ENCODING    => 'utf-8',

        #TOLERANT => 1,
        #RELATIVE => 1,
        CACHE_SIZE => 128,    # cache 128 templates
        EXTENSIONS => [
            {   extension  => "pod",
                translator => Combust::Template::Translator::POD->new()
            },
        ],

    );

    Scalar::Util::weaken(my $weak_self = $self);
    my $provider =
      Combust::Template::Provider->new( %provider_config,
        INCLUDE_PATH => [ sub { $weak_self->get_include_path } ], );

    my %tt_config = (
        FILTERS =>
          { 'navigation' => [ \&Combust::Template::Filters::navigation_filter_factory, 1 ],
            $args{filters} ? %{$args{filters}} : ()
          },
        RELATIVE       => 1,
        LOAD_TEMPLATES => [$provider],

        #'LOAD_TEMPLATES' => [ $file, $http ],
        #PREFIX_MAP => {
        #               file => 0,
        #               http => 1,
        #		    default => 1,
        #	            },
        'PRE_PROCESS' => 'tpl/defaults',
        'PROCESS'     => 'tpl/wrapper',
        'PLUGIN_BASE' => 'Combust::Template::Plugin',
        # 'DEBUG'  => DEBUG_VARS | DEBUG_DIRS | DEBUG_STASH
        #             | DEBUG_PARSER | DEBUG_PROVIDER | DEBUG_SERVICE
        #             | DEBUG_CONTEXT,
    );

    if ( $config->template_timer ) {
        require Template::Timer;
        $tt_config{CONTEXT} = Template::Timer->new(%tt_config);
    }

    $self->{provider} = $provider;

    $self->{tt} = Template->new( \%tt_config )
{'fraction_non_alphanumeric': 0.16438631790744468, 'fraction_numerical': 0.006841046277665996, 'mean_word_length': 2.259672131147541, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4002591', 'n_tokens_mistral': 1691, 'n_tokens_neox': 1621, 'n_words': 297}