<commit_before>package Eldhelm::Util::Template;

use strict;
use Data::Dumper;
use Eldhelm::Server::Parser::Json;
use Eldhelm::Util::Factory;
use Carp;

sub new {
	my ($class, %args) = @_;
	my $self = {
		rootPath => $args{rootPath},
		name     => $args{name},
		params   => $args{params} || {},
		var      => {},
		function => {},
		block    => {},
	};
	bless $self, $class;

	$self->parse if $self->{name};

	return $self;
}

sub getPath {
	my ($self, $name) = @_;
	my $relPath = "Eldhelm/Application/Template/".join("/", split(/\./, $name)).".tpl";
	return $self->{rootPath}.$relPath if $self->{rootPath};
	return Eldhelm::Util::Factory->getAbsoluteClassPath($relPath) || $relPath;
}

sub load {
	my ($self, $name) = @_;
	my $path = $self->getPath($name || $self->{name});
	open FR, $path or confess "Can not load '$path' with params: ".Dumper($self->{params})."$!";
	my $src = $self->{source} = join "", <FR>;
	close FR;
	return $src;
}

sub parse {
	my ($self, $stream) = @_;

	$self->{source} = $self->parseSource($stream || $self->load);

	my $fns = $self->{function};
	$self->extend($fns->{extends}) if $fns->{extends};

	return $self;
}

sub parseSource {
	my ($self, $source) = @_;
	return unless $source;

	my $blocks = $self->{block};
	$source =~
		s/\{block\s+(.+?)\s*\}(.*?)\{block\}/$blocks->{$1} = $self->parseSource($2); ";;~~eldhelm~template~placeholder~block~block~$1~~;;"/gei;

	my $vars = $self->{var};
	$source =~ s/\{([a-z][a-z0-9_\.]*)\|(.+?)\}/$vars->{$1} = $2; ";;~~eldhelm~template~placeholder~var~var~$1~~;;"/gei;
	$source =~ s/\{([a-z][a-z0-9_\.]*)\}/$vars->{$1} = undef; ";;~~eldhelm~template~placeholder~var~var~$1~~;;"/gei;

	my $fns = $self->{function};
	my $i   = "";
	$source =~ s/\{([a-z][a-z0-9_]*)(:?[a-z0-9_]*)[\t\s\r\n]+(.+?)\}/ 
		my $fn = $1; 
		my $nm = $2 || "$fn$i";
		$i++;
		(my $args = $3) =~ s|[\n\r\t]||g;
		$fns->{$nm} = $args;
		";;~~eldhelm~template~placeholder~function~$fn~$nm~~;;"
	/geis;

	return $source;
}

sub extend {
	my ($self, $name) = @_;

	my $tpl = Eldhelm::Util::Template->new(<commit_msg>Fixing a bug causing template blocks to be single line only<commit_after>package Eldhelm::Util::Template;

use strict;
use Data::Dumper;
use Eldhelm::Server::Parser::Json;
use Eldhelm::Util::Factory;
use Carp;

sub new {
	my ($class, %args) = @_;
	my $self = {
		rootPath => $args{rootPath},
		name     => $args{name},
		params   => $args{params} || {},
		var      => {},
		function => {},
		block    => {},
	};
	bless $self, $class;

	$self->parse if $self->{name};

	return $self;
}

sub getPath {
	my ($self, $name) = @_;
	my $relPath = "Eldhelm/Application/Template/".join("/", split(/\./, $name)).".tpl";
	return $self->{rootPath}.$relPath if $self->{rootPath};
	return Eldhelm::Util::Factory->getAbsoluteClassPath($relPath) || $relPath;
}

sub load {
	my ($self, $name) = @_;
	my $path = $self->getPath($name || $self->{name});
	open FR, $path or confess "Can not load '$path' with params: ".Dumper($self->{params})."$!";
	my $src = $self->{source} = join "", <FR>;
	close FR;
	return $src;
}

sub parse {
	my ($self, $stream) = @_;

	$self->{source} = $self->parseSource($stream || $self->load);

	my $fns = $self->{function};
	$self->extend($fns->{extends}) if $fns->{extends};

	return $self;
}

sub parseSource {
	my ($self, $source) = @_;
	return unless $source;

	my $blocks = $self->{block};
	$source =~
		s/\{block\s+(.+?)\s*\}(.*?)\{block\}/$blocks->{$1} = $self->parseSource($2); ";;~~eldhelm~template~placeholder~block~block~$1~~;;"/gsei;

	my $vars = $self->{var};
	$source =~ s/\{([a-z][a-z0-9_\.]*)\|(.+?)\}/$vars->{$1} = $2; ";;~~eldhelm~template~placeholder~var~var~$1~~;;"/gei;
	$source =~ s/\{([a-z][a-z0-9_\.]*)\}/$vars->{$1} = undef; ";;~~eldhelm~template~placeholder~var~var~$1~~;;"/gei;

	my $fns = $self->{function};
	my $i   = "";
	$source =~ s/\{([a-z][a-z0-9_]*)(:?[a-z0-9_]*)[\t\s\r\n]+(.+?)\}/ 
		my $fn = $1; 
		my $nm = $2 || "$fn$i";
		$i++;
		(my $args = $3) =~ s|[\n\r\t]||g;
		$fns->{$nm} = $args;
		";;~~eldhelm~template~placeholder~function~$fn~$nm~~;;"
	/geis;

	return $source;
}

sub extend {
	my ($self, $name) = @_;

	my $tpl = Eldhelm::Util::Template->new(
{'fraction_non_alphanumeric': 0.2874309060322038, 'fraction_numerical': 0.0091324200913242, 'mean_word_length': 4.020506634499397, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 67, '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': '1978224', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1761, 'n_words': 325}