...
 
Commits (27)
Revision history for perl module Text::Hogan
1.09 2019-02-23
* Loads of great Perlish refactors from Yanick Champoux <3
1.08 2019-02-23
* Use Dist::Zilla::Plugin::Git::Tag for automatic git tags (just for jonassmedegaard :-))
1.07 2018-12-21
* Variable $key and $Key typo (thanks Yanick Champoux!)
......
This software is copyright (c) 2018 by Alex Balhatchet.
This software is copyright (c) 2019 by Alex Balhatchet.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
......@@ -12,7 +12,7 @@ b) the "Artistic License"
--- The GNU General Public License, Version 1, February 1989 ---
This software is Copyright (c) 2018 by Alex Balhatchet.
This software is Copyright (c) 2019 by Alex Balhatchet.
This is free software, licensed under:
......@@ -272,7 +272,7 @@ That's all there is to it!
--- The Artistic License 1.0 ---
This software is Copyright (c) 2018 by Alex Balhatchet.
This software is Copyright (c) 2019 by Alex Balhatchet.
This is free software, licensed under:
......
......@@ -17,7 +17,7 @@
"configure" : {
"requires" : {
"ExtUtils::MakeMaker" : "0",
"perl" : "5.006"
"perl" : "v5.10.0"
}
},
"develop" : {
......@@ -31,7 +31,7 @@
"Clone" : "0.37",
"Scalar::Util" : "1.41",
"Text::Trim" : "1.02",
"perl" : "5.006"
"perl" : "v5.10.0"
}
},
"test" : {
......@@ -41,7 +41,7 @@
"Test::More" : "1.001008",
"Try::Tiny" : "0.22",
"YAML" : "1.13",
"perl" : "5.006"
"perl" : "v5.10.0"
}
}
},
......@@ -56,7 +56,7 @@
"web" : "http://github.com/kaoru/Text-Hogan"
}
},
"version" : "1.07",
"version" : "1.09",
"x_serialization_backend" : "Cpanel::JSON::XS version 3.0214"
}
......@@ -8,10 +8,10 @@ build_requires:
Test::More: '1.001008'
Try::Tiny: '0.22'
YAML: '1.13'
perl: '5.006'
perl: v5.10.0
configure_requires:
ExtUtils::MakeMaker: '0'
perl: '5.006'
perl: v5.10.0
dynamic_config: 0
generated_by: 'Dist::Zilla version 6.008, CPAN::Meta::Converter version 2.150010'
license: perl
......@@ -23,9 +23,9 @@ requires:
Clone: '0.37'
Scalar::Util: '1.41'
Text::Trim: '1.02'
perl: '5.006'
perl: v5.10.0
resources:
bugtracker: https://github.com/kaoru/Text-Hogan/issues
repository: git://github.com/kaoru/Text-Hogan
version: '1.07'
version: '1.09'
x_serialization_backend: 'YAML::Tiny version 1.69'
......@@ -2,7 +2,7 @@
use strict;
use warnings;
use 5.006;
use 5.010000;
use ExtUtils::MakeMaker;
......@@ -14,7 +14,7 @@ my %WriteMakefileArgs = (
},
"DISTNAME" => "Text-Hogan",
"LICENSE" => "perl",
"MIN_PERL_VERSION" => "5.006",
"MIN_PERL_VERSION" => "5.010000",
"NAME" => "Text::Hogan",
"PREREQ_PM" => {
"Clone" => "0.37",
......@@ -28,7 +28,7 @@ my %WriteMakefileArgs = (
"Try::Tiny" => "0.22",
"YAML" => "1.13"
},
"VERSION" => "1.07",
"VERSION" => "1.09",
"test" => {
"TESTS" => "t/*.t"
}
......
This archive contains the distribution Text-Hogan,
version 1.07:
version 1.09:
Text::Hogan - A mustache templating engine statement-for-statement cloned from hogan.js
This software is copyright (c) 2018 by Alex Balhatchet.
This software is copyright (c) 2019 by Alex Balhatchet.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
......
libtext-hogan-perl (1.09-1) unstable; urgency=medium
[ upstream ]
* New release(s).
[ Jonas Smedegaard ]
* Update copyright info:
+ Bump (yes not extend) coverage for main upstream author.
-- Jonas Smedegaard <dr@jones.dk> Mon, 25 Feb 2019 17:38:23 +0100
libtext-hogan-perl (1.07-1) unstable; urgency=medium
[ upstream ]
......
......@@ -5,7 +5,7 @@ Source: https://github.com/kaoru/Text-Hogan
https://github.com/kaoru/Text-Hogan
Files: *
Copyright: 2018, Alex Balhatchet <alex@balhatchet.net>
Copyright: 2019, Alex Balhatchet <alex@balhatchet.net>
License-Grant:
This is free software;
you can redistribute it and/or modify it
......
......@@ -39,7 +39,7 @@ License: UNKNOWN
FIXME
Files: README
Copyright: 2018, Alex Balhatchet.
Copyright: 2019, Alex Balhatchet.
License: Artistic or GPL-1+
FIXME
......@@ -47,7 +47,7 @@ Files: LICENSE
Copyright: 1989, Free Software Foundation, Inc.
19xx name of author
19yy <name of author>
2018, Alex Balhatchet.
2019, Alex Balhatchet.
disclaimer" for the program, if
ed by the Free
interest in the
......
name = Text-Hogan
version = 1.07
version = 1.09
abstract = Text::Hogan - A mustache templating engine statement-for-statement cloned from hogan.js
author = Alex Balhatchet
......@@ -36,6 +36,10 @@ bugtracker.web = https://github.com/kaoru/Text-Hogan/issues
[ExtraTests]
[Test::CPAN::Changes]
[Git::Tag]
tag_format = v%v
tag_message = ; use lightweight tags
[FileFinder::Filter / MyTestsSkipYML]
finder = :TestFiles
skip = .yml$
......
package Text::Hogan;
$Text::Hogan::VERSION = '1.07';
$Text::Hogan::VERSION = '1.09';
use strict;
use warnings;
......@@ -13,7 +13,7 @@ Text::Hogan - A mustache templating engine statement-for-statement cloned from h
=head1 VERSION
version 1.07
version 1.09
=head1 DESCRIPTION
......
package Text::Hogan::Compiler;
$Text::Hogan::Compiler::VERSION = '1.07';
$Text::Hogan::Compiler::VERSION = '1.09';
use Text::Hogan::Template;
use 5.10.0;
use strict;
use warnings;
......@@ -431,10 +432,7 @@ my %codegen = (
$context->{'code'} .= sprintf('if($t->s($t->%s("%s",$c,$p,1),$c,$p,0,%s,%s,"%s %s")) { $t->rs($c,$p,sub { my ($c,$p,$t) = @_;',
choose_method($node->{'n'}),
esc($node->{'n'}),
$node->{'i'},
$node->{'end'},
$node->{'otag'},
$node->{'ctag'}
@{$node}{qw/ i end otag ctag /},
);
walk($node->{'nodes'}, $context);
$context->{'code'} .= '}); pop @$c;}';
......@@ -495,18 +493,16 @@ sub triple_stache {
);
}
sub twrite {
my ($s) = @_;
return sprintf('$t->b(%s);', $s);
}
sub twrite { sprintf '$t->b(%s);', @_ }
sub walk {
my ($nodelist, $context) = @_;
my $func;
for (my $i = 0, my $l = scalar(@$nodelist); $i < $l; $i++) {
$func = $codegen{$nodelist->[$i]{'tag'}};
$func && $func->($nodelist->[$i], $context);
for my $node (@$nodelist) {
my $func = $codegen{$node->{'tag'}} or next;
$func->($node, $context);
}
return $context;
}
......@@ -526,9 +522,9 @@ sub cache_key {
sub compile {
my ($self, $text, $options) = @_;
$options ||= {};
if (!defined $text) {
$text = "";
}
$text //= "";
my $key = cache_key($text, $options);
my $template = $cache{$key};
......@@ -559,7 +555,7 @@ Text::Hogan::Compiler - parse templates and output Perl code
=head1 VERSION
version 1.07
version 1.09
=head1 SYNOPSIS
......
package Text::Hogan::Template;
$Text::Hogan::Template::VERSION = '1.07';
$Text::Hogan::Template::VERSION = '1.09';
use strict;
use warnings;
......@@ -7,29 +7,20 @@ use Scalar::Util qw(looks_like_number);
use Clone qw(clone);
sub new {
my $orig = shift;
my ($code_obj, $text, $compiler, $options) = @_;
my ($orig, $code_obj, $text, $compiler, $options) = @_;
$code_obj ||= {};
my $class = ref($orig) || $orig;
my $self = bless {}, $class;
$self->{'r'} = $code_obj->{'code'} || (ref($orig) && $orig->{'r'});
$self->{'c'} = $compiler;
$self->{'options'} = $options || {};
$self->{'text'} = $text || "";
$self->{'partials'} = $code_obj->{'partials'} || {};
$self->{'subs'} = $code_obj->{'subs'} || {};
$self->{'buf'} = "";
$self->{'numeric_string_as_string'} = 0; # by default treat numbers as strings
if ($options->{'numeric_string_as_string'}){
$self->{'numeric_string_as_string'} = 1;
}
return $self;
return bless {
r => $code_obj->{'code'} || (ref($orig) && $orig->{'r'}),
c => $compiler,
buf => "",
options => $options || {},
text => $text || "",
partials => $code_obj->{'partials'} || {},
subs => $code_obj->{'subs'} || {},
numeric_string_as_string => !!$options->{'numeric_string_as_string'},
}, ref($orig)||$orig;
}
sub r {
......@@ -42,17 +33,21 @@ sub r {
return "";
}
my %mapping = (
'&' => '&amp;',
'<' => '&lt;',
'>' => '&gt;',
q{'} => '&#39;',
'"' => '&quot;',
);
sub v {
my ($self, $str) = @_;
$str = defined($str) ? $str : "";
if ($str =~ m{[&<>'"]}) {
$str =~ s/&/&amp;/g;
$str =~ s/</&lt;/g;
$str =~ s/>/&gt;/g;
$str =~ s/'/&#39;/g;
$str =~ s/"/&quot;/g;
}
$str //= "";
my $re = join '', '[', ( sort keys %mapping ), ']';
$str =~ s/($re)/$mapping{$1}/ge;
return $str;
}
......@@ -83,23 +78,19 @@ sub ep {
}
if (!ref($template)) {
if (!$self->{'c'}) {
die "No compiler available";
}
die "No compiler available" unless $self->{'c'};
$template = $self->{'c'}->compile($template, $self->{'options'});
}
if (!$template) {
return undef;
}
return undef unless $template;
$self->{'partials'}{$symbol}{'base'} = $template;
if ($partial->{'subs'}) {
# make sure we consider parent template now
if (!$partials->{'stack_text'}) {
$partials->{'stack_text'} = {};
}
$partials->{'stack_text'} ||= {};
for my $key (sort keys %{ $partial->{'subs'} }) {
if (!$partials->{'stack_text'}{$key}) {
$partials->{'stack_text'}{$key} =
......@@ -118,10 +109,8 @@ sub ep {
# tries to find a partial in the current scope and render it
sub rp {
my ($self, $symbol, $context, $partials, $indent) = @_;
my $partial = $self->ep($symbol, $partials);
if (!$partial) {
return "";
}
my $partial = $self->ep($symbol, $partials) or return "";
return $partial->ri($context, $partials, $indent);
}
......@@ -135,8 +124,8 @@ sub rs {
return;
}
for (my $i = 0; $i < @$tail; $i++) {
push @$context, $tail->[$i];
for my $t (@$tail) {
push @$context, $t;
$section->($context, $partials, $self);
pop @$context;
}
......@@ -146,9 +135,8 @@ sub rs {
sub s {
my ($self, $val, $ctx, $partials, $inverted, $start, $end, $tags) = @_;
my $pass;
if ((ref($val) eq 'ARRAY') && !@$val) {
return 0;
}
return 0 if (ref($val) eq 'ARRAY') && !@$val;
if (ref($val) eq 'CODE') {
$val = $self->ms($val, $ctx, $partials, $inverted, $start, $end, $tags);
......@@ -178,13 +166,8 @@ sub d {
# > ".".split(".")
# [ '', '' ]
#
my @names;
if ($key eq '.') {
@names = ("", "");
}
else {
@names = split m/[.]/, $key;
}
my @names = $key eq '.' ? ( '' ) x 2 : split /\./, $key;
my $val = $self->f($names[0], $ctx, $partials, $return_found);
my $cx;
......@@ -193,8 +176,8 @@ sub d {
$val = $ctx->[-1];
}
else {
for (my $i = 1; $i < @names; $i++) {
$found = find_in_scope($names[$i], $val);
for my $name (@names[1..$#names] ) {
$found = find_in_scope($name, $val);
if (defined $found) {
$cx = $val;
$val = $found;
......@@ -205,17 +188,15 @@ sub d {
}
}
if ($return_found && !$val) {
return 0;
}
return 0 if $return_found && !$val;
if (!$return_found && ref($val) eq 'CODE') {
push @$ctx, $cx;
$val = $self->mv($val, $ctx, $partials);
pop @$ctx;
}
$val = $self->_check_for_num($val);
return $val;
return $self->_check_for_num($val);
}
# handle numerical interpolation for decimal numbers "properly"...
......@@ -228,38 +209,32 @@ sub _check_for_num {
my $val = shift;
return $val if ($self->{'numeric_string_as_string'} == 1);
if (looks_like_number($val)) {
$val = $val + 0;
}
$val += 0 if looks_like_number($val);
return $val;
}
# find values with normal names
sub f {
my ($self, $key, $ctx, $partials, $return_found) = @_;
my $val = 0;
my $v = undef;
my $found = 0;
my ( $val, $found ) = ( 0 );
for (my $i = @$ctx - 1; $i >= 0; $i--) {
$v = $ctx->[$i];
for my $v ( reverse @$ctx ) {
$val = find_in_scope($key, $v);
if (defined $val) {
$found = 1;
last;
}
}
if (!$found) {
return $return_found ? 0 : "";
next unless defined $val;
$found = 1;
last;
}
return $return_found ? 0 : "" unless $found;
if (!$return_found && (ref($val) eq 'CODE')) {
$val = $self->mv($val, $ctx, $partials);
}
$val = $self->_check_for_num($val);
return $val;
return $self->_check_for_num($val);
}
# higher order templates
......@@ -277,9 +252,10 @@ sub ls {
# compile text
sub ct {
my ($self, $text, $cx, $partials) = @_;
if ($self->{'options'}{'disable_lambda'}) {
die "Lambda features disabled";
}
die "Lambda features disabled"
if $self->{'options'}{'disable_lambda'};
return $self->{'c'}->compile($text, $self->{'options'})->render($cx, $partials);
}
......@@ -300,20 +276,17 @@ sub fl {
sub ms {
my ($self, $func, $ctx, $partials, $inverted, $start, $end, $tags) = @_;
if ($inverted) {
return 1;
}
else {
my $text_source = ($self->{'active_sub'} && $self->{'subs_text'} && $self->{'subs_text'}{$self->{'active_sub'}})
? $self->{'subs_text'}{$self->{'active_sub'}}
: $self->{'text'};
return 1 if $inverted;
my $s = substr($text_source,$start,($end-$start));
my $text_source = ($self->{'active_sub'} && $self->{'subs_text'} && $self->{'subs_text'}{$self->{'active_sub'}})
? $self->{'subs_text'}{$self->{'active_sub'}}
: $self->{'text'};
$self->ls($func, $ctx->[-1], $ctx, $partials, $s, $tags);
my $s = substr($text_source,$start,($end-$start));
return 0;
}
$self->ls($func, $ctx->[-1], $ctx, $partials, $s, $tags);
return 0;
}
# method replace variable
......@@ -327,27 +300,19 @@ sub mv {
sub sub {
my ($self, $name, $context, $partials, $indent) = @_;
my $f = $self->{'subs'}{$name};
if ($f) {
$self->{'active_sub'} = $name;
$f->($context,$partials,$self,$indent);
$self->{'active_sub'} = 0;
}
my $f = $self->{'subs'}{$name} or return;
$self->{'active_sub'} = $name;
$f->($context,$partials,$self,$indent);
$self->{'active_sub'} = 0;
}
################################################
sub find_in_scope {
my ($key, $scope) = @_;
my $val;
if ($scope && ref($scope) eq 'HASH') {
if (defined $scope->{$key}) {
$val = $scope->{$key};
}
}
return $val;
return eval { $scope->{$key} };
}
sub create_specialized_partial {
......@@ -400,7 +365,7 @@ Text::Hogan::Template - represent and render compiled templates
=head1 VERSION
version 1.07
version 1.09
=head1 SYNOPSIS
......